PEL: Fix for error - unable to read PEL ID file

This is a case when PEL daemon can't read pelID file to get the next
pel Id to use. It could be that the file got corrupted during code
update or factory reset leading to zeros data in file. The fix is to
delete the file as there is already code later to recreate file if
not available.

Signed-off-by: Sumit Kumar <sumit_kumar@in.ibm.com>
Change-Id: I42372cc1d79ece5914e7fdb51f3fa2a2d9cb07e7
diff --git a/extensions/openpower-pels/log_id.cpp b/extensions/openpower-pels/log_id.cpp
index debcc99..ee215c2 100644
--- a/extensions/openpower-pels/log_id.cpp
+++ b/extensions/openpower-pels/log_id.cpp
@@ -64,6 +64,7 @@
     if (idFilename.empty())
     {
         idFilename = getPELIDFile();
+        checkFileForZeroData(idFilename);
     }
 
     uint32_t id = 0;
@@ -108,5 +109,26 @@
     return detail::addLogIDPrefix(id);
 }
 
+void checkFileForZeroData(const std::string& filename)
+{
+    if (fs::exists(filename))
+    {
+        char ch;
+
+        std::ifstream rf{filename, std::ios::binary};
+        rf.read(&ch, sizeof(ch));
+        while (ch == '\0')
+        {
+            if (rf.eof())
+            {
+                fs::remove(filename);
+                log<level::WARNING>(
+                    "PEL ID file seems corrupted. Deleting it.");
+                break;
+            }
+            rf.read(&ch, sizeof(ch));
+        }
+    }
+}
 } // namespace pels
 } // namespace openpower