From c73877280b878dab23c1024a777aea163e88a5ce Mon Sep 17 00:00:00 2001 From: lihan Date: Tue, 23 Jan 2024 18:04:02 +0800 Subject: [PATCH] bugfix: Clean all temporary files before today --- internal/config/config.go | 2 +- internal/tmp.go | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index 4e6c98c6..b4aecc37 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -38,7 +38,7 @@ var ( func NewConfigWithPath(p string) (*Config, error) { if !util.FileExists(p) { content, err := yaml.Marshal(defaultConfig) - if err != nil { + if err == nil { _ = os.WriteFile(p, content, 0644) return defaultConfig, nil } diff --git a/internal/tmp.go b/internal/tmp.go index 069e6b0d..c4cfb5bb 100644 --- a/internal/tmp.go +++ b/internal/tmp.go @@ -20,6 +20,8 @@ import ( "fmt" "os" "path/filepath" + "strconv" + "strings" "github.com/version-fox/vfox/internal/util" ) @@ -30,7 +32,27 @@ type Temp struct { } func (t *Temp) Remove() { - _ = os.RemoveAll(t.CurProcessPath) + dir, err := os.ReadDir(t.dirPath) + if err == nil { + _ = os.RemoveAll(t.CurProcessPath) + for _, file := range dir { + if !file.IsDir() { + continue + } + names := strings.SplitN(file.Name(), "-", 2) + if len(names) != 2 { + continue + } + timestamp := names[0] + i, err := strconv.ParseInt(timestamp, 10, 64) + if err != nil { + continue + } + if util.IsBeforeToday(i) { + _ = os.Remove(filepath.Join(t.dirPath, file.Name())) + } + } + } } func NewTemp(dirPath string, pid int) (*Temp, error) {