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

Use isaccessiblefile for isfile_casesensitive #42922

Merged
merged 5 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
37 changes: 33 additions & 4 deletions base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ const require_lock = ReentrantLock()

if Sys.isunix() && !Sys.isapple()
# assume case-sensitive filesystems, don't have to do anything
isfile_casesensitive(path) = isfile(path)
isfile_casesensitive(path) = isaccessiblefile(path)
elseif Sys.iswindows()
# GetLongPathName Win32 function returns the case-preserved filename on NTFS.
function isfile_casesensitive(path)
isfile(path) || return false # Fail fast
isaccessiblefile(path) || return false # Fail fast
basename(Filesystem.longpath(path)) == basename(path)
end
elseif Sys.isapple()
Expand Down Expand Up @@ -44,7 +44,7 @@ elseif Sys.isapple()
# Buffer buf;
# getattrpath(path, &attr_list, &buf, sizeof(buf), FSOPT_);
function isfile_casesensitive(path)
isfile(path) || return false
isaccessiblefile(path) || return false
path_basename = String(basename(path))
local casepreserved_basename
header_size = 12
Expand Down Expand Up @@ -75,12 +75,41 @@ elseif Sys.isapple()
else
# Generic fallback that performs a slow directory listing.
function isfile_casesensitive(path)
isfile(path) || return false
isaccessiblefile(path) || return false
dir, filename = splitdir(path)
any(readdir(dir) .== filename)
end
end

# Check if the file is accessible. If stat fails return `false`

function isaccessibledir(dir)
return try
isdir(dir)
catch err
err isa IOError || rethrow()
false
end
end

function isaccessiblefile(file)
return try
isfile(file)
catch err
err isa IOError || rethrow()
false
end
end

function isaccessiblepath(path)
return try
ispath(path)
catch err
err isa IOError || rethrow()
false
end
end

## SHA1 ##

struct SHA1
Expand Down
6 changes: 6 additions & 0 deletions test/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ end
@test Base.in_sysimage(Base.PkgId(Base.UUID("cf7118a7-6976-5b1a-9a39-7adc72f591a4"), "UUIDs"))
@test Base.in_sysimage(Base.PkgId(Base.UUID("3a7fdc7e-7467-41b4-9f64-ea033d046d5b"), "NotAPackage")) == false

## Unit tests for safe file operations ##

@test Base.isaccessiblefile("/root/path/doesn't/exist") == false
@test Base.isaccessiblepath("/root/path/doesn't/exist") == false
@test Base.isaccessibledir("/root/path/doesn't/exist") == false

# Issue #5789 and PR #13542:
mktempdir() do dir
cd(dir) do
Expand Down