aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTobias Klauser <tklauser@distanz.ch>2024-11-15 20:41:33 +0100
committerTobias Klauser <tklauser@distanz.ch>2024-11-16 08:22:18 +0000
commitff2376dbe3b1a65fdb6855b1f831228d1e54b71f (patch)
tree58d1d5d9b1515eae0b05b53e657d6d3df604132a /src
parenteea5e13de4b57e7307a20192c8a27565f136484f (diff)
downloadgo-ff2376dbe3b1a65fdb6855b1f831228d1e54b71f.tar.xz
os: add and use ignoringEINTR2
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>
Diffstat (limited to 'src')
-rw-r--r--src/os/file_plan9.go4
-rw-r--r--src/os/file_posix.go10
-rw-r--r--src/os/file_unix.go19
-rw-r--r--src/os/getwd.go7
-rw-r--r--src/os/removeall_at.go19
5 files changed, 26 insertions, 33 deletions
diff --git a/src/os/file_plan9.go b/src/os/file_plan9.go
index ef277deccc..c123fe6961 100644
--- a/src/os/file_plan9.go
+++ b/src/os/file_plan9.go
@@ -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()
+}
diff --git a/src/os/file_posix.go b/src/os/file_posix.go
index 8ff0ada462..f0cdfdae5c 100644
--- a/src/os/file_posix.go
+++ b/src/os/file_posix.go
@@ -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
+ }
+ }
+}
diff --git a/src/os/file_unix.go b/src/os/file_unix.go
index 73069faa56..b5c0baf3ab 100644
--- a/src/os/file_unix.go
+++ b/src/os/file_unix.go
@@ -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
diff --git a/src/os/getwd.go b/src/os/getwd.go
index 82f0d944df..5ce948faf5 100644
--- a/src/os/getwd.go
+++ b/src/os/getwd.go
@@ -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
diff --git a/src/os/removeall_at.go b/src/os/removeall_at.go
index cc254e0043..f52f6213f5 100644
--- a/src/os/removeall_at.go
+++ b/src/os/removeall_at.go
@@ -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 {