]> Cypherpunks repositories - gostls13.git/commitdiff
os: RemoveAll: fix error returned
authorKir Kolyshkin <kolyshkin@gmail.com>
Thu, 30 May 2024 21:00:40 +0000 (14:00 -0700)
committerGopher Robot <gobot@golang.org>
Tue, 11 Jun 2024 17:09:26 +0000 (17:09 +0000)
When unlink fails, it is not yet known if the argument is a directory or not.
Since CL 588495, we figure out if it's a directory when trying to open
it (and, for a directory, return the original unlink error).

The (very minor) issue is, in case of a symlink, a different error is
returned -- usually it's ELOOP, but some systems use other values. Let's
account for that error code, too.

This is a followup to CL 588495.

Change-Id: I4ee10fe9b57f045fbca02f13e5c9ea16972803bc
Reviewed-on: https://go-review.googlesource.com/c/go/+/589376
Reviewed-by: Michael Pratt <mpratt@google.com>
Run-TryBot: Kirill Kolyshkin <kolyshkin@gmail.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
src/internal/syscall/unix/constants.go
src/internal/syscall/unix/nofollow_bsd.go [new file with mode: 0644]
src/internal/syscall/unix/nofollow_netbsd.go [new file with mode: 0644]
src/internal/syscall/unix/nofollow_posix.go [new file with mode: 0644]
src/os/removeall_at.go

index e3245897055cce0d1abb51475f4ef02ee15faa2c..28092c2ddfde996ffbcca3bbb23e3d4919ac5ee1 100644 (file)
@@ -10,4 +10,9 @@ const (
        R_OK = 0x4
        W_OK = 0x2
        X_OK = 0x1
+
+       // NoFollowErrno is the error returned from open/openat called with
+       // O_NOFOLLOW flag, when the trailing component (basename) of the path
+       // is a symbolic link.
+       NoFollowErrno = noFollowErrno
 )
diff --git a/src/internal/syscall/unix/nofollow_bsd.go b/src/internal/syscall/unix/nofollow_bsd.go
new file mode 100644 (file)
index 0000000..32c4de1
--- /dev/null
@@ -0,0 +1,14 @@
+// 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 dragonfly || freebsd
+
+package unix
+
+import "syscall"
+
+// References:
+// - https://man.freebsd.org/cgi/man.cgi?open(2)
+// - https://man.dragonflybsd.org/?command=open&section=2
+const noFollowErrno = syscall.EMLINK
diff --git a/src/internal/syscall/unix/nofollow_netbsd.go b/src/internal/syscall/unix/nofollow_netbsd.go
new file mode 100644 (file)
index 0000000..3ae91e7
--- /dev/null
@@ -0,0 +1,10 @@
+// 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"
+
+// Reference: https://man.netbsd.org/open.2
+const noFollowErrno = syscall.EFTYPE
diff --git a/src/internal/syscall/unix/nofollow_posix.go b/src/internal/syscall/unix/nofollow_posix.go
new file mode 100644 (file)
index 0000000..de2ea14
--- /dev/null
@@ -0,0 +1,22 @@
+// 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 unix && !dragonfly && !freebsd && !netbsd
+
+package unix
+
+import "syscall"
+
+// POSIX.1-2008 says it's ELOOP. Most platforms follow:
+//
+//   - aix: O_NOFOLLOW not documented (https://www.ibm.com/docs/ssw_aix_73/o_bostechref/open.html), assuming ELOOP
+//   - android: see linux
+//   - darwin: https://github.com/apple/darwin-xnu/blob/main/bsd/man/man2/open.2
+//   - hurd: who knows if it works at all (https://www.gnu.org/software/hurd/open_issues/open_symlink.html)
+//   - illumos: https://illumos.org/man/2/open
+//   - ios: see darwin
+//   - linux: https://man7.org/linux/man-pages/man2/openat.2.html
+//   - openbsd: https://man.openbsd.org/open.2
+//   - solaris: https://docs.oracle.com/cd/E23824_01/html/821-1463/open-2.html
+const noFollowErrno = syscall.ELOOP
index 2a12add7a25e6bd703bad94974f05114b58d3417..cc254e0043fc0931ba77cb49a9b7fbba6c09e841 100644 (file)
@@ -88,7 +88,7 @@ func removeAllFrom(parent *File, base string) error {
                        if IsNotExist(err) {
                                return nil
                        }
-                       if err == syscall.ENOTDIR {
+                       if err == syscall.ENOTDIR || err == unix.NoFollowErrno {
                                // Not a directory; return the error from the unix.Unlinkat.
                                return &PathError{Op: "unlinkat", Path: base, Err: uErr}
                        }