Files
beads/internal/lockfile/lock_windows.go
Steve Yegge ba1b856acb Standardize daemon detection with tryDaemonLock probe (bd-wgu4)
- Extract lock checking to internal/lockfile package
- Add lock probe in RPC client before connection attempts
- Update daemon discovery to use lock probe
- Eliminates unnecessary connection attempts when socket missing

Closes bd-wgu4

Amp-Thread-ID: https://ampcode.com/threads/T-3b863f21-3af4-49d3-9214-477d904b80fe
Co-authored-by: Amp <amp@ampcode.com>
2025-11-07 21:02:38 -08:00

39 lines
928 B
Go

//go:build windows
package lockfile
import (
"errors"
"os"
"syscall"
"golang.org/x/sys/windows"
)
var errDaemonLocked = errors.New("daemon lock already held by another process")
// flockExclusive acquires an exclusive non-blocking lock on the file using LockFileEx
func flockExclusive(f *os.File) error {
// LOCKFILE_EXCLUSIVE_LOCK (2) | LOCKFILE_FAIL_IMMEDIATELY (1) = 3
const flags = windows.LOCKFILE_EXCLUSIVE_LOCK | windows.LOCKFILE_FAIL_IMMEDIATELY
// Create overlapped structure for the entire file
ol := &windows.Overlapped{}
// Lock entire file (0xFFFFFFFF, 0xFFFFFFFF = maximum range)
err := windows.LockFileEx(
windows.Handle(f.Fd()),
flags,
0, // reserved
0xFFFFFFFF, // number of bytes to lock (low)
0xFFFFFFFF, // number of bytes to lock (high)
ol,
)
if err == windows.ERROR_LOCK_VIOLATION || err == syscall.EWOULDBLOCK {
return errDaemonLocked
}
return err
}