[Question] temp文件夹如何清理
Closed this issue · 1 comments
squallliu commented
Version
0.6.10
OS
ubuntu25.04
Question Description
temp文件夹如何清理
jan-bar commented
新建终端时会检测一天前创建且未在使用的目录,每天只会检测一次。
Lines 734 to 776 in af6f2b3
| func (m *Manager) CleanTmp() { | |
| // once per day | |
| cleanFlagPath := filepath.Join(m.PathMeta.TempPath, cleanupFlagFilename) | |
| if str, err := os.ReadFile(cleanFlagPath); err == nil { | |
| if i, err := strconv.ParseInt(string(str), 10, 64); err == nil && !util.IsBeforeToday(i) { | |
| return | |
| } | |
| } | |
| _ = os.WriteFile(cleanFlagPath, []byte(strconv.FormatInt(util.GetBeginOfToday(), 10)), os.ModePerm) | |
| procExists := make(map[string]struct{}) | |
| if procList, err := process.Pids(); err == nil { | |
| for _, v := range procList { | |
| procExists[fmt.Sprintf("%d", v)] = struct{}{} | |
| } | |
| } else { | |
| return | |
| } | |
| dir, err := os.ReadDir(m.PathMeta.TempPath) | |
| if err == nil { | |
| for _, file := range dir { | |
| if !file.IsDir() { | |
| continue | |
| } | |
| timestamp, pid, ok := strings.Cut(file.Name(), "-") | |
| if !ok { | |
| continue | |
| } | |
| if _, ok = procExists[pid]; ok { | |
| continue | |
| } | |
| i, err := strconv.ParseInt(timestamp, 10, 64) | |
| if err != nil { | |
| continue | |
| } | |
| if util.IsBeforeToday(i) { | |
| _ = os.RemoveAll(filepath.Join(m.PathMeta.TempPath, file.Name())) | |
| } | |
| } | |
| } | |
| } |