-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlock.go
154 lines (151 loc) · 3.81 KB
/
lock.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package service
import (
"syscall"
)
func lockFile(fd uintptr) error { return syscall.Flock(int(fd), syscall.LOCK_EX|syscall.LOCK_NB) }
func unlockFile(fd uintptr) error { return syscall.Flock(int(fd), syscall.LOCK_UN) }
//type Locker interface {
// LockRead() error
// LockWrite() error
// WaitAndLockRead() error
// WaitAndLockWrite() error
// Unlock()
//}
//
//type Lockfile struct {
// Path string
// file *os.File
// maintainFile bool
// ft *syscall.Flock_t
//}
//
//func NewLockfile(path string) *Lockfile {
// return &Lockfile{Path: path, maintainFile: true}
//}
//
//func NewLockfileFromFile(file *os.File) *Lockfile {
// return &Lockfile{file: file, maintainFile: false}
//}
//
//func (self *Lockfile) LockRead() error {
// return self.lock(false, false, 0, os.SEEK_SET, 0)
//}
//
//func (self *Lockfile) LockWrite() error {
// return self.lock(true, false, 0, os.SEEK_SET, 0)
//}
//
//func (self *Lockfile) WaitAndLockRead() error {
// return self.lock(false, true, 0, os.SEEK_SET, 0)
//}
//
//func (self *Lockfile) WaitAndLockWrite() error {
// return self.lock(true, true, 0, os.SEEK_SET, 0)
//}
//
//func (self *Lockfile) Unlock() {
// self.unlock(0, os.SEEK_SET, 0)
//}
//
//func (self *Lockfile) LockReadRange(offset int64, to int, len int64) error {
// return self.lock(false, false, offset, to, len)
//}
//
//func (self *Lockfile) LockWriteRange(offset int64, to int, len int64) error {
// return self.lock(true, false, offset, to, len)
//}
//
//func (self *Lockfile) WaitAndLockReadRange(offset int64, to int, len int64) error {
// return self.lock(false, true, offset, to, len)
//}
//
//func (self *Lockfile) WaitAndLockWriteRange(offset int64, to int, len int64) error {
// return self.lock(true, true, offset, to, len)
//}
//
//func (self *Lockfile) UnlockRange(offset int64, to int, len int64) {
// self.unlock(offset, to, 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.
//func (self *Lockfile) Owner() int {
// ft := &syscall.Flock_t{}
// *ft = *self.ft
//
// if err := syscall.FcntlFlock(self.file.Fd(), syscall.F_GETLK, ft); err != nil {
// fmt.Println(err)
// return -1
// if ft.Type == syscall.F_UNLCK {
// fmt.Println(err)
// return -1
// }
// }
// return int(ft.Pid)
//}
//
//func (self *Lockfile) lock(exclusive, blocking bool, offset int64, to int, len int64) error {
// if self.file == nil {
// f, err := os.OpenFile(self.Path, os.O_CREATE|os.O_RDWR, 0666)
// if err != nil {
// return err
// }
// self.file = f
// }
//
// ft := &syscall.Flock_t{
// Whence: int16(to),
// Start: offset,
// Len: len,
// Pid: int32(os.Getpid()),
// }
// self.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
// }
//
// if err := syscall.FcntlFlock(self.file.Fd(), flags, self.ft); err != nil {
// if self.maintainFile {
// self.file.Close()
// }
// return fmt.Errorf("failed to obtain lock")
// }
// return nil
//}
//
//func (self *Lockfile) unlock(offset int64, to int, len int64) {
// self.ft.Len = len
// self.ft.Start = offset
// self.ft.Whence = int16(to)
// self.ft.Type = syscall.F_UNLCK
// syscall.FcntlFlock(self.file.Fd(), syscall.F_SETLK, self.ft)
// if self.maintainFile {
// self.file.Close()
// }
//}
//
//type RangeLocker interface {
// Locker
// LockReadRange(int64, int, int64) error
// LockWriteRange(int64, int, int64) error
// WaitAndLockReadRange(int64, int, int64) error
// WaitAndLockWriteRange(int64, int, int64) error
// UnlockRange(int64, int, int64)
//}
//
//func NewLockfile(path string) RangeLocker {
// return NewLockfile(path)
//}
//
//func NewLockfileFromFile(file *os.File) RangeLocker {
// return NewLockfileFromFile(file)
//}