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

Move isexecutable, isreadable, iswritable to filesystem.jl #53699

Merged
merged 7 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,7 @@ export
isblockdev,
ischardev,
isdir,
isexecutable,
isfifo,
isfile,
islink,
Expand Down
90 changes: 89 additions & 1 deletion base/filesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ import .Base:
IOError, _UVError, _sizeof_uv_fs, check_open, close, eof, eventloop, fd, isopen,
bytesavailable, position, read, read!, readavailable, seek, seekend, show,
skip, stat, unsafe_read, unsafe_write, write, transcode, uv_error,
setup_stdio, rawhandle, OS_HANDLE, INVALID_OS_HANDLE, windowserror, filesize
setup_stdio, rawhandle, OS_HANDLE, INVALID_OS_HANDLE, windowserror, filesize,
isexecutable, isreadable, iswritable

import .Base.RefValue

Expand Down Expand Up @@ -365,5 +366,92 @@ function touch(f::File)
f
end

"""
isexecutable(path::String)

Return `true` if the given `path` has executable permissions.

!!! note
This permission may change before the user executes `path`,
so it is recommended to execute the file and handle the error if that fails,
rather than calling `isexecutable` first.

!!! note
Prior to Julia 1.6, this did not correctly interrogate filesystem
ACLs on Windows, therefore it would return `true` for any
file. From Julia 1.6 on, it correctly determines whether the
file is marked as executable or not.

See also [`ispath`](@ref), [`isreadable`](@ref), [`iswritable`](@ref).
"""
function isexecutable(path::String)
# We use `access()` and `X_OK` to determine if a given path is
# executable by the current user. `X_OK` comes from `unistd.h`.
X_OK = 0x01
return ccall(:jl_fs_access, Cint, (Cstring, Cint), path, X_OK) == 0
end
isexecutable(path::AbstractString) = isexecutable(String(path))

"""
isreadable(path::String)

Return `true` if the access permissions for the given `path` permitted reading by the current user.

!!! note
This permission may change before the user calls `open`,
so it is recommended to just call `open` alone and handle the error if that fails,
rather than calling `isreadable` first.

!!! note
Currently this function does not correctly interrogate filesystem
ACLs on Windows, therefore it can return wrong results.

!!! compat "Julia 1.11"
This function requires at least Julia 1.11.

See also [`ispath`](@ref), [`isexecutable`](@ref), [`iswritable`](@ref).
"""
function isreadable(path::String)
# We use `access()` and `R_OK` to determine if a given path is
# readable by the current user. `R_OK` comes from `unistd.h`.
R_OK = 0x04
return ccall(:jl_fs_access, Cint, (Cstring, Cint), path, R_OK) == 0
end
isreadable(path::AbstractString) = isreadable(String(path))

"""
iswritable(path::String)

Return `true` if the access permissions for the given `path` permitted writing by the current user.

!!! note
This permission may change before the user calls `open`,
so it is recommended to just call `open` alone and handle the error if that fails,
rather than calling `iswritable` first.

!!! note
Currently this function does not correctly interrogate filesystem
ACLs on Windows, therefore it can return wrong results.

!!! compat "Julia 1.11"
This function requires at least Julia 1.11.

See also [`ispath`](@ref), [`isexecutable`](@ref), [`isreadable`](@ref).
"""
function iswritable(path::String)
# We use `access()` and `W_OK` to determine if a given path is
# writeable by the current user. `W_OK` comes from `unistd.h`.
W_OK = 0x02
return ccall(:jl_fs_access, Cint, (Cstring, Cint), path, W_OK) == 0
end
iswritable(path::AbstractString) = iswritable(String(path))

# define aliases in Sys for backwards compatibility
for f in (isexecutable, isreadable, iswritable)
Sys.eval(quote
const $(Symbol(f)) = $f
fatteneder marked this conversation as resolved.
Show resolved Hide resolved
end)
end


end
2 changes: 2 additions & 0 deletions base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ data has already been buffered. The result is a `Vector{UInt8}`.
"""
function readavailable end

function isexecutable end
fatteneder marked this conversation as resolved.
Show resolved Hide resolved

"""
isreadable(io) -> Bool

Expand Down
80 changes: 0 additions & 80 deletions base/sysinfo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -553,86 +553,6 @@ windows_version

const WINDOWS_VISTA_VER = v"6.0"

"""
Sys.isexecutable(path::String)

Return `true` if the given `path` has executable permissions.

!!! note
This permission may change before the user executes `path`,
so it is recommended to execute the file and handle the error if that fails,
rather than calling `isexecutable` first.

!!! note
Prior to Julia 1.6, this did not correctly interrogate filesystem
ACLs on Windows, therefore it would return `true` for any
file. From Julia 1.6 on, it correctly determines whether the
file is marked as executable or not.

See also [`ispath`](@ref), [`isreadable`](@ref), [`iswriteable`](@ref).
"""
function isexecutable(path::String)
# We use `access()` and `X_OK` to determine if a given path is
# executable by the current user. `X_OK` comes from `unistd.h`.
X_OK = 0x01
return ccall(:jl_fs_access, Cint, (Cstring, Cint), path, X_OK) == 0
end
isexecutable(path::AbstractString) = isexecutable(String(path))

"""
Sys.isreadable(path::String)

Return `true` if the access permissions for the given `path` permitted reading by the current user.

!!! note
This permission may change before the user calls `open`,
so it is recommended to just call `open` alone and handle the error if that fails,
rather than calling `isreadable` first.

!!! note
Currently this function does not correctly interrogate filesystem
ACLs on Windows, therefore it can return wrong results.

!!! compat "Julia 1.11"
This function requires at least Julia 1.11.

See also [`ispath`](@ref), [`isexecutable`](@ref), [`iswriteable`](@ref).
"""
function isreadable(path::String)
# We use `access()` and `R_OK` to determine if a given path is
# readable by the current user. `R_OK` comes from `unistd.h`.
R_OK = 0x04
return ccall(:jl_fs_access, Cint, (Cstring, Cint), path, R_OK) == 0
end
isreadable(path::AbstractString) = isreadable(String(path))

"""
Sys.iswriteable(path::String)

Return `true` if the access permissions for the given `path` permitted writing by the current user.

!!! note
This permission may change before the user calls `open`,
so it is recommended to just call `open` alone and handle the error if that fails,
rather than calling `iswriteable` first.

!!! note
Currently this function does not correctly interrogate filesystem
ACLs on Windows, therefore it can return wrong results.

!!! compat "Julia 1.11"
This function requires at least Julia 1.11.

See also [`ispath`](@ref), [`isexecutable`](@ref), [`isreadable`](@ref).
"""
function iswriteable(path::String)
# We use `access()` and `W_OK` to determine if a given path is
# writeable by the current user. `W_OK` comes from `unistd.h`.
W_OK = 0x02
return ccall(:jl_fs_access, Cint, (Cstring, Cint), path, W_OK) == 0
end
iswriteable(path::AbstractString) = iswriteable(String(path))

"""
Sys.which(program_name::String)

Expand Down