mirror of
https://github.com/golang/go
synced 2024-11-21 22:24:40 -07:00
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>
This commit is contained in:
parent
eea5e13de4
commit
ff2376dbe3
@ -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()
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user