Skip to content

Commit c10e8aa

Browse files
readdir: add join option to join dir with names
1 parent 116cfab commit c10e8aa

File tree

2 files changed

+82
-12
lines changed

2 files changed

+82
-12
lines changed

base/file.jl

+54-12
Original file line numberDiff line numberDiff line change
@@ -671,27 +671,70 @@ struct uv_dirent_t
671671
end
672672

673673
"""
674-
readdir(dir::AbstractString=".") -> Vector{String}
674+
readdir(dir::AbstractString=pwd(); join::Bool=true) -> Vector{String}
675675
676-
Return the files and directories in the directory `dir` (or the current working directory if not given).
676+
Return the names in the directory `dir` or the current working directory if not
677+
given. When `join` is false, `readdir` returns just the names in the directory
678+
as is; when `join` is true, it returns `joinpath(dir, name)` for each `name` so
679+
that the returned strings are full paths. If you want to get absolute paths
680+
back, call `readdir` with an absolute directory path and `join` set to true.
677681
678682
# Examples
679683
```julia-repl
680-
julia> readdir("/home/JuliaUser/Projects/julia")
681-
34-element Array{String,1}:
682-
".circleci"
683-
".freebsdci.sh"
684+
julia> cd("/home/JuliaUser/dev/julia")
685+
686+
julia> readdir()
687+
30-element Array{String,1}:
688+
".appveyor.yml"
684689
".git"
685690
".gitattributes"
686-
".github"
687691
688-
"test"
689692
"ui"
690693
"usr"
691694
"usr-staging"
695+
696+
julia> readdir(join=true)
697+
30-element Array{String,1}:
698+
"/home/JuliaUser/dev/julia/.appveyor.yml"
699+
"/home/JuliaUser/dev/julia/.git"
700+
"/home/JuliaUser/dev/julia/.gitattributes"
701+
702+
"/home/JuliaUser/dev/julia/ui"
703+
"/home/JuliaUser/dev/julia/usr"
704+
"/home/JuliaUser/dev/julia/usr-staging"
705+
706+
julia> readdir("base")
707+
145-element Array{String,1}:
708+
".gitignore"
709+
"Base.jl"
710+
"Enums.jl"
711+
712+
"version_git.sh"
713+
"views.jl"
714+
"weakkeydict.jl"
715+
716+
julia> readdir("base", join=true)
717+
145-element Array{String,1}:
718+
"base/.gitignore"
719+
"base/Base.jl"
720+
"base/Enums.jl"
721+
722+
"base/version_git.sh"
723+
"base/views.jl"
724+
"base/weakkeydict.jl"```
725+
726+
julia> readdir(abspath("base"), join=true)
727+
145-element Array{String,1}:
728+
"/home/JuliaUser/dev/julia/base/.gitignore"
729+
"/home/JuliaUser/dev/julia/base/Base.jl"
730+
"/home/JuliaUser/dev/julia/base/Enums.jl"
731+
732+
"/home/JuliaUser/dev/julia/base/version_git.sh"
733+
"/home/JuliaUser/dev/julia/base/views.jl"
734+
"/home/JuliaUser/dev/julia/base/weakkeydict.jl"
692735
```
693736
"""
694-
function readdir(dir::AbstractString)
737+
function readdir(dir::AbstractString=pwd(); join::Bool=false)
695738
# Allocate space for uv_fs_t struct
696739
uv_readdir_req = zeros(UInt8, ccall(:jl_sizeof_uv_fs_t, Int32, ()))
697740

@@ -704,7 +747,8 @@ function readdir(dir::AbstractString)
704747
entries = String[]
705748
ent = Ref{uv_dirent_t}()
706749
while Base.UV_EOF != ccall(:uv_fs_scandir_next, Cint, (Ptr{Cvoid}, Ptr{uv_dirent_t}), uv_readdir_req, ent)
707-
push!(entries, unsafe_string(ent[].name))
750+
name = unsafe_string(ent[].name)
751+
push!(entries, join ? joinpath(dir, name) : name)
708752
end
709753

710754
# Clean up the request string
@@ -713,8 +757,6 @@ function readdir(dir::AbstractString)
713757
return entries
714758
end
715759

716-
readdir() = readdir(".")
717-
718760
"""
719761
walkdir(dir; topdown=true, follow_symlinks=false, onerror=throw)
720762

test/file.jl

+28
Original file line numberDiff line numberDiff line change
@@ -1404,3 +1404,31 @@ end
14041404
@test sizeof(basename(tmpdir)) == 6
14051405
end
14061406
end
1407+
1408+
@testset "readir tests" begin
1409+
(a, b) = sort(a) == sort(b)
1410+
mktempdir() do dir
1411+
d = cd(pwd, dir) # might resolve symlinks
1412+
@test isempty(readdir(d))
1413+
@test isempty(readdir(d, join=true))
1414+
cd(d) do
1415+
@test isempty(readdir())
1416+
@test isempty(readdir(join=true))
1417+
end
1418+
touch(joinpath(d, "file"))
1419+
mkdir(joinpath(d, "dir"))
1420+
names = ["dir", "file"]
1421+
paths = [joinpath(d, x) for x in names]
1422+
@test readdir(d) names
1423+
@test readdir(d, join=true) paths
1424+
cd(d) do
1425+
@test readdir() names
1426+
@test readdir(join=true) paths
1427+
end
1428+
t, b = splitdir(d)
1429+
cd(t) do
1430+
@test readdir(b) names
1431+
@test readdir(b, join=true) [joinpath(b, x) for x in names]
1432+
end
1433+
end
1434+
end

0 commit comments

Comments
 (0)