]> Cypherpunks repositories - gostls13.git/commitdiff
os: add and use ignoringEINTR2
authorTobias Klauser <tklauser@distanz.ch>
Fri, 15 Nov 2024 19:41:33 +0000 (20:41 +0100)
committerTobias Klauser <tklauser@distanz.ch>
Sat, 16 Nov 2024 08:22:18 +0000 (08:22 +0000)
Copy ignoringEINTR2 from internal/poll and make use of it to remove
open-coded implementations.

Change-Id: I8802862f2012980f2af445b75eb45bb5a97bcc2a
Reviewed-on: https://go-review.googlesource.com/c/go/+/627479
Auto-Submit: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/os/file_plan9.go
src/os/file_posix.go
src/os/file_unix.go
src/os/getwd.go
src/os/removeall_at.go

index ef277deccce02265327a25b205683d2805439927..c123fe696191dd1687a9af511cadae5258491b26 100644 (file)
@@ -617,3 +617,7 @@ func newRawConn(file *File) (*rawConn, error) {
 func ignoringEINTR(fn func() error) error {
        return fn()
 }
+
+func ignoringEINTR2[T any](fn func() (T, error)) (T, error) {
+       return fn()
+}
index 8ff0ada46290339fa59d6f9a267114a8a92ae817..f0cdfdae5c9cf8d7a69acebb44be4e85e9b6450e 100644 (file)
@@ -254,3 +254,13 @@ func ignoringEINTR(fn func() error) error {
                }
        }
 }
+
+// ignoringEINTR2 is ignoringEINTR, but returning an additional value.
+func ignoringEINTR2[T any](fn func() (T, error)) (T, error) {
+       for {
+               v, err := fn()
+               if err != syscall.EINTR {
+                       return v, err
+               }
+       }
+}
index 73069faa56796d102c98a329a3c706b89ea71fca..b5c0baf3ab7f18691cb2c577a29d701cd447836e 100644 (file)
@@ -446,22 +446,15 @@ func Symlink(oldname, newname string) error {
 func readlink(name string) (string, error) {
        for len := 128; ; len *= 2 {
                b := make([]byte, len)
-               var (
-                       n int
-                       e error
-               )
-               for {
-                       n, e = fixCount(syscall.Readlink(name, b))
-                       if e != syscall.EINTR {
-                               break
-                       }
-               }
+               n, err := ignoringEINTR2(func() (int, error) {
+                       return fixCount(syscall.Readlink(name, b))
+               })
                // buffer too small
-               if (runtime.GOOS == "aix" || runtime.GOOS == "wasip1") && e == syscall.ERANGE {
+               if (runtime.GOOS == "aix" || runtime.GOOS == "wasip1") && err == syscall.ERANGE {
                        continue
                }
-               if e != nil {
-                       return "", &PathError{Op: "readlink", Path: name, Err: e}
+               if err != nil {
+                       return "", &PathError{Op: "readlink", Path: name, Err: err}
                }
                if n < len {
                        return string(b[0:n]), nil
index 82f0d944df97de1c108e1d16eab0ab54b35aebae..5ce948faf5d57d814eb3707fdf282c93449f0e12 100644 (file)
@@ -53,12 +53,7 @@ func Getwd() (dir string, err error) {
 
        // If the operating system provides a Getwd call, use it.
        if syscall.ImplementsGetwd {
-               for {
-                       dir, err = syscall.Getwd()
-                       if err != syscall.EINTR {
-                               break
-                       }
-               }
+               dir, err = ignoringEINTR2(syscall.Getwd)
                // Linux returns ENAMETOOLONG if the result is too long.
                // Some BSD systems appear to return EINVAL.
                // FreeBSD systems appear to use ENOMEM
index cc254e0043fc0931ba77cb49a9b7fbba6c09e841..f52f6213f5f5a14fe8713289a7bd3ac5ac077e9a 100644 (file)
@@ -166,20 +166,11 @@ func removeAllFrom(parent *File, base string) error {
 // we are going to (try to) remove the file.
 // The contents of this file are not relevant for test caching.
 func openDirAt(dirfd int, name string) (*File, error) {
-       var r int
-       for {
-               var e error
-               r, e = unix.Openat(dirfd, name, O_RDONLY|syscall.O_CLOEXEC|syscall.O_DIRECTORY|syscall.O_NOFOLLOW, 0)
-               if e == nil {
-                       break
-               }
-
-               // See comment in openFileNolog.
-               if e == syscall.EINTR {
-                       continue
-               }
-
-               return nil, e
+       r, err := ignoringEINTR2(func() (int, error) {
+               return unix.Openat(dirfd, name, O_RDONLY|syscall.O_CLOEXEC|syscall.O_DIRECTORY|syscall.O_NOFOLLOW, 0)
+       })
+       if err != nil {
+               return nil, err
        }
 
        if !supportsCloseOnExec {