serialize: Use flash id instead of version id

Update the serialize functions to take the flash id instead of the
version id since version id can be different every time the BMC boots up
once support for uploading the same version is added.

For the erase() function, if the flash id cannot be determined, it won't
be able to clean up the serialize directory since it doesn't know the
name for it, but this should be a rare scenario since the flash id (Path
property) is set when the BMC boots up.

Tested: The serialize directory name remains the same for ubi (version
id), and it changes to "a" or "b" for mmc. The priority of the versions
are found and restored upon reboot.

Change-Id: Ic2bd39caadbf7147e30200be8c4080f2b030c17a
Signed-off-by: Adriana Kobylak <anoo@us.ibm.com>
diff --git a/activation.cpp b/activation.cpp
index c90b537..91f3f33 100644
--- a/activation.cpp
+++ b/activation.cpp
@@ -197,7 +197,8 @@
     ubootEnvVarsUpdated = false;
     Activation::unsubscribeFromSystemdSignals();
 
-    storePurpose(versionId, parent.versions.find(versionId)->second->purpose());
+    auto flashId = parent.versions.find(versionId)->second->path();
+    storePurpose(flashId, parent.versions.find(versionId)->second->purpose());
 
     if (!redundancyPriority)
     {
diff --git a/item_updater.cpp b/item_updater.cpp
index 550f845..e7de3d0 100644
--- a/item_updater.cpp
+++ b/item_updater.cpp
@@ -227,8 +227,11 @@
                 continue;
             }
 
+            // The flash location is part of the mount name: rofs-<location>
+            auto flashId = iter.path().native().substr(BMC_RO_PREFIX_LEN);
+
             auto purpose = server::Version::VersionPurpose::BMC;
-            restorePurpose(id, purpose);
+            restorePurpose(flashId, purpose);
 
             // Read os-release from /etc/ to get the BMC extended version
             std::string extendedVersion =
@@ -260,9 +263,6 @@
             // association.
             createUpdateableAssociation(path);
 
-            // The flash location is part of the mount name: rofs-<location>
-            auto flashId = iter.path().native().substr(BMC_RO_PREFIX_LEN);
-
             // Create Version instance for this version.
             auto versionPtr = std::make_unique<VersionClass>(
                 bus, path, version, purpose, extendedVersion, flashId,
@@ -286,7 +286,7 @@
             if (activationState == server::Activation::Activations::Active)
             {
                 uint8_t priority = std::numeric_limits<uint8_t>::max();
-                if (!restorePriority(id, priority))
+                if (!restorePriority(flashId, priority))
                 {
                     if (isVersionFunctional)
                     {
@@ -373,9 +373,11 @@
 
     if (it != versions.end())
     {
+        auto flashId = it->second->path();
+
         // Delete ReadOnly partitions if it's not active
         removeReadOnlyPartition(entryId);
-        removePersistDataDirectory(entryId);
+        removePersistDataDirectory(flashId);
 
         // Removing entry in versions map
         this->versions.erase(entryId);
@@ -384,7 +386,6 @@
     {
         // Delete ReadOnly partitions even if we can't find the version
         removeReadOnlyPartition(entryId);
-        removePersistDataDirectory(entryId);
 
         error(
             "Failed to find version ({VERSIONID}) in item updater versions map; unable to remove.",
@@ -442,7 +443,8 @@
 
 void ItemUpdater::savePriority(const std::string& versionId, uint8_t value)
 {
-    storePriority(versionId, value);
+    auto flashId = versions.find(versionId)->second->path();
+    storePriority(flashId, value);
     helper.setEntry(versionId, value);
 }
 
diff --git a/serialize.cpp b/serialize.cpp
index 52c5ac5..8b7fca4 100644
--- a/serialize.cpp
+++ b/serialize.cpp
@@ -22,9 +22,9 @@
 const std::string priorityName = "priority";
 const std::string purposeName = "purpose";
 
-void storePriority(const std::string& versionId, uint8_t priority)
+void storePriority(const std::string& flashId, uint8_t priority)
 {
-    auto path = fs::path(PERSIST_DIR) / versionId;
+    auto path = fs::path(PERSIST_DIR) / flashId;
     if (!fs::is_directory(path))
     {
         if (fs::exists(path))
@@ -42,9 +42,9 @@
     oarchive(cereal::make_nvp(priorityName, priority));
 }
 
-void storePurpose(const std::string& versionId, VersionPurpose purpose)
+void storePurpose(const std::string& flashId, VersionPurpose purpose)
 {
-    auto path = fs::path(PERSIST_DIR) / versionId;
+    auto path = fs::path(PERSIST_DIR) / flashId;
     if (!fs::is_directory(path))
     {
         if (fs::exists(path))
@@ -62,9 +62,9 @@
     oarchive(cereal::make_nvp(purposeName, purpose));
 }
 
-bool restorePriority(const std::string& versionId, uint8_t& priority)
+bool restorePriority(const std::string& flashId, uint8_t& priority)
 {
-    auto path = fs::path(PERSIST_DIR) / versionId / priorityName;
+    auto path = fs::path(PERSIST_DIR) / flashId / priorityName;
     if (fs::exists(path))
     {
         std::ifstream is(path.c_str(), std::ios::in);
@@ -101,13 +101,13 @@
             std::string envVars;
             std::getline(input, envVars);
 
-            std::string versionVar = versionId + "=";
+            std::string versionVar = flashId + "=";
             auto varPosition = envVars.find(versionVar);
 
             if (varPosition != std::string::npos)
             {
-                // Grab the environment variable for this versionId. These
-                // variables follow the format "versionId=priority\0"
+                // Grab the environment variable for this flashId. These
+                // variables follow the format "flashId=priority\0"
                 auto var = envVars.substr(varPosition);
                 priority = std::stoi(var.substr(versionVar.length()));
                 return true;
@@ -122,9 +122,9 @@
     return false;
 }
 
-bool restorePurpose(const std::string& versionId, VersionPurpose& purpose)
+bool restorePurpose(const std::string& flashId, VersionPurpose& purpose)
 {
-    auto path = fs::path(PERSIST_DIR) / versionId / purposeName;
+    auto path = fs::path(PERSIST_DIR) / flashId / purposeName;
     if (fs::exists(path))
     {
         std::ifstream is(path.c_str(), std::ios::in);
@@ -143,9 +143,9 @@
     return false;
 }
 
-void removePersistDataDirectory(const std::string& versionId)
+void removePersistDataDirectory(const std::string& flashId)
 {
-    auto path = fs::path(PERSIST_DIR) / versionId;
+    auto path = fs::path(PERSIST_DIR) / flashId;
     if (fs::exists(path))
     {
         fs::remove_all(path);
diff --git a/serialize.hpp b/serialize.hpp
index aada2bf..f447e73 100644
--- a/serialize.hpp
+++ b/serialize.hpp
@@ -17,35 +17,40 @@
     sdbusplus::xyz::openbmc_project::Software::server::Version::VersionPurpose;
 
 /** @brief Serialization function - stores priority information to file
- *  @param[in] versionId - The version for which to store information.
+ *  @param[in] flashId - The flash id of the version for which to store
+ *                       information.
  *  @param[in] priority - RedundancyPriority value for that version.
  **/
-void storePriority(const std::string& versionId, uint8_t priority);
+void storePriority(const std::string& flashId, uint8_t priority);
 
 /** @brief Serialization function - stores purpose information to file
- *  @param[in] versionId - The version for which to store information.
+ *  @param[in] flashId - The flash id of the version for which to store
+ *                       information.
  *  @param[in] purpose - VersionPurpose value for that version.
  **/
-void storePurpose(const std::string& versionId, VersionPurpose purpose);
+void storePurpose(const std::string& flashId, VersionPurpose purpose);
 
 /** @brief Serialization function - restores priority information from file
- *  @param[in] versionId - The version for which to retrieve information.
+ *  @param[in] flashId - The flash id of the version for which to retrieve
+ *                       information.
  *  @param[in] priority - RedundancyPriority reference for that version.
  *  @return true if restore was successful, false if not
  **/
-bool restorePriority(const std::string& versionId, uint8_t& priority);
+bool restorePriority(const std::string& flashId, uint8_t& priority);
 
 /** @brief Serialization function - restores purpose information from file
- *  @param[in] versionId - The version for which to retrieve information.
+ *  @param[in] flashId - The flash id of the version for which to retrieve
+ *                       information.
  *  @param[in] purpose - VersionPurpose reference for that version.
  *  @return true if restore was successful, false if not
  **/
-bool restorePurpose(const std::string& versionId, VersionPurpose& purpose);
+bool restorePurpose(const std::string& flashId, VersionPurpose& purpose);
 
 /** @brief Removes the serial directory for a given version.
- *  @param[in] versionId - The version for which to remove a file, if it exists.
+ *  @param[in] flash Id - The flash id of the version for which to remove a
+ *                        file, if it exists.
  **/
-void removePersistDataDirectory(const std::string& versionId);
+void removePersistDataDirectory(const std::string& flashId);
 
 } // namespace updater
 } // namespace software