@@ -68,6 +68,17 @@ local function open_dir(p)
6868 return iter , dir_obj
6969end
7070
71+ local function check_dir (p )
72+ local mode , err = lfs .attributes (p , " mode" )
73+ if not mode then
74+ return nil , err
75+ end
76+ if mode ~= " directory" then
77+ return nil , " Not a directory"
78+ end
79+ return true
80+ end
81+
7182--- @param name LuaFileSystem.AttributeName
7283local function get_attr (p , name )
7384 local value , errmsg , errcode = lfs .attributes (p , name )
@@ -426,8 +437,14 @@ function M.listdir(p, opts)
426437 assert_arg (2 , opts , " table" , true )
427438 opts = normalize_dir_opts (" listdir" , opts )
428439
440+ local ok , err
441+ ok , err = check_dir (p )
442+ if not ok then
443+ return nil , err
444+ end
445+
429446 local items = {}
430- local ok , err = collect_dir_items (p , opts , items , not opts .names )
447+ ok , err = collect_dir_items (p , opts , items , not opts .names )
431448 if not ok then
432449 return nil , err
433450 end
@@ -444,16 +461,14 @@ function M.dir(p, opts)
444461 assert_arg (2 , opts , " table" , true )
445462 opts = normalize_dir_opts (" dir" , opts )
446463
447- local mode , err = lfs .attributes (p , " mode" )
448- if not mode then
464+ local ok , err
465+ ok , err = check_dir (p )
466+ if not ok then
449467 return nil , err
450468 end
451- if mode ~= " directory" then
452- return nil , " Not a directory"
453- end
454469
455470 local items = {}
456- local ok , err = collect_dir_items (p , opts , items , false )
471+ ok , err = collect_dir_items (p , opts , items , false )
457472 if not ok then
458473 return nil , err
459474 end
0 commit comments