bugfix: Clean stale data on blob creation

It is possible that previous actions has stored stale data that are
valid protobufs, with different parameters. Detect stale data and
overwrite them.

Signed-off-by: Kun Yi <kunyi731@gmail.com>
Change-Id: I25b54885cdab928d1ad0b740d1c9b96335d2c60e
diff --git a/binarystore.cpp b/binarystore.cpp
index 29e6b9f..a18a980 100644
--- a/binarystore.cpp
+++ b/binarystore.cpp
@@ -42,6 +42,11 @@
     auto store =
         std::make_unique<BinaryStore>(baseBlobId, std::move(file), maxSize);
 
+    if (!store->loadSerializedData())
+    {
+        return nullptr;
+    }
+
     return std::move(store);
 }
 
@@ -79,6 +84,19 @@
         return false;
     }
 
+    if (blob_.blob_base_id() != baseBlobId_)
+    {
+        /* Uh oh, stale data loaded. Clean it and commit. */
+        // TODO: it might be safer to add an option in config to error out
+        // instead of to overwrite.
+        log<level::ERR>("Stale blob data, resetting internals...",
+                        entry("LOADED=%s", blob_.blob_base_id().c_str()),
+                        entry("EXPECTED=%s", baseBlobId_.c_str()));
+        blob_.Clear();
+        blob_.set_blob_base_id(baseBlobId_);
+        return this->commit();
+    }
+
     return true;
 }