Skip to content

Commit 76dfc31

Browse files
committed
f Avoid race-y metadata syscall by using DirEntry::metadata
1 parent 73af4dd commit 76dfc31

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

lightning-persister/src/fs_store.rs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -316,13 +316,11 @@ impl KVStore for FilesystemStore {
316316
let entry = entry?;
317317
let p = entry.path();
318318

319-
// We skip any entries that do not adhere to our key requirements, or for which we get
320-
// an error.
321-
if !dir_entry_is_key(&p).unwrap_or(false) {
319+
if !dir_entry_is_key(&entry)? {
322320
continue;
323321
}
324322

325-
let key = get_key_from_dir_entry(&p, &prefixed_dest)?;
323+
let key = git_key_from_dir_entry_path(&p, &prefixed_dest)?;
326324

327325
keys.push(key);
328326
}
@@ -333,7 +331,8 @@ impl KVStore for FilesystemStore {
333331
}
334332
}
335333

336-
fn dir_entry_is_key(p: &Path) -> Result<bool, lightning::io::Error> {
334+
fn dir_entry_is_key(dir_entry: &fs::DirEntry) -> Result<bool, lightning::io::Error> {
335+
let p = dir_entry.path();
337336
if let Some(ext) = p.extension() {
338337
#[cfg(target_os = "windows")]
339338
{
@@ -348,7 +347,7 @@ fn dir_entry_is_key(p: &Path) -> Result<bool, lightning::io::Error> {
348347
}
349348
}
350349

351-
let metadata = p.metadata().map_err(|e| {
350+
let metadata = dir_entry.metadata().map_err(|e| {
352351
let msg = format!(
353352
"Failed to list keys at path {}: {}",
354353
PrintableString(p.to_str().unwrap_or_default()),
@@ -379,7 +378,7 @@ fn dir_entry_is_key(p: &Path) -> Result<bool, lightning::io::Error> {
379378
Ok(true)
380379
}
381380

382-
fn get_key_from_dir_entry(p: &Path, base_path: &Path) -> Result<String, lightning::io::Error> {
381+
fn git_key_from_dir_entry_path(p: &Path, base_path: &Path) -> Result<String, lightning::io::Error> {
383382
match p.strip_prefix(&base_path) {
384383
Ok(stripped_path) => {
385384
if let Some(relative_path) = stripped_path.to_str() {
@@ -437,24 +436,26 @@ impl MigratableKVStore for FilesystemStore {
437436
let mut keys = Vec::new();
438437

439438
'primary_loop: for primary_entry in fs::read_dir(prefixed_dest)? {
440-
let primary_path = primary_entry?.path();
439+
let primary_entry = primary_entry?;
440+
let primary_path = primary_entry.path();
441441

442-
if dir_entry_is_key(&primary_path)? {
442+
if dir_entry_is_key(&primary_entry)? {
443443
let primary_namespace = String::new();
444444
let secondary_namespace = String::new();
445-
let key = get_key_from_dir_entry(&primary_path, prefixed_dest)?;
445+
let key = git_key_from_dir_entry_path(&primary_path, prefixed_dest)?;
446446
keys.push((primary_namespace, secondary_namespace, key));
447447
continue 'primary_loop;
448448
}
449449

450450
// The primary_entry is actually also a directory.
451451
'secondary_loop: for secondary_entry in fs::read_dir(&primary_path)? {
452-
let secondary_path = secondary_entry?.path();
452+
let secondary_entry = secondary_entry?;
453+
let secondary_path = secondary_entry.path();
453454

454-
if dir_entry_is_key(&secondary_path)? {
455-
let primary_namespace = get_key_from_dir_entry(&primary_path, prefixed_dest)?;
455+
if dir_entry_is_key(&secondary_entry)? {
456+
let primary_namespace = git_key_from_dir_entry_path(&primary_path, prefixed_dest)?;
456457
let secondary_namespace = String::new();
457-
let key = get_key_from_dir_entry(&secondary_path, &primary_path)?;
458+
let key = git_key_from_dir_entry_path(&secondary_path, &primary_path)?;
458459
keys.push((primary_namespace, secondary_namespace, key));
459460
continue 'secondary_loop;
460461
}
@@ -464,12 +465,12 @@ impl MigratableKVStore for FilesystemStore {
464465
let tertiary_entry = tertiary_entry?;
465466
let tertiary_path = tertiary_entry.path();
466467

467-
if dir_entry_is_key(&tertiary_path)? {
468+
if dir_entry_is_key(&tertiary_entry)? {
468469
let primary_namespace =
469-
get_key_from_dir_entry(&primary_path, prefixed_dest)?;
470+
git_key_from_dir_entry_path(&primary_path, prefixed_dest)?;
470471
let secondary_namespace =
471-
get_key_from_dir_entry(&secondary_path, &primary_path)?;
472-
let key = get_key_from_dir_entry(&tertiary_path, &secondary_path)?;
472+
git_key_from_dir_entry_path(&secondary_path, &primary_path)?;
473+
let key = git_key_from_dir_entry_path(&tertiary_path, &secondary_path)?;
473474
keys.push((primary_namespace, secondary_namespace, key));
474475
} else {
475476
debug_assert!(

0 commit comments

Comments
 (0)