8073061: (fs) Files.copy(foo, bar, REPLACE_EXISTING) deletes bar even if foo is not readable

Reviewed-by: alanb
This commit is contained in:
Brian Burkhalter 2023-09-26 15:27:44 +00:00
parent efb7e85ecf
commit 36ac83904c
5 changed files with 314 additions and 14 deletions

View file

@ -891,6 +891,10 @@ abstract class UnixFileSystem
// get attributes of source file (don't follow links)
try {
sourceAttrs = UnixFileAttributes.get(source, false);
if (sourceAttrs.isDirectory()) {
// ensure source can be moved
access(source, W_OK);
}
} catch (UnixException x) {
x.rethrowAsIOException(source);
}
@ -910,10 +914,9 @@ abstract class UnixFileSystem
if (targetExists) {
if (sourceAttrs.isSameFile(targetAttrs))
return; // nothing to do as files are identical
if (!flags.replaceExisting) {
if (!flags.replaceExisting)
throw new FileAlreadyExistsException(
target.getPathForExceptionMessage());
}
// attempt to delete target
try {
@ -1019,6 +1022,17 @@ abstract class UnixFileSystem
sm.checkPermission(new LinkPermission("symbolic"));
}
// ensure source can be copied
if (!sourceAttrs.isSymbolicLink() || flags.followLinks) {
try {
// the access(2) system call always follows links so it
// is suppressed if the source is an unfollowed link
access(source, R_OK);
} catch (UnixException exc) {
exc.rethrowAsIOException(source);
}
}
// get attributes of target file (don't follow links)
try {
targetAttrs = UnixFileAttributes.get(target, false);
@ -1037,6 +1051,7 @@ abstract class UnixFileSystem
if (!flags.replaceExisting)
throw new FileAlreadyExistsException(
target.getPathForExceptionMessage());
try {
if (targetAttrs.isDirectory()) {
rmdir(target);

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -495,7 +495,7 @@ public abstract class UnixFileSystemProvider
if (attrs.length > 0) {
UnixFileModeAttribute.toUnixMode(0, attrs); // may throw NPE or UOE
throw new UnsupportedOperationException("Initial file attributes" +
"not supported when creating symbolic link");
" not supported when creating symbolic link");
}
// permission check