forked from cockroachdb/pebble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_lock_unix.go
70 lines (60 loc) · 1.54 KB
/
file_lock_unix.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
// Copyright 2014 The LevelDB-Go and Pebble Authors. All rights reserved. Use
// of this source code is governed by a BSD-style license that can be found in
// the LICENSE file.
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
// +build darwin dragonfly freebsd linux netbsd openbsd solaris
package vfs
import (
"io"
"os"
"sync"
"github.com/cockroachdb/errors"
"golang.org/x/sys/unix"
)
var lockedFiles struct {
mu struct {
sync.Mutex
files map[string]bool
}
}
// lockCloser hides all of an os.File's methods, except for Close.
type lockCloser struct {
name string
f *os.File
}
func (l lockCloser) Close() error {
lockedFiles.mu.Lock()
defer lockedFiles.mu.Unlock()
if !lockedFiles.mu.files[l.name] {
panic(errors.Errorf("lock file %q is not locked", l.name))
}
delete(lockedFiles.mu.files, l.name)
return l.f.Close()
}
func (defaultFS) Lock(name string) (io.Closer, error) {
lockedFiles.mu.Lock()
defer lockedFiles.mu.Unlock()
if lockedFiles.mu.files == nil {
lockedFiles.mu.files = map[string]bool{}
}
if lockedFiles.mu.files[name] {
return nil, errors.New("lock held by current process")
}
f, err := os.Create(name)
if err != nil {
return nil, err
}
spec := unix.Flock_t{
Type: unix.F_WRLCK,
Whence: io.SeekStart,
Start: 0,
Len: 0, // 0 means to lock the entire file.
Pid: int32(os.Getpid()),
}
if err := unix.FcntlFlock(f.Fd(), unix.F_SETLK, &spec); err != nil {
f.Close()
return nil, err
}
lockedFiles.mu.files[name] = true
return lockCloser{name, f}, nil
}