-
Notifications
You must be signed in to change notification settings - Fork 2
/
flock.go
43 lines (37 loc) · 856 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
37
38
39
40
41
42
43
package bitcask
import (
"os"
"path/filepath"
"syscall"
)
type FileLock struct {
fd *os.File
}
const lockFileName = "bitcask.lock"
func AcquireFileLock(path string, readOnly bool) (*FileLock, error) {
var flag = os.O_RDWR
if readOnly {
flag = os.O_RDONLY
}
lockFile := filepath.Join(path, lockFileName)
file, err := os.OpenFile(lockFile, flag|os.O_EXCL|os.O_CREATE, 0644)
if err != nil {
return nil, err
}
var how = syscall.LOCK_EX | syscall.LOCK_NB
if readOnly {
how = syscall.LOCK_SH | syscall.LOCK_NB
}
if err := syscall.Flock(int(file.Fd()), how); err != nil {
return nil, err
}
return &FileLock{fd: file}, nil
}
func (fl *FileLock) Release() error {
flag := syscall.LOCK_UN | syscall.LOCK_NB
if err := syscall.Flock(int(fl.fd.Fd()), flag); err != nil {
return err
}
fl.fd.Close()
return os.Remove(fl.fd.Name())
}