/
command_fsck.go
249 lines (217 loc) · 5.91 KB
/
command_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
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
package commands
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/git-lfs/git-lfs/v2/errors"
"github.com/git-lfs/git-lfs/v2/filepathfilter"
"github.com/git-lfs/git-lfs/v2/git"
"github.com/git-lfs/git-lfs/v2/lfs"
"github.com/git-lfs/git-lfs/v2/tools"
"github.com/spf13/cobra"
)
var (
fsckDryRun bool
fsckObjects bool
fsckPointers bool
)
type corruptPointer struct {
blobOid string
treeOid string
lfsOid string
path string
message string
kind string
}
func (p corruptPointer) String() string {
return fmt.Sprintf("%s: %s", p.kind, p.message)
}
// TODO(zeroshirts): 'git fsck' reports status (percentage, current#/total) as
// it checks... we should do the same, as we are rehashing potentially gigs and
// gigs of content.
//
// NOTE(zeroshirts): Ideally git would have hooks for fsck such that we could
// chain a lfs-fsck, but I don't think it does.
func fsckCommand(cmd *cobra.Command, args []string) {
installHooks(false)
setupRepository()
useIndex := false
start := ""
end := "HEAD"
switch len(args) {
case 0:
useIndex = true
ref, err := git.CurrentRef()
if err != nil {
ExitWithError(err)
}
end = ref.Sha
case 1:
pieces := strings.SplitN(args[0], "..", 2)
refs, err := git.ResolveRefs(pieces)
if err != nil {
ExitWithError(err)
}
if len(refs) == 2 {
start = refs[0].Sha
end = refs[1].Sha
} else {
end = refs[0].Sha
}
}
if !fsckPointers && !fsckObjects {
fsckPointers = true
fsckObjects = true
}
ok := true
var corruptOids []string
var corruptPointers []corruptPointer
if fsckObjects {
corruptOids = doFsckObjects(start, end, useIndex)
ok = ok && len(corruptOids) == 0
}
if fsckPointers {
corruptPointers = doFsckPointers(start, end)
ok = ok && len(corruptPointers) == 0
}
if ok {
Print("Git LFS fsck OK")
return
}
if fsckDryRun || len(corruptOids) == 0 {
os.Exit(1)
}
badDir := filepath.Join(cfg.LFSStorageDir(), "bad")
Print("objects: repair: moving corrupt objects to %s", badDir)
if err := tools.MkdirAll(badDir, cfg); err != nil {
ExitWithError(err)
}
for _, oid := range corruptOids {
badFile := filepath.Join(badDir, oid)
if err := os.Rename(cfg.Filesystem().ObjectPathname(oid), badFile); err != nil {
ExitWithError(err)
}
}
os.Exit(1)
}
// doFsckObjects checks that the objects in the given ref are correct and exist.
func doFsckObjects(start, end string, useIndex bool) []string {
var corruptOids []string
gitscanner := lfs.NewGitScanner(cfg, func(p *lfs.WrappedPointer, err error) {
if err == nil {
var pointerOk bool
pointerOk, err = fsckPointer(p.Name, p.Oid, p.Size)
if !pointerOk {
corruptOids = append(corruptOids, p.Oid)
}
}
if err != nil {
Panic(err, "Error checking Git LFS files")
}
})
// If 'lfs.fetchexclude' is set and 'git lfs fsck' is run after the
// initial fetch (i.e., has elected to fetch a subset of Git LFS
// objects), the "missing" ones will fail the fsck.
//
// Attach a filepathfilter to avoid _only_ the excluded paths.
gitscanner.Filter = filepathfilter.New(nil, cfg.FetchExcludePaths(), filepathfilter.GitAttributes)
if start == "" {
if err := gitscanner.ScanRef(end, nil); err != nil {
ExitWithError(err)
}
} else {
if err := gitscanner.ScanRefRange(start, end, nil); err != nil {
ExitWithError(err)
}
}
if useIndex {
if err := gitscanner.ScanIndex("HEAD", nil); err != nil {
ExitWithError(err)
}
}
gitscanner.Close()
return corruptOids
}
// doFsckPointers checks that the pointers in the given ref are correct and canonical.
func doFsckPointers(start, end string) []corruptPointer {
var corruptPointers []corruptPointer
gitscanner := lfs.NewGitScanner(cfg, func(p *lfs.WrappedPointer, err error) {
if p != nil {
Debug("Examining %v (%v)", p.Oid, p.Name)
if !p.Canonical {
cp := corruptPointer{
blobOid: p.Sha1,
lfsOid: p.Oid,
message: fmt.Sprintf("Pointer for %s (blob %s) was not canonical", p.Oid, p.Sha1),
kind: "nonCanonicalPointer",
}
Print("pointer: %s", cp.String())
corruptPointers = append(corruptPointers, cp)
}
} else if errors.IsPointerScanError(err) {
psErr, ok := err.(errors.PointerScanError)
if ok {
cp := corruptPointer{
treeOid: psErr.OID(),
path: psErr.Path(),
message: fmt.Sprintf("%q (treeish %s) should have been a pointer but was not", psErr.Path(), psErr.OID()),
kind: "unexpectedGitObject",
}
Print("pointer: %s", cp.String())
corruptPointers = append(corruptPointers, cp)
}
} else {
Panic(err, "Error checking Git LFS files")
}
})
if start == "" {
if err := gitscanner.ScanRefByTree(end, nil); err != nil {
ExitWithError(err)
}
} else {
if err := gitscanner.ScanRefRangeByTree(start, end, nil); err != nil {
ExitWithError(err)
}
}
gitscanner.Close()
return corruptPointers
}
func fsckPointer(name, oid string, size int64) (bool, error) {
path := cfg.Filesystem().ObjectPathname(oid)
Debug("Examining %v (%v)", name, path)
f, err := os.Open(path)
if pErr, pOk := err.(*os.PathError); pOk {
// This is an empty file. No problem here.
if size == 0 {
return true, nil
}
Print("objects: openError: %s (%s) could not be checked: %s", name, oid, pErr.Err)
return false, nil
}
if err != nil {
return false, err
}
oidHash := sha256.New()
_, err = io.Copy(oidHash, f)
f.Close()
if err != nil {
return false, err
}
recalculatedOid := hex.EncodeToString(oidHash.Sum(nil))
if recalculatedOid == oid {
return true, nil
}
Print("objects: corruptObject: %s (%s) is corrupt", name, oid)
return false, nil
}
func init() {
RegisterCommand("fsck", fsckCommand, func(cmd *cobra.Command) {
cmd.Flags().BoolVarP(&fsckDryRun, "dry-run", "d", false, "List corrupt objects without deleting them.")
cmd.Flags().BoolVarP(&fsckObjects, "objects", "", false, "Fsck objects.")
cmd.Flags().BoolVarP(&fsckPointers, "pointers", "", false, "Fsck pointers.")
})
}