]> Cypherpunks repositories - gostls13.git/commitdiff
internal/syscall/unix: implement Eaccess on all unix platforms
authorKir Kolyshkin <kolyshkin@gmail.com>
Thu, 5 Sep 2024 18:48:00 +0000 (11:48 -0700)
committerGopher Robot <gobot@golang.org>
Fri, 6 Sep 2024 13:29:47 +0000 (13:29 +0000)
Eaccess, initially added by CL 414824 for linux only, was later
implemented for freebsd (CL 531155), netbsd (CL 531876), dragonfly
(CL 532675), openbsd (CL 538836), and darwin (CL 579976).

The only unix platforms which lack Eaccess are Solaris/Illumos and AIX.

For AIX, syscall.Faccessat is already available, the only missing piece
was AT_EACCESS constant. Let's take it from [1], which, judging by a few
other known AT_ constants, appears to be accurate.

For Solaris, wire the faccessat using the same logic as in the syscall
package.

Now, when we have faccessat for every unix, we can drop eaccess_other.go
and consolidate Eaccess implementations to use faccessat.

[1]: https://github.com/rust-lang/libc/blob/main/src/unix/aix/mod.rs

Change-Id: I7e1b90dedc5d8174235d3a79d5c662f3dcb909c3
Reviewed-on: https://go-review.googlesource.com/c/go/+/611295
Auto-Submit: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Run-TryBot: Kirill Kolyshkin <kolyshkin@gmail.com>

src/internal/syscall/unix/at_aix.go
src/internal/syscall/unix/at_solaris.go
src/internal/syscall/unix/eaccess.go [moved from src/internal/syscall/unix/eaccess_linux.go with 56% similarity]
src/internal/syscall/unix/eaccess_other.go [deleted file]
src/internal/syscall/unix/faccessat_bsd.go [moved from src/internal/syscall/unix/eaccess_bsd.go with 85% similarity]
src/internal/syscall/unix/faccessat_darwin.go [moved from src/internal/syscall/unix/eaccess_darwin.go with 87% similarity]
src/internal/syscall/unix/faccessat_openbsd.go [moved from src/internal/syscall/unix/eaccess_openbsd.go with 89% similarity]
src/internal/syscall/unix/faccessat_solaris.go [new file with mode: 0644]
src/internal/syscall/unix/faccessat_syscall.go [new file with mode: 0644]

index 3fe3285ce21257c527eda5307614526f723a3112..04cacf7f30333c89924e3c4b1a72fcc5c45d1efc 100644 (file)
@@ -9,6 +9,8 @@ package unix
 //go:cgo_import_dynamic libc_unlinkat unlinkat "libc.a/shr_64.o"
 
 const (
+       AT_EACCESS          = 0x1
+       AT_FDCWD            = -0x02
        AT_REMOVEDIR        = 0x1
        AT_SYMLINK_NOFOLLOW = 0x1
        UTIME_OMIT          = -0x3
index ae1c1d64ca7bc2834cc8792cd3f80e94a584ffd5..7a29eb309cc5c9bf773eac2468302e734999e007 100644 (file)
@@ -12,12 +12,15 @@ func syscall6(trap, nargs, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err
 // Implemented as rawsysvicall6 in runtime/syscall_solaris.go.
 func rawSyscall6(trap, nargs, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err syscall.Errno)
 
+//go:cgo_import_dynamic libc_faccessat faccessat "libc.so"
 //go:cgo_import_dynamic libc_fstatat fstatat "libc.so"
 //go:cgo_import_dynamic libc_openat openat "libc.so"
 //go:cgo_import_dynamic libc_unlinkat unlinkat "libc.so"
 //go:cgo_import_dynamic libc_uname uname "libc.so"
 
 const (
+       AT_EACCESS          = 0x4
+       AT_FDCWD            = 0xffd19553
        AT_REMOVEDIR        = 0x1
        AT_SYMLINK_NOFOLLOW = 0x1000
 
similarity index 56%
rename from src/internal/syscall/unix/eaccess_linux.go
rename to src/internal/syscall/unix/eaccess.go
index 5695a5e4ce7cdfaf51243f427dd21bd868a6009b..531fd2f1067b24d75bab5e26047cd9ef35d53242 100644 (file)
@@ -1,11 +1,11 @@
-// Copyright 2022 The Go Authors. All rights reserved.
+// Copyright 2024 The Go Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-package unix
+//go:build unix
 
-import "syscall"
+package unix
 
 func Eaccess(path string, mode uint32) error {
-       return syscall.Faccessat(AT_FDCWD, path, mode, AT_EACCESS)
+       return faccessat(AT_FDCWD, path, mode, AT_EACCESS)
 }
diff --git a/src/internal/syscall/unix/eaccess_other.go b/src/internal/syscall/unix/eaccess_other.go
deleted file mode 100644 (file)
index 3da3a64..0000000
+++ /dev/null
@@ -1,13 +0,0 @@
-// Copyright 2022 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-//go:build unix && !darwin && !dragonfly && !freebsd && !linux && !openbsd && !netbsd
-
-package unix
-
-import "syscall"
-
-func Eaccess(path string, mode uint32) error {
-       return syscall.ENOSYS
-}
similarity index 85%
rename from src/internal/syscall/unix/eaccess_bsd.go
rename to src/internal/syscall/unix/faccessat_bsd.go
index 7077af17b672e5f1b7ad734a5da6b2f4fe322cd1..78fca18e27b3dc1873fbf2a712e1a36c42cb7ed8 100644 (file)
@@ -22,7 +22,3 @@ func faccessat(dirfd int, path string, mode uint32, flags int) error {
        }
        return err
 }
-
-func Eaccess(path string, mode uint32) error {
-       return faccessat(AT_FDCWD, path, mode, AT_EACCESS)
-}
similarity index 87%
rename from src/internal/syscall/unix/eaccess_darwin.go
rename to src/internal/syscall/unix/faccessat_darwin.go
index 0fa8d17afeaefd30d28fd5330585f31ab00745e6..ef790aa949a9a48c0ee14a3fac34f0d53078a1aa 100644 (file)
@@ -25,7 +25,3 @@ func faccessat(dirfd int, path string, mode uint32, flags int) error {
        }
        return nil
 }
-
-func Eaccess(path string, mode uint32) error {
-       return faccessat(AT_FDCWD, path, mode, AT_EACCESS)
-}
similarity index 89%
rename from src/internal/syscall/unix/eaccess_openbsd.go
rename to src/internal/syscall/unix/faccessat_openbsd.go
index 5e91f11f66542af3a1802be690ba7b5ec547a697..9d4ed9729120325540ddb9e91552a252e11736e0 100644 (file)
@@ -30,7 +30,3 @@ func faccessat(dirfd int, path string, mode uint32, flags int) error {
        }
        return err
 }
-
-func Eaccess(path string, mode uint32) error {
-       return faccessat(AT_FDCWD, path, mode, AT_EACCESS)
-}
diff --git a/src/internal/syscall/unix/faccessat_solaris.go b/src/internal/syscall/unix/faccessat_solaris.go
new file mode 100644 (file)
index 0000000..47e05fb
--- /dev/null
@@ -0,0 +1,28 @@
+// Copyright 2024 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package unix
+
+import (
+       "syscall"
+       "unsafe"
+)
+
+//go:linkname procFaccessat libc_faccessat
+
+var procFaccessat uintptr
+
+func faccessat(dirfd int, path string, mode uint32, flags int) error {
+       p, err := syscall.BytePtrFromString(path)
+       if err != nil {
+               return err
+       }
+
+       _, _, errno := syscall6(uintptr(unsafe.Pointer(&procFaccessat)), 4, uintptr(dirfd), uintptr(unsafe.Pointer(p)), uintptr(mode), uintptr(flags), 0, 0)
+       if errno != 0 {
+               return errno
+       }
+
+       return nil
+}
diff --git a/src/internal/syscall/unix/faccessat_syscall.go b/src/internal/syscall/unix/faccessat_syscall.go
new file mode 100644 (file)
index 0000000..865e40b
--- /dev/null
@@ -0,0 +1,11 @@
+// Copyright 2024 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+//go:build aix || linux
+
+package unix
+
+import "syscall"
+
+var faccessat = syscall.Faccessat