aboutsummaryrefslogtreecommitdiff
path: root/internal/syscallcompat/eintr.go
blob: 122af9bf7c93ea098cfbd885b4038b48948d83fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package syscallcompat

import (
	"syscall"
)

// retryEINTR executes operation `op` and retries if it gets EINTR.
//
// Like ignoringEINTR() in the Go stdlib:
// https://github.com/golang/go/blob/d2a80f3fb5b44450e0b304ac5a718f99c053d82a/src/os/file_posix.go#L243
//
// This is needed because CIFS throws lots of EINTR errors:
// https://github.com/rfjakob/gocryptfs/issues/483
func retryEINTR(op func() error) error {
	for {
		err := op()
		if err != syscall.EINTR {
			return err
		}
	}
}

// retryEINTR2 is like retryEINTR but for functions that return an (int, error)
// pair like syscall.Create().
func retryEINTR2(op func() (int, error)) (int, error) {
	for {
		ret, err := op()
		if err != syscall.EINTR {
			return ret, err
		}
	}
}

// Open wraps syscall.Open.
// Retries on EINTR.
func Open(path string, mode int, perm uint32) (fd int, err error) {
	fd, err = retryEINTR2(func() (int, error) {
		return syscall.Open(path, mode, perm)
	})
	return fd, err
}

// Close wraps syscall.Close.
// Retries on EINTR.
func Close(fd int) (err error) {
	err = retryEINTR(func() error { return syscall.Close(fd) })
	return err
}