-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsck.go
174 lines (168 loc) · 4.72 KB
/
fsck.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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
)
var (
filenameRegex = regexp.MustCompile(`(\d+)_(.*)`)
)
func newPassPrinter() func(title string) {
var num = 1
return func(title string) {
log.Printf("Pass %d: %s...", num, title)
num++
}
}
func moveFile(srcFilename, dstFilename string) error {
return exec.Command("mv", srcFilename, dstFilename).Run()
}
func fixFilename(itemID uint64) error {
var i item
tx := db.Begin()
defer tx.RollbackUnlessCommitted()
tx.First(&i, "id = ?", itemID)
i.Name = strings.ReplaceAll(i.Name, "|", "\u00a6")
path, err := filePathWithTx(tx, itemID)
if err != nil {
return fmt.Errorf("error getting path of file with id = %d: %s", itemID, err)
}
fn := strings.ReplaceAll(filepath.Base(path), "|", "\u00a6")
newPath := filepath.Join(filepath.Dir(path), fn)
if err := os.Rename(path, newPath); err != nil {
return fmt.Errorf("error renaming %s => %s: %s", path, newPath, err)
}
log.Printf("Renamed %s => %s", path, newPath)
tx.Save(&i)
tx.Commit()
return nil
}
func fsck(fix bool) error {
var errors, fixed int
pass := newPassPrinter()
pass("checking database")
rows, err := db.Model(&item{}).Where("type = ?", file).Select("id, name").Rows()
if err != nil {
return err
}
var badIDs []id
for rows.Next() {
var i item
db.ScanRows(rows, &i)
path, err := filePathWithNameTx(uint64(i.ID), i.Name)
if err != nil {
return err
}
_, err = os.Stat(path)
if os.IsNotExist(err) {
log.Printf("File %s doesn't exist but is present in the database", path)
badIDs = append(badIDs, i.ID)
}
}
errors += len(badIDs)
if fix && len(badIDs) > 0 {
pass("removing incorrect database entries")
log.Printf("Deleting %d file records from the database...", len(badIDs))
db.Exec("DELETE FROM item_tags WHERE item_id IN (?)", badIDs)
db.Exec("DELETE FROM items WHERE id IN (?)", badIDs)
fixed += len(badIDs)
log.Println("Done.")
}
pass("checking dangling tags")
var dangling int
db.Raw("WITH allids AS (SELECT id FROM items) SELECT COUNT(*) FROM item_tags WHERE item_id NOT IN allids OR other_id NOT IN allids").Count(&dangling)
if dangling > 0 {
log.Printf("Found %d dangling tag references", dangling)
}
errors += dangling
if dangling > 0 && fix {
rows := db.Exec("WITH allids AS (SELECT id FROM items) DELETE FROM item_tags WHERE item_id NOT IN allids OR other_id NOT IN allids").RowsAffected
log.Printf("Deleted %d dangling tag references", rows)
fixed += int(rows)
}
var lostFiles []string
var badFiles []uint64
pass("checking storage")
filepath.Walk(storagePath, func(path string, info os.FileInfo, _ error) error {
if info.IsDir() {
return nil
}
rel, err := filepath.Rel(storagePath, path)
if err != nil {
return err
}
if rel == "version.txt" {
return nil
}
match := filenameRegex.FindStringSubmatch(info.Name())
if match == nil {
log.Printf("Bad filename %s", path)
lostFiles = append(lostFiles, path)
return nil
}
dir := filepath.Dir(rel)
id6, id2 := filepath.Split(dir)
id6 = filepath.Base(id6)
if len(id6) != 6 || len(id2) != 2 || !strings.HasPrefix(match[1], id6+id2) {
lostFiles = append(lostFiles, path)
log.Printf("Invalid path %s/%s != %s", id6, id2, match[1])
return nil
}
var i item
if db.First(&i, "id = ? AND name = ?", match[1], match[2]).RecordNotFound() {
lostFiles = append(lostFiles, path)
log.Printf("File %s is in storage but not in database", path)
}
if strings.Contains(info.Name(), "|") {
fileID, _ := strconv.ParseUint(match[1], 10, 64)
badFiles = append(badFiles, fileID)
log.Printf("File %s name contains invalid characters", path)
}
return nil
})
errors += len(lostFiles) + len(badFiles)
if fix && len(lostFiles) > 0 {
pass("recovering lost files")
lftag := path.Join(mountpoint, "tags", "lost+found")
_, err := os.Stat(lftag)
if os.IsNotExist(err) {
log.Println("lost+found tag doesn't exist, creating...")
if err := os.Mkdir(lftag, 0755); err != nil {
log.Println("Error creating lost+found tag:", err)
return err
}
}
lfbrowse := path.Join(mountpoint, "browse", "lost+found", "@")
for _, src := range lostFiles {
dst := path.Join(lfbrowse, filepath.Base(src))
log.Printf("Recovering %s to %s...", src, dst)
err := moveFile(src, dst)
if err != nil {
log.Printf("Error recovering file %s: %s", src, err)
} else {
log.Printf("Recovered file %s to lost+found", src)
fixed++
}
}
}
if fix && len(badFiles) > 0 {
pass("fixing invalid filenames")
for _, itemID := range badFiles {
if err := fixFilename(itemID); err != nil {
log.Println(err)
} else {
fixed++
}
}
}
if errors > 0 {
return fmt.Errorf("found %d errors, %d fixed", errors, fixed)
}
return nil
}