8322166: Files.isReadable/isWritable/isExecutable expensive when file does not exist

Reviewed-by: alanb
This commit is contained in:
Brian Burkhalter 2023-12-19 18:27:06 +00:00
parent 0f8e4e0a81
commit 51be857f3c
6 changed files with 98 additions and 50 deletions

View file

@ -349,11 +349,35 @@ public abstract class UnixFileSystemProvider
}
mode |= X_OK;
}
try {
access(file, mode);
} catch (UnixException exc) {
exc.rethrowAsIOException(file);
int errno = access(file, mode);
if (errno != 0)
new UnixException(errno).rethrowAsIOException(file);
}
@Override
public boolean isReadable(Path path) {
UnixPath file = UnixPath.toUnixPath(path);
file.checkRead();
return access(file, R_OK) == 0;
}
@Override
public boolean isWritable(Path path) {
UnixPath file = UnixPath.toUnixPath(path);
file.checkWrite();
return access(file, W_OK) == 0;
}
@Override
public boolean isExecutable(Path path) {
UnixPath file = UnixPath.toUnixPath(path);
@SuppressWarnings("removal")
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
// not cached
sm.checkExec(file.getPathForPermissionCheck());
}
return access(file, X_OK) == 0;
}
@Override
@ -561,7 +585,7 @@ public abstract class UnixFileSystemProvider
if (Util.followLinks(options)) {
UnixPath file = UnixPath.toUnixPath(path);
file.checkRead();
return UnixNativeDispatcher.exists(file);
return access(file, F_OK) == 0;
} else {
return super.exists(path, options);
}