/
lockfile_fcntl.go
137 lines (112 loc) · 2.96 KB
/
lockfile_fcntl.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// +build linux darwin freebsd openbsd netbsd dragonfly
// +build go1.3
package lockfile
import (
"fmt"
"os"
"syscall"
)
type FcntlLockfile struct {
Path string
file *os.File
maintainFile bool
ft *syscall.Flock_t
}
func NewFcntlLockfile(path string) *FcntlLockfile {
return &FcntlLockfile{Path: path, maintainFile: true}
}
func NewFcntlLockfileFromFile(file *os.File) *FcntlLockfile {
return &FcntlLockfile{file: file, maintainFile: false}
}
func (l *FcntlLockfile) LockRead() error {
return l.lock(false, false, 0, os.SEEK_SET, 0)
}
func (l *FcntlLockfile) LockWrite() error {
return l.lock(true, false, 0, os.SEEK_SET, 0)
}
func (l *FcntlLockfile) LockReadB() error {
return l.lock(false, true, 0, os.SEEK_SET, 0)
}
func (l *FcntlLockfile) LockWriteB() error {
return l.lock(true, true, 0, os.SEEK_SET, 0)
}
func (l *FcntlLockfile) Unlock() {
l.unlock(0, os.SEEK_SET, 0)
}
func (l *FcntlLockfile) LockReadRange(offset int64, whence int, len int64) error {
return l.lock(false, false, offset, whence, len)
}
func (l *FcntlLockfile) LockWriteRange(offset int64, whence int, len int64) error {
return l.lock(true, false, offset, whence, len)
}
func (l *FcntlLockfile) LockReadRangeB(offset int64, whence int, len int64) error {
return l.lock(false, true, offset, whence, len)
}
func (l *FcntlLockfile) LockWriteRangeB(offset int64, whence int, len int64) error {
return l.lock(true, true, offset, whence, len)
}
func (l *FcntlLockfile) UnlockRange(offset int64, whence int, len int64) {
l.unlock(offset, whence, len)
}
// Owner will return the pid of the process that owns an fcntl based
// lock on the file. If the file is not locked it will return -1. If
// a lock is owned by the current process, it will return -1.
func (l *FcntlLockfile) Owner() int {
ft := &syscall.Flock_t{}
*ft = *l.ft
err := syscall.FcntlFlock(l.file.Fd(), syscall.F_GETLK, ft)
if err != nil {
fmt.Println(err)
return -1
}
if ft.Type == syscall.F_UNLCK {
fmt.Println(err)
return -1
}
return int(ft.Pid)
}
func (l *FcntlLockfile) lock(exclusive, blocking bool, offset int64, whence int, len int64) error {
if l.file == nil {
f, err := os.OpenFile(l.Path, os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
return err
}
l.file = f
}
ft := &syscall.Flock_t{
Whence: int16(whence),
Start: offset,
Len: len,
Pid: int32(os.Getpid()),
}
l.ft = ft
if exclusive {
ft.Type = syscall.F_WRLCK
} else {
ft.Type = syscall.F_RDLCK
}
var flags int
if blocking {
flags = syscall.F_SETLKW
} else {
flags = syscall.F_SETLK
}
err := syscall.FcntlFlock(l.file.Fd(), flags, l.ft)
if err != nil {
if l.maintainFile {
l.file.Close()
}
return ErrFailedToLock
}
return nil
}
func (l *FcntlLockfile) unlock(offset int64, whence int, len int64) {
l.ft.Len = len
l.ft.Start = offset
l.ft.Whence = int16(whence)
l.ft.Type = syscall.F_UNLCK
syscall.FcntlFlock(l.file.Fd(), syscall.F_SETLK, l.ft)
if l.maintainFile {
l.file.Close()
}
}