mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-06 10:00:25 +08:00
节点启动时删除缓存目录下遗留的*.trash文件
This commit is contained in:
@@ -22,6 +22,7 @@ import (
|
|||||||
"github.com/iwind/TeaGo/rands"
|
"github.com/iwind/TeaGo/rands"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
stringutil "github.com/iwind/TeaGo/utils/string"
|
stringutil "github.com/iwind/TeaGo/utils/string"
|
||||||
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
"github.com/shirou/gopsutil/v3/load"
|
"github.com/shirou/gopsutil/v3/load"
|
||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
@@ -299,6 +300,9 @@ func (this *FileStorage) Init() error {
|
|||||||
// 检查磁盘空间
|
// 检查磁盘空间
|
||||||
this.checkDiskSpace()
|
this.checkDiskSpace()
|
||||||
|
|
||||||
|
// clean *.trash directories
|
||||||
|
this.cleanAllDeletedDirs()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -746,7 +750,7 @@ func (this *FileStorage) CleanAll() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, info := range subDirs {
|
for _, info := range subDirs {
|
||||||
subDir := info.Name()
|
var subDir = info.Name()
|
||||||
|
|
||||||
// 检查目录名
|
// 检查目录名
|
||||||
if !dirNameReg.MatchString(subDir) {
|
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)
|
err = os.Rename(dir+"/"+subDir, tmpDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -765,7 +769,11 @@ func (this *FileStorage) CleanAll() error {
|
|||||||
goman.New(func() {
|
goman.New(func() {
|
||||||
err = this.cleanDeletedDirs(dir)
|
err = this.cleanDeletedDirs(dir)
|
||||||
if err != nil {
|
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
|
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 {
|
func (this *FileStorage) cleanDeletedDirs(dir string) error {
|
||||||
fp, err := os.Open(dir)
|
fp, err := os.Open(dir)
|
||||||
@@ -1243,8 +1269,8 @@ func (this *FileStorage) cleanDeletedDirs(dir string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, info := range subDirs {
|
for _, info := range subDirs {
|
||||||
subDir := info.Name()
|
var subDir = info.Name()
|
||||||
if !strings.HasSuffix(subDir, "-deleted") {
|
if !strings.HasSuffix(subDir, ".trash") {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user