Skip to content

Commit 683a2b2

Browse files
committed
Prefactor: DRY up dir entry checks and key extraction to helper methods
.. which we'll be reusing shortly.
1 parent 91ae8b8 commit 683a2b2

File tree

1 file changed

+73
-71
lines changed

1 file changed

+73
-71
lines changed

lightning-persister/src/fs_store.rs

Lines changed: 73 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
use crate::utils::{check_namespace_key_validity, is_valid_kvstore_str};
33

44
use lightning::util::persist::KVStore;
5-
use lightning::util::string::PrintableString;
65

76
use std::collections::HashMap;
87
use std::fs;
@@ -316,89 +315,92 @@ impl KVStore for FilesystemStore {
316315
let entry = entry?;
317316
let p = entry.path();
318317

319-
if let Some(ext) = p.extension() {
320-
#[cfg(target_os = "windows")]
321-
{
322-
// Clean up any trash files lying around.
323-
if ext == "trash" {
324-
fs::remove_file(p).ok();
325-
continue;
326-
}
327-
}
328-
if ext == "tmp" {
329-
continue;
330-
}
318+
if !dir_entry_is_key(&p)? {
319+
continue;
331320
}
332321

333-
let metadata = p.metadata()?;
322+
let key = get_key_from_dir_entry(&p, &prefixed_dest)?;
334323

335-
// We allow the presence of directories in the empty primary namespace and just skip them.
336-
if metadata.is_dir() {
337-
continue;
338-
}
324+
keys.push(key);
325+
}
339326

340-
// If we otherwise don't find a file at the given path something went wrong.
341-
if !metadata.is_file() {
342-
debug_assert!(
343-
false,
344-
"Failed to list keys of {}/{}: file couldn't be accessed.",
345-
PrintableString(primary_namespace),
346-
PrintableString(secondary_namespace)
347-
);
348-
let msg = format!(
349-
"Failed to list keys of {}/{}: file couldn't be accessed.",
350-
PrintableString(primary_namespace),
351-
PrintableString(secondary_namespace)
352-
);
353-
return Err(lightning::io::Error::new(lightning::io::ErrorKind::Other, msg));
327+
self.garbage_collect_locks();
328+
329+
Ok(keys)
330+
}
331+
}
332+
333+
fn dir_entry_is_key(p: &Path) -> Result<bool, lightning::io::Error> {
334+
if let Some(ext) = p.extension() {
335+
#[cfg(target_os = "windows")]
336+
{
337+
// Clean up any trash files lying around.
338+
if ext == "trash" {
339+
fs::remove_file(p).ok();
340+
return Ok(false);
354341
}
342+
}
343+
if ext == "tmp" {
344+
return Ok(false);
345+
}
346+
}
355347

356-
match p.strip_prefix(&prefixed_dest) {
357-
Ok(stripped_path) => {
358-
if let Some(relative_path) = stripped_path.to_str() {
359-
if is_valid_kvstore_str(relative_path) {
360-
keys.push(relative_path.to_string())
361-
}
362-
} else {
363-
debug_assert!(
364-
false,
365-
"Failed to list keys of {}/{}: file path is not valid UTF-8",
366-
PrintableString(primary_namespace),
367-
PrintableString(secondary_namespace)
368-
);
369-
let msg = format!(
370-
"Failed to list keys of {}/{}: file path is not valid UTF-8",
371-
PrintableString(primary_namespace),
372-
PrintableString(secondary_namespace)
373-
);
374-
return Err(lightning::io::Error::new(
375-
lightning::io::ErrorKind::Other,
376-
msg,
377-
));
378-
}
379-
},
380-
Err(e) => {
348+
let metadata = p.metadata()?;
349+
350+
// We allow the presence of directories in the empty primary namespace and just skip them.
351+
if metadata.is_dir() {
352+
return Ok(false);
353+
}
354+
355+
// If we otherwise don't find a file at the given path something went wrong.
356+
if !metadata.is_file() {
357+
debug_assert!(
358+
false,
359+
"Failed to list keys at path {:?}: file couldn't be accessed.",
360+
p.to_str()
361+
);
362+
let msg =
363+
format!("Failed to list keys at path {:?}: file couldn't be accessed.", p.to_str());
364+
return Err(lightning::io::Error::new(lightning::io::ErrorKind::Other, msg));
365+
}
366+
367+
Ok(true)
368+
}
369+
370+
fn get_key_from_dir_entry(p: &Path, base_path: &Path) -> Result<String, lightning::io::Error> {
371+
match p.strip_prefix(&base_path) {
372+
Ok(stripped_path) => {
373+
if let Some(relative_path) = stripped_path.to_str() {
374+
if is_valid_kvstore_str(relative_path) {
375+
return Ok(relative_path.to_string());
376+
} else {
381377
debug_assert!(
382378
false,
383-
"Failed to list keys of {}/{}: {}",
384-
PrintableString(primary_namespace),
385-
PrintableString(secondary_namespace),
386-
e
379+
"Failed to list keys of path {:?}: file path is not valid key",
380+
p.to_str()
387381
);
388382
let msg = format!(
389-
"Failed to list keys of {}/{}: {}",
390-
PrintableString(primary_namespace),
391-
PrintableString(secondary_namespace),
392-
e
383+
"Failed to list keys of path {:?}: file path is not valid key",
384+
p.to_str()
393385
);
394386
return Err(lightning::io::Error::new(lightning::io::ErrorKind::Other, msg));
395-
},
387+
}
388+
} else {
389+
debug_assert!(
390+
false,
391+
"Failed to list keys of path {:?}: file path is not valid UTF-8",
392+
p
393+
);
394+
let msg =
395+
format!("Failed to list keys of path {:?}: file path is not valid UTF-8", p);
396+
return Err(lightning::io::Error::new(lightning::io::ErrorKind::Other, msg));
396397
}
397-
}
398-
399-
self.garbage_collect_locks();
400-
401-
Ok(keys)
398+
},
399+
Err(e) => {
400+
debug_assert!(false, "Failed to list keys of path {:?}: {}", p, e);
401+
let msg = format!("Failed to list keys of path {:?}: {}", p, e);
402+
return Err(lightning::io::Error::new(lightning::io::ErrorKind::Other, msg));
403+
},
402404
}
403405
}
404406

0 commit comments

Comments
 (0)