Add file locks to prevent multiple processes at the same time

This commit is contained in:
2025-10-04 17:24:20 +03:00
parent 53e08ce7f9
commit eeb9a01e31
2 changed files with 281 additions and 69 deletions
+43
View File
@@ -3,9 +3,52 @@ package bpmlib
import (
"fmt"
"math"
"os"
"path"
"syscall"
)
type BPMLock struct {
file *os.File
path string
}
func (lock *BPMLock) Unlock() error {
err := lock.file.Close()
if err != nil {
return err
}
err = os.Remove(lock.path)
if err != nil {
return err
}
return nil
}
func LockBPM(rootDir string) (*BPMLock, error) {
// Create parent directories if they don't already exist
err := os.MkdirAll(path.Join(rootDir, "/var/lib/bpm"), 0755)
if err != nil {
return nil, err
}
// Create file
f, err := os.Create(path.Join(rootDir, "var/lib/bpm/bpm.lock"))
if err != nil {
return nil, err
}
// Get exclusive file lock on file
err = syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
if err != nil {
return nil, err
}
return &BPMLock{f, path.Join(rootDir, "var/lib/bpm/bpm.lock")}, nil
}
func GetArch() string {
uname := syscall.Utsname{}
err := syscall.Uname(&uname)