From d689897cb071fe6406535593d123dd8ed20ae2cf Mon Sep 17 00:00:00 2001 From: Nicu Stiurca Date: Fri, 7 Jul 2023 17:44:53 -0500 Subject: [PATCH 1/2] More robust handling of filenames, paritucalrly errors about too long --- src/utils.jl | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index 63fa16c..4c3791f 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -189,10 +189,22 @@ Base.@pure function symbolin(names::Tuple{Vararg{Symbol}}, name::Symbol) return false end +read_json_str(json::VectorString) = json function read_json_str(json) # length check is to ensure that isfile doesn't thrown an error # see issue for details https://github.com/JuliaLang/julia/issues/39774 - !(json isa VectorString) && sizeof(json) < 255 && isfile(json) ? - VectorString(Mmap.mmap(json)) : - json + isfilename(json) && isfile(json) ? + VectorString(Mmap.mmap(json)) : + json +end + +isfilename(filename) = try + stat(filename) + true +catch e + if isa(e, Base.IOError) + e.code != -36 # -36 is ENAMETOOLONG; other IO errors indicate valid filename + else + rethrow() + end end From ec5fd2cc6d63bb61e97d39b8b553948f5b498b03 Mon Sep 17 00:00:00 2001 From: Nicu Stiurca Date: Fri, 7 Jul 2023 18:10:10 -0500 Subject: [PATCH 2/2] Define methods on VectorString only after the struct itself is defined --- src/read.jl | 1 + src/utils.jl | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/src/read.jl b/src/read.jl index 50354d4..b689f61 100644 --- a/src/read.jl +++ b/src/read.jl @@ -4,6 +4,7 @@ struct VectorString{T <: AbstractVector{UInt8}} <: AbstractString end Base.codeunits(x::VectorString) = x.bytes +read_json_str(json::VectorString) = json # high-level user API functions read(io::Union{IO, Base.AbstractCmd}; kw...) = read(Base.read(io, String); kw...) diff --git a/src/utils.jl b/src/utils.jl index 4c3791f..8609047 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -189,7 +189,6 @@ Base.@pure function symbolin(names::Tuple{Vararg{Symbol}}, name::Symbol) return false end -read_json_str(json::VectorString) = json function read_json_str(json) # length check is to ensure that isfile doesn't thrown an error # see issue for details https://github.com/JuliaLang/julia/issues/39774