@@ -236,7 +236,6 @@ fn copy_file(_: &Lua, (source, destination): (LuaFilePath, LuaFilePath)) -> LuaR
236236
237237fn absolute ( _: & Lua , path : LuaFilePath ) -> LuaResult < LuaFilePath > {
238238 let canonical_path = std:: fs:: canonicalize ( & path. path ) ?;
239-
240239 let full_path_str = canonical_path. to_str ( ) . unwrap_or ( "" ) . to_string ( ) ;
241240
242241 // 移除 \\?\ 前缀
@@ -360,6 +359,35 @@ fn symlink_status(_: &Lua, path: LuaFilePath) -> LuaResult<SymlinkStatus> {
360359 Ok ( SymlinkStatus :: new ( file_type) )
361360}
362361
362+ fn pairs ( lua : & Lua , path : LuaFilePath ) -> LuaResult < ( mlua:: Function , mlua:: Table , mlua:: Value ) > {
363+ let table = lua. create_table ( ) ?;
364+ if let Ok ( _) = std:: fs:: exists ( & path. path ) {
365+ for entry in std:: fs:: read_dir ( & path. path ) ? {
366+ let entry = entry?;
367+ let path = entry. path ( ) ;
368+ let path = LuaFilePath :: new ( path. to_str ( ) . unwrap_or ( "" ) . to_string ( ) ) ;
369+ let file_type = match std:: fs:: symlink_metadata ( & path. path ) {
370+ Ok ( metadata) => {
371+ if metadata. file_type ( ) . is_symlink ( ) {
372+ "symlink" . to_string ( )
373+ } else if metadata. file_type ( ) . is_file ( ) {
374+ "file" . to_string ( )
375+ } else if metadata. file_type ( ) . is_dir ( ) {
376+ "directory" . to_string ( )
377+ } else {
378+ "unknown" . to_string ( )
379+ }
380+ }
381+ Err ( _) => "unknown" . to_string ( ) ,
382+ } ;
383+ let file_status = SymlinkStatus :: new ( file_type) ;
384+ table. set ( path. clone ( ) , file_status) ?;
385+ }
386+ }
387+ let next = lua. globals ( ) . get :: < mlua:: Function > ( "next" ) . unwrap ( ) ;
388+ Ok ( ( next, table, mlua:: Nil ) )
389+ }
390+
363391pub fn bee_filesystem ( lua : & Lua ) -> LuaResult < Table > {
364392 let exports = lua. create_table ( ) ?;
365393
@@ -397,17 +425,7 @@ pub fn bee_filesystem(lua: &Lua) -> LuaResult<Table> {
397425 ) ?;
398426 exports. set (
399427 "pairs" ,
400- lua. create_function ( |lua, path : LuaFilePath | -> LuaResult < _ > {
401- let table = lua. create_table ( ) ?;
402- for entry in std:: fs:: read_dir ( & path. path ) ? {
403- let entry = entry?;
404- let path = entry. path ( ) ;
405- let path = LuaFilePath :: new ( path. to_str ( ) . unwrap_or ( "" ) . to_string ( ) ) ;
406- table. set ( path. clone ( ) , true ) ?;
407- }
408- let next = lua. globals ( ) . get :: < mlua:: Function > ( "next" ) . unwrap ( ) ;
409- Ok ( ( next, table, mlua:: Nil ) )
410- } ) ?,
428+ lua. create_function ( pairs) ?,
411429 ) ?;
412430 exports. set ( "fullpath" , lua. create_function ( full_path) ?) ?;
413431 exports. set ( "symlink_status" , lua. create_function ( symlink_status) ?) ?;
0 commit comments