config-file: search multiple directories
Restricting the config file to `/usr/share` makes testing difficult
and prevents the config file from being present on a, typically for
the BMC, read-write volume. Change the config loading code to search:
1. pwd
2. /var/lib/phosphor-virtual-sensor
3. /usr/share/phosphor-virtual-sensor
Tested:
Ran strace on the changes and observed the expected file search.
```
getcwd("pwd/phosphor-virtual-sensor/builddir", 4096) = 75
newfstatat(AT_FDCWD, "pwd/phosphor-virtual-sensor/builddir/virtual_sensor_config.json", 0x7fff60a0af90, 0) = -1 ENOENT (No such file or directory)
newfstatat(AT_FDCWD, "/var/lib/phosphor-virtual-sensor/virtual_sensor_config.json", 0x7fff60a0af90, 0) = -1 ENOENT (No such file or directory)
newfstatat(AT_FDCWD, "/usr/share/phosphor-virtual-sensor/virtual_sensor_config.json", 0x7fff60a0af90, 0) = -1 ENOENT (No such file or directory)
```
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: I70854d565a3b13cd48c27a80da84de54c00a0603
diff --git a/virtualSensor.cpp b/virtualSensor.cpp
index e2e0399..e8fc218 100644
--- a/virtualSensor.cpp
+++ b/virtualSensor.cpp
@@ -1,7 +1,5 @@
#include "virtualSensor.hpp"
-#include "config.hpp"
-
#include <phosphor-logging/lg2.hpp>
#include <fstream>
@@ -772,8 +770,25 @@
}
/** @brief Parsing Virtual Sensor config JSON file */
-Json VirtualSensors::parseConfigFile(const std::string& configFile)
+Json VirtualSensors::parseConfigFile()
{
+ using path = std::filesystem::path;
+ auto configFile = []() -> path {
+ static constexpr auto name = "virtual_sensor_config.json";
+
+ for (auto pathSeg : {std::filesystem::current_path(),
+ path{"/var/lib/phosphor-virtual-sensor"},
+ path{"/usr/share/phosphor-virtual-sensor"}})
+ {
+ auto file = pathSeg / name;
+ if (std::filesystem::exists(file))
+ {
+ return file;
+ }
+ }
+ return name;
+ }();
+
std::ifstream jsonFile(configFile);
if (!jsonFile.is_open())
{
@@ -955,7 +970,7 @@
{
static const Json empty{};
- auto data = parseConfigFile(VIRTUAL_SENSOR_CONFIG_FILE);
+ auto data = parseConfigFile();
// print values
if (DEBUG)