Repository navigation
Expand file tree
/
Copy pathprofiles.go
More file actions
150 lines (131 loc) · 4.03 KB
/
Copy pathprofiles.go
File metadata and controls
150 lines (131 loc) · 4.03 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
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
package devbrowser
import (
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
// ProfilePrefix is the name prefix the vendored allocator gives every throwaway
// Chrome profile directory it creates.
const ProfilePrefix = "chromedp-runner"
// singletonLockName is the symlink Chrome maintains in a profile root while it
// is running. Its target is "<hostname>-<pid>".
const singletonLockName = "SingletonLock"
// DefaultStaleGrace is how long a profile directory with no lock is left alone,
// so a browser that is still starting up is never swept from under itself.
const DefaultStaleGrace = 5 * time.Minute
// ProfileCleaner removes Chrome profile directories left behind by browsers
// whose owning process is gone. Zero value is not usable; build it with
// NewProfileCleaner and override the fields in tests.
type ProfileCleaner struct {
Root string // where profiles live; default os.TempDir()
Grace time.Duration // default DefaultStaleGrace
Now func() time.Time // default time.Now
ProcessAlive func(pid int) bool // default processAlive
}
// NewProfileCleaner initializes a new ProfileCleaner with sensible defaults.
func NewProfileCleaner() *ProfileCleaner {
return &ProfileCleaner{
Root: os.TempDir(),
Grace: DefaultStaleGrace,
Now: time.Now,
ProcessAlive: processAlive,
}
}
// dirSize calculates total bytes of files inside dir. Errors contribute 0.
func dirSize(dir string) int64 {
var size int64
_ = filepath.Walk(dir, func(_ string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if !info.IsDir() {
size += info.Size()
}
return nil
})
return size
}
// CleanStale removes every stale profile directory under Root and reports how
// many went and how many bytes that freed. A directory is stale when it has no
// SingletonLock and is older than Grace, or when the pid in its SingletonLock
// is not a live process. Errors on individual directories are skipped, not
// returned: a sweep must never fail a caller's startup.
func (c *ProfileCleaner) CleanStale() (removed int, freed int64, err error) {
root := c.Root
if root == "" {
root = os.TempDir()
}
entries, readErr := os.ReadDir(root)
if readErr != nil {
if os.IsNotExist(readErr) {
return 0, 0, nil
}
return 0, 0, readErr
}
now := c.Now
if now == nil {
now = time.Now
}
isAlive := c.ProcessAlive
if isAlive == nil {
isAlive = processAlive
}
// Grace is defended like every other field: a zero value here would make
// each lock-less directory instantly stale and sweep a browser that is
// still starting up, which is the one thing the grace window exists for.
grace := c.Grace
if grace <= 0 {
grace = DefaultStaleGrace
}
for _, entry := range entries {
if !entry.IsDir() || !strings.HasPrefix(entry.Name(), ProfilePrefix) {
continue
}
dirPath := filepath.Join(root, entry.Name())
lockPath := filepath.Join(dirPath, singletonLockName)
pid, hasLock, readable := lockOwner(lockPath)
stale := false
if hasLock {
if !readable {
continue // cannot tell -> treat the directory as in use
}
stale = !isAlive(pid)
} else {
// Readlink failed (no lock or error). Check modtime against Grace.
info, statErr := entry.Info()
if statErr != nil {
continue
}
if now().Sub(info.ModTime()) > grace {
stale = true
}
}
if stale {
sz := dirSize(dirPath)
if removeErr := os.RemoveAll(dirPath); removeErr == nil {
removed++
freed += sz
}
}
}
return removed, freed, nil
}
// lockOwner reads a Chrome SingletonLock symlink, whose target is "<hostname>-<pid>". hasLock is
// false when there is no lock; readable is false when its target cannot be parsed.
func lockOwner(lockPath string) (pid int, hasLock, readable bool) {
target, err := os.Readlink(lockPath)
if err != nil {
return 0, false, false
}
idx := strings.LastIndex(target, "-")
if idx == -1 || idx == len(target)-1 {
return 0, true, false
}
pid, err = strconv.Atoi(target[idx+1:])
if err != nil {
return 0, true, false
}
return pid, true, true
}