-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFLock.go
36 lines (30 loc) · 948 Bytes
/
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
package bitcaspy
import (
"fmt"
"os"
"golang.org/x/sys/unix"
)
func getFLock(flockfile string) (*os.File, error) {
flockF, err := os.Create(flockfile)
if err != nil {
return nil, fmt.Errorf("cannot create lock file %q: %w", flockF, 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", flockF, err)
}
return flockF, nil
}
func destroyFLock(flockF *os.File) error {
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
}