Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete all empty parent directories if they are empty #181

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
//! - Alternative: subdirectory <==> RP hash, everything else in flat files
//! - In any case need to "list dirs excluding . and .." or similar

use littlefs2::{driver::Storage, fs::Filesystem};
use littlefs2::{driver::Storage, fs::Filesystem, path};

use crate::error::Error;
use crate::types::{Bytes, Location};
Expand Down Expand Up @@ -554,7 +554,32 @@ pub fn store(
#[inline(never)]
pub fn delete(store: impl Store, location: Location, path: &Path) -> bool {
debug_now!("deleting {}", &path);
store.fs(location).remove(path).is_ok()
let fs = store.fs(location);
if fs.remove(path).is_err() {
return false;
}

// Only delete ancestors for volatile FS
if location != Location::Volatile {
return true;
}
// first ancestor is the file itself
for parent in path.ancestors().skip(1) {
if &*parent == path!("/") {
break;
}
let Ok(meta) = fs.metadata(&parent) else {
return false;
};
if meta.is_dir() && meta.is_empty() {
if fs.remove_dir(&parent).is_err() {
return false;
}
} else {
break;
}
}
true
}

#[inline(never)]
Expand Down
Loading