Skip to content
quarry Artifact [8dd7922a9f]
Login

Artifact [8dd7922a9f]

Artifact 8dd7922a9f38fa892d02afa4336f4739331033eb58a18c378c84dfdf558203f5:


//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
}