From e62c527d05b095b1a7006a452792d365f0b71608 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Tue, 13 Dec 2022 10:07:36 +0100 Subject: [PATCH] create files as well --- mission/tmtc/TmStore.cpp | 31 +++++++++++++++++++++++++++---- mission/tmtc/TmStore.h | 2 +- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/mission/tmtc/TmStore.cpp b/mission/tmtc/TmStore.cpp index d29538ed..18a32cb7 100644 --- a/mission/tmtc/TmStore.cpp +++ b/mission/tmtc/TmStore.cpp @@ -57,14 +57,23 @@ ReturnValue_t TmStore::storePacket(PusTmReader& reader) { } // It is assumed here that the filesystem is usable. if (not mostRecentFile) { - assignMostRecentFile(); + assignAndOrCreateMostRecentFile(); } if (currentTv.tv_sec < mostRecentTv.value().tv_sec or currentTv.tv_sec - mostRecentTv.value().tv_sec > static_cast(rolloverDiffSeconds)) { if (file_size(mostRecentFile.value()) + reader.getFullPacketLen() > fileBuf.size()) { - // TODO: Rename old file to XYZ.1..z , create new file with the same name as old one, - // update most recent file with that name + uint8_t appendedCounter = 1; + path rolloverName; + while (true) { + rolloverName = path(mostRecentFile.value().string() + std::to_string(appendedCounter)); + if (not exists(rolloverName)) { + break; + } + appendedCounter++; + } + rename(mostRecentFile.value(), rolloverName); + std::ofstream of(mostRecentFile.value(), std::ios::binary); } } @@ -96,7 +105,7 @@ void TmStore::updateBaseDir() { ReturnValue_t TmStore::updateCurrentTimestamp() { return Clock::getClock_timeval(¤tTv); } -void TmStore::assignMostRecentFile() { +void TmStore::assignAndOrCreateMostRecentFile() { using namespace std::filesystem; for (auto const& file : directory_iterator(baseDir)) { if (file.is_directory()) { @@ -123,6 +132,20 @@ void TmStore::assignMostRecentFile() { mostRecentFile = file.path(); } } + if (not mostRecentFile) { + updateCurrentTimestamp(); + unsigned currentIdx = 0; + memcpy(fileBuf.data() + currentIdx, baseName.data(), baseName.size()); + currentIdx += baseName.size(); + Clock::TimeOfDay_t tod; + Clock::convertTimevalToTimeOfDay(¤tTv, &tod); + currentIdx += sprintf(reinterpret_cast(fileBuf.data() + currentIdx), + "%4" SCNu32 "-%2" SCNu32 "-%2" SCNu32 "T%2" SCNu32 ":%2" SCNu32 + ":%2" SCNu32 "Z.bin", + tod.year, tod.month, tod.day, tod.hour, tod.minute, tod.second); + path newPath(std::string(reinterpret_cast(fileBuf.data()), currentIdx)); + std::ofstream of(newPath, std::ios::binary); + } } ReturnValue_t TmStore::storePacketInternal(PusTmReader& reader) { return returnvalue::OK; } diff --git a/mission/tmtc/TmStore.h b/mission/tmtc/TmStore.h index 3151a818..98e7bc62 100644 --- a/mission/tmtc/TmStore.h +++ b/mission/tmtc/TmStore.h @@ -47,7 +47,7 @@ class TmStore : public SystemObject { SdCardMountedIF& sdcMan; void calcDiffSeconds(RolloverInterval intervalUnit, uint32_t intervalCount); - void assignMostRecentFile(); + void assignAndOrCreateMostRecentFile(); ReturnValue_t storePacketInternal(PusTmReader& reader); };