26 lines
512 B
Go
26 lines
512 B
Go
//go:build unix
|
|
|
|
package cachefile
|
|
|
|
import (
|
|
"os"
|
|
"syscall"
|
|
)
|
|
|
|
// lock takes path as a lock file, waiting while another lazysecrets has
|
|
// it, and returns how to let go.
|
|
func lock(path string) (unlock func(), err error) {
|
|
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0o600)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX); err != nil {
|
|
f.Close()
|
|
return nil, err
|
|
}
|
|
return func() {
|
|
_ = syscall.Flock(int(f.Fd()), syscall.LOCK_UN)
|
|
f.Close()
|
|
}, nil
|
|
}
|