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 1 commit
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
Next Next commit
Add safe_isfile, safe_isdir, safe_ispath and tests
  • Loading branch information
ven-k committed Nov 5, 2021
commit 4d622b11f13ee03fee4659cf7aaeb71842080bb4
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) = safe_isfile(path)
elseif Sys.iswindows()
# GetLongPathName Win32 function returns the case-preserved filename on NTFS.
function isfile_casesensitive(path)
isfile(path) || return false # Fail fast
safe_isfile(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
safe_isfile(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
safe_isfile(path) || return false
dir, filename = splitdir(path)
any(readdir(dir) .== filename)
end
end

# Safe, as in, `isdir`, `isfile`, `ispath` return false if an IOError is encountered

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

function safe_isfile(file)
stevengj marked this conversation as resolved.
Show resolved Hide resolved
return try
isfile(file)
catch err
err isa IOError || rethrow()
false
end
end

function safe_ispath(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.safe_isfile("/root/path/doesn't/exist") == false
@test Base.safe_ispath("/root/path/doesn't/exist") == false
@test Base.safe_isdir("/root/path/doesn't/exist") == false

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