37 #include <sys/types.h>
41 #include "hostPersistence.hpp"
42 #include "exception.hpp"
43 #include "illegalArgumentException.hpp"
46 #include "dsserverlogger.h"
48 #define HOSTPERSIST_ERROR -1
49 #define HOSTPERSIST_SUCCESS 0
81 HostPersistence::HostPersistence() {
89 #if defined(HAS_HDD_PERSISTENT)
91 filePath =
"/tmp/mnt/diska3/persistent/ds/hostData";
94 #elif defined(HAS_FLASH_PERSISTENT)
96 filePath =
"/opt/persistent/ds/hostData";
100 filePath =
"/opt/ds/hostData";
105 defaultFilePath =
"/etc/hostDataDefault";
110 HostPersistence::HostPersistence(
const std::string &storeFileName) {
112 filePath = storeFileName;
114 HostPersistence::~HostPersistence() {
133 void HostPersistence::load() {
136 loadFromFile(filePath, _properties);
140 cout <<
"Backup file is currupt or not available.." << endl;
142 loadFromFile(filePath +
"tmpDB", _properties);
151 loadFromFile(defaultFilePath, _defaultProperties);
155 cout <<
"System file "<< defaultFilePath <<
" is currupt or not available.." << endl;
172 std::string HostPersistence::getProperty(
const string &key)
177 cout <<
"The KEY is empty..." << endl;
181 std::map <std::string, std::string> :: const_iterator eFound = _properties.find (key);
182 if (eFound == _properties.end())
184 cout <<
"The Item IS NOT FOUND " << endl;
186 throw IllegalArgumentException();
191 return eFound->second;
206 std::string HostPersistence::getProperty(
const std::string &key,
const std::string &defValue)
211 cout <<
"The KEY is empty..." << endl;
217 std::map <std::string, std::string> :: const_iterator eFound = _properties.find (key);
219 if (eFound == _properties.end())
226 return eFound->second;
239 std::string HostPersistence::getDefaultProperty(
const string &key)
244 cout <<
"The KEY is empty..." << endl;
248 std::map <std::string, std::string> :: const_iterator eFound = _defaultProperties.find (key);
249 if (eFound == _defaultProperties.end())
251 cout <<
"The Item IS NOT FOUND " << endl;
257 cout <<
"The Item " << eFound->first <<
" is found & the value is " << eFound->second << endl;
258 return eFound->second;
273 void HostPersistence::persistHostProperty(
const std::string &key,
const std::string &value)
275 if( key.empty() || value.empty())
277 cout <<
"Given KEY or VALUE is empty..." << endl;
283 string eRet = getProperty (key);
287 if (eRet.compare(value) == 0) {
293 writeToFile(filePath +
"tmpDB");
297 _properties.erase (key);
301 cout <<
"Entry Not found..!\n";
307 _properties.insert ({key, value });
309 writeToFile(filePath);
324 void HostPersistence::loadFromFile (
const string &fileName, std::map <std::string, std::string> &map)
326 char keyValue[1024] =
"";
328 FILE *filePtr = NULL;
330 filePtr = fopen (fileName.c_str(),
"r");
331 if (filePtr != NULL) {
332 while (!feof(filePtr))
335 if (fscanf (filePtr,
"%s\t%s\n", key, keyValue) <= 0 )
337 cout <<
"fscanf failed !\n";
342 map.insert ({key, keyValue });
357 void HostPersistence::writeToFile (
const string &fileName)
359 unlink(fileName.c_str());
361 if(_properties.size() > 0)
370 FILE *file = fopen (fileName.c_str(),
"w");
373 for (
auto it = _properties.begin(); it != _properties.end(); ++it ) {
374 string dataToWrite = it->first +
"\t" + it->second +
"\n";
375 unsigned int size = dataToWrite.length();
376 fwrite(dataToWrite.c_str(),1,size,file);