-
Notifications
You must be signed in to change notification settings - Fork 115
/
repair.go
525 lines (419 loc) · 15 KB
/
repair.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
// Copyright 2011-2018 Paul Ruane.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cli
import (
"errors"
"fmt"
"github.com/oniony/TMSU/common/fingerprint"
"github.com/oniony/TMSU/common/log"
_path "github.com/oniony/TMSU/common/path"
"github.com/oniony/TMSU/entities"
"github.com/oniony/TMSU/storage"
"os"
"path/filepath"
"strings"
)
var RepairCommand = Command{
Name: "repair",
Aliases: []string{"fix"},
Synopsis: "Repair the database",
Usages: []string{"tmsu repair [OPTION]... [PATH]...",
"tmsu repair [OPTION]... repair --manual OLD NEW"},
Description: `Fixes broken paths and stale fingerprints in the database caused by file modifications and moves.
Modified files are identified by a change to the file's modification time or file size. These files are repaired by updating the details in the database.
An attempt is made to find missing files under PATHs specified. If a file with the same fingerprint is found then the database is updated with the new file's details. If no PATHs are specified, or no match can be found, then the file is instead reported as missing.
Files that have been both moved and modified cannot be repaired and must be manually relocated.
When run with the --manual option, any paths that begin with OLD are updated to begin with NEW. Any affected files' fingerprints are updated providing the file exists at the new location. No further repairs are attempted in this mode.`,
Examples: []string{"$ tmsu repair",
"$ tmsu repair /new/path # look for missing files here",
"$ tmsu repair --path=/home/sally # repair subset of database",
"$ tmsu repair --manual /home/bob /home/fred # manually repair paths"},
Options: Options{{"--path", "-p", "limit repair to files in database under path", true, ""},
{"--pretend", "-P", "do not make any changes", false, ""},
{"--remove", "-R", "remove missing files from the database", false, ""},
{"--manual", "-m", "manually relocate files", false, ""},
{"--unmodified", "-u", "recalculate fingerprints for unmodified files", false, ""},
{"--rationalize", "", "remove explicit taggings where an implicit tagging exists", false, ""}},
Exec: repairExec,
}
// unexported
func repairExec(options Options, args []string, databasePath string) (error, warnings) {
pretend := options.HasOption("--pretend")
store, err := openDatabase(databasePath)
if err != nil {
return err, nil
}
defer store.Close()
tx, err := store.Begin()
if err != nil {
return err, nil
}
defer tx.Commit()
if options.HasOption("--manual") {
if len(args) < 2 {
return errors.New("too few arguments"), nil
}
fromPath := args[0]
toPath := args[1]
if err := manualRepair(store, tx, fromPath, toPath, pretend); err != nil {
return err, nil
}
} else {
searchPaths := args
removeMissing := options.HasOption("--remove")
recalcUnmodified := options.HasOption("--unmodified")
rationalize := options.HasOption("--rationalize")
limitPath := ""
if options.HasOption("--path") {
limitPath = options.Get("--path").Argument
}
if err := fullRepair(store, tx, searchPaths, limitPath, removeMissing, recalcUnmodified, rationalize, pretend); err != nil {
return err, nil
}
}
return nil, nil
}
func manualRepair(store *storage.Storage, tx *storage.Tx, fromPath, toPath string, pretend bool) error {
absFromPath, err := filepath.Abs(fromPath)
if err != nil {
return fmt.Errorf("%v: could not determine absolute path", err)
}
absToPath, err := filepath.Abs(toPath)
if err != nil {
return fmt.Errorf("%v: could not determine absolute path", err)
}
log.Infof(2, "retrieving files under '%v' from the database", fromPath)
dbFile, err := store.FileByPath(tx, absFromPath)
if err != nil {
return fmt.Errorf("%v: could not retrieve file: %v", fromPath, err)
}
if dbFile != nil {
log.Infof(2, "%v: updating to %v", fromPath, toPath)
if !pretend {
if err := manualRepairFile(store, tx, dbFile, absToPath); err != nil {
return err
}
}
}
dbFiles, err := store.FilesByDirectory(tx, absFromPath)
if err != nil {
return fmt.Errorf("could not retrieve files from storage: %v", err)
}
for _, dbFile = range dbFiles {
relFileFromPath := _path.Rel(dbFile.Path())
absFileToPath := strings.Replace(dbFile.Path(), absFromPath, absToPath, 1)
relFileToPath := _path.Rel(absFileToPath)
log.Infof(2, "%v: updating to %v", relFileFromPath, relFileToPath)
if !pretend {
if err := manualRepairFile(store, tx, dbFile, absFileToPath); err != nil {
return err
}
}
}
return nil
}
func manualRepairFile(store *storage.Storage, tx *storage.Tx, file *entities.File, toPath string) error {
stat, err := os.Stat(toPath)
if err != nil {
switch {
case os.IsPermission(err):
return fmt.Errorf("%v: permission denied", toPath)
case os.IsNotExist(err):
return fmt.Errorf("%v: file not found", toPath)
default:
return err
}
} else {
settings, err := store.Settings(tx)
if err != nil {
return err
}
fingerprint, err := fingerprint.Create(toPath, settings.FileFingerprintAlgorithm(), settings.DirectoryFingerprintAlgorithm(), settings.SymlinkFingerprintAlgorithm())
if err != nil {
log.Warnf("%v: could not create fingerprint: %v", toPath, err)
fingerprint = file.Fingerprint
}
modTime := stat.ModTime()
size := stat.Size()
isDir := stat.IsDir()
_, err = store.UpdateFile(tx, file.Id, toPath, fingerprint, modTime, size, isDir)
return err
}
}
func fullRepair(store *storage.Storage, tx *storage.Tx, searchPaths []string, limitPath string, removeMissing, recalcUnmodified, rationalize, pretend bool) error {
absLimitPath := ""
if limitPath != "" {
var err error
absLimitPath, err = filepath.Abs(limitPath)
if err != nil {
return fmt.Errorf("%v: could not determine absolute path", err)
}
}
settings, err := store.Settings(tx)
if err != nil {
return err
}
log.Infof(2, "retrieving files under '%v' from the database", absLimitPath)
dbFiles, err := store.FilesByDirectory(tx, absLimitPath)
if err != nil {
return fmt.Errorf("could not retrieve files from storage: %v", err)
}
dbFile, err := store.FileByPath(tx, absLimitPath)
if err != nil {
return fmt.Errorf("could not retrieve file from storage: %v", err)
}
if dbFile != nil {
dbFiles = append(dbFiles, dbFile)
}
log.Infof(2, "retrieved %v files from the database for path '%v'", len(dbFiles), absLimitPath)
unmodfied, modified, missing := determineStatuses(dbFiles)
if recalcUnmodified {
if err = repairUnmodified(store, tx, unmodfied, pretend, settings); err != nil {
return err
}
}
if err = repairModified(store, tx, modified, pretend, settings); err != nil {
return err
}
if err = repairMoved(store, tx, missing, searchPaths, pretend, settings); err != nil {
return err
}
if err = repairMissing(store, tx, missing, pretend, removeMissing); err != nil {
return err
}
if err = deleteUntaggedFiles(store, tx, dbFiles); err != nil {
return err
}
if rationalize {
if err = rationalizeFileTags(store, tx, dbFiles); err != nil {
return err
}
}
return nil
}
func deleteUntaggedFiles(store *storage.Storage, tx *storage.Tx, files entities.Files) error {
log.Infof(2, "purging untagged files")
fileIds := make([]entities.FileId, len(files))
for index, file := range files {
fileIds[index] = file.Id
}
return store.DeleteUntaggedFiles(tx, fileIds)
}
func rationalizeFileTags(store *storage.Storage, tx *storage.Tx, files entities.Files) error {
log.Infof(2, "rationalizing file tags")
for _, file := range files {
fileTags, err := store.FileTagsByFileId(tx, file.Id, false)
if err != nil {
return fmt.Errorf("could not determine tags for file '%v': %v", file.Path(), err)
}
for _, fileTag := range fileTags {
if fileTag.Implicit && fileTag.Explicit {
log.Infof(2, "%v: removing explicit tagging %v as implicit tagging exists", file.Path(), fileTag.TagId)
if err := store.DeleteFileTag(tx, fileTag.FileId, fileTag.TagId, fileTag.ValueId); err != nil {
return fmt.Errorf("could not delete file tag for file %v, tag %v and value %v", fileTag.FileId, fileTag.TagId, fileTag.ValueId)
}
}
}
}
return nil
}
func determineStatuses(dbFiles entities.Files) (unmodified, modified, missing entities.Files) {
log.Infof(2, "determining file statuses")
unmodified = make(entities.Files, 0, 10)
modified = make(entities.Files, 0, 10)
missing = make(entities.Files, 0, 10)
for _, dbFile := range dbFiles {
stat, err := os.Stat(dbFile.Path())
if err != nil {
switch {
case os.IsPermission(err):
//TODO return as warning
log.Warnf("%v: permission denied", dbFile.Path())
continue
case os.IsNotExist(err):
//TODO return as warning
log.Infof(2, "%v: missing", dbFile.Path())
missing = append(missing, dbFile)
continue
}
}
if dbFile.ModTime.Equal(stat.ModTime().UTC()) && dbFile.Size == stat.Size() {
log.Infof(2, "%v: unmodified", dbFile.Path())
unmodified = append(unmodified, dbFile)
} else {
log.Infof(2, "%v: modified", dbFile.Path())
modified = append(modified, dbFile)
}
}
return
}
func repairUnmodified(store *storage.Storage, tx *storage.Tx, unmodified entities.Files, pretend bool, settings entities.Settings) error {
log.Infof(2, "recalculating fingerprints for unmodified files")
for _, dbFile := range unmodified {
stat, err := os.Stat(dbFile.Path())
if err != nil {
return err
}
fingerprint, err := fingerprint.Create(dbFile.Path(), settings.FileFingerprintAlgorithm(), settings.DirectoryFingerprintAlgorithm(), settings.SymlinkFingerprintAlgorithm())
if err != nil {
log.Warnf("%v: could not create fingerprint: %v", dbFile.Path(), err)
continue
}
if !pretend {
_, err := store.UpdateFile(tx, dbFile.Id, dbFile.Path(), fingerprint, stat.ModTime(), stat.Size(), stat.IsDir())
if err != nil {
return fmt.Errorf("%v: could not update file in database: %v", dbFile.Path(), err)
}
}
fmt.Printf("%v: recalculated fingerprint\n", dbFile.Path())
}
return nil
}
func repairModified(store *storage.Storage, tx *storage.Tx, modified entities.Files, pretend bool, settings entities.Settings) error {
log.Infof(2, "repairing modified files")
for _, dbFile := range modified {
stat, err := os.Stat(dbFile.Path())
if err != nil {
return err
}
fingerprint, err := fingerprint.Create(dbFile.Path(), settings.FileFingerprintAlgorithm(), settings.DirectoryFingerprintAlgorithm(), settings.SymlinkFingerprintAlgorithm())
if err != nil {
log.Warnf("%v: could not create fingerprint: %v", dbFile.Path(), err)
continue
}
if !pretend {
_, err := store.UpdateFile(tx, dbFile.Id, dbFile.Path(), fingerprint, stat.ModTime(), stat.Size(), stat.IsDir())
if err != nil {
return fmt.Errorf("%v: could not update file in database: %v", dbFile.Path(), err)
}
}
fmt.Printf("%v: updated fingerprint\n", dbFile.Path())
}
return nil
}
func repairMoved(store *storage.Storage, tx *storage.Tx, missing entities.Files, searchPaths []string, pretend bool, settings entities.Settings) error {
log.Infof(2, "repairing moved files")
if len(missing) == 0 || len(searchPaths) == 0 {
// don't bother enumerating filesystem if nothing to do
return nil
}
pathsBySize, err := buildPathBySizeMap(searchPaths)
if err != nil {
return err
}
for index, dbFile := range missing {
log.Infof(2, "%v: searching for new location", dbFile.Path())
pathsOfSize := pathsBySize[dbFile.Size]
log.Infof(2, "%v: file is of size %v, identified %v files of this size", dbFile.Path(), dbFile.Size, len(pathsOfSize))
for _, candidatePath := range pathsOfSize {
candidateFile, err := store.FileByPath(tx, candidatePath)
if err != nil {
return err
}
if candidateFile != nil {
// file is already tagged
continue
}
stat, err := os.Stat(candidatePath)
if err != nil {
return fmt.Errorf("%v: could not stat file: %v", candidatePath, err)
}
fingerprint, err := fingerprint.Create(candidatePath, settings.FileFingerprintAlgorithm(), settings.DirectoryFingerprintAlgorithm(), settings.SymlinkFingerprintAlgorithm())
if err != nil {
return fmt.Errorf("%v: could not create fingerprint: %v", candidatePath, err)
}
if fingerprint == dbFile.Fingerprint {
if !pretend {
_, err := store.UpdateFile(tx, dbFile.Id, candidatePath, dbFile.Fingerprint, stat.ModTime(), dbFile.Size, dbFile.IsDir)
if err != nil {
return fmt.Errorf("%v: could not update file in database: %v", dbFile.Path(), err)
}
}
fmt.Printf("%v: updated path to %v\n", dbFile.Path(), candidatePath)
missing[index] = nil
break
}
}
}
return nil
}
func repairMissing(store *storage.Storage, tx *storage.Tx, missing entities.Files, pretend, force bool) error {
for _, dbFile := range missing {
if dbFile == nil {
continue
}
if force {
if !pretend {
if err := store.DeleteFileTagsByFileId(tx, dbFile.Id); err != nil {
return fmt.Errorf("%v: could not delete file-tags: %v", dbFile.Path(), err)
}
}
fmt.Printf("%v: removed\n", dbFile.Path())
} else {
fmt.Printf("%v: missing\n", dbFile.Path())
}
}
return nil
}
func buildPathBySizeMap(paths []string) (map[int64][]string, error) {
log.Infof(2, "building map of paths by size")
pathsBySize := make(map[int64][]string, 10)
for _, path := range paths {
if err := buildPathBySizeMapRecursive(path, pathsBySize); err != nil {
return nil, err
}
}
log.Infof(2, "path by size map has %v sizes", len(pathsBySize))
return pathsBySize, nil
}
func buildPathBySizeMapRecursive(path string, pathBySizeMap map[int64][]string) error {
absPath, err := filepath.Abs(path)
if err != nil {
return fmt.Errorf("%v: could not get absolute path", path)
}
stat, err := os.Stat(absPath)
if err != nil {
switch {
case os.IsPermission(err):
log.Warnf("%v: permission denied", path)
default:
return err
}
}
if stat.IsDir() {
log.Infof(3, "%v: examining directory contents", absPath)
dir, err := os.Open(absPath)
if err != nil {
return fmt.Errorf("%v: could not open directory: %v", path, err)
}
names, err := dir.Readdirnames(0)
dir.Close()
if err != nil {
return fmt.Errorf("%v: could not read directory entries: %v", path, err)
}
for _, name := range names {
childPath := filepath.Join(path, name)
if err := buildPathBySizeMapRecursive(childPath, pathBySizeMap); err != nil {
return err
}
}
} else {
log.Infof(3, "%v: file is of size %v", absPath, stat.Size())
filesOfSize, ok := pathBySizeMap[stat.Size()]
if ok {
pathBySizeMap[stat.Size()] = append(filesOfSize, absPath)
} else {
pathBySizeMap[stat.Size()] = []string{absPath}
}
}
return nil
}