Repository navigation
Expand file tree
/
Copy pathtmp_cleaner.go
More file actions
86 lines (74 loc) · 2 KB
/
Copy pathtmp_cleaner.go
File metadata and controls
86 lines (74 loc) · 2 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
package main
import (
"os"
"path"
"strings"
"syscall"
"time"
log "github.com/sirupsen/logrus"
)
type tmpCleaner struct {
ticker *time.Ticker
exit chan interface{}
}
func (r *tmpCleaner) schedule(duration time.Duration) {
if duration.Seconds() <= 0 {
log.Warn("tmpcleaner/schedule: No duration provided, not scheduling")
return
}
log.Debugf("tmpcleaner/schedule: Scheduling tmpcleaner to run every %s", duration.String())
r.ticker = time.NewTicker(duration)
for {
select {
case <-r.ticker.C:
r.cleanTmp()
case <-r.exit:
return
}
}
}
func (r *tmpCleaner) cleanTmp() {
minTime := time.Now().Add(time.Minute * -15)
cleanFolder(os.TempDir(), minTime)
}
func cleanFolder(folder string, minTime time.Time) {
// Remove temp files that have not been accessed in the last 15 minutes
files, err := os.ReadDir(folder)
if err != nil {
log.Errorf("tmpcleaner/cleanFolder: Error reading from %s. %s", folder, err)
return
}
for _, f := range files {
if strings.Contains(f.Name(), "gpg2") {
continue // Ignore gpg2 related files
}
fullPath := path.Join(folder, f.Name())
if f.IsDir() {
cleanFolder(fullPath, minTime)
os.Remove(fullPath) // if folder is not empty this will fail silently, intentional
} else {
fs, err := f.Info()
if err != nil {
log.Warnf("tmpcleaner/cleanFolder: Error when retrieving stats for %s . %s", fullPath, err)
}
// This will not work on windows, where there is no concept of atime
stat := fs.Sys().(*syscall.Stat_t)
atime := time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec))
if atime.Compare(minTime) < 0 {
err := os.Remove(fullPath)
size := fs.Size()
if err != nil {
log.Errorf("tmpcleaner/cleanFolder: Failed to remove %s . %s", fullPath, err)
} else {
log.Debugf("tmpcleaner/cleanFolder: Removed %s with %d bytes.", fullPath, size)
}
}
}
}
}
func newTmpCleaner(ag *autographer, duration time.Duration) *tmpCleaner {
r := new(tmpCleaner)
r.exit = ag.exit
go r.schedule(duration)
return r
}