init
This commit is contained in:
76 files changed
+22739
No files matched your search
@@ -0,0 +1,49 @@
|
||||
//go:build darwin || linux
|
||||
|
||||
package lockfile
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
type Lock struct {
|
||||
file *os.File
|
||||
}
|
||||
|
||||
func Acquire(path string) (*Lock, error) {
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0o600)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := unix.Flock(int(file.Fd()), unix.LOCK_EX|unix.LOCK_NB); err != nil {
|
||||
_ = file.Close()
|
||||
if err == unix.EWOULDBLOCK {
|
||||
return nil, fmt.Errorf("DomainOps is already running for this data directory")
|
||||
}
|
||||
return nil, fmt.Errorf("lock DomainOps state: %w", err)
|
||||
}
|
||||
_ = file.Truncate(0)
|
||||
_, _ = fmt.Fprintf(file, "%d\n", os.Getpid())
|
||||
_ = file.Sync()
|
||||
return &Lock{file: file}, nil
|
||||
}
|
||||
|
||||
func (l *Lock) Close() error {
|
||||
if l == nil || l.file == nil {
|
||||
return nil
|
||||
}
|
||||
unlockErr := unix.Flock(int(l.file.Fd()), unix.LOCK_UN)
|
||||
closeErr := l.file.Close()
|
||||
l.file = nil
|
||||
if unlockErr != nil {
|
||||
return unlockErr
|
||||
}
|
||||
return closeErr
|
||||
}
|
||||
Reference in new issue
Block a user