@@ -346,7 +346,6 @@ impl RemoteFs for LocalDirRemoteFs {
346346 let result = Self :: list_recursive (
347347 remote_dir. clone ( ) . unwrap_or ( self . dir . clone ( ) ) ,
348348 remote_prefix. to_string ( ) ,
349- remote_dir. unwrap_or ( self . dir . clone ( ) ) ,
350349 )
351350 . await ?;
352351 Ok ( result)
@@ -395,45 +394,50 @@ impl LocalDirRemoteFs {
395394 Ok ( ( ) )
396395 }
397396
398- fn list_recursive_boxed (
397+ pub async fn list_recursive (
399398 remote_dir : PathBuf ,
400399 remote_prefix : String ,
401- dir : PathBuf ,
402- ) -> BoxFuture < ' static , Result < Vec < RemoteFile > , CubeError > > {
403- async move { Self :: list_recursive ( remote_dir, remote_prefix, dir) . await } . boxed ( )
400+ ) -> Result < Vec < RemoteFile > , CubeError > {
401+ let mut result_builder = Vec :: new ( ) ;
402+ Self :: list_recursive_helper ( remote_dir, remote_prefix, & mut result_builder, & mut PathBuf :: new ( ) ) . await ?;
403+ Ok ( result_builder)
404404 }
405405
406- pub async fn list_recursive (
407- remote_dir : PathBuf ,
406+ fn list_recursive_boxed_helper < ' a > (
407+ dir : PathBuf ,
408408 remote_prefix : String ,
409+ result_builder : & ' a mut Vec < RemoteFile > ,
410+ relative_prefix : & ' a mut PathBuf ,
411+ ) -> BoxFuture < ' a , Result < ( ) , CubeError > > {
412+ async move { Self :: list_recursive_helper ( dir, remote_prefix, result_builder, relative_prefix) . await } . boxed ( )
413+ }
414+
415+ async fn list_recursive_helper (
409416 dir : PathBuf ,
410- ) -> Result < Vec < RemoteFile > , CubeError > {
411- let mut result = Vec :: new ( ) ;
417+ remote_prefix : String ,
418+ result_builder : & mut Vec < RemoteFile > ,
419+ relative_prefix : & mut PathBuf ,
420+ ) -> Result < ( ) , CubeError > {
412421 if fs:: metadata ( dir. clone ( ) ) . await . is_err ( ) {
413- return Ok ( vec ! [ ] ) ;
422+ return Ok ( ( ) ) ;
414423 }
415424 if let Ok ( mut dir) = fs:: read_dir ( dir) . await {
416425 while let Ok ( Some ( file) ) = dir. next_entry ( ) . await {
417426 if let Ok ( true ) = file. file_type ( ) . await . map ( |r| r. is_dir ( ) ) {
418- result. append (
419- & mut Self :: list_recursive_boxed (
420- remote_dir. clone ( ) ,
421- remote_prefix. to_string ( ) ,
427+ relative_prefix. push ( file. file_name ( ) ) ;
428+ Self :: list_recursive_boxed_helper (
422429 file. path ( ) ,
430+ remote_prefix. to_string ( ) ,
431+ result_builder,
432+ relative_prefix,
423433 )
424- . await ?,
425- ) ;
434+ . await ?;
435+ relative_prefix . pop ( ) ;
426436 } else if let Ok ( metadata) = file. metadata ( ) . await {
427- let relative_name = file
428- . path ( )
429- . to_str ( )
430- . unwrap ( )
431- . to_string ( )
432- . replace ( & remote_dir. to_str ( ) . unwrap ( ) . to_string ( ) , "" )
433- . trim_start_matches ( "/" )
434- . to_string ( ) ;
437+ let relative_path = relative_prefix. join ( file. file_name ( ) ) ;
438+ let relative_name = relative_path. to_str ( ) . unwrap ( ) ;
435439 if relative_name. starts_with ( & remote_prefix) {
436- result . push ( RemoteFile {
440+ result_builder . push ( RemoteFile {
437441 remote_path : relative_name. to_string ( ) ,
438442 updated : DateTime :: from ( metadata. modified ( ) ?) ,
439443 file_size : metadata. len ( ) ,
@@ -442,7 +446,7 @@ impl LocalDirRemoteFs {
442446 }
443447 }
444448 }
445- Ok ( result )
449+ Ok ( ( ) )
446450 }
447451}
448452
0 commit comments