@@ -316,89 +316,106 @@ impl KVStore for FilesystemStore {
316
316
let entry = entry?;
317
317
let p = entry. path ( ) ;
318
318
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
- }
319
+ if !dir_entry_is_key ( & p) ? {
320
+ continue ;
331
321
}
332
322
333
- let metadata = p . metadata ( ) ?;
323
+ let key = get_key_from_dir_entry ( & p , & prefixed_dest ) ?;
334
324
335
- // We allow the presence of directories in the empty primary namespace and just skip them.
336
- if metadata. is_dir ( ) {
337
- continue ;
338
- }
325
+ keys. push ( key) ;
326
+ }
339
327
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 ) ) ;
328
+ self . garbage_collect_locks ( ) ;
329
+
330
+ Ok ( keys )
331
+ }
332
+ }
333
+
334
+ fn dir_entry_is_key ( p : & Path ) -> Result < bool , lightning :: io :: Error > {
335
+ if let Some ( ext ) = p . extension ( ) {
336
+ # [ cfg ( target_os = "windows" ) ]
337
+ {
338
+ // Clean up any trash files lying around.
339
+ if ext == "trash" {
340
+ fs :: remove_file ( p ) . ok ( ) ;
341
+ return Ok ( false ) ;
354
342
}
343
+ }
344
+ if ext == "tmp" {
345
+ return Ok ( false ) ;
346
+ }
347
+ }
355
348
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) => {
349
+ let metadata = p. metadata ( ) . map_err ( |e| {
350
+ let msg =
351
+ format ! ( "Failed to list keys at path {:?}: {}" , p. to_str( ) . map( PrintableString ) , e) ;
352
+ lightning:: io:: Error :: new ( lightning:: io:: ErrorKind :: Other , msg)
353
+ } ) ?;
354
+
355
+ // We allow the presence of directories in the empty primary namespace and just skip them.
356
+ if metadata. is_dir ( ) {
357
+ return Ok ( false ) ;
358
+ }
359
+
360
+ // If we otherwise don't find a file at the given path something went wrong.
361
+ if !metadata. is_file ( ) {
362
+ debug_assert ! (
363
+ false ,
364
+ "Failed to list keys at path {:?}: file couldn't be accessed." ,
365
+ p. to_str( ) . map( PrintableString )
366
+ ) ;
367
+ let msg = format ! (
368
+ "Failed to list keys at path {:?}: file couldn't be accessed." ,
369
+ p. to_str( ) . map( PrintableString )
370
+ ) ;
371
+ return Err ( lightning:: io:: Error :: new ( lightning:: io:: ErrorKind :: Other , msg) ) ;
372
+ }
373
+
374
+ Ok ( true )
375
+ }
376
+
377
+ fn get_key_from_dir_entry ( p : & Path , base_path : & Path ) -> Result < String , lightning:: io:: Error > {
378
+ match p. strip_prefix ( & base_path) {
379
+ Ok ( stripped_path) => {
380
+ if let Some ( relative_path) = stripped_path. to_str ( ) {
381
+ if is_valid_kvstore_str ( relative_path) {
382
+ return Ok ( relative_path. to_string ( ) ) ;
383
+ } else {
381
384
debug_assert ! (
382
385
false ,
383
- "Failed to list keys of {}/{}: {}" ,
384
- PrintableString ( primary_namespace) ,
385
- PrintableString ( secondary_namespace) ,
386
- e
386
+ "Failed to list keys of path {:?}: file path is not valid key" ,
387
+ p. to_str( ) . map( PrintableString )
387
388
) ;
388
389
let msg = format ! (
389
- "Failed to list keys of {}/{}: {}" ,
390
- PrintableString ( primary_namespace) ,
391
- PrintableString ( secondary_namespace) ,
392
- e
390
+ "Failed to list keys of path {:?}: file path is not valid key" ,
391
+ p. to_str( ) . map( PrintableString )
393
392
) ;
394
393
return Err ( lightning:: io:: Error :: new ( lightning:: io:: ErrorKind :: Other , msg) ) ;
395
- } ,
394
+ }
395
+ } else {
396
+ debug_assert ! (
397
+ false ,
398
+ "Failed to list keys of path {:?}: file path is not valid UTF-8" ,
399
+ p
400
+ ) ;
401
+ let msg = format ! (
402
+ "Failed to list keys of path {:?}: file path is not valid UTF-8" ,
403
+ p. to_str( ) . map( PrintableString )
404
+ ) ;
405
+ return Err ( lightning:: io:: Error :: new ( lightning:: io:: ErrorKind :: Other , msg) ) ;
396
406
}
397
- }
398
-
399
- self . garbage_collect_locks ( ) ;
400
-
401
- Ok ( keys)
407
+ } ,
408
+ Err ( e) => {
409
+ debug_assert ! (
410
+ false ,
411
+ "Failed to list keys of path {:?}: {}" ,
412
+ p. to_str( ) . map( PrintableString ) ,
413
+ e
414
+ ) ;
415
+ let msg =
416
+ format ! ( "Failed to list keys of path {:?}: {}" , p. to_str( ) . map( PrintableString ) , e) ;
417
+ return Err ( lightning:: io:: Error :: new ( lightning:: io:: ErrorKind :: Other , msg) ) ;
418
+ } ,
402
419
}
403
420
}
404
421
0 commit comments