create files as well

This commit is contained in:
Robin Müller 2022-12-13 10:07:36 +01:00
parent 1f381d9477
commit e62c527d05
No known key found for this signature in database
GPG Key ID: 71B58F8A3CDFA9AC
2 changed files with 28 additions and 5 deletions

View File

@ -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<int>(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(&currentTv); }
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(&currentTv, &tod);
currentIdx += sprintf(reinterpret_cast<char*>(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<const char*>(fileBuf.data()), currentIdx));
std::ofstream of(newPath, std::ios::binary);
}
}
ReturnValue_t TmStore::storePacketInternal(PusTmReader& reader) { return returnvalue::OK; }

View File

@ -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);
};