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

More robust handling of filenames, paritucalrly errors about too long #264

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 src/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
17 changes: 14 additions & 3 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,18 @@ end
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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this code consistent across OS platforms? can we add a test to catch the new code paths here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect the original code might work as intended on Windows, and this branch wouldn't ever be triggered. Still, yeah I'll try to write a test.

else
rethrow()
end
end