Files
rvbox/test/harness/lock_unix.go
T

25 lines
509 B
Go

//go:build !windows
package main
import (
"fmt"
"os"
"syscall"
)
func acquireLock(path string) (func(), error) {
file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0o600)
if err != nil {
return nil, err
}
if err := syscall.Flock(int(file.Fd()), syscall.LOCK_EX|syscall.LOCK_NB); err != nil {
_ = file.Close()
return nil, fmt.Errorf("another test harness process holds %s: %w", path, err)
}
return func() {
_ = syscall.Flock(int(file.Fd()), syscall.LOCK_UN)
_ = file.Close()
}, nil
}