RFR: 8062795: (fs) Files.setPermissions requires read access when NOFOLLOW_LINKS specified [v2]
Alan Bateman
alanb at openjdk.org
Tue Aug 15 09:27:08 UTC 2023
On Thu, 10 Aug 2023 00:40:00 GMT, Brian Burkhalter <bpb at openjdk.org> wrote:
>> If setting POSIX permissions while not following links fails with `AccessDeniedException` but the path does not contain any symbolic links, then retry the operation as if the `NOFOLLOW_LINKS` option had not been specified.
>
> Brian Burkhalter has updated the pull request incrementally with one additional commit since the last revision:
>
> 8062795: Add missing bug ID to test
I don't think this is the right thing as it may do the wrong thing when there are concurrency changes on the file system. I think the simplest is to just attempt to open the file for write access, this should work for regular files.
Not tested, but I expect something like this is what you want here.
diff --git a/src/java.base/unix/classes/sun/nio/fs/UnixFileAttributeViews.java b/src/java.base/unix/classes/sun/nio/fs/UnixFileAttributeViews.java
index f35541a9e19..bcab5319d8d 100644
--- a/src/java.base/unix/classes/sun/nio/fs/UnixFileAttributeViews.java
+++ b/src/java.base/unix/classes/sun/nio/fs/UnixFileAttributeViews.java
@@ -31,6 +31,7 @@ import java.util.*;
import java.util.concurrent.TimeUnit;
import java.io.IOException;
+import static sun.nio.fs.UnixConstants.*;
import static sun.nio.fs.UnixNativeDispatcher.*;
class UnixFileAttributeViews {
@@ -262,20 +263,45 @@ class UnixFileAttributeViews {
// chmod
final void setMode(int mode) throws IOException {
checkWriteExtended();
- try {
- if (followLinks) {
+
+ if (followLinks) {
+ try {
chmod(file, mode);
- } else {
- int fd = file.openForAttributeAccess(false);
+ } catch (UnixException e) {
+ e.rethrowAsIOException(file);
+ }
+ return;
+ }
+
+ if (O_NOFOLLOW == 0) {
+ throw new IOException("NOFOLLOW_LINKS is not supported on this platform");
+ }
+
+ int fd = -1;
+ try {
+ fd = open(file, O_RDONLY, O_NOFOLLOW);
+ } catch (UnixException e1) {
+ if (e1.errno() == EACCES) {
try {
- fchmod(fd, mode);
- } finally {
- close(fd);
+ fd = open(file, O_WRONLY, O_NOFOLLOW);
+ } catch (UnixException e2) {
+ e2.rethrowAsIOException(file);
}
+ } else {
+ e1.rethrowAsIOException(file);
}
- } catch (UnixException x) {
- x.rethrowAsIOException(file);
}
+
+ try {
+ try {
+ fchmod(fd, mode);
+ } finally {
+ close(fd);
+ }
+ } catch (UnixException e) {
+ e.rethrowAsIOException(file);
+ }
+
}
// chown
-------------
PR Comment: https://git.openjdk.org/jdk/pull/15214#issuecomment-1678648583
More information about the nio-dev
mailing list