cereal: Fix empty file scenario
Cereal throws execption when deserialising empty file, add code
to handle empty exception and default the setting to the default
value.
Change-Id: I466f44d07c902a27344a483b5b574e0507baa2f4
Signed-off-by: Tom Joseph <tomjoseph@in.ibm.com>
diff --git a/settings_manager.mako.hpp b/settings_manager.mako.hpp
index 1dbe550..9e0b3fd 100644
--- a/settings_manager.mako.hpp
+++ b/settings_manager.mako.hpp
@@ -43,6 +43,7 @@
#include <phosphor-logging/log.hpp>
#include "config.h"
#include <xyz/openbmc_project/Common/error.hpp>
+using namespace phosphor::logging;
% for i in set(sdbusplus_includes):
#include "${i}"
@@ -297,22 +298,36 @@
% for index, path in enumerate(objects):
path = fs::path(SETTINGS_PERSIST_PATH) / "${path}";
path += persistent::fileSuffix;
- if (fs::exists(path))
- {
- std::ifstream is(path.c_str(), std::ios::in);
- cereal::JSONInputArchive iarchive(is);
- iarchive(*std::get<${index}>(settings));
- }
- else
+ auto initSetting${index} = [&]()
{
% for item in settingsDict[path]:
% for propName, metaDict in item['Properties'].items():
<% p = propName[:1].lower() + propName[1:] %>\
<% defaultValue = metaDict['Default'] %>\
std::get<${index}>(settings)->
- ${get_setting_sdbusplus_type(item['Interface'])}::${p}(${defaultValue});
- % endfor
+ ${get_setting_sdbusplus_type(item['Interface'])}::${p}(${defaultValue});
% endfor
+% endfor
+ };
+
+ try
+ {
+ if (fs::exists(path))
+ {
+ std::ifstream is(path.c_str(), std::ios::in);
+ cereal::JSONInputArchive iarchive(is);
+ iarchive(*std::get<${index}>(settings));
+ }
+ else
+ {
+ initSetting${index}();
+ }
+ }
+ catch (cereal::Exception& e)
+ {
+ log<level::ERR>(e.what());
+ fs::remove(path);
+ initSetting${index}();
}
std::get<${index}>(settings)->emit_object_added();