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

Export stat! and add it to the manual #50300

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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 @@ -946,6 +946,7 @@ export
rm,
samefile,
stat,
stat!,
symlink,
tempdir,
tempname,
Expand Down
23 changes: 11 additions & 12 deletions base/stat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ export
mtime,
operm,
stat,
stat!,
uperm

const STAT_BUFFER_SIZE = Int(ccall(:jl_sizeof_stat, Int32, ()))

struct StatStruct
desc :: Union{String, OS_HANDLE} # for show method, not included in equality or hash
device :: UInt
Expand Down Expand Up @@ -146,6 +149,7 @@ show(io::IO, ::MIME"text/plain", st::StatStruct) = show_statstruct(io, st, false

macro stat_call!(stat_buf, sym, arg1type, arg)
return quote
length($(esc(stat_buf))) < STAT_BUFFER_SIZE && resize!($(esc(stat_buf)), STAT_BUFFER_SIZE)
r = ccall($(Expr(:quote, sym)), Int32, ($(esc(arg1type)), Ptr{UInt8}), $(esc(arg)), $(esc(stat_buf)))
if !(r in (0, Base.UV_ENOENT, Base.UV_ENOTDIR, Base.UV_EINVAL))
uv_error(string("stat(", repr($(esc(arg))), ")"), r)
Expand All @@ -161,9 +165,11 @@ end
"""
stat!(stat_buf::Vector{UInt8}, file)

Like [`stat`](@ref), but avoids internal allocations by using a pre-allocated buffer,
`stat_buf`. For a small performance gain over `stat`, consecutive calls to `stat!` can use
the same `stat_buf`. See also [`Base.Filesystem.get_stat_buf`](@ref).
Like [`stat`](@ref), but tries to avoid internal allocations by using a pre-allocated
buffer, `stat_buf`. For a small performance gain over `stat`, consecutive calls to `stat!`
can use the same `stat_buf`. If `stat_buf` is not large enough to hold the result, it will
be automatically resized. A buffer with the minimum capacity can be allocated using:
`zeros(UInt8, Base.Filesystem.STAT_BUFFER_SIZE)`.
"""
stat!(stat_buf::Vector{UInt8}, fd::OS_HANDLE) = @stat_call! stat_buf jl_fstat OS_HANDLE fd
stat!(stat_buf::Vector{UInt8}, path::AbstractString) = @stat_call! stat_buf jl_stat Cstring path
Expand All @@ -173,15 +179,8 @@ if RawFD !== OS_HANDLE
end
stat!(stat_buf::Vector{UInt8}, fd::Integer) = stat!(stat_buf, RawFD(fd))

stat(x) = stat!(get_stat_buf(), x)
lstat(x) = lstat!(get_stat_buf(), x)

"""
get_stat_buf()

Return a buffer of bytes of the right size for [`stat!`](@ref).
"""
get_stat_buf() = zeros(UInt8, Int(ccall(:jl_sizeof_stat, Int32, ())))
stat(x) = stat!(zeros(UInt8, STAT_BUFFER_SIZE), x)
lstat(x) = lstat!(zeros(UInt8, STAT_BUFFER_SIZE), x)

"""
stat(file)
Expand Down
1 change: 1 addition & 0 deletions doc/src/base/file.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Base.Filesystem.chmod
Base.Filesystem.chown
Base.RawFD
Base.stat
Base.stat!
Base.Filesystem.diskstat
Base.Filesystem.lstat
Base.Filesystem.ctime
Expand Down