From 69713e077dd951d74dfaa7cb658c16b83289ecb7 Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Sun, 13 Aug 2023 18:18:55 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8A=82=E7=82=B9=E5=90=AF=E5=8A=A8=E6=97=B6?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E7=BC=93=E5=AD=98=E7=9B=AE=E5=BD=95=E4=B8=8B?= =?UTF-8?q?=E9=81=97=E7=95=99=E7=9A=84*.trash=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/caches/storage_file.go | 38 +++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/internal/caches/storage_file.go b/internal/caches/storage_file.go index e880dc9..062aee0 100644 --- a/internal/caches/storage_file.go +++ b/internal/caches/storage_file.go @@ -22,6 +22,7 @@ import ( "github.com/iwind/TeaGo/rands" "github.com/iwind/TeaGo/types" stringutil "github.com/iwind/TeaGo/utils/string" + timeutil "github.com/iwind/TeaGo/utils/time" "github.com/shirou/gopsutil/v3/load" "math" "os" @@ -299,6 +300,9 @@ func (this *FileStorage) Init() error { // 检查磁盘空间 this.checkDiskSpace() + // clean *.trash directories + this.cleanAllDeletedDirs() + return nil } @@ -746,7 +750,7 @@ func (this *FileStorage) CleanAll() error { return err } for _, info := range subDirs { - subDir := info.Name() + var subDir = info.Name() // 检查目录名 if !dirNameReg.MatchString(subDir) { @@ -754,7 +758,7 @@ func (this *FileStorage) CleanAll() error { } // 修改目录名 - tmpDir := dir + "/" + subDir + "-deleted" + var tmpDir = dir + "/" + subDir + "." + timeutil.Format("YmdHis") + ".trash" err = os.Rename(dir+"/"+subDir, tmpDir) if err != nil { return err @@ -765,7 +769,11 @@ func (this *FileStorage) CleanAll() error { goman.New(func() { err = this.cleanDeletedDirs(dir) if err != nil { - remotelogs.Warn("CACHE", "delete '*-deleted' dirs failed: "+err.Error()) + remotelogs.Warn("CACHE", "delete '*.trash' dirs failed: "+err.Error()) + } else { + // try to clean again, to delete writing files when deleting + time.Sleep(10 * time.Minute) + _ = this.cleanDeletedDirs(dir) } }) @@ -1228,7 +1236,25 @@ func (this *FileStorage) diskCapacityBytes() int64 { return c1 } -// 清理 *-deleted 目录 +// remove all *.trash directories under policy directory +func (this *FileStorage) cleanAllDeletedDirs() { + var rootDirs = []string{this.options.Dir} + var subDirs = this.subDirs // copy slice + if len(subDirs) > 0 { + for _, subDir := range subDirs { + rootDirs = append(rootDirs, subDir.Path) + } + } + + for _, rootDir := range rootDirs { + var dir = rootDir + "/p" + types.String(this.policy.Id) + goman.New(func() { + _ = this.cleanDeletedDirs(dir) + }) + } +} + +// 清理 *.trash 目录 // 由于在很多硬盘上耗时非常久,所以应该放在后台运行 func (this *FileStorage) cleanDeletedDirs(dir string) error { fp, err := os.Open(dir) @@ -1243,8 +1269,8 @@ func (this *FileStorage) cleanDeletedDirs(dir string) error { return err } for _, info := range subDirs { - subDir := info.Name() - if !strings.HasSuffix(subDir, "-deleted") { + var subDir = info.Name() + if !strings.HasSuffix(subDir, ".trash") { continue }