Skip to content
quarry Artifact [4567a66f60]
Login

Artifact [4567a66f60]

Artifact 4567a66f603e16af3950ffefc2d1a501cc09caa996a0317e68a328dd84255b71:


//go:build windows

package pickupclient

import (
	"os"

	"golang.org/x/sys/windows"
)

// lockFile takes an exclusive lock on path, made if need be, without
// waiting. The system drops it when the process ends. 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
	}
	ol := new(windows.Overlapped)
	err = windows.LockFileEx(windows.Handle(f.Fd()), windows.LOCKFILE_EXCLUSIVE_LOCK|windows.LOCKFILE_FAIL_IMMEDIATELY, 0, 1, 0, ol)
	if err != nil {
		f.Close()
		if err == windows.ERROR_LOCK_VIOLATION {
			return nil, false, nil
		}
		return nil, false, err
	}
	return func() { f.Close() }, true, nil
}