Skip to content

Commit

Permalink
add paths to file error messages (#38242)
Browse files Browse the repository at this point in the history
closes #38005, closes #33236, closes #33201
  • Loading branch information
simonbyrne committed Nov 10, 2020
1 parent 08201c1 commit 53a781d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 35 deletions.
37 changes: 21 additions & 16 deletions base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function pwd()
elseif rc == Base.UV_ENOBUFS
resize!(buf, sz[] - 1) # space for null-terminator implied by StringVector
else
uv_error(:cwd, rc)
uv_error("pwd()", rc)
end
end
end
Expand All @@ -81,7 +81,9 @@ julia> pwd()
```
"""
function cd(dir::AbstractString)
uv_error("chdir $dir", ccall(:uv_chdir, Cint, (Cstring,), dir))
err = ccall(:uv_chdir, Cint, (Cstring,), dir)
err < 0 && uv_error("cd($(repr(dir)))", err)
return nothing
end
cd() = cd(homedir())

Expand Down Expand Up @@ -174,7 +176,7 @@ function mkdir(path::AbstractString; mode::Integer = 0o777)
C_NULL, req, path, checkmode(mode), C_NULL)
if ret < 0
ccall(:uv_fs_req_cleanup, Cvoid, (Ptr{Cvoid},), req)
uv_error("mkdir", ret)
uv_error("mkdir($(repr(path)); mode=0o$(string(mode,base=8)))", ret)
end
ccall(:uv_fs_req_cleanup, Cvoid, (Ptr{Cvoid},), req)
return path
Expand Down Expand Up @@ -252,7 +254,7 @@ julia> rm("my", recursive=true)
julia> rm("this_file_does_not_exist", force=true)
julia> rm("this_file_does_not_exist")
ERROR: IOError: unlink: no such file or directory (ENOENT)
ERROR: IOError: unlink("this_file_does_not_exist"): no such file or directory (ENOENT)
Stacktrace:
[...]
```
Expand Down Expand Up @@ -455,7 +457,7 @@ function tempdir()
elseif rc == Base.UV_ENOBUFS
resize!(buf, sz[] - 1) # space for null-terminator implied by StringVector
else
uv_error(:tmpdir, rc)
uv_error("tempdir()", rc)
end
end
end
Expand Down Expand Up @@ -664,7 +666,7 @@ function mktempdir(parent::AbstractString=tempdir();
C_NULL, req, tpath, C_NULL)
if ret < 0
ccall(:uv_fs_req_cleanup, Cvoid, (Ptr{Cvoid},), req)
uv_error("mktempdir", ret)
uv_error("mktempdir($(repr(parent)))", ret)
end
path = unsafe_string(ccall(:jl_uv_fs_t_path, Cstring, (Ptr{Cvoid},), req))
ccall(:uv_fs_req_cleanup, Cvoid, (Ptr{Cvoid},), req)
Expand Down Expand Up @@ -809,7 +811,7 @@ function readdir(dir::AbstractString; join::Bool=false, sort::Bool=true)
# defined in sys.c, to call uv_fs_readdir, which sets errno on error.
err = ccall(:uv_fs_scandir, Int32, (Ptr{Cvoid}, Ptr{UInt8}, Cstring, Cint, Ptr{Cvoid}),
C_NULL, uv_readdir_req, dir, 0, C_NULL)
err < 0 && throw(_UVError("readdir", err, "with ", repr(dir)))
err < 0 && uv_error("readdir($(repr(dir)))", err)

# iterate the listing into entries
entries = String[]
Expand Down Expand Up @@ -913,7 +915,7 @@ end

function unlink(p::AbstractString)
err = ccall(:jl_fs_unlink, Int32, (Cstring,), p)
uv_error("unlink", err)
err < 0 && uv_error("unlink($(repr(p)))", err)
nothing
end

Expand Down Expand Up @@ -977,13 +979,16 @@ function symlink(p::AbstractString, np::AbstractString)
end
end
err = ccall(:jl_fs_symlink, Int32, (Cstring, Cstring, Cint), p, np, flags)
sym = "symlink"
@static if Sys.iswindows()
if err < 0 && !isdir(p)
sym = "On Windows, creating symlinks requires Administrator privileges.\nsymlink"
if err < 0
msg = "symlink($(repr(p)), $(repr(np)))"
@static if Sys.iswindows()
if !isdir(p)
msg = "On Windows, creating symlinks requires Administrator privileges.\n$msg"
end
end
uv_error(msg, err)
end
uv_error(sym, err)
return nothing
end

"""
Expand All @@ -999,7 +1004,7 @@ function readlink(path::AbstractString)
C_NULL, req, path, C_NULL)
if ret < 0
ccall(:uv_fs_req_cleanup, Cvoid, (Ptr{Cvoid},), req)
uv_error("readlink", ret)
uv_error("readlink($(repr(path)))", ret)
@assert false
end
tgt = unsafe_string(ccall(:jl_uv_fs_t_ptr, Cstring, (Ptr{Cvoid},), req))
Expand All @@ -1025,7 +1030,7 @@ Return `path`.
"""
function chmod(path::AbstractString, mode::Integer; recursive::Bool=false)
err = ccall(:jl_fs_chmod, Int32, (Cstring, Cint), path, mode)
uv_error("chmod", err)
err < 0 && uv_error("chmod($(repr(path)), 0o$(string(mode, base=8)))", err)
if recursive && isdir(path)
for p in readdir(path)
if !islink(joinpath(path, p))
Expand All @@ -1045,6 +1050,6 @@ Return `path`.
"""
function chown(path::AbstractString, owner::Integer, group::Integer=-1)
err = ccall(:jl_fs_chown, Int32, (Cstring, Cint, Cint), path, owner, group)
uv_error("chown",err)
err < 0 && uv_error("chown($(repr(path)), $owner, $group)", err)
path
end
2 changes: 1 addition & 1 deletion base/filesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function open(path::AbstractString, flags::Integer, mode::Integer=0)
C_NULL, req, path, flags, mode, C_NULL)
handle = ccall(:uv_fs_get_result, Cssize_t, (Ptr{Cvoid},), req)
ccall(:uv_fs_req_cleanup, Cvoid, (Ptr{Cvoid},), req)
uv_error("open", ret)
ret < 0 && uv_error("open($(repr(path)), $flags, $mode)", ret)
finally # conversion to Cstring could cause an exception
Libc.free(req)
end
Expand Down
4 changes: 2 additions & 2 deletions base/path.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function homedir()
elseif rc == Base.UV_ENOBUFS
resize!(buf, sz[] - 1) # space for null-terminator implied by StringVector
else
uv_error(:homedir, rc)
uv_error("homedir()", rc)
end
end
end
Expand Down Expand Up @@ -433,7 +433,7 @@ function realpath(path::AbstractString)
C_NULL, req, path, C_NULL)
if ret < 0
ccall(:uv_fs_req_cleanup, Cvoid, (Ptr{Cvoid},), req)
uv_error("realpath", ret)
uv_error("realpath($(repr(path)))", ret)
end
path = unsafe_string(ccall(:jl_uv_fs_t_ptr, Cstring, (Ptr{Cvoid},), req))
ccall(:uv_fs_req_cleanup, Cvoid, (Ptr{Cvoid},), req)
Expand Down
2 changes: 1 addition & 1 deletion base/stat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ macro stat_call(sym, arg1type, arg)
stat_buf = zeros(UInt8, ccall(:jl_sizeof_stat, Int32, ()))
r = ccall($(Expr(:quote, sym)), Int32, ($(esc(arg1type)), Ptr{UInt8}), $(esc(arg)), stat_buf)
if !(r in (0, Base.UV_ENOENT, Base.UV_ENOTDIR, Base.UV_EINVAL))
throw(_UVError("stat", r, "for file ", repr($(esc(arg)))))
uv_error(string("stat(",repr($(esc(arg))),")"), r)
end
st = StatStruct(stat_buf)
if ispath(st) != (r == 0)
Expand Down
29 changes: 14 additions & 15 deletions test/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ subdir = joinpath(dir, "adir")
mkdir(subdir)
subdir2 = joinpath(dir, "adir2")
mkdir(subdir2)
@test_throws Base._UVError("mkdir", Base.UV_EEXIST) mkdir(file)
@test_throws Base._UVError("mkdir($(repr(file)); mode=0o777)", Base.UV_EEXIST) mkdir(file)
let err = nothing
try
mkdir(file)
catch err
io = IOBuffer()
showerror(io, err)
@test startswith(String(take!(io)), "IOError: mkdir: file already exists (EEXIST)")
@test startswith(String(take!(io)), "IOError: mkdir") && err.code == Base.UV_EEXIST
end
end

Expand Down Expand Up @@ -362,9 +362,8 @@ function test_stat_error(stat::Function, pth)
end
ex = try; stat(pth); false; catch ex; ex; end::Base.IOError
@test ex.code == (pth isa AbstractString ? Base.UV_EACCES : Base.UV_EBADF)
@test startswith(ex.msg, "stat: ")
pth isa AbstractString || (pth = Base.INVALID_OS_HANDLE)
@test endswith(ex.msg, repr(pth))
@test startswith(ex.msg, "stat($(repr(pth)))")
nothing
end
@testset "stat errors" begin # PR 32031
Expand Down Expand Up @@ -459,9 +458,9 @@ close(f)

rm(c_tmpdir, recursive=true)
@test !isdir(c_tmpdir)
@test_throws Base._UVError("unlink", Base.UV_ENOENT) rm(c_tmpdir)
@test_throws Base._UVError("unlink($(repr(c_tmpdir)))", Base.UV_ENOENT) rm(c_tmpdir)
@test rm(c_tmpdir, force=true) === nothing
@test_throws Base._UVError("unlink", Base.UV_ENOENT) rm(c_tmpdir, recursive=true)
@test_throws Base._UVError("unlink($(repr(c_tmpdir)))", Base.UV_ENOENT) rm(c_tmpdir, recursive=true)
@test rm(c_tmpdir, force=true, recursive=true) === nothing

if !Sys.iswindows()
Expand All @@ -476,8 +475,8 @@ if !Sys.iswindows()
@test stat(file).gid == 0
@test stat(file).uid == 0
else
@test_throws Base._UVError("chown", Base.UV_EPERM) chown(file, -2, -1) # Non-root user cannot change ownership to another user
@test_throws Base._UVError("chown", Base.UV_EPERM) chown(file, -1, -2) # Non-root user cannot change group to a group they are not a member of (eg: nogroup)
@test_throws Base._UVError("chown($(repr(file)), -2, -1)", Base.UV_EPERM) chown(file, -2, -1) # Non-root user cannot change ownership to another user
@test_throws Base._UVError("chown($(repr(file)), -1, -2)", Base.UV_EPERM) chown(file, -1, -2) # Non-root user cannot change group to a group they are not a member of (eg: nogroup)
end
else
# test that chown doesn't cause any errors for Windows
Expand Down Expand Up @@ -926,10 +925,10 @@ if !Sys.iswindows() || Sys.windows_version() >= Sys.WINDOWS_VISTA_VER
@test_throws(ArgumentError("'$nonexisting_src' is not a directory. Use `cp(src, dst)`"),
Base.cptree(nonexisting_src, dst; force=true, follow_symlinks=true))
# cp
@test_throws Base._UVError("open", Base.UV_ENOENT) cp(nonexisting_src, dst; force=true, follow_symlinks=false)
@test_throws Base._UVError("open", Base.UV_ENOENT) cp(nonexisting_src, dst; force=true, follow_symlinks=true)
@test_throws Base._UVError("open($(repr(nonexisting_src)), $(Base.JL_O_RDONLY), 0)", Base.UV_ENOENT) cp(nonexisting_src, dst; force=true, follow_symlinks=false)
@test_throws Base._UVError("open($(repr(nonexisting_src)), $(Base.JL_O_RDONLY), 0)", Base.UV_ENOENT) cp(nonexisting_src, dst; force=true, follow_symlinks=true)
# mv
@test_throws Base._UVError("open", Base.UV_ENOENT) mv(nonexisting_src, dst; force=true)
@test_throws Base._UVError("open($(repr(nonexisting_src)), $(Base.JL_O_RDONLY), 0)", Base.UV_ENOENT) mv(nonexisting_src, dst; force=true)
end
end

Expand Down Expand Up @@ -1302,7 +1301,7 @@ cd(dirwalk) do
@test files == ["file1", "file2"]

rm(joinpath("sub_dir1"), recursive=true)
@test_throws(Base._UVError("readdir", Base.UV_ENOENT, "with ", repr(joinpath(".", "sub_dir1"))),
@test_throws(Base._UVError("readdir($(repr(joinpath(".", "sub_dir1"))))", Base.UV_ENOENT),
take!(chnl_error)) # throws an error because sub_dir1 do not exist

root, dirs, files = take!(chnl_noerror)
Expand All @@ -1322,7 +1321,7 @@ cd(dirwalk) do
symlink("foo", foo)

let files = walkdir(joinpath(".", "sub_dir3"); follow_symlinks=true)
@test_throws Base._UVError("stat", Base.UV_ELOOP, "for file ", repr(foo)) take!(files)
@test_throws Base._UVError("stat($(repr(foo)))", Base.UV_ELOOP) take!(files)
end
root, dirs, files = take!(walkdir(joinpath(".", "sub_dir3"); follow_symlinks=false))
@test root == joinpath(".", "sub_dir3")
Expand Down Expand Up @@ -1512,8 +1511,8 @@ end
rm(d, recursive=true)
@test !ispath(d)
@test isempty(readdir())
@test_throws Base._UVError("readdir", Base.UV_ENOENT, "with ", repr(d)) readdir(d)
@test_throws Base._UVError("cwd", Base.UV_ENOENT) readdir(join=true)
@test_throws Base._UVError("readdir($(repr(d)))", Base.UV_ENOENT) readdir(d)
@test_throws Base._UVError("pwd()", Base.UV_ENOENT) readdir(join=true)
end
end
end
Expand Down

0 comments on commit 53a781d

Please sign in to comment.