@@ -565,6 +565,7 @@ bool DataStore::putBlobByKey(const uint8_t key[], int key_len, const uint8_t src
565565bool 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
569570inline 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
0 commit comments