-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_mgr.go
158 lines (125 loc) · 3.17 KB
/
file_mgr.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package file_mgr
import (
"io/fs"
"os"
"path/filepath"
"strings"
"time"
"github.com/meson-network/peer-node/basic"
"github.com/meson-network/peer-node/src/cdn_cache_folder"
)
func RemoveFileFromDisk(fileHash string) {
fileAbsPath := GetFileAbsPath(fileHash)
os.Remove(fileAbsPath)
os.Remove(fileAbsPath + ".header")
DeleteEmptyFolder(fileAbsPath)
}
func GetFileAbsPath(file_hash string) string {
return filepath.Join(cdn_cache_folder.GetInstance().Abs_path, cdn_cache_folder.CacheFileFolder, UrlHashToPublicFileRelPath(file_hash))
}
func CleanDownloadingFiles() error {
result, err := QueryFile(nil, nil, &[]string{STATUS_DOWNLOADING}, nil, 0, 0, false, false)
if err != nil {
return err
}
if len(result.Files) == 0 {
return nil
}
for _, v := range result.Files {
//todo get abs path
fileAbsPath := GetFileAbsPath(v.File_hash)
os.Remove(fileAbsPath)
os.Remove(fileAbsPath + ".header")
DeleteFile(v.File_hash)
DeleteEmptyFolder(fileAbsPath)
}
return nil
}
type DiskFile struct {
absPath string
fileName string
}
//ScanLeakFiles Scan disk and delete files not in db
func ScanLeakFiles() {
files := []*DiskFile{}
rootPath := filepath.Join(cdn_cache_folder.GetInstance().Abs_path, cdn_cache_folder.CacheFileFolder)
filepath.WalkDir(rootPath, func(path string, d fs.DirEntry, err error) error {
if d == nil {
return nil
}
if d.IsDir() {
return nil
}
fileName := d.Name()
fileExtension := filepath.Ext(fileName)
//extension is not ".header"
if fileExtension != "" && fileExtension != ".header" {
//not cache file
err := os.Remove(path)
if err != nil {
basic.Logger.Errorln("scanLeakFiles remove file error:", err, "path:", path)
} else {
DeleteEmptyFolder(path)
}
return nil
}
//fileBaseName is not 16 hash
fileBase := strings.TrimSuffix(fileName, ".header")
if len(fileBase) != 16 {
err := os.Remove(path)
if err != nil {
basic.Logger.Errorln("scanLeakFiles remove file error:", err, "path:", path)
} else {
DeleteEmptyFolder(path)
}
return nil
}
if strings.HasSuffix(fileName, ".header") {
return nil
}
diskFile := &DiskFile{
absPath: path,
fileName: d.Name(),
}
files = append(files, diskFile)
if len(files) >= 100 {
checkFileLeak(files)
files = []*DiskFile{}
time.Sleep(10 * time.Second)
}
return nil
})
if len(files) > 0 {
checkFileLeak(files)
files = []*DiskFile{}
time.Sleep(10 * time.Second)
}
}
func checkFileLeak(files []*DiskFile) {
fileNames := []string{}
for _, file := range files {
fileNames = append(fileNames, file.fileName)
}
result, err := QueryFile(nil, nil, nil, &fileNames, 0, 0, false, false)
if err != nil {
basic.Logger.Errorln("checkFileLeak QueryFile err:", err)
return
}
//all file find in db
if len(fileNames) == len(result.Files) {
return
}
nameHashMap := map[string]struct{}{}
for _, v := range result.Files {
nameHashMap[v.File_hash] = struct{}{}
}
for _, file := range files {
_, exist := nameHashMap[file.fileName]
if !exist {
os.Remove(file.absPath)
os.Remove(file.absPath + ".header")
basic.Logger.Debugln("deleted leak file", file.absPath)
DeleteEmptyFolder(file.absPath)
}
}
}