Download this file
| 1 | //go:build windows |
| 2 | |
| 3 | package team |
| 4 | |
| 5 | import ( |
| 6 | "errors" |
| 7 | "os" |
| 8 | |
| 9 | "golang.org/x/sys/windows" |
| 10 | ) |
| 11 | |
| 12 | // tryStoreLock takes a non-blocking exclusive lock on byte 0 of f. It reports |
| 13 | // false with a nil error when another holder owns the lock. |
| 14 | func tryStoreLock(f *os.File) (bool, error) { |
| 15 | ol := new(windows.Overlapped) |
| 16 | err := windows.LockFileEx(windows.Handle(f.Fd()), |
| 17 | windows.LOCKFILE_EXCLUSIVE_LOCK|windows.LOCKFILE_FAIL_IMMEDIATELY, 0, 1, 0, ol) |
| 18 | if err == nil { |
| 19 | return true, nil |
| 20 | } |
| 21 | if errors.Is(err, windows.ERROR_LOCK_VIOLATION) { |
| 22 | return false, nil |
| 23 | } |
| 24 | return false, err |
| 25 | } |
| 26 | |
| 27 | // unlockStoreFile releases the byte-range lock; the caller closes the handle next. |
| 28 | func unlockStoreFile(f *os.File) { |
| 29 | ol := new(windows.Overlapped) |
| 30 | _ = windows.UnlockFileEx(windows.Handle(f.Fd()), 0, 1, 0, ol) |
| 31 | } |
| 32 | |