Skip to content
Open
Show file tree
Hide file tree
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
21 changes: 16 additions & 5 deletions lightning-persister/src/fs_store/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,9 @@ impl FilesystemStoreState {
'primary_loop: for primary_entry in fs::read_dir(prefixed_dest)? {
let primary_entry = primary_entry?;
let primary_path = primary_entry.path();
if dir_entry_is_store_artifact(&primary_path) {
continue 'primary_loop;
}

if dir_entry_is_key(&primary_entry)? {
let primary_namespace = String::new();
Expand All @@ -666,6 +669,9 @@ impl FilesystemStoreState {
'secondary_loop: for secondary_entry in fs::read_dir(&primary_path)? {
let secondary_entry = secondary_entry?;
let secondary_path = secondary_entry.path();
if dir_entry_is_store_artifact(&secondary_path) {
continue 'secondary_loop;
}

if dir_entry_is_key(&secondary_entry)? {
let primary_namespace = get_key_from_dir_entry_path(
Expand All @@ -683,6 +689,9 @@ impl FilesystemStoreState {
for tertiary_entry in fs::read_dir(&secondary_path)? {
let tertiary_entry = tertiary_entry?;
let tertiary_path = tertiary_entry.path();
if dir_entry_is_store_artifact(&tertiary_path) {
continue;
}

if dir_entry_is_key(&tertiary_entry)? {
let primary_namespace = get_key_from_dir_entry_path(
Expand Down Expand Up @@ -720,20 +729,22 @@ impl FilesystemStoreState {
}
}

fn dir_entry_is_store_artifact(path: &Path) -> bool {
path.extension().and_then(|ext| ext.to_str()).is_some_and(|ext| ext == "tmp" || ext == "trash")
}

pub(crate) fn dir_entry_is_key(dir_entry: &fs::DirEntry) -> Result<bool, lightning::io::Error> {
let p = dir_entry.path();
if let Some(ext) = p.extension() {
if dir_entry_is_store_artifact(&p) {
#[cfg(target_os = "windows")]
{
// Clean up any trash files lying around.
if ext == "trash" {
if p.extension().and_then(|ext| ext.to_str()) == Some("trash") {
fs::remove_file(p).ok();
return Ok(false);
}
}
if ext == "tmp" {
return Ok(false);
}
return Ok(false);
}

Comment on lines 729 to 749
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old dir_entry_is_key had a #[cfg(target_os = "windows")] block that would call fs::remove_file(p).ok() on .trash files, providing opportunistic cleanup. This refactor removes that cleanup.

The comment at line 412–413 in the remove() function says:

"We're fine if this remove would fail as the trash file will be cleaned up in list eventually."

That relied on dir_entry_is_key() cleaning up .trash files when called from list(). With this change, .trash files are now silently skipped everywhere and never cleaned up, so that comment is stale and .trash files will accumulate on Windows if remove_file at line 414 fails.

Consider either:

  1. Keeping the Windows .trash cleanup in dir_entry_is_key() (or the new dir_entry_is_store_artifact function), or
  2. Updating the comment at line 412 to reflect that trash files may now persist.

let file_type = dir_entry.file_type()?;
Expand Down
22 changes: 22 additions & 0 deletions lightning-persister/src/fs_store/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,28 @@ mod tests {
assert_eq!(listed_keys.len(), 0);
}

#[test]
fn list_all_keys_skips_leftover_store_artifacts() {
let mut temp_path = std::env::temp_dir();
temp_path.push("test_list_all_keys_skips_leftover_store_artifacts");
let fs_store = FilesystemStore::new(temp_path.clone());
KVStoreSync::write(&fs_store, "primary", "secondary", "key", vec![1]).unwrap();

fs::write(temp_path.join("top_level.0.tmp"), b"stale").unwrap();
fs::write(temp_path.join("top_level.0.trash"), b"stale").unwrap();

let primary_path = temp_path.join("primary");
fs::write(primary_path.join("primary_level.0.tmp"), b"stale").unwrap();
fs::write(primary_path.join("primary_level.0.trash"), b"stale").unwrap();

let secondary_path = primary_path.join("secondary");
fs::write(secondary_path.join("secondary_level.0.tmp"), b"stale").unwrap();
fs::write(secondary_path.join("secondary_level.0.trash"), b"stale").unwrap();

let keys = fs_store.list_all_keys().unwrap();
assert_eq!(keys, vec![("primary".to_string(), "secondary".to_string(), "key".to_string())]);
}

#[test]
fn test_data_migration() {
let mut source_temp_path = std::env::temp_dir();
Expand Down
Loading