ibm-read-vpd: Switch to the right symlinked JSON

This commit:
* Makes the VPD parser use the symlinked JSON if one is setup.
  For example via a system VPD service.
* Makes the vpd-tool and vpd-manager use the symlinked JSON.

Signed-off-by: Santosh Puranik <santosh.puranik@in.ibm.com>
Change-Id: Id7f845e02917e3a66d1fa9754da71ed0005c0cb8
diff --git a/ibm_vpd_app.cpp b/ibm_vpd_app.cpp
index 48b930c..e4c2c06 100644
--- a/ibm_vpd_app.cpp
+++ b/ibm_vpd_app.cpp
@@ -539,7 +539,9 @@
             target = INVENTORY_JSON_2U;
         }
 
-        // unlink the symlink which is created at build time
+        // Create the directory for hosting the symlink
+        fs::create_directories(VPD_FILES_PATH);
+        // unlink the symlink previously created (if any)
         remove(INVENTORY_JSON_SYM_LINK);
         // create a new symlink based on the system
         fs::create_symlink(target, link);
@@ -576,8 +578,17 @@
 
         CLI11_PARSE(app, argc, argv);
 
+        auto jsonToParse = INVENTORY_JSON_DEFAULT;
+
+        // If the symlink exists, it means it has been setup for us, switch the
+        // path
+        if (fs::exists(INVENTORY_JSON_SYM_LINK))
+        {
+            jsonToParse = INVENTORY_JSON_SYM_LINK;
+        }
+
         // Make sure that the file path we get is for a supported EEPROM
-        ifstream inventoryJson(INVENTORY_JSON);
+        ifstream inventoryJson(jsonToParse);
         auto js = json::parse(inventoryJson);
 
         if ((js.find("frus") == js.end()) ||
diff --git a/meson.build b/meson.build
index f430baf..7cb1e69 100644
--- a/meson.build
+++ b/meson.build
@@ -22,7 +22,8 @@
 add_global_arguments('-Wno-psabi', language : ['c', 'cpp'])
 configure_file(output: 'config.h',
                        configuration :{
-                       'INVENTORY_JSON': '"'+get_option('INVENTORY_JSON')+'"',
+                       'INVENTORY_JSON_DEFAULT': '"'+get_option('INVENTORY_JSON_DEFAULT')+'"',
+                       'VPD_FILES_PATH': '"'+get_option('VPD_FILES_PATH')+'"',
                        'INVENTORY_PATH': '"'+get_option('INVENTORY_PATH')+'"',
                        'IPZ_INTERFACE': '"'+get_option('IPZ_INTERFACE')+'"',
                        'INVENTORY_MANAGER_SERVICE': '"'+get_option('INVENTORY_MANAGER_SERVICE')+'"',
@@ -33,9 +34,9 @@
                        'OBJECT_MAPPER_OBJECT' : '"'+get_option('OBJECT_MAPPER_OBJECT')+'"',
                        'POWER_SUPPLY_TYPE_INTERFACE' : '"'+get_option('POWER_SUPPLY_TYPE_INTERFACE')+'"',
                        'INVENTORY_MANAGER_CACHE' : '"'+get_option('INVENTORY_MANAGER_CACHE')+'"',
-		       'INVENTORY_JSON_SYM_LINK': '"'+get_option('INVENTORY_JSON_SYM_LINK')+'"',
-		       'INVENTORY_JSON_2U': '"'+get_option('INVENTORY_JSON_2U')+'"',
-		       'INVENTORY_JSON_4U': '"'+get_option('INVENTORY_JSON_4U')+'"'
+                       'INVENTORY_JSON_SYM_LINK': '"'+get_option('INVENTORY_JSON_SYM_LINK')+'"',
+                       'INVENTORY_JSON_2U': '"'+get_option('INVENTORY_JSON_2U')+'"',
+                       'INVENTORY_JSON_4U': '"'+get_option('INVENTORY_JSON_4U')+'"'
                        }
   )
 
diff --git a/meson_options.txt b/meson_options.txt
index 2f43526..a06c63a 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -3,7 +3,8 @@
 option('FRU_YAML',type: 'string', value: 'writefru.yaml',  description: 'YAML STRING')
 option('PROP_YAML',type: 'string', value: 'extra-properties-example.yaml',  description: 'YAML PROPERTY')
 option('ibm-parser', type: 'feature', description: 'ENABLE IBM PARSER')
-option('INVENTORY_JSON',type: 'string', value: '/var/lib/vpd/vpd_inventory.json',  description: 'JSON file that defines inventory blueprint')
+option('VPD_FILES_PATH',type: 'string', value: '/var/lib/vpd', description: 'Directory to hold VPD runtime files')
+option('INVENTORY_JSON_DEFAULT',type: 'string', value: '/usr/share/vpd/vpd_inventory.json',  description: 'JSON file that defines inventory blueprint. The default path before system VPD service sets up the symlink.')
 option('INVENTORY_PATH',type: 'string', value: '/xyz/openbmc_project/inventory', description: 'Prefix for inventory D-Bus objects')
 option('INVENTORY_MANAGER_SERVICE',type: 'string', value: 'xyz.openbmc_project.Inventory.Manager', description: 'Inventory manager service')
 option('IPZ_INTERFACE', type: 'string', value: 'com.ibm.ipzvpd', description: 'IPZ VPD interface')
diff --git a/vpd-manager/manager.cpp b/vpd-manager/manager.cpp
index a313e86..9eeb6e2 100644
--- a/vpd-manager/manager.cpp
+++ b/vpd-manager/manager.cpp
@@ -48,7 +48,7 @@
 
 void Manager::processJSON()
 {
-    std::ifstream json(INVENTORY_JSON, std::ios::binary);
+    std::ifstream json(INVENTORY_JSON_SYM_LINK, std::ios::binary);
 
     if (!json)
     {
diff --git a/vpd-manager/meson_options.txt b/vpd-manager/meson_options.txt
index 1cc5f50..87ee61b 100644
--- a/vpd-manager/meson_options.txt
+++ b/vpd-manager/meson_options.txt
@@ -2,4 +2,4 @@
 option('OBJPATH', type : 'string', value : '/com/ibm/VPD/Manager', description : 'OBJECT PATH FOR THE SERVICE')
 option('IFACE', type : 'string', value : 'com.ibm.VPD.Manager', description : 'INTERFACE NAME')
 option('oe-sdk', type : 'feature', description : 'ENABLE OE SDK FOR VPD KEYWORD EDITOR')
-option('INVENTORY_JSON', type : 'string', value : '/usr/share/vpd/vpd_inventory.json', description : 'PATH TO INVENTORY JSON FILE')
+option('INVENTORY_JSON', type : 'string', value : '/var/lib/vpd/vpd_inventory.json', description : 'PATH TO INVENTORY JSON FILE')
diff --git a/vpd_tool.cpp b/vpd_tool.cpp
index 9129eca..e0e010a 100644
--- a/vpd_tool.cpp
+++ b/vpd_tool.cpp
@@ -66,7 +66,7 @@
 
     CLI11_PARSE(app, argc, argv);
 
-    ifstream inventoryJson(INVENTORY_JSON);
+    ifstream inventoryJson(INVENTORY_JSON_SYM_LINK);
     auto jsObject = json::parse(inventoryJson);
 
     try