user_mgmt: Recover corrupted nv files

For unknown reasons the nv file size become to 0.
To not affect the service, add this condition:
If the file is 0 in size, delete the file and returns EIO.

Resolves openbmc/phosphor-host-ipmid#185

Tested:

Step1: Manually create an empty size file

$ rm /var/lib/ipmi/channel_access_nv.json
$ touch /var/lib/ipmi/channel_access_nv.json

Step2: Restart ipmid and confirm the recovery was successful

$ systemctl restart phosphor-ipmi-host.service

Fusion-Link:
https://fusion2.corp.google.com/3ee7154b-5d58-3c69-b1fc-371aa9705613
https://fusion2.corp.google.com/77bcd09c-0dff-3636-878d-cb24f80d94db

Google-Bug-Id: 343233449
Change-Id: I710dd7c588ed811641266bf8178fb85610bd1609
Signed-off-by: Konda Reddy Kachana <kkachana@google.com>
(cherry picked from commit 46c5c3ac63acf5a69c2058665fc773902a3dfb64)
diff --git a/recipes-phosphor/ipmi/phosphor-ipmi-host/0008-user_mgmt-Recover-corrupted-nv-files.patch b/recipes-phosphor/ipmi/phosphor-ipmi-host/0008-user_mgmt-Recover-corrupted-nv-files.patch
new file mode 100644
index 0000000..3da91ca
--- /dev/null
+++ b/recipes-phosphor/ipmi/phosphor-ipmi-host/0008-user_mgmt-Recover-corrupted-nv-files.patch
@@ -0,0 +1,51 @@
+From a56e7ba9726fca23cc1e62f12affb6d5cd5807de Mon Sep 17 00:00:00 2001
+From: Lulu Su <Lulu_Su@wistron.com>
+Date: Tue, 20 Sep 2022 13:09:15 +0800
+Subject: [PATCH] user_mgmt: Recover corrupted nv files
+
+For unknown reasons the nv file size become to 0.
+To not affect the service, add this condition:
+If the file is 0 in size, delete the file and returns EIO.
+
+Resolves openbmc/phosphor-host-ipmid#185
+
+Tested:
+
+Step1: Manually create an empty size file
+
+$ rm /var/lib/ipmi/channel_access_nv.json
+$ touch /var/lib/ipmi/channel_access_nv.json
+
+Step2: Restart ipmid and confirm the recovery was successful
+
+$ systemctl restart phosphor-ipmi-host.service
+
+Signed-off-by: Lulu Su <Lulu_Su@wistron.com>
+Change-Id: I6205709ca50b0ddfb11973af56d1ee8a7ecbc44a
+---
+ user_channel/channel_mgmt.cpp | 9 +++++++++
+ 1 file changed, 9 insertions(+)
+
+diff --git a/user_channel/channel_mgmt.cpp b/user_channel/channel_mgmt.cpp
+index 50505aa..c382fa5 100644
+--- a/user_channel/channel_mgmt.cpp
++++ b/user_channel/channel_mgmt.cpp
+@@ -1106,6 +1106,15 @@ int ChannelConfig::readChannelPersistData()
+         log<level::DEBUG>("Error in opening IPMI Channel data file");
+         return -EIO;
+     }
++    else if (!std::filesystem::file_size(channelNvDataFilename))
++    {
++        log<level::DEBUG>("NV file (channelNvDataFilename) has a size of zero");
++        if (std::filesystem::remove(channelNvDataFilename))
++        {
++            log<level::DEBUG>("NV file (channelNvDataFilename) is deleted");
++        }
++        return -EIO;
++    }
+     try
+     {
+         // Fill in global structure
+-- 
+2.45.1.288.g0e0cd299f1-goog
+
diff --git a/recipes-phosphor/ipmi/phosphor-ipmi-host_%.bbappend b/recipes-phosphor/ipmi/phosphor-ipmi-host_%.bbappend
index 43b4412..8f8feb4 100644
--- a/recipes-phosphor/ipmi/phosphor-ipmi-host_%.bbappend
+++ b/recipes-phosphor/ipmi/phosphor-ipmi-host_%.bbappend
@@ -10,4 +10,5 @@
   file://0005-dbus-sdr-add-event-type-for-sdr-count.patch \
   file://0006-dbus-sdr-fix-write-permission-for-external-sen.patch \
   file://0007-dbus-sdr-Add-limited-log-on-sensorTree-Resets.patch \
+  file://0008-user_mgmt-Recover-corrupted-nv-files.patch \
 "