19
19
rename,
20
20
readlink,
21
21
readdir,
22
+ readdirx,
22
23
rm,
23
24
samefile,
24
25
sendfile,
29
30
unlink,
30
31
walkdir
31
32
33
+ import Base.==
34
+
32
35
# get and set current directory
33
36
34
37
"""
@@ -913,7 +916,56 @@ julia> readdir(abspath("base"), join=true)
913
916
"/home/JuliaUser/dev/julia/base/weakkeydict.jl"
914
917
```
915
918
"""
916
- function readdir (dir:: AbstractString ; join:: Bool = false , sort:: Bool = true )
919
+ readdir (; join:: Bool = false , kwargs... ) = readdir (join ? pwd () : " ." ; join, kwargs... ):: Vector{String}
920
+ readdir (dir:: AbstractString ; kwargs... ) = _readdir (dir; return_objects= false , kwargs... ):: Vector{String}
921
+
922
+ # this might be better as an Enum but they're not available here
923
+ # UV_DIRENT_T
924
+ const UV_DIRENT_UNKNOWN = Cint (0 )
925
+ const UV_DIRENT_FILE = Cint (1 )
926
+ const UV_DIRENT_DIR = Cint (2 )
927
+ const UV_DIRENT_LINK = Cint (3 )
928
+ const UV_DIRENT_FIFO = Cint (4 )
929
+ const UV_DIRENT_SOCKET = Cint (5 )
930
+ const UV_DIRENT_CHAR = Cint (6 )
931
+ const UV_DIRENT_BLOCK = Cint (7 )
932
+ struct FileKind
933
+ dir:: String
934
+ name:: String
935
+ rawtype:: Cint
936
+ end
937
+ function Base. getproperty (obj:: FileKind , p:: Symbol )
938
+ if p == :path
939
+ return joinpath (obj. dir, obj. name)
940
+ else
941
+ return getfield (obj, p)
942
+ end
943
+ end
944
+ Base. isless (a:: FileKind , b:: FileKind ) = isless (a. name, b. name)
945
+ == (a:: FileKind , b:: FileKind ) = a. name == b. name
946
+ == (a:: AbstractString , b:: FileKind ) = a == b. name
947
+ == (a:: FileKind , b:: AbstractString ) = b == a
948
+ joinpath (obj:: FileKind , args... ) = joinpath (obj. path, args... )
949
+ Base. convert (:: Type{String} , obj:: FileKind ) = obj. name
950
+ isunknown (obj:: FileKind ) = obj. rawtype == UV_DIRENT_UNKNOWN
951
+ islink (obj:: FileKind ) = isunknown (obj) ? islink (obj. path) : obj. rawtype == UV_DIRENT_LINK
952
+ isfile (obj:: FileKind ) = (isunknown (obj) || islink (obj)) ? isfile (obj. path) : obj. rawtype == UV_DIRENT_FILE
953
+ isdir (obj:: FileKind ) = (isunknown (obj) || islink (obj)) ? isdir (obj. path) : obj. rawtype == UV_DIRENT_DIR
954
+ isfifo (obj:: FileKind ) = (isunknown (obj) || islink (obj)) ? isfifo (obj. path) : obj. rawtype == UV_DIRENT_FIFO
955
+ issocket (obj:: FileKind ) = (isunknown (obj) || islink (obj)) ? issocket (obj. path) : obj. rawtype == UV_DIRENT_SOCKET
956
+ ischardev (obj:: FileKind ) = (isunknown (obj) || islink (obj)) ? ischardev (obj. path) : obj. rawtype == UV_DIRENT_CHAR
957
+ isblockdev (obj:: FileKind ) = (isunknown (obj) || islink (obj)) ? isblockdev (obj. path) : obj. rawtype == UV_DIRENT_BLOCK
958
+
959
+ """
960
+ readdirx(dir::AbstractString=pwd();
961
+ join::Bool = false,
962
+ sort::Bool = true,
963
+ ) -> Vector{FileKind}
964
+ """
965
+ readdirx (; join:: Bool = false , kwargs... ) = readdirx (join ? pwd () : " ." ; join, kwargs... ):: Vector{FileKind}
966
+ readdirx (dir:: AbstractString ; kwargs... ) = _readdir (dir; return_objects= true , kwargs... ):: Vector{FileKind}
967
+
968
+ function _readdir (dir:: AbstractString ; return_objects:: Bool = false , join:: Bool = false , sort:: Bool = true )
917
969
# Allocate space for uv_fs_t struct
918
970
req = Libc. malloc (_sizeof_uv_fs)
919
971
try
@@ -923,11 +975,16 @@ function readdir(dir::AbstractString; join::Bool=false, sort::Bool=true)
923
975
err < 0 && uv_error (" readdir($(repr (dir)) )" , err)
924
976
925
977
# iterate the listing into entries
926
- entries = String[]
978
+ entries = return_objects ? FileKind[] : String[]
927
979
ent = Ref {uv_dirent_t} ()
928
980
while Base. UV_EOF != ccall (:uv_fs_scandir_next , Cint, (Ptr{Cvoid}, Ptr{uv_dirent_t}), req, ent)
929
981
name = unsafe_string (ent[]. name)
930
- push! (entries, join ? joinpath (dir, name) : name)
982
+ if return_objects
983
+ rawtype = ent[]. typ
984
+ push! (entries, FileKind (dir, name, rawtype))
985
+ else
986
+ push! (entries, join ? joinpath (dir, name) : name)
987
+ end
931
988
end
932
989
933
990
# Clean up the request string
@@ -941,8 +998,6 @@ function readdir(dir::AbstractString; join::Bool=false, sort::Bool=true)
941
998
Libc. free (req)
942
999
end
943
1000
end
944
- readdir (; join:: Bool = false , sort:: Bool = true ) =
945
- readdir (join ? pwd () : " ." , join= join, sort= sort)
946
1001
947
1002
"""
948
1003
walkdir(dir; topdown=true, follow_symlinks=false, onerror=throw)
0 commit comments