8230085: (fs) FileStore::isReadOnly is always true on macOS Catalina
authorbpb
Fri, 13 Sep 2019 16:03:31 -0700
changeset 58131 3054503bad7d
parent 58130 ff0eae1719d0
child 58132 caa25ab47aca
child 58156 68031e660872
8230085: (fs) FileStore::isReadOnly is always true on macOS Catalina Reviewed-by: alanb
src/java.base/macosx/classes/sun/nio/fs/BsdFileStore.java
src/java.base/macosx/classes/sun/nio/fs/BsdNativeDispatcher.java
src/java.base/macosx/native/libnio/fs/BsdNativeDispatcher.c
src/java.base/unix/classes/sun/nio/fs/UnixNativeDispatcher.java
--- a/src/java.base/macosx/classes/sun/nio/fs/BsdFileStore.java	Fri Sep 13 18:54:43 2019 -0400
+++ b/src/java.base/macosx/classes/sun/nio/fs/BsdFileStore.java	Fri Sep 13 16:03:31 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2008, 2019, 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
@@ -62,22 +62,14 @@
         }
 
         // step 2: find mount point
-        UnixPath parent = path.getParent();
-        while (parent != null) {
-            UnixFileAttributes attrs = null;
-            try {
-                attrs = UnixFileAttributes.get(parent, true);
-            } catch (UnixException x) {
-                x.rethrowAsIOException(parent);
-            }
-            if (attrs.dev() != dev())
-                break;
-            path = parent;
-            parent = parent.getParent();
+        byte[] dir = null;
+        try {
+            dir = BsdNativeDispatcher.getmntonname(path);
+        } catch (UnixException x) {
+            x.rethrowAsIOException(path);
         }
 
         // step 3: lookup mounted file systems
-        byte[] dir = path.asByteArray();
         for (UnixMountEntry entry: fs.getMountEntries()) {
             if (Arrays.equals(dir, entry.dir()))
                 return entry;
--- a/src/java.base/macosx/classes/sun/nio/fs/BsdNativeDispatcher.java	Fri Sep 13 18:54:43 2019 -0400
+++ b/src/java.base/macosx/classes/sun/nio/fs/BsdNativeDispatcher.java	Fri Sep 13 16:03:31 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2008, 2019, 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
@@ -51,6 +51,20 @@
     */
     static native void endfsstat(long iter) throws UnixException;
 
+    /**
+     * int statfs(const char *path, struct statfs *buf);
+     * returns buf->f_mntonname (directory on which mounted)
+     */
+    static byte[] getmntonname(UnixPath path) throws UnixException {
+        NativeBuffer pathBuffer = copyToNativeBuffer(path);
+        try {
+            return getmntonname0(pathBuffer.address());
+        } finally {
+            pathBuffer.release();
+        }
+    }
+    static native byte[] getmntonname0(long pathAddress) throws UnixException;
+
     // initialize field IDs
     private static native void initIDs();
 
--- a/src/java.base/macosx/native/libnio/fs/BsdNativeDispatcher.c	Fri Sep 13 18:54:43 2019 -0400
+++ b/src/java.base/macosx/native/libnio/fs/BsdNativeDispatcher.c	Fri Sep 13 16:03:31 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2008, 2019, 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
@@ -203,3 +203,24 @@
         free(iter);
     }
 }
+
+JNIEXPORT jbyteArray JNICALL
+Java_sun_nio_fs_BsdNativeDispatcher_getmntonname0(JNIEnv *env, jclass this,
+    jlong pathAddress)
+{
+    struct statfs buf;
+    const char* path = (const char*)jlong_to_ptr(pathAddress);
+
+    if (statfs(path, &buf) != 0) {
+        throwUnixException(env, errno);
+    }
+
+    jsize len = strlen(buf.f_mntonname);
+    jbyteArray mntonname = (*env)->NewByteArray(env, len);
+    if (mntonname != NULL) {
+        (*env)->SetByteArrayRegion(env, mntonname, 0, len,
+            (jbyte*)buf.f_mntonname);
+    }
+
+    return mntonname;
+}
--- a/src/java.base/unix/classes/sun/nio/fs/UnixNativeDispatcher.java	Fri Sep 13 18:54:43 2019 -0400
+++ b/src/java.base/unix/classes/sun/nio/fs/UnixNativeDispatcher.java	Fri Sep 13 16:03:31 2019 -0700
@@ -33,7 +33,7 @@
     protected UnixNativeDispatcher() { }
 
     // returns a NativeBuffer containing the given path
-    private static NativeBuffer copyToNativeBuffer(UnixPath path) {
+    static NativeBuffer copyToNativeBuffer(UnixPath path) {
         byte[] cstr = path.getByteArrayForSysCalls();
         int size = cstr.length + 1;
         NativeBuffer buffer = NativeBuffers.getNativeBufferFromCache(size);