From 1c154f4fb11fcd57914a82d06deb64cd8ae83ea7 Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Wed, 26 Aug 2015 13:48:57 -0400 Subject: [PATCH] deprecate isreadable/writable/executable for filesystem paths. closes #7385 --- base/deprecated.jl | 21 ++++++++++++++++ base/docs/helpdb.jl | 15 ++++-------- base/file.jl | 2 +- base/stat.jl | 58 +++++++++++++++++++++------------------------ test/file.jl | 10 ++++---- 5 files changed, 58 insertions(+), 48 deletions(-) diff --git a/base/deprecated.jl b/base/deprecated.jl index 156b86e5981a1..d3e36b023f987 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -787,3 +787,24 @@ export FloatingPoint end @deprecate cartesianmap(f, dims) for idx in CartesianRange(dims); f(idx.I...); end + +# 0.5 deprecations + +# 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`", :isexecutable) + return (st.mode & 0o111) > 0 +end +export isreadable, iswritable, isexecutable diff --git a/base/docs/helpdb.jl b/base/docs/helpdb.jl index 675d13d9db800..b9e1b8a512f44 100644 --- a/base/docs/helpdb.jl +++ b/base/docs/helpdb.jl @@ -2276,13 +2276,6 @@ See :func:`schurfact` """ schur(A,B) -doc""" - isexecutable(path) -> Bool - -Returns `true` if the current user has permission to execute `path`, `false` otherwise. -""" -isexecutable - doc""" acot(x) @@ -6134,9 +6127,9 @@ Get the fully-qualified name of a module as a tuple of symbols. For example, `fu fullname doc""" - isreadable(path) -> Bool + isreadable(io) -> Bool -Returns `true` if the current user has permission to read `path`, `false` otherwise. +Returns `true` if the specified IO object is readable (if that can be determined). """ isreadable @@ -11549,9 +11542,9 @@ Create a "value type" out of `c`, which must be an `isbits` value. The intent of Val doc""" - iswritable(path) -> Bool + iswritable(io) -> Bool -Returns `true` if the current user has permission to write to `path`, `false` otherwise. +Returns `true` if the specified IO object is writable (if that can be determined). """ iswritable diff --git a/base/file.jl b/base/file.jl index 94f753d8b93d8..2787e1ef66df6 100644 --- a/base/file.jl +++ b/base/file.jl @@ -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 diff --git a/base/stat.jl b/base/stat.jl index 9455e5b7b2858..defd3b7238bcf 100644 --- a/base/stat.jl +++ b/base/stat.jl @@ -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 @@ -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") @@ -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 @@ -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) diff --git a/test/file.jl b/test/file.jl index ed3a385229bbd..c4a8677446c85 100644 --- a/test/file.jl +++ b/test/file.jl @@ -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. @@ -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)