Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep track of file permissions #32

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions src/RelocatableFolders.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,26 @@ macro path(expr, ignore=:nothing)
return :($(Path)($__module__, $dir, $(esc(expr)), $(esc(ignore))))
end

struct File
relative_path::String
bytes::Vector{UInt8}
mode::UInt64
end

struct Path <: AbstractString
is_dir::Bool
mod::Module
path::String
hash::String
files::Dict{String,Vector{UInt8}}
files::Dict{String,File}

function Path(mod::Module, dir, path::AbstractString, ignore=nothing)
path = isabspath(path) ? path : joinpath(dir, path)
path = normpath(path)
safe_ispath(path) || throw(ArgumentError("not a path: `$path`"))
is_dir = isdir(path)
dir = is_dir ? path : dirname(path)
files = Dict{String,Vector{UInt8}}()
files = Dict{String,File}()
ctx = SHA.SHA1_CTX()
for (root, _, fs) in walkdir(dir), f in fs
fullpath = joinpath(root, f)
Expand All @@ -66,11 +72,11 @@ struct Path <: AbstractString
SHA.update!(ctx, codeunits(fullpath))
content = read(fullpath)
SHA.update!(ctx, content)
files[rel] = content
files[rel] = File(rel, content, filemode(fullpath))
end
end
end
return new(is_dir, mod, dir, string(Base.SHA1(SHA.digest!(ctx))), files)
return new(is_dir, mod, dir, bytes2hex(SHA.digest!(ctx)), files)
end
end

Expand Down Expand Up @@ -100,11 +106,11 @@ function getpath(f::Path)
end
dir = Scratch.get_scratch!(f.mod, f.hash * "_" * string(hash(f.path), base=62))
if !isempty(f.files) && !safe_ispath(joinpath(dir, first(keys(f.files))))
cd(dir) do
for (file, blob) in f.files
mkpath(dirname(file))
write(file, blob)
end
for file in values(f.files)
fullpath = joinpath(dir, file.relative_path)
mkpath(dirname(fullpath))
write(fullpath, file.bytes)
chmod(fullpath, file.mode)
end
end
return getroot(f, dir)
Expand Down
1 change: 1 addition & 0 deletions test/path/readonly.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file is readonly.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Git doesn't track readonly permissions; it only tracks executable or not; you'll need to create a readonly file (or modify this one via chmod()) in order to properly test.

18 changes: 12 additions & 6 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,31 @@ end
@test read(M.OTHER, String) == "# other.jl"

@test isdir(M.DIR)
@test sort(readdir(M.DIR)) == ["file.jl", "file.json", "subfolder", "text.md"]
@test sort(readdir(M.DIR)) == ["file.jl", "file.json", "readonly.txt", "subfolder", "text.md"]
@test readdir(joinpath(M.DIR, "subfolder")) == ["other.jl"]

@test read(joinpath(M.DIR, "file.jl"), String) == "# file.jl"
@test read(joinpath(M.DIR, "text.md"), String) == "text.md"
@test read(joinpath(M.DIR, "subfolder", "other.jl"), String) == "# other.jl"
@test read(joinpath(M.DIR, "readonly.txt"), String) == "This file is readonly.\n"

@test length(M.DIR.files) == 4
# File permission test
m = filemode(joinpath(M.DIR, "readonly.txt"))
@test m & 0o400 == 0o400 # user read bit
@test m & 0o200 == 0o000 # user write bit

@test length(M.DIR.files) == 5
@test M.DIR.mod == M
@test M.DIR.path == joinpath(@__DIR__, "path")

@test length(M.IGNORE_RE.files) == 3
@test length(M.IGNORE_RE.files) == 4
@test !haskey(M.IGNORE_RE.files, joinpath("path", "text.md"))
@test length(M.IGNORE_RE_REL.files) == 3
@test length(M.IGNORE_RE_REL.files) == 4
@test !haskey(M.IGNORE_RE_REL.files, joinpath("path", "text.md"))
@test !haskey(M.IGNORE_RE_REL.files, joinpath("path", "file.jl"))
@test length(M.IGNORE_RES.files) == 3
@test length(M.IGNORE_RES.files) == 4
@test !haskey(M.IGNORE_RES.files, joinpath("path", "text.md"))
@test length(M.IGNORE_FN.files) == 3
@test length(M.IGNORE_FN.files) == 4
@test !haskey(M.IGNORE_FN.files, joinpath("path", "text.md"))
@test length(M.IGNORE_FN_REL.files) == 2
@test !haskey(M.IGNORE_FN_REL.files, joinpath("path", "file.jl"))
Expand Down
Loading