@@ -316,89 +316,112 @@ impl KVStore for FilesystemStore {
316316 let entry = entry?;
317317 let p = entry. path ( ) ;
318318
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 ;
331321 }
332322
333- let metadata = p . metadata ( ) ?;
323+ let key = get_key_from_dir_entry ( & p , & prefixed_dest ) ?;
334324
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+ }
339327
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 ) ;
354342 }
343+ }
344+ if ext == "tmp" {
345+ return Ok ( false ) ;
346+ }
347+ }
355348
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 = format ! (
351+ "Failed to list keys at path {}: {}" ,
352+ PrintableString ( p. to_str( ) . unwrap_or_default( ) ) ,
353+ e
354+ ) ;
355+ lightning:: io:: Error :: new ( lightning:: io:: ErrorKind :: Other , msg)
356+ } ) ?;
357+
358+ // We allow the presence of directories in the empty primary namespace and just skip them.
359+ if metadata. is_dir ( ) {
360+ return Ok ( false ) ;
361+ }
362+
363+ // If we otherwise don't find a file at the given path something went wrong.
364+ if !metadata. is_file ( ) {
365+ debug_assert ! (
366+ false ,
367+ "Failed to list keys at path {}: file couldn't be accessed." ,
368+ PrintableString ( p. to_str( ) . unwrap_or_default( ) )
369+ ) ;
370+ let msg = format ! (
371+ "Failed to list keys at path {}: file couldn't be accessed." ,
372+ PrintableString ( p. to_str( ) . unwrap_or_default( ) )
373+ ) ;
374+ return Err ( lightning:: io:: Error :: new ( lightning:: io:: ErrorKind :: Other , msg) ) ;
375+ }
376+
377+ Ok ( true )
378+ }
379+
380+ fn get_key_from_dir_entry ( p : & Path , base_path : & Path ) -> Result < String , lightning:: io:: Error > {
381+ match p. strip_prefix ( & base_path) {
382+ Ok ( stripped_path) => {
383+ if let Some ( relative_path) = stripped_path. to_str ( ) {
384+ if is_valid_kvstore_str ( relative_path) {
385+ return Ok ( relative_path. to_string ( ) ) ;
386+ } else {
381387 debug_assert ! (
382388 false ,
383- "Failed to list keys of {}/{}: {}" ,
384- PrintableString ( primary_namespace) ,
385- PrintableString ( secondary_namespace) ,
386- e
389+ "Failed to list keys of path {}: file path is not valid key" ,
390+ PrintableString ( p. to_str( ) . unwrap_or_default( ) )
387391 ) ;
388392 let msg = format ! (
389- "Failed to list keys of {}/{}: {}" ,
390- PrintableString ( primary_namespace) ,
391- PrintableString ( secondary_namespace) ,
392- e
393+ "Failed to list keys of path {}: file path is not valid key" ,
394+ PrintableString ( p. to_str( ) . unwrap_or_default( ) )
393395 ) ;
394396 return Err ( lightning:: io:: Error :: new ( lightning:: io:: ErrorKind :: Other , msg) ) ;
395- } ,
397+ }
398+ } else {
399+ debug_assert ! (
400+ false ,
401+ "Failed to list keys of path {}: file path is not valid UTF-8" ,
402+ PrintableString ( p. to_str( ) . unwrap_or_default( ) )
403+ ) ;
404+ let msg = format ! (
405+ "Failed to list keys of path {}: file path is not valid UTF-8" ,
406+ PrintableString ( p. to_str( ) . unwrap_or_default( ) )
407+ ) ;
408+ return Err ( lightning:: io:: Error :: new ( lightning:: io:: ErrorKind :: Other , msg) ) ;
396409 }
397- }
398-
399- self . garbage_collect_locks ( ) ;
400-
401- Ok ( keys)
410+ } ,
411+ Err ( e) => {
412+ debug_assert ! (
413+ false ,
414+ "Failed to list keys of path {}: {}" ,
415+ PrintableString ( p. to_str( ) . unwrap_or_default( ) ) ,
416+ e
417+ ) ;
418+ let msg = format ! (
419+ "Failed to list keys of path {}: {}" ,
420+ PrintableString ( p. to_str( ) . unwrap_or_default( ) ) ,
421+ e
422+ ) ;
423+ return Err ( lightning:: io:: Error :: new ( lightning:: io:: ErrorKind :: Other , msg) ) ;
424+ } ,
402425 }
403426}
404427
0 commit comments