mirror of
https://github.com/golang/go
synced 2024-11-21 21:04:41 -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 {
|
func ignoringEINTR(fn func() error) error {
|
||||||
return fn()
|
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) {
|
func readlink(name string) (string, error) {
|
||||||
for len := 128; ; len *= 2 {
|
for len := 128; ; len *= 2 {
|
||||||
b := make([]byte, len)
|
b := make([]byte, len)
|
||||||
var (
|
n, err := ignoringEINTR2(func() (int, error) {
|
||||||
n int
|
return fixCount(syscall.Readlink(name, b))
|
||||||
e error
|
})
|
||||||
)
|
|
||||||
for {
|
|
||||||
n, e = fixCount(syscall.Readlink(name, b))
|
|
||||||
if e != syscall.EINTR {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// buffer too small
|
// buffer too small
|
||||||
if (runtime.GOOS == "aix" || runtime.GOOS == "wasip1") && e == syscall.ERANGE {
|
if (runtime.GOOS == "aix" || runtime.GOOS == "wasip1") && err == syscall.ERANGE {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if e != nil {
|
if err != nil {
|
||||||
return "", &PathError{Op: "readlink", Path: name, Err: e}
|
return "", &PathError{Op: "readlink", Path: name, Err: err}
|
||||||
}
|
}
|
||||||
if n < len {
|
if n < len {
|
||||||
return string(b[0:n]), nil
|
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 the operating system provides a Getwd call, use it.
|
||||||
if syscall.ImplementsGetwd {
|
if syscall.ImplementsGetwd {
|
||||||
for {
|
dir, err = ignoringEINTR2(syscall.Getwd)
|
||||||
dir, err = syscall.Getwd()
|
|
||||||
if err != syscall.EINTR {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Linux returns ENAMETOOLONG if the result is too long.
|
// Linux returns ENAMETOOLONG if the result is too long.
|
||||||
// Some BSD systems appear to return EINVAL.
|
// Some BSD systems appear to return EINVAL.
|
||||||
// FreeBSD systems appear to use ENOMEM
|
// 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.
|
// we are going to (try to) remove the file.
|
||||||
// The contents of this file are not relevant for test caching.
|
// The contents of this file are not relevant for test caching.
|
||||||
func openDirAt(dirfd int, name string) (*File, error) {
|
func openDirAt(dirfd int, name string) (*File, error) {
|
||||||
var r int
|
r, err := ignoringEINTR2(func() (int, error) {
|
||||||
for {
|
return unix.Openat(dirfd, name, O_RDONLY|syscall.O_CLOEXEC|syscall.O_DIRECTORY|syscall.O_NOFOLLOW, 0)
|
||||||
var e error
|
})
|
||||||
r, e = unix.Openat(dirfd, name, O_RDONLY|syscall.O_CLOEXEC|syscall.O_DIRECTORY|syscall.O_NOFOLLOW, 0)
|
if err != nil {
|
||||||
if e == nil {
|
return nil, err
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
// See comment in openFileNolog.
|
|
||||||
if e == syscall.EINTR {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, e
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !supportsCloseOnExec {
|
if !supportsCloseOnExec {
|
||||||
|
Loading…
Reference in New Issue
Block a user