package gitcache import ( "errors" "os" "syscall" ) const errorSharingViolation syscall.Errno = 32 // lockFile opens path with no sharing, so no other handle can open it while // this one is open. func lockFile(path string) (*os.File, error) { name, err := syscall.UTF16PtrFromString(path) if err != nil { return nil, err } h, err := syscall.CreateFile(name, syscall.GENERIC_READ|syscall.GENERIC_WRITE, 0, nil, syscall.OPEN_ALWAYS, syscall.FILE_ATTRIBUTE_NORMAL, 0) if errors.Is(err, errorSharingViolation) { return nil, errLocked } if err != nil { return nil, err } return os.NewFile(uintptr(h), path), nil }