@@ -236,7 +236,6 @@ fn copy_file(_: &Lua, (source, destination): (LuaFilePath, LuaFilePath)) -> LuaR
236
236
237
237
fn absolute ( _: & Lua , path : LuaFilePath ) -> LuaResult < LuaFilePath > {
238
238
let canonical_path = std:: fs:: canonicalize ( & path. path ) ?;
239
-
240
239
let full_path_str = canonical_path. to_str ( ) . unwrap_or ( "" ) . to_string ( ) ;
241
240
242
241
// 移除 \\?\ 前缀
@@ -360,6 +359,35 @@ fn symlink_status(_: &Lua, path: LuaFilePath) -> LuaResult<SymlinkStatus> {
360
359
Ok ( SymlinkStatus :: new ( file_type) )
361
360
}
362
361
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
+
363
391
pub fn bee_filesystem ( lua : & Lua ) -> LuaResult < Table > {
364
392
let exports = lua. create_table ( ) ?;
365
393
@@ -397,17 +425,7 @@ pub fn bee_filesystem(lua: &Lua) -> LuaResult<Table> {
397
425
) ?;
398
426
exports. set (
399
427
"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) ?,
411
429
) ?;
412
430
exports. set ( "fullpath" , lua. create_function ( full_path) ?) ?;
413
431
exports. set ( "symlink_status" , lua. create_function ( symlink_status) ?) ?;
0 commit comments