forked from plexdrive/plexdrive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclean.go
More file actions
54 lines (47 loc) · 1.1 KB
/
clean.go
File metadata and controls
54 lines (47 loc) · 1.1 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
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) {
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 nil
})
}
}
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
}