Skip to content

Commit

Permalink
Add and use isaccessiblefile for isfile_casesensitive (JuliaLang#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ven-k authored and LilithHafner committed Mar 8, 2022
1 parent 65ce532 commit 118a413
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
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_NOFOLLOW);
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

0 comments on commit 118a413

Please sign in to comment.