Skip to content

Commit

Permalink
fix: do not delete files if cannot read their metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
link2xt committed Feb 26, 2025
1 parent a9fbdaf commit ae1bc54
Showing 1 changed file with 36 additions and 31 deletions.
67 changes: 36 additions & 31 deletions src/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -914,42 +914,47 @@ pub async fn remove_unused_files(context: &Context) -> Result<()> {
continue;
}

if let Ok(stats) = tokio::fs::metadata(entry.path()).await {
if stats.is_dir() {
if let Err(e) = tokio::fs::remove_dir(entry.path()).await {
// The dir could be created not by a user, but by a desktop
// environment f.e. So, no warning.
info!(
context,
"Housekeeping: Cannot rmdir {}: {:#}.",
entry.path().display(),
e
);
}
continue;
}
unreferenced_count += 1;
let recently_created =
stats.created().is_ok_and(|t| t > keep_files_newer_than);
let recently_modified =
stats.modified().is_ok_and(|t| t > keep_files_newer_than);
let recently_accessed =
stats.accessed().is_ok_and(|t| t > keep_files_newer_than);

if p == blobdir
&& (recently_created || recently_modified || recently_accessed)
{
let Ok(stats) = tokio::fs::metadata(entry.path()).await else {
warn!(
context,
"Cannot get metadata for {}.",
entry.path().display()
);
continue;
};

if stats.is_dir() {
if let Err(e) = tokio::fs::remove_dir(entry.path()).await {
// The dir could be created not by a user, but by a desktop
// environment f.e. So, no warning.
info!(
context,
"Housekeeping: Keeping new unreferenced file #{}: {:?}.",
unreferenced_count,
entry.file_name(),
"Housekeeping: Cannot rmdir {}: {:#}.",
entry.path().display(),
e
);
continue;
}
} else {
unreferenced_count += 1;
continue;
}

unreferenced_count += 1;
let recently_created = stats.created().is_ok_and(|t| t > keep_files_newer_than);
let recently_modified =
stats.modified().is_ok_and(|t| t > keep_files_newer_than);
let recently_accessed =
stats.accessed().is_ok_and(|t| t > keep_files_newer_than);

if p == blobdir && (recently_created || recently_modified || recently_accessed)
{
info!(
context,
"Housekeeping: Keeping new unreferenced file #{}: {:?}.",
unreferenced_count,
entry.file_name(),
);
continue;
}

info!(
context,
"Housekeeping: Deleting unreferenced file #{}: {:?}.",
Expand Down

0 comments on commit ae1bc54

Please sign in to comment.