-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclean.go
61 lines (54 loc) · 1.09 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
package main
import (
"fmt"
"github.com/robfig/cron/v3"
log "github.com/sirupsen/logrus"
"os"
"path/filepath"
)
func CleanTask() {
c := cron.New()
_, err := c.AddFunc("* * * * *", func() {
if err := CleanTmpFile(GetGlobalConfig().TmpPath); err != nil {
}
})
if err != nil {
fmt.Println("Error adding task:", err)
return
}
c.Start()
}
func CleanTmpFile(dir string) error {
log.Infof("clean path:%s", dir)
// 打开目录
dir = fmt.Sprintf(".%s", dir)
d, err := os.Open(dir)
if err != nil {
log.Errorf("open dir err:%s", err)
return err
}
defer func() {
_ = d.Close()
}()
// 读取目录中的文件和子目录
entries, err := d.Readdir(-1)
if err != nil {
log.Errorf("read dir err:%s", err)
return err
}
for _, entry := range entries {
// 构建完整路径
path := filepath.Join(dir, entry.Name())
// 如果是文件,则删除
if !entry.IsDir() {
if err := os.Remove(path); err != nil {
log.Errorf("remove file err:%s", err)
return err
}
log.Infof("delete file:%s", path)
} else {
log.Infof("skip dir: %s", path)
}
}
return nil
}