mirror of
https://github.com/github/gh-stack.git
synced 2026-09-14 20:26:28 +08:00
38 lines
792 B
Go
38 lines
792 B
Go
//go:build windows
|
|
|
|
package stack
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
|
|
"golang.org/x/sys/windows"
|
|
)
|
|
|
|
// tryLockFile attempts a non-blocking exclusive lock.
|
|
// Returns nil on success, or an error if the lock is held by another process.
|
|
func tryLockFile(f *os.File) error {
|
|
ol := new(windows.Overlapped)
|
|
return windows.LockFileEx(
|
|
windows.Handle(f.Fd()),
|
|
windows.LOCKFILE_EXCLUSIVE_LOCK|windows.LOCKFILE_FAIL_IMMEDIATELY,
|
|
0, // reserved
|
|
1, // lock 1 byte
|
|
0, // high word
|
|
ol,
|
|
)
|
|
}
|
|
|
|
// isLockBusy reports whether err indicates the lock is held by another process.
|
|
func isLockBusy(err error) bool {
|
|
return errors.Is(err, windows.ERROR_LOCK_VIOLATION)
|
|
}
|
|
|
|
func unlockFile(f *os.File) {
|
|
ol := new(windows.Overlapped)
|
|
_ = windows.UnlockFileEx(
|
|
windows.Handle(f.Fd()),
|
|
0, 1, 0, ol,
|
|
)
|
|
}
|