diff --git a/persistencefile.cpp b/persistencefile.cpp index e70ca9f..87dc773 100644 --- a/persistencefile.cpp +++ b/persistencefile.cpp @@ -320,3 +320,8 @@ void PersistenceFile::closeFile() throw std::runtime_error(formatString("Saving '%s' failed: rename of temp file to target failed with: %s", filePath.c_str(), strerror(errno))); } } + +const std::string &PersistenceFile::getFilePath() const +{ + return this->filePath; +} diff --git a/persistencefile.h b/persistencefile.h index 6000d01..2932e39 100644 --- a/persistencefile.h +++ b/persistencefile.h @@ -87,6 +87,8 @@ public: void openWrite(const std::string &versionString); void openRead(); void closeFile(); + + const std::string &getFilePath() const; }; #endif // PERSISTENCEFILE_H diff --git a/sessionsandsubscriptionsdb.cpp b/sessionsandsubscriptionsdb.cpp index 03ae120..3812c28 100644 --- a/sessionsandsubscriptionsdb.cpp +++ b/sessionsandsubscriptionsdb.cpp @@ -41,7 +41,7 @@ SessionsAndSubscriptionsDB::SessionsAndSubscriptionsDB(const std::string &filePa void SessionsAndSubscriptionsDB::openWrite() { - PersistenceFile::openWrite(MAGIC_STRING_SESSION_FILE_V1); + PersistenceFile::openWrite(MAGIC_STRING_SESSION_FILE_V2); } void SessionsAndSubscriptionsDB::openRead() @@ -50,11 +50,13 @@ void SessionsAndSubscriptionsDB::openRead() if (detectedVersionString == MAGIC_STRING_SESSION_FILE_V1) readVersion = ReadVersion::v1; + else if (detectedVersionString == MAGIC_STRING_SESSION_FILE_V2) + readVersion = ReadVersion::v2; else throw std::runtime_error("Unknown file version."); } -SessionsAndSubscriptionsResult SessionsAndSubscriptionsDB::readDataV1() +SessionsAndSubscriptionsResult SessionsAndSubscriptionsDB::readDataV2() { SessionsAndSubscriptionsResult result; @@ -74,11 +76,11 @@ SessionsAndSubscriptionsResult SessionsAndSubscriptionsDB::readDataV1() if (eofFound) continue; - std::vector reserved(RESERVED_SPACE_SESSIONS_DB_V1); + std::vector reserved(RESERVED_SPACE_SESSIONS_DB_V2); for (uint32_t i = 0; i < nrOfSessions; i++) { - readCheck(buf.data(), 1, RESERVED_SPACE_SESSIONS_DB_V1, f); + readCheck(buf.data(), 1, RESERVED_SPACE_SESSIONS_DB_V2, f); uint32_t usernameLength = readUint32(eofFound); readCheck(buf.data(), 1, usernameLength, f); @@ -188,8 +190,8 @@ void SessionsAndSubscriptionsDB::saveData(const std::vectorlogf(LOG_DEBUG, "Saving program first start time stamp as %ld", start_stamp); @@ -203,7 +205,7 @@ void SessionsAndSubscriptionsDB::saveData(const std::vectorusername.length()); writeCheck(ses->username.c_str(), 1, ses->username.length(), f); @@ -294,7 +296,9 @@ SessionsAndSubscriptionsResult SessionsAndSubscriptionsDB::readData() return defaultResult; if (readVersion == ReadVersion::v1) - return readDataV1(); + logger->logf(LOG_WARNING, "File '%s' is version 1, an internal development version that was never finalized. Not reading.", getFilePath().c_str()); + if (readVersion == ReadVersion::v2) + return readDataV2(); return defaultResult; } diff --git a/sessionsandsubscriptionsdb.h b/sessionsandsubscriptionsdb.h index 9b5f63a..d8b8650 100644 --- a/sessionsandsubscriptionsdb.h +++ b/sessionsandsubscriptionsdb.h @@ -25,7 +25,8 @@ License along with FlashMQ. If not, see . #include "session.h" #define MAGIC_STRING_SESSION_FILE_V1 "FlashMQRetainedDBv1" -#define RESERVED_SPACE_SESSIONS_DB_V1 32 +#define MAGIC_STRING_SESSION_FILE_V2 "FlashMQRetainedDBv2" +#define RESERVED_SPACE_SESSIONS_DB_V2 32 /** * @brief The SubscriptionForSerializing struct contains the fields we're interested in when saving a subscription. @@ -51,12 +52,13 @@ class SessionsAndSubscriptionsDB : public PersistenceFile enum class ReadVersion { unknown, - v1 + v1, + v2 }; ReadVersion readVersion = ReadVersion::unknown; - SessionsAndSubscriptionsResult readDataV1(); + SessionsAndSubscriptionsResult readDataV2(); void writeRowHeader(); public: SessionsAndSubscriptionsDB(const std::string &filePath);