Is there any direct way to seek relative to the end of a file or stream with a single call? For example, C and Python allow you set a positive negative offset relative to SEEK_END via fseek
or lseek
. Cross posted from Slack.
I think the best you can do is seek(io, length(io)-x)
. Adding a two-arg seekend
seems reasonable to me though
What is the advantage of a two-arg seekend instead of a read or write directly to an offset? For example libUV doesn't support seeking files https://github.com/libuv/libuv/issues/2378 but does allow an additional argument offset
to read and write.
Also from what I can tell, length
is not documented to work on files or streams. You could do:
function fsize(x)
seekend(x)
position(x)
end
To get the file size before doing the seek.
StaticTools.jl fseek
has SEEK_END
but yeah, not the most kosher package
Currently we have the following
function seekend(f::File)
ret = ccall(:jl_lseek, Int64, (OS_HANDLE, Int64, Int32), f.handle, 0, SEEK_END)
ret == -1 && (@static Sys.iswindows() ? windowserror : systemerror)("seekend")
return f
end
Could we do this instead?
function seekend(f::File, n::Integer=0)
ret = ccall(:jl_lseek, Int64, (OS_HANDLE, Int64, Int32), f.handle, n, SEEK_END)
ret == -1 && (@static Sys.iswindows() ? windowserror : systemerror)("seekend")
return f
end
looks like that ought to work AFAICT, except maybe should explicitly convert n
to Int32
first if I'm looking at that right?
n
should be converted to Int64
, but I think ccall
will do that for us.
https://github.com/JuliaLang/julia/pull/51908
Last updated: Nov 06 2024 at 04:40 UTC