Skip to content

Commit fca93c7

Browse files
committed
Move cleanup logic to DataStore
1 parent cc09104 commit fca93c7

3 files changed

Lines changed: 37 additions & 29 deletions

File tree

examples/companion_radio/DataStore.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,7 @@ bool DataStore::putBlobByKey(const uint8_t key[], int key_len, const uint8_t src
565565
bool DataStore::deleteBlobByKey(const uint8_t key[], int key_len) {
566566
return true; // this is just a stub on NRF52/STM32 platforms
567567
}
568+
void DataStore::cleanOrphanBlobs(DataStoreHost* host) {}
568569
#else
569570
inline void makeBlobPath(const uint8_t key[], int key_len, char* path, size_t path_size) {
570571
char fname[18];
@@ -608,7 +609,39 @@ bool DataStore::deleteBlobByKey(const uint8_t key[], int key_len) {
608609
makeBlobPath(key, key_len, path, sizeof(path));
609610

610611
_fs->remove(path);
611-
612+
612613
return true; // return true even if file did not exist
613614
}
615+
616+
void DataStore::cleanOrphanBlobs(DataStoreHost* host) {
617+
if (_fs->exists("/bl/.cleaned")) return;
618+
MESH_DEBUG_PRINTLN("Cleaning orphan blobs...");
619+
File root = openRead("/bl");
620+
if (root) {
621+
for (File f = root.openNextFile(); f; f = root.openNextFile()) {
622+
const char* name = f.name();
623+
f.close();
624+
if (name[0] == '.' || strlen(name) != 16) continue;
625+
uint8_t file_key[8];
626+
if (!mesh::Utils::fromHex(file_key, 8, name)) continue;
627+
bool found = false;
628+
ContactInfo c;
629+
for (uint32_t i = 0; host->getContactForSave(i, c) && !found; i++) {
630+
found = (memcmp(file_key, c.id.pub_key, 8) == 0);
631+
}
632+
if (!found) {
633+
char path[24];
634+
sprintf(path, "/bl/%s", name);
635+
_fs->remove(path);
636+
}
637+
}
638+
root.close();
639+
}
640+
#if defined(ESP32)
641+
File m = _fs->open("/bl/.cleaned", "w", true);
642+
#else
643+
File m = _fs->open("/bl/.cleaned", "w");
644+
#endif
645+
if (m) m.close();
646+
}
614647
#endif

examples/companion_radio/DataStore.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class DataStore {
4343
uint8_t getBlobByKey(const uint8_t key[], int key_len, uint8_t dest_buf[]);
4444
bool putBlobByKey(const uint8_t key[], int key_len, const uint8_t src_buf[], uint8_t len);
4545
bool deleteBlobByKey(const uint8_t key[], int key_len);
46+
void cleanOrphanBlobs(DataStoreHost* host);
4647
File openRead(const char* filename);
4748
File openRead(FILESYSTEM* fs, const char* filename);
4849
bool removeFile(const char* filename);

examples/companion_radio/MyMesh.cpp

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -879,34 +879,8 @@ void MyMesh::begin(bool has_display) {
879879
_store->loadContacts(this);
880880
bootstrapRTCfromContacts();
881881

882-
#if defined(ESP32) || defined(RP2040_PLATFORM)
883-
// One-time cleanup of orphan blobs from pre-v1.13 firmware
884-
FILESYSTEM* fs = _store->getPrimaryFS();
885-
if (!fs->exists("/bl/.cleaned")) {
886-
MESH_DEBUG_PRINTLN("Cleaning orphan blobs...");
887-
File root = _store->openRead("/bl");
888-
if (root) {
889-
for (File f = root.openNextFile(); f; f = root.openNextFile()) {
890-
const char* name = f.name();
891-
f.close();
892-
uint8_t key[8];
893-
if (name[0] != '.' && strlen(name) == 16 && mesh::Utils::fromHex(key, 8, name)) {
894-
bool found = false;
895-
for (int i = 0; i < num_contacts && !found; i++)
896-
found = (memcmp(contacts[i].id.pub_key, key, 8) == 0);
897-
if (!found) _store->deleteBlobByKey(key, 8);
898-
}
899-
}
900-
root.close();
901-
}
902-
#if defined(ESP32)
903-
File m = fs->open("/bl/.cleaned", "w", true);
904-
#else
905-
File m = fs->open("/bl/.cleaned", "w");
906-
#endif
907-
if (m) m.close();
908-
}
909-
#endif
882+
_store->cleanOrphanBlobs(this);
883+
910884
addChannel("Public", PUBLIC_GROUP_PSK); // pre-configure Andy's public channel
911885
_store->loadChannels(this);
912886

0 commit comments

Comments
 (0)