This commit is contained in:
jdl
2024-11-11 06:36:55 +01:00
parent d0587cc585
commit c5419d662e
102 changed files with 4181 additions and 0 deletions

2
flock/README.md Normal file
View File

@@ -0,0 +1,2 @@
# flock

69
flock/flock.go Normal file
View File

@@ -0,0 +1,69 @@
// The flock package provides a file-system mediated locking mechanism on linux
// using the `flock` system call.
package flock
import (
"errors"
"os"
"syscall"
)
// Lock gets an exclusive lock on the file at the given path. If the file
// doesn't exist, it's created.
func Lock(path string) (*os.File, error) {
return lock(path, syscall.LOCK_EX)
}
// TryLock will return a nil file if the file is already locked.
func TryLock(path string) (*os.File, error) {
return lock(path, syscall.LOCK_EX|syscall.LOCK_NB)
}
func LockFile(f *os.File) error {
_, err := lockFile(f, syscall.LOCK_EX)
return err
}
// Returns true if the lock was successfully acquired.
func TryLockFile(f *os.File) (bool, error) {
return lockFile(f, syscall.LOCK_EX|syscall.LOCK_NB)
}
func lockFile(f *os.File, flags int) (bool, error) {
if err := flock(int(f.Fd()), flags); err != nil {
if flags&syscall.LOCK_NB != 0 && errors.Is(err, syscall.EAGAIN) {
return false, nil
}
return false, err
}
return true, nil
}
func flock(fd int, how int) error {
_, _, e1 := syscall.Syscall(syscall.SYS_FLOCK, uintptr(fd), uintptr(how), 0)
if e1 != 0 {
return syscall.Errno(e1)
}
return nil
}
func lock(path string, flags int) (*os.File, error) {
perm := os.O_CREATE | os.O_RDWR
f, err := os.OpenFile(path, perm, 0600)
if err != nil {
return nil, err
}
ok, err := lockFile(f, flags)
if err != nil || !ok {
f.Close()
f = nil
}
return f, err
}
// Unlock releases the lock acquired via the Lock function.
func Unlock(f *os.File) error {
return f.Close()
}

66
flock/flock_test.go Normal file
View File

@@ -0,0 +1,66 @@
package flock
import (
"testing"
"time"
)
func Test_Lock_basic(t *testing.T) {
ch := make(chan int, 1)
f, err := Lock("/tmp/fsutil-test-lock")
if err != nil {
t.Fatal(err)
}
go func() {
time.Sleep(time.Second)
ch <- 10
Unlock(f)
}()
select {
case x := <-ch:
t.Fatal(x)
default:
}
f2, _ := Lock("/tmp/fsutil-test-lock")
defer Unlock(f2)
select {
case i := <-ch:
if i != 10 {
t.Fatal(i)
}
default:
t.Fatal("No value available.")
}
}
func Test_Lock_badPath(t *testing.T) {
_, err := Lock("./dne/file.lock")
if err == nil {
t.Fatal(err)
}
}
func TestTryLock(t *testing.T) {
lockPath := "/tmp/fsutil-test-lock"
f, err := TryLock(lockPath)
if err != nil {
t.Fatalf("%#v", err)
t.Fatal(err)
}
f2, err := TryLock(lockPath)
if err != nil {
t.Fatal(err)
}
if f2 != nil {
t.Fatal(f2)
}
if err := Unlock(f); err != nil {
t.Fatal(err)
}
}

3
flock/go.mod Normal file
View File

@@ -0,0 +1,3 @@
module git.crumpington.com/lib/flock
go 1.23.0

0
flock/go.sum Normal file
View File