-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlock.go
More file actions
46 lines (40 loc) · 1012 Bytes
/
lock.go
File metadata and controls
46 lines (40 loc) · 1012 Bytes
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
//go:build unix
package main
import (
"fmt"
"hash/fnv"
"os"
"path/filepath"
"syscall"
)
// acquireLock takes a non-blocking exclusive flock on a per-root lock file.
// Returns the open file (caller must defer releaseLock) or an error if locked.
func acquireLock(root string) (*os.File, error) {
dir, err := os.UserCacheDir()
if err != nil {
// Fall back to /tmp if no cache dir.
dir = os.TempDir()
}
lockDir := filepath.Join(dir, "fastdedup")
os.MkdirAll(lockDir, 0755)
h := fnv.New64a()
h.Write([]byte(root))
lockPath := filepath.Join(lockDir, fmt.Sprintf("%016x.lock", h.Sum64()))
f, err := os.OpenFile(lockPath, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
return nil, err
}
if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB); err != nil {
f.Close()
return nil, err
}
return f, nil
}
// releaseLock releases the flock and closes the file.
func releaseLock(f *os.File) {
if f == nil {
return
}
syscall.Flock(int(f.Fd()), syscall.LOCK_UN)
f.Close()
}