//go:build unix
package pickupclient
import (
"os"
"syscall"
)
// lockFile takes an exclusive lock on path, made if need be, without
// waiting. The kernel drops it when the process ends, so a killed run
// leaves nothing to clear. ok is false when another run holds it.
func lockFile(path string) (release func(), ok bool, err error) {
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0o600)
if err != nil {
return nil, false, err
}
if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB); err != nil {
f.Close()
if err == syscall.EWOULDBLOCK {
return nil, false, nil
}
return nil, false, err
}
return func() { f.Close() }, true, nil
}