Skip to content

Commit eccd827

Browse files
authored
docs: add more documentation about the origin of the source code (#82)
1 parent 9236f43 commit eccd827

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

flock_unix_variants.go renamed to flock_unix_fcntl.go

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
// To avoid unlocking files prematurely when the same file is opened through different descriptors,
1313
// we allow only one read-lock at a time.
1414
//
15-
// This code is adapted from the Go package:
16-
// cmd/go/internal/lockedfile/internal/filelock
15+
// This code is adapted from the Go package (go.12):
16+
// https://github.com/golang/go/blob/release-branch.go1.12/src/cmd/go/internal/lockedfile/internal/filelock/filelock_fcntl.go
1717

1818
//go:build aix || (solaris && !illumos)
1919

@@ -29,27 +29,45 @@ import (
2929
"golang.org/x/sys/unix"
3030
)
3131

32+
// https://github.com/golang/go/blob/09aeb6e33ab426eff4676a3baf694d5a3019e9fc/src/cmd/go/internal/lockedfile/internal/filelock/filelock_fcntl.go#L28
3233
type lockType int16
3334

35+
// String returns the name of the function corresponding to lt
36+
// (Lock, RLock, or Unlock).
37+
// https://github.com/golang/go/blob/09aeb6e33ab426eff4676a3baf694d5a3019e9fc/src/cmd/go/internal/lockedfile/internal/filelock/filelock.go#L67
38+
func (lt lockType) String() string {
39+
switch lt {
40+
case readLock:
41+
return "RLock"
42+
case writeLock:
43+
return "Lock"
44+
default:
45+
return "Unlock"
46+
}
47+
}
48+
49+
// https://github.com/golang/go/blob/09aeb6e33ab426eff4676a3baf694d5a3019e9fc/src/cmd/go/internal/lockedfile/internal/filelock/filelock_fcntl.go#L30-L33
3450
const (
3551
readLock lockType = unix.F_RDLCK
3652
writeLock lockType = unix.F_WRLCK
3753
)
3854

39-
type cmdType int
40-
41-
const (
42-
tryLock cmdType = unix.F_SETLK
43-
waitLock cmdType = unix.F_SETLKW
44-
)
45-
55+
// https://github.com/golang/go/blob/09aeb6e33ab426eff4676a3baf694d5a3019e9fc/src/cmd/go/internal/lockedfile/internal/filelock/filelock_fcntl.go#L35
4656
type inode = uint64
4757

58+
// https://github.com/golang/go/blob/09aeb6e33ab426eff4676a3baf694d5a3019e9fc/src/cmd/go/internal/lockedfile/internal/filelock/filelock_fcntl.go#L37-L40
4859
type inodeLock struct {
4960
owner *Flock
5061
queue []<-chan *Flock
5162
}
5263

64+
type cmdType int
65+
66+
const (
67+
tryLock cmdType = unix.F_SETLK
68+
waitLock cmdType = unix.F_SETLKW
69+
)
70+
5371
var (
5472
mu sync.Mutex
5573
inodes = map[*Flock]inode{}
@@ -109,6 +127,7 @@ func (f *Flock) lock(locked *bool, flag lockType) error {
109127
return nil
110128
}
111129

130+
// https://github.com/golang/go/blob/09aeb6e33ab426eff4676a3baf694d5a3019e9fc/src/cmd/go/internal/lockedfile/internal/filelock/filelock_fcntl.go#L48
112131
func (f *Flock) doLock(cmd cmdType, lt lockType, blocking bool) (bool, error) {
113132
// POSIX locks apply per inode and process,
114133
// and the lock for an inode is released when *any* descriptor for that inode is closed.
@@ -119,14 +138,15 @@ func (f *Flock) doLock(cmd cmdType, lt lockType, blocking bool) (bool, error) {
119138
return false, err
120139
}
121140

122-
ino := inode(fi.Sys().(*syscall.Stat_t).Ino)
141+
ino := fi.Sys().(*syscall.Stat_t).Ino
123142

124143
mu.Lock()
125144

126145
if i, dup := inodes[f]; dup && i != ino {
127146
mu.Unlock()
128147

129148
return false, &os.PathError{
149+
Op: lt.String(),
130150
Path: f.Path(),
131151
Err: errors.New("inode for file changed since last Lock or RLock"),
132152
}
@@ -168,7 +188,7 @@ func (f *Flock) doLock(cmd cmdType, lt lockType, blocking bool) (bool, error) {
168188
if err != nil {
169189
f.doUnlock()
170190

171-
if cmd == tryLock && err == unix.EACCES {
191+
if cmd == tryLock && errors.Is(err, unix.EACCES) {
172192
return false, nil
173193
}
174194

@@ -197,6 +217,7 @@ func (f *Flock) Unlock() error {
197217
return nil
198218
}
199219

220+
// https://github.com/golang/go/blob/09aeb6e33ab426eff4676a3baf694d5a3019e9fc/src/cmd/go/internal/lockedfile/internal/filelock/filelock_fcntl.go#L163
200221
func (f *Flock) doUnlock() (err error) {
201222
var owner *Flock
202223

@@ -288,6 +309,7 @@ func (f *Flock) try(locked *bool, flag lockType) (bool, error) {
288309
}
289310

290311
// setlkw calls FcntlFlock with cmd for the entire file indicated by fd.
312+
// https://github.com/golang/go/blob/09aeb6e33ab426eff4676a3baf694d5a3019e9fc/src/cmd/go/internal/lockedfile/internal/filelock/filelock_fcntl.go#L198
291313
func setlkw(fd uintptr, cmd cmdType, lt lockType) error {
292314
for {
293315
err := unix.FcntlFlock(fd, int(cmd), &unix.Flock_t{
@@ -296,7 +318,7 @@ func setlkw(fd uintptr, cmd cmdType, lt lockType) error {
296318
Start: 0,
297319
Len: 0, // All bytes.
298320
})
299-
if err != unix.EINTR {
321+
if !errors.Is(err, unix.EINTR) {
300322
return err
301323
}
302324
}

0 commit comments

Comments
 (0)