-
Notifications
You must be signed in to change notification settings - Fork 0
/
flock.go
37 lines (33 loc) · 1.09 KB
/
flock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package barrel
import (
"fmt"
"os"
"golang.org/x/sys/unix"
)
// createFlockFile creates a file lock for the database directory.
func createFlockFile(flockFile string) (*os.File, error) {
flockF, err := os.Create(flockFile)
if err != nil {
return nil, fmt.Errorf("cannot create lock file %q: %w", flockFile, err)
}
if err := unix.Flock(int(flockF.Fd()), unix.LOCK_EX|unix.LOCK_NB); err != nil {
return nil, fmt.Errorf("cannot acquire lock on file %q: %w", flockFile, err)
}
return flockF, nil
}
// destroyFlockFile removes a file lock for the database directory.
func destroyFlockFile(flockF *os.File) error {
// Unlock the file.
if err := unix.Flock(int(flockF.Fd()), unix.LOCK_UN); err != nil {
return fmt.Errorf("cannot unlock lock on file %q: %w", flockF.Name(), err)
}
// Close any open fd.
if err := flockF.Close(); err != nil {
return fmt.Errorf("cannot close fd on file %q: %w", flockF.Name(), err)
}
// Remove the lock file from the filesystem.
if err := os.Remove(flockF.Name()); err != nil {
return fmt.Errorf("cannot remove file %q: %w", flockF.Name(), err)
}
return nil
}