Skip to content

Commit

Permalink
deprecate isreadable/writable/executable for filesystem paths. closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
vtjnash committed Aug 26, 2015
1 parent 9d37308 commit 150d6ae
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 37 deletions.
19 changes: 19 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -783,3 +783,22 @@ function Regex(pattern::AbstractString, options::Integer)
"use string flags instead: Regex(\"$pattern\", \"$flags\").", :Regex)
Regex(pattern, flags)
end

# Filesystem module updates

isreadable(path...) = isreadable(stat(path...))
iswritable(path...) = iswritable(stat(path...))
isexecutable(path...) = isexecutable(stat(path...))
function isreadable(st::StatStruct)
depwarn("isreadable is deprecated as it implied that the file would actually be readable by the user. see also the man page for `access`", :isreadable)
return (st.mode & 0o444) > 0
end
function iswritable(st::StatStruct)
depwarn("iswritable is deprecated as it implied that the file would actually be writable by the user. see also the man page for `access`", :iswritable)
return (st.mode & 0o222) > 0
end
function isexecutable(st::StatStruct)
depwarn("isexecutable is deprecated as it implied that the file would actually be executable by the user. see also the man page for `access`", :isreadable)
return (st.mode & 0o111) > 0
end
export isreadable, iswritable, isexecutable
2 changes: 1 addition & 1 deletion base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ mkpath(path::AbstractString, mode::Signed) = throw(ArgumentError("mode must be a

function rm(path::AbstractString; recursive::Bool=false)
if islink(path) || !isdir(path)
@windows_only if !iswritable(path); chmod(path, 0o777); end
@windows_only if (filemode(path) & 0o222) == 0; chmod(path, 0o777); end # is writable on windows actually means "is deletable"
FS.unlink(path)
else
if recursive
Expand Down
58 changes: 27 additions & 31 deletions base/stat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ StatStruct(buf::Union{Vector{UInt8},Ptr{UInt8}}) = StatStruct(
ccall(:jl_stat_ctime, Float64, (Ptr{UInt8},), buf),
)

show(io::IO, st::StatStruct) = print(io, "StatStruct(mode=$(oct(st.mode,6)), size=$(st.size))")
show(io::IO, st::StatStruct) = print(io, "StatStruct(mode=$(oct(filemode(st),6)), size=$(filesize(st)))")

# stat & lstat functions

Expand All @@ -41,7 +41,7 @@ macro stat_call(sym, arg1type, arg)
quote
fill!(stat_buf,0)
r = ccall($(Expr(:quote,sym)), Int32, ($arg1type, Ptr{UInt8}), $(esc(arg)), stat_buf)
r==0 || r==UV_ENOENT || r==UV_ENOTDIR || throw(UVError("stat",r))
r==0 || r==Base.UV_ENOENT || r==Base.UV_ENOTDIR || throw(UVError("stat",r))
st = StatStruct(stat_buf)
if ispath(st) != (r==0)
error("stat returned zero type for a valid path")
Expand All @@ -58,30 +58,33 @@ lstat(path::AbstractString) = @stat_call jl_lstat Cstring path
stat(path...) = stat(joinpath(path...))
lstat(path...) = lstat(joinpath(path...))

# some convenience functions

filemode(st::StatStruct) = st.mode
filesize(st::StatStruct) = st.size
mtime(st::StatStruct) = st.mtime
ctime(st::StatStruct) = st.ctime

# mode type predicates

ispath(st::StatStruct) = st.mode & 0xf000 != 0x0000
isfifo(st::StatStruct) = st.mode & 0xf000 == 0x1000
ischardev(st::StatStruct) = st.mode & 0xf000 == 0x2000
isdir(st::StatStruct) = st.mode & 0xf000 == 0x4000
isblockdev(st::StatStruct) = st.mode & 0xf000 == 0x6000
isfile(st::StatStruct) = st.mode & 0xf000 == 0x8000
islink(st::StatStruct) = st.mode & 0xf000 == 0xa000
issocket(st::StatStruct) = st.mode & 0xf000 == 0xc000
ispath(st::StatStruct) = filemode(st) & 0xf000 != 0x0000
isfifo(st::StatStruct) = filemode(st) & 0xf000 == 0x1000
ischardev(st::StatStruct) = filemode(st) & 0xf000 == 0x2000
isdir(st::StatStruct) = filemode(st) & 0xf000 == 0x4000
isblockdev(st::StatStruct) = filemode(st) & 0xf000 == 0x6000
isfile(st::StatStruct) = filemode(st) & 0xf000 == 0x8000
islink(st::StatStruct) = filemode(st) & 0xf000 == 0xa000
issocket(st::StatStruct) = filemode(st) & 0xf000 == 0xc000

# mode permission predicates

issetuid(st::StatStruct) = (st.mode & 0o4000) > 0
issetgid(st::StatStruct) = (st.mode & 0o2000) > 0
issticky(st::StatStruct) = (st.mode & 0o1000) > 0

isreadable(st::StatStruct) = (st.mode & 0o444) > 0
iswritable(st::StatStruct) = (st.mode & 0o222) > 0
isexecutable(st::StatStruct) = (st.mode & 0o111) > 0
issetuid(st::StatStruct) = (filemode(st) & 0o4000) > 0
issetgid(st::StatStruct) = (filemode(st) & 0o2000) > 0
issticky(st::StatStruct) = (filemode(st) & 0o1000) > 0

uperm(st::StatStruct) = UInt8((st.mode >> 6) & 0x7)
gperm(st::StatStruct) = UInt8((st.mode >> 3) & 0x7)
operm(st::StatStruct) = UInt8((st.mode ) & 0x7)
uperm(st::StatStruct) = UInt8((filemode(st) >> 6) & 0x7)
gperm(st::StatStruct) = UInt8((filemode(st) >> 3) & 0x7)
operm(st::StatStruct) = UInt8((filemode(st) ) & 0x7)

# mode predicate methods for file names

Expand All @@ -97,26 +100,19 @@ for f in Symbol[
:issetuid
:issetgid
:issticky
:isreadable
:iswritable
:isexecutable
:uperm
:gperm
:operm
:filemode
:filesize
:mtime
:ctime
]
@eval ($f)(path...) = ($f)(stat(path...))
end

islink(path...) = islink(lstat(path...))


# some convenience functions

filemode(path...) = stat(path...).mode
filesize(path...) = stat(path...).size
mtime(path...) = stat(path...).mtime
ctime(path...) = stat(path...).ctime

# samefile can be used for files and directories: #11145#issuecomment-99511194
samefile(a::StatStruct, b::StatStruct) = a.device==b.device && a.inode==b.inode
function samefile(a::AbstractString, b::AbstractString)
Expand Down
10 changes: 5 additions & 5 deletions test/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ end
@test !isdir(file)
@test isfile(file)
@test !islink(file)
@test isreadable(file)
@test iswritable(file)
@test filemode(file) & 0o444 > 0 # readable
@test filemode(file) & 0o222 > 0 # writable
chmod(file, filemode(file) & 0o7555)
@test !iswritable(file)
@test filemode(file) & 0o222 == 0
chmod(file, filemode(file) | 0o222)
@test !isexecutable(file)
@test filemode(file) & 0o111 == 0
@test filesize(file) == 0
# On windows the filesize of a folder is the accumulation of all the contained
# files and is thus zero in this case.
Expand Down Expand Up @@ -840,7 +840,7 @@ close(f)
test_LibcFILE(Libc.FILE(f,Libc.modestr(true,false)))

# issue #10994: pathnames cannot contain embedded NUL chars
for f in (mkdir, cd, Base.FS.unlink, readlink, rm, touch, readdir, mkpath, stat, lstat, ctime, mtime, filemode, filesize, uperm, gperm, operm, touch, isblockdev, ischardev, isdir, isexecutable, isfifo, isfile, islink, ispath, isreadable, issetgid, issetuid, issocket, issticky, iswritable, realpath, watch_file, poll_file)
for f in (mkdir, cd, Base.FS.unlink, readlink, rm, touch, readdir, mkpath, stat, lstat, ctime, mtime, filemode, filesize, uperm, gperm, operm, touch, isblockdev, ischardev, isdir, isfifo, isfile, islink, ispath, issetgid, issetuid, issocket, issticky, realpath, watch_file, poll_file)
@test_throws ArgumentError f("adir\0bad")
end
@test_throws ArgumentError chmod("ba\0d", 0o222)
Expand Down

0 comments on commit 150d6ae

Please sign in to comment.