Download this file
| 1 | //go:build unix |
| 2 | |
| 3 | package team |
| 4 | |
| 5 | import ( |
| 6 | "errors" |
| 7 | "os" |
| 8 | |
| 9 | "golang.org/x/sys/unix" |
| 10 | ) |
| 11 | |
| 12 | // tryStoreLock takes a non-blocking exclusive flock on f. It reports false |
| 13 | // with a nil error when another holder owns the lock. |
| 14 | func tryStoreLock(f *os.File) (bool, error) { |
| 15 | err := unix.Flock(int(f.Fd()), unix.LOCK_EX|unix.LOCK_NB) |
| 16 | if err == nil { |
| 17 | return true, nil |
| 18 | } |
| 19 | if errors.Is(err, unix.EWOULDBLOCK) || errors.Is(err, unix.EAGAIN) || errors.Is(err, unix.EINTR) { |
| 20 | return false, nil |
| 21 | } |
| 22 | return false, err |
| 23 | } |
| 24 | |
| 25 | // unlockStoreFile releases the flock; the caller closes the descriptor next. |
| 26 | func unlockStoreFile(f *os.File) { |
| 27 | _ = unix.Flock(int(f.Fd()), unix.LOCK_UN) |
| 28 | } |
| 29 | |