Skip to content

Commit

Permalink
Added function walkdir
Browse files Browse the repository at this point in the history
  • Loading branch information
peremato committed Dec 11, 2024
1 parent 4726c6a commit 02f4879
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "XRootD"
uuid = "164e3b87-dc46-4cbc-97a6-5b310108fce0"
authors = ["Pere Mato <[email protected]>"]
version = "0.2.1"
version = "0.2.2"

[deps]
CxxWrap = "1f15a43c-97ca-5a2a-ae31-89f07a497df4"
Expand Down
3 changes: 3 additions & 0 deletions docs/src/release_notes.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

# Release Notes

## 0.2.2 (11-12-2024)
- Added function walkdir to walk on a directory tree

## 0.2.1 (4-11-2024)
- Added some protection to avoid pre-compilation errors in case the XRootD binary artifacts do not exist (e.g. Windows platform)

Expand Down
53 changes: 53 additions & 0 deletions src/FileSystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,59 @@ function Base.readdir(fs::FileSystem, path::String, flags::XRootD.XrdCl!DirListF
end
end


"""
Base.walkdir(fs::FileSystem, root::AbstractString; topdown=true)
List entries in a directory.
# Arguments
- `fs::FileSystem`: The FileSystem object
- `root::AbstractString`: The path of the directory
- `flags::XrdCl.DirListFlags`: The flags
- `topdown::Bool`: start from the top of the directory tree
# Returns
- `Tuple` of:
- dirpath, dirnames, files
"""
function Base.walkdir(fs::FileSystem, root::AbstractString; topdown=true)
function _walkdir(chnl, root)
dirs = String[]
files = String[]
entries_p = Ref(CxxPtr{XRootD.XrdCl!DirectoryList}(C_NULL))
st = XRootD.DirList(fs, root, XRootD.XrdCl!DirListFlags!Stat, entries_p)
if !isOK(st)
try
throw(ErrorException("$st"))
catch err
close(chnl, err)
end
return
end
for i in 0:GetSize(entries_p[])-1
entry = At(entries_p[], i)
name = entry |> GetName |> String
stat = entry |> GetStatInfo
if isdir(stat[])
push!(dirs, name)
else
push!(files, name)
end
end
if topdown
push!(chnl, (root, dirs, files))
end
for dir in dirs
_walkdir(chnl, joinpath(root, dir))
end
if !topdown
push!(chnl, (root, dirs, files))
end
nothing
end
return Channel{Tuple{String,Vector{String},Vector{String}}}(chnl -> _walkdir(chnl, root))
end


"""
query(fs::FileSystem, code::XRootD.XrdCl!QueryCode!Code , arg::String, timeout::UInt16=0x0000)
Expand Down
47 changes: 47 additions & 0 deletions test/testFileSystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,53 @@ using XRootD.XrdCl
@test isOK(st)
@test length(entries) > 0

# WalkDir
mkpath("/tmp/walkdir")
write("/tmp/walkdir/testfile1", "Hello, world!")
write("/tmp/walkdir/testfile2", "Hello, world!")
mkpath("/tmp/walkdir/someotherdir")
write("/tmp/walkdir/someotherdir/testfile3", "Hello, world!")
write("/tmp/walkdir/someotherdir/testfile4", "Hello, world!")
# topdown == true
iteration = 1
for (root, dirs, files) in walkdir(fs, "/tmp/walkdir")
if iteration == 1
@test root == "/tmp/walkdir"
@test length(dirs) == 1
@test length(files) == 2
@test dirs[1] == "someotherdir"
@test files[1] == "testfile1"
@test files[2] == "testfile2"
iteration += 1
else # iteration == 2
@test root == "/tmp/walkdir/someotherdir"
@test length(dirs) == 0
@test length(files) == 2
@test files[1] == "testfile3"
@test files[2] == "testfile4"
end
end
# topdown == false
iteration = 1
for (root, dirs, files) in walkdir(fs, "/tmp/walkdir"; topdown=false)
if iteration == 1
@test root == "/tmp/walkdir/someotherdir"
@test length(dirs) == 0
@test length(files) == 2
@test files[1] == "testfile3"
@test files[2] == "testfile4"
iteration += 1
else # iteration == 2
@test root == "/tmp/walkdir"
@test length(dirs) == 1
@test length(files) == 2
@test dirs[1] == "someotherdir"
@test files[1] == "testfile1"
@test files[2] == "testfile2"
end
end
@test_throws ErrorException walkdir(fs, "/tmp/doesnotexist") |> take!

# Query
st, response = query(fs, QueryCode.Stats, "/tmp")
@test isOK(st)
Expand Down

0 comments on commit 02f4879

Please sign in to comment.