From dcee8f5d518407b5926fd55443aa6f8b6d6fbc7b Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Fri, 30 Aug 2024 13:26:54 -0400 Subject: [PATCH 1/6] fall back to slower stat filesize if optimized filesize fails --- base/iostream.jl | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/base/iostream.jl b/base/iostream.jl index 762f881cfbecb..2c95546377648 100644 --- a/base/iostream.jl +++ b/base/iostream.jl @@ -228,7 +228,13 @@ function position(s::IOStream) end function filesize(s::IOStream) - sz = @_lock_ios s ccall(:ios_filesize, Int64, (Ptr{Cvoid},), s.ios) + sz = try + @_lock_ios s ccall(:ios_filesize, Int64, (Ptr{Cvoid},), s.ios) + catch e + e isa IOError || rethrow() + # if `s` is not seekable `ios_filesize` can fail, so fall back to slower stat method + stat(s).filesize + end if sz == -1 err = Libc.errno() throw(IOError(string("filesize: ", Libc.strerror(err), " for ", s.name), err)) From 0d691e5a4f00b6ffe5e6f2422da437a167cc539d Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Sat, 31 Aug 2024 14:25:29 -0400 Subject: [PATCH 2/6] fix --- base/iostream.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/iostream.jl b/base/iostream.jl index 2c95546377648..30c8b65f9ea63 100644 --- a/base/iostream.jl +++ b/base/iostream.jl @@ -233,7 +233,7 @@ function filesize(s::IOStream) catch e e isa IOError || rethrow() # if `s` is not seekable `ios_filesize` can fail, so fall back to slower stat method - stat(s).filesize + filesize(stat(s)) end if sz == -1 err = Libc.errno() From 984f884dfe3d56178037b46d5675483f090519f6 Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Sat, 31 Aug 2024 16:05:50 -0400 Subject: [PATCH 3/6] Update iostream.jl Co-authored-by: Jameson Nash --- base/iostream.jl | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/base/iostream.jl b/base/iostream.jl index 30c8b65f9ea63..74908344e078e 100644 --- a/base/iostream.jl +++ b/base/iostream.jl @@ -228,16 +228,10 @@ function position(s::IOStream) end function filesize(s::IOStream) - sz = try - @_lock_ios s ccall(:ios_filesize, Int64, (Ptr{Cvoid},), s.ios) - catch e - e isa IOError || rethrow() - # if `s` is not seekable `ios_filesize` can fail, so fall back to slower stat method - filesize(stat(s)) - end + sz = @_lock_ios s ccall(:ios_filesize, Int64, (Ptr{Cvoid},), s.ios) if sz == -1 - err = Libc.errno() - throw(IOError(string("filesize: ", Libc.strerror(err), " for ", s.name), err)) + # if `s` is not seekable `ios_filesize` can fail, so fall back to slower stat method + sz = filesize(stat(s)) end return sz end From bc0c3d940c9cc9650d6b482461ede384580bd83b Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Tue, 3 Sep 2024 22:20:48 -0400 Subject: [PATCH 4/6] if non-regular file skip trying to grow since we know that will fail the ftruncate syscall Co-Authored-By: Jameson Nash --- stdlib/Mmap/src/Mmap.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/stdlib/Mmap/src/Mmap.jl b/stdlib/Mmap/src/Mmap.jl index e6987582bf511..b45042e055c51 100644 --- a/stdlib/Mmap/src/Mmap.jl +++ b/stdlib/Mmap/src/Mmap.jl @@ -86,6 +86,8 @@ grow!(::Anonymous,o::Integer,l::Integer) = return function grow!(io::IO, offset::Integer, len::Integer) pos = position(io) filelen = filesize(io) + # If non-regular file skip trying to grow since we know that will fail the ftruncate syscall + filelen == 0 && isfile(stat(io)) && return if filelen < offset + len failure = ccall(:jl_ftruncate, Cint, (Cint, Int64), fd(io), offset+len) Base.systemerror(:ftruncate, failure != 0) From 30c3721e7fb4b317ddba41f1bcac7f1ba4d0694c Mon Sep 17 00:00:00 2001 From: Oscar Smith Date: Fri, 20 Sep 2024 14:29:24 -0400 Subject: [PATCH 5/6] address review --- stdlib/Mmap/src/Mmap.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/Mmap/src/Mmap.jl b/stdlib/Mmap/src/Mmap.jl index b45042e055c51..229dc00bbd627 100644 --- a/stdlib/Mmap/src/Mmap.jl +++ b/stdlib/Mmap/src/Mmap.jl @@ -87,7 +87,7 @@ function grow!(io::IO, offset::Integer, len::Integer) pos = position(io) filelen = filesize(io) # If non-regular file skip trying to grow since we know that will fail the ftruncate syscall - filelen == 0 && isfile(stat(io)) && return + filelen == 0 && !isfile(stat(io)) && return if filelen < offset + len failure = ccall(:jl_ftruncate, Cint, (Cint, Int64), fd(io), offset+len) Base.systemerror(:ftruncate, failure != 0) From 69c9a3c95b86e97928ca5cea57abf81d925bb1d9 Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Mon, 23 Sep 2024 16:02:44 -0400 Subject: [PATCH 6/6] defer the grow! fix to a following PR --- stdlib/Mmap/src/Mmap.jl | 2 -- 1 file changed, 2 deletions(-) diff --git a/stdlib/Mmap/src/Mmap.jl b/stdlib/Mmap/src/Mmap.jl index 229dc00bbd627..e6987582bf511 100644 --- a/stdlib/Mmap/src/Mmap.jl +++ b/stdlib/Mmap/src/Mmap.jl @@ -86,8 +86,6 @@ grow!(::Anonymous,o::Integer,l::Integer) = return function grow!(io::IO, offset::Integer, len::Integer) pos = position(io) filelen = filesize(io) - # If non-regular file skip trying to grow since we know that will fail the ftruncate syscall - filelen == 0 && !isfile(stat(io)) && return if filelen < offset + len failure = ccall(:jl_ftruncate, Cint, (Cint, Int64), fd(io), offset+len) Base.systemerror(:ftruncate, failure != 0)