-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsession_sync_filelock.go
More file actions
57 lines (52 loc) · 1.39 KB
/
Copy pathsession_sync_filelock.go
File metadata and controls
57 lines (52 loc) · 1.39 KB
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
package main
import (
"strings"
)
// isRolloutFileBusyError checks if an error indicates the file is locked by another process.
func isRolloutFileBusyError(err error) bool {
if err == nil {
return false
}
msg := strings.ToLower(err.Error())
return strings.Contains(msg, "ebusy") ||
strings.Contains(msg, "resource busy or locked") ||
strings.Contains(msg, "being used by another process") ||
strings.Contains(msg, "currently in use") ||
strings.Contains(msg, "eperm")
}
// findLockedFiles returns paths that are currently locked (unable to acquire exclusive access).
func findLockedFiles(paths []string) []string {
if len(paths) == 0 {
return nil
}
locked := make([]string, 0)
for _, p := range paths {
f, ok, _ := tryAcquireExclusive(p)
if !ok {
locked = append(locked, p)
}
if f != nil {
releaseExclusiveLock(f)
}
}
return locked
}
// splitLockedChanges separates changes into writable and locked slices.
func splitLockedChanges(changes []syncRolloutChange) (writable []syncRolloutChange, locked []syncRolloutChange) {
paths := make([]string, len(changes))
for i, c := range changes {
paths[i] = c.Path
}
lockedSet := make(map[string]bool, len(paths))
for _, p := range findLockedFiles(paths) {
lockedSet[p] = true
}
for _, c := range changes {
if lockedSet[c.Path] {
locked = append(locked, c)
} else {
writable = append(writable, c)
}
}
return
}