forked from plexdrive/plexdrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean.go
90 lines (79 loc) · 2.24 KB
/
clean.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"io"
"os"
"path/filepath"
"time"
. "github.com/claudetech/loggo/default"
)
// CleanChunkDir check frequently the temporary directory and
// cleans old stuff
func CleanChunkDir(chunkDir string, clearInterval, chunkAge time.Duration, chunkSize, maxTempSize int64) {
if maxTempSize > 0 {
Log.Info("Using clear-by-size method for chunk cleaning")
clearBySize(chunkDir, clearInterval)
} else {
Log.Info("Using clear-by-interval method for chunk cleaning")
clearByInterval(chunkDir, clearInterval, chunkAge)
}
}
// clearBySize clears the chunk dir temporarily and deletes only the oldest files
func clearBySize(chunkDir string, clearInterval time.Duration) {
for _ = range time.Tick(clearInterval) {
deleteEmptyDirs(chunkDir)
}
}
// clearByInterval clears the chunk dir temporarily regardless of the size
func clearByInterval(chunkDir string, clearInterval, chunkAge time.Duration) {
for _ = range time.Tick(clearInterval) {
Log.Debugf("Cleaning chunk directory %v", chunkDir)
filepath.Walk(chunkDir, func(path string, f os.FileInfo, err error) error {
if path == chunkDir {
return nil
}
now := time.Now()
if !f.IsDir() {
if now.Sub(f.ModTime()) > chunkAge {
if err := os.Remove(path); nil != err {
Log.Warningf("Could not delete temp file %v", path)
}
}
} else {
if empty, err := isEmptyDir(path); nil == err && empty {
if err := os.RemoveAll(path); nil != err {
Log.Warningf("Could not delete temp dir %v", path)
}
}
}
return err
})
}
}
// deleteEmptyDirs deletes empty directories
func deleteEmptyDirs(dir string) error {
err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
if f.IsDir() && path != dir {
if empty, err := isEmptyDir(path); nil == err && empty {
Log.Debugf("Cleaning empty directory %v", path)
if err := os.RemoveAll(path); nil != err {
Log.Warningf("Could not delete temp dir %v", path)
}
}
}
return err
})
return err
}
// isEmptyDir checks if the directory is empty
func isEmptyDir(name string) (bool, error) {
f, err := os.Open(name)
if err != nil {
return false, err
}
defer f.Close()
_, err = f.Readdirnames(1)
if err == io.EOF {
return true, nil
}
return false, err
}