From 9f6d5b46ce2cfcdb39f94b8ac8621ee21f4e8740 Mon Sep 17 00:00:00 2001
From: "David M. Lloyd" <david.lloyd@redhat.com>
Date: Mon, 28 Oct 2024 16:58:44 +0000
Subject: [PATCH] 8343020: (fs) Add support for SecureDirectoryStream on macOS

Reviewed-by: bpb, alanb
---
 .../sun/nio/fs/UnixNativeDispatcher.java      |  2 +-
 .../native/libnio/fs/UnixNativeDispatcher.c   | 30 +++++++++----------
 .../nio/file/DirectoryStream/SecureDS.java    |  9 ++----
 3 files changed, 19 insertions(+), 22 deletions(-)

diff --git a/src/java.base/unix/classes/sun/nio/fs/UnixNativeDispatcher.java b/src/java.base/unix/classes/sun/nio/fs/UnixNativeDispatcher.java
index a069a9a04ba..ab8975c6d12 100644
--- a/src/java.base/unix/classes/sun/nio/fs/UnixNativeDispatcher.java
+++ b/src/java.base/unix/classes/sun/nio/fs/UnixNativeDispatcher.java
@@ -569,7 +569,7 @@ class UnixNativeDispatcher {
     }
 
     /**
-     * Supports futimes or futimesat
+     * Supports futimes
      */
     static boolean futimesSupported() {
         return (capabilities & SUPPORTS_FUTIMES) != 0;
diff --git a/src/java.base/unix/native/libnio/fs/UnixNativeDispatcher.c b/src/java.base/unix/native/libnio/fs/UnixNativeDispatcher.c
index 61e9215471a..9a68a12c219 100644
--- a/src/java.base/unix/native/libnio/fs/UnixNativeDispatcher.c
+++ b/src/java.base/unix/native/libnio/fs/UnixNativeDispatcher.c
@@ -204,7 +204,7 @@ typedef int openat_func(int, const char *, int, ...);
 typedef int fstatat_func(int, const char *, struct stat *, int);
 typedef int unlinkat_func(int, const char*, int);
 typedef int renameat_func(int, const char*, int, const char*);
-typedef int futimesat_func(int, const char *, const struct timeval *);
+typedef int futimes_func(int, const struct timeval *);
 typedef int futimens_func(int, const struct timespec *);
 typedef int lutimes_func(const char *, const struct timeval *);
 typedef DIR* fdopendir_func(int);
@@ -217,7 +217,7 @@ static openat_func* my_openat_func = NULL;
 static fstatat_func* my_fstatat_func = NULL;
 static unlinkat_func* my_unlinkat_func = NULL;
 static renameat_func* my_renameat_func = NULL;
-static futimesat_func* my_futimesat_func = NULL;
+static futimes_func* my_futimes_func = NULL;
 static futimens_func* my_futimens_func = NULL;
 static lutimes_func* my_lutimes_func = NULL;
 static fdopendir_func* my_fdopendir_func = NULL;
@@ -363,8 +363,8 @@ Java_sun_nio_fs_UnixNativeDispatcher_init(JNIEnv* env, jclass this)
     /* system calls that might not be available at run time */
 
 #if defined(_ALLBSD_SOURCE)
-    my_openat_func = (openat_func*)dlsym(RTLD_DEFAULT, "openat");
-    my_fstatat_func = (fstatat_func*)dlsym(RTLD_DEFAULT, "fstatat");
+    my_openat_func = (openat_func*) openat;
+    my_fstatat_func = (fstatat_func*) fstatat;
 #else
     // Make sure we link to the 64-bit version of the functions
     my_openat_func = (openat_func*) dlsym(RTLD_DEFAULT, "openat64");
@@ -373,22 +373,22 @@ Java_sun_nio_fs_UnixNativeDispatcher_init(JNIEnv* env, jclass this)
     my_unlinkat_func = (unlinkat_func*) dlsym(RTLD_DEFAULT, "unlinkat");
     my_renameat_func = (renameat_func*) dlsym(RTLD_DEFAULT, "renameat");
 #if defined(__linux__) && defined(__arm__)
-    my_futimesat_func = (futimesat_func*) lookup_time_t_function("futimesat",
-        "__futimesat64");
+    my_futimes_func = (futimes_func*) lookup_time_t_function("futimes",
+        "__futimes64");
     my_lutimes_func = (lutimes_func*) lookup_time_t_function("lutimes",
         "__lutimes64");
     my_futimens_func = (futimens_func*) lookup_time_t_function("futimens",
         "__futimens64");
 #else
-#ifndef _ALLBSD_SOURCE
-    my_futimesat_func = (futimesat_func*) dlsym(RTLD_DEFAULT, "futimesat");
+    my_futimes_func = (futimes_func*) dlsym(RTLD_DEFAULT, "futimes");
     my_lutimes_func = (lutimes_func*) dlsym(RTLD_DEFAULT, "lutimes");
-#endif
     my_futimens_func = (futimens_func*) dlsym(RTLD_DEFAULT, "futimens");
 #endif
 #if defined(_AIX)
     // Make sure we link to the 64-bit version of the function
     my_fdopendir_func = (fdopendir_func*) dlsym(RTLD_DEFAULT, "fdopendir64");
+#elif defined(_ALLBSD_SOURCE)
+    my_fdopendir_func = (fdopendir_func*) fdopendir;
 #else
     my_fdopendir_func = (fdopendir_func*) dlsym(RTLD_DEFAULT, "fdopendir");
 #endif
@@ -399,13 +399,13 @@ Java_sun_nio_fs_UnixNativeDispatcher_init(JNIEnv* env, jclass this)
         my_fstatat_func = (fstatat_func*)&fstatat_wrapper;
 #endif
 
-    /* supports futimes or futimesat, futimens, and/or lutimes */
+    /* supports futimes, futimens, and/or lutimes */
 
 #ifdef _ALLBSD_SOURCE
     capabilities |= sun_nio_fs_UnixNativeDispatcher_SUPPORTS_FUTIMES;
     capabilities |= sun_nio_fs_UnixNativeDispatcher_SUPPORTS_LUTIMES;
 #else
-    if (my_futimesat_func != NULL)
+    if (my_futimes_func != NULL)
         capabilities |= sun_nio_fs_UnixNativeDispatcher_SUPPORTS_FUTIMES;
     if (my_lutimes_func != NULL)
         capabilities |= sun_nio_fs_UnixNativeDispatcher_SUPPORTS_LUTIMES;
@@ -417,7 +417,7 @@ Java_sun_nio_fs_UnixNativeDispatcher_init(JNIEnv* env, jclass this)
 
     if (my_openat_func != NULL &&  my_fstatat_func != NULL &&
         my_unlinkat_func != NULL && my_renameat_func != NULL &&
-        my_futimesat_func != NULL && my_fdopendir_func != NULL)
+        my_futimes_func != NULL && my_fdopendir_func != NULL)
     {
         capabilities |= sun_nio_fs_UnixNativeDispatcher_SUPPORTS_OPENAT;
     }
@@ -914,11 +914,11 @@ Java_sun_nio_fs_UnixNativeDispatcher_futimes0(JNIEnv* env, jclass this, jint fil
 #ifdef _ALLBSD_SOURCE
     RESTARTABLE(futimes(filedes, &times[0]), err);
 #else
-    if (my_futimesat_func == NULL) {
-        JNU_ThrowInternalError(env, "my_futimesat_func is NULL");
+    if (my_futimes_func == NULL) {
+        JNU_ThrowInternalError(env, "my_futimes_func is NULL");
         return;
     }
-    RESTARTABLE((*my_futimesat_func)(filedes, NULL, &times[0]), err);
+    RESTARTABLE((*my_futimes_func)(filedes, &times[0]), err);
 #endif
     if (err == -1) {
         throwUnixException(env, errno);
diff --git a/test/jdk/java/nio/file/DirectoryStream/SecureDS.java b/test/jdk/java/nio/file/DirectoryStream/SecureDS.java
index d4f197eb09b..e058d782c52 100644
--- a/test/jdk/java/nio/file/DirectoryStream/SecureDS.java
+++ b/test/jdk/java/nio/file/DirectoryStream/SecureDS.java
@@ -22,8 +22,9 @@
  */
 
 /* @test
- * @bug 4313887 6838333
+ * @bug 4313887 6838333 8343020
  * @summary Unit test for java.nio.file.SecureDirectoryStream
+ * @requires (os.family == "linux" | os.family == "mac")
  * @library ..
  */
 
@@ -45,11 +46,7 @@ public class SecureDS {
             DirectoryStream<Path> stream = newDirectoryStream(dir);
             stream.close();
             if (!(stream instanceof SecureDirectoryStream)) {
-                if (System.getProperty("os.name").equals("Linux"))
-                    throw new AssertionError(
-                            "SecureDirectoryStream not supported.");
-                System.out.println("SecureDirectoryStream not supported.");
-                return;
+                throw new AssertionError("SecureDirectoryStream not supported.");
             }
 
             supportsSymbolicLinks = TestUtil.supportsSymbolicLinks(dir);