8
8
"gorm.io/gorm"
9
9
"gorm.io/gorm/clause"
10
10
"k8s.io/klog/v2"
11
+ "os"
12
+ "path/filepath"
13
+ "strings"
11
14
"sync"
12
15
"time"
13
16
)
@@ -22,6 +25,8 @@ type PathList struct {
22
25
UpdateTime time.Time `gorm:"not null;type:timestamptz;autoUpdateTime"`
23
26
}
24
27
28
+ var pathListInited bool = false
29
+
25
30
func createPathListTable () {
26
31
// Migrate the schema, create the table if it does not exist
27
32
err := DBServer .AutoMigrate (& PathList {})
@@ -60,6 +65,8 @@ func InitDrivePathList() {
60
65
}
61
66
}
62
67
68
+ pathListInited = true
69
+
63
70
if err := logPathList (); err != nil {
64
71
fmt .Println ("Error logging path list:" , err )
65
72
}
@@ -82,14 +89,31 @@ func GenerateOtherPathList(ctx context.Context) {
82
89
//klog.Info("~~~Temp log: cookie hasn't come")
83
90
}
84
91
mu .Unlock ()
92
+
93
+ select {
94
+ case <- ctx .Done ():
95
+ return
96
+ default :
97
+ }
85
98
}
86
99
}()
87
100
88
101
for {
89
102
mu .Lock ()
90
103
for len (common .BflCookieCache ) == 0 {
91
104
//klog.Info("~~~Temp log: waiting for cookie")
92
- cond .Wait ()
105
+ done := ctx .Done ()
106
+ if done != nil {
107
+ select {
108
+ case <- done :
109
+ mu .Unlock ()
110
+ return
111
+ default :
112
+ cond .Wait ()
113
+ }
114
+ } else {
115
+ cond .Wait ()
116
+ }
93
117
}
94
118
95
119
var srcTypeList = []string {
@@ -136,7 +160,7 @@ func logPathList() error {
136
160
}
137
161
138
162
func ProcessDirectory (db * gorm.DB , drive , path string , modTime time.Time ) error {
139
- if drive == "Unknown " || drive == "Error " || path == "" {
163
+ if drive == "unknown " || drive == "error " || path == "" {
140
164
// won't deal with these on purpose
141
165
return nil
142
166
}
@@ -187,3 +211,70 @@ func batchUpdate(paths []PathList) error {
187
211
}),
188
212
}).Create (& paths ).Error
189
213
}
214
+
215
+ func CheckAndUpdateStatus (ctx context.Context ) {
216
+ if ! pathListInited {
217
+ return
218
+ }
219
+
220
+ var pathEntries []PathList
221
+
222
+ if err := DBServer .WithContext (ctx ).Where ("drive IN (?, ?, ?, ?)" , "drive" , "data" , "cache" , "external" ).Find (& pathEntries ).Error ; err != nil {
223
+ klog .Errorf ("failed to query drive path list: %v" , err )
224
+ return
225
+ }
226
+
227
+ for _ , entry := range pathEntries {
228
+ var fullPath string
229
+ var exists bool
230
+ var err error
231
+
232
+ switch entry .Drive {
233
+ case "drive" :
234
+ fullPath = "/data/" + entry .Path
235
+ case "data" :
236
+ fullPath = "/data/" + entry .Path
237
+ case "cache" :
238
+ fullPath = "/appcache/" + entry .Path
239
+ case "external" :
240
+ pathSplit := strings .Split (entry .Path , "/" )
241
+ fullPath = "/data/External/" + filepath .Join (pathSplit [3 :]... )
242
+ default :
243
+ continue
244
+ }
245
+
246
+ exists , err = pathExists (fullPath )
247
+ if err != nil {
248
+ klog .Errorf ("failed to check if path exists: %v" , err )
249
+ return
250
+ }
251
+
252
+ var newStatus int
253
+ if exists {
254
+ newStatus = 0
255
+ } else {
256
+ newStatus = 1
257
+ }
258
+
259
+ if entry .Status == newStatus {
260
+ continue
261
+ }
262
+
263
+ if err := DBServer .WithContext (ctx ).Model (& PathList {}).Where ("drive = ? AND path = ?" , entry .Drive , entry .Path ).Update ("status" , newStatus ).Error ; err != nil {
264
+ klog .Errorf ("failed to update drive path status: %v" , err )
265
+ return
266
+ }
267
+ }
268
+
269
+ return
270
+ }
271
+
272
+ func pathExists (path string ) (bool , error ) {
273
+ _ , err := os .Stat (path )
274
+ if os .IsNotExist (err ) {
275
+ return false , nil
276
+ } else if err != nil {
277
+ return false , err
278
+ }
279
+ return true , nil
280
+ }
0 commit comments