Skip to content

Commit 1b62220

Browse files
Delete all empty parent directories if they are empty
This saves a lot of memory since each directory takes at least one block We are starting to see this as an issue because the filesystems are now much deeper due to the SE050
1 parent ceacafc commit 1b62220

1 file changed

Lines changed: 27 additions & 2 deletions

File tree

src/store.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
//! - Alternative: subdirectory <==> RP hash, everything else in flat files
7373
//! - In any case need to "list dirs excluding . and .." or similar
7474
75-
use littlefs2::{driver::Storage, fs::Filesystem};
75+
use littlefs2::{driver::Storage, fs::Filesystem, path};
7676

7777
use crate::error::Error;
7878
use crate::types::{Bytes, Location};
@@ -554,7 +554,32 @@ pub fn store(
554554
#[inline(never)]
555555
pub fn delete(store: impl Store, location: Location, path: &Path) -> bool {
556556
debug_now!("deleting {}", &path);
557-
store.fs(location).remove(path).is_ok()
557+
let fs = store.fs(location);
558+
if fs.remove(path).is_err() {
559+
return false;
560+
}
561+
562+
// Only delete ancestors for volatile FS
563+
if location != Location::Volatile {
564+
return true;
565+
}
566+
// first ancestor is the file itself
567+
for parent in path.ancestors().skip(1) {
568+
if &*parent == path!("/") {
569+
break;
570+
}
571+
let Ok(meta) = fs.metadata(&parent) else {
572+
return false;
573+
};
574+
if meta.is_dir() && meta.is_empty() {
575+
if fs.remove_dir(&parent).is_err() {
576+
return false;
577+
}
578+
} else {
579+
break;
580+
}
581+
}
582+
true
558583
}
559584

560585
#[inline(never)]

0 commit comments

Comments
 (0)