Debian Codegraph @

commands/command_fsck.go

raw symbols SCIP
1 package commands
2
3 import (
4 "crypto/sha256"
5 "encoding/hex"
6 "fmt"
7 "io"
8 "os"
9 "path/filepath"
10 "strings"
11
12 "github.com/git-lfs/git-lfs/v3/errors"
13 "github.com/git-lfs/git-lfs/v3/filepathfilter"
14 "github.com/git-lfs/git-lfs/v3/git"
15 "github.com/git-lfs/git-lfs/v3/lfs"
16 "github.com/git-lfs/git-lfs/v3/tools"
17 "github.com/git-lfs/git-lfs/v3/tr"
18 "github.com/rubyist/tracerx"
19 "github.com/spf13/cobra"
20 )
21
22 var (
23 fsckDryRun bool
24 fsckObjects bool
25 fsckPointers bool
26 )
27
28 type corruptPointer struct {
29 blobOid string
30 treeOid string
31 lfsOid string
32 path string
33 message string
34 kind string
35 }
36
37 func (p corruptPointer) String() string {
38 return fmt.Sprintf("%s: %s", p.kind, p.message)
39 }
40
41 // TODO(zeroshirts): 'git fsck' reports status (percentage, current#/total) as
42 // it checks... we should do the same, as we are rehashing potentially gigs and
43 // gigs of content.
44 //
45 // NOTE(zeroshirts): Ideally git would have hooks for fsck such that we could
46 // chain a lfs-fsck, but I don't think it does.
47 func fsckCommand(cmd *cobra.Command, args []string) {
48 installHooks(false)
49 setupRepository()
50
51 useIndex := false
52 exclude := ""
53 include := "HEAD"
54
55 switch len(args) {
56 case 0:
57 useIndex = true
58 ref, err := git.CurrentRef()
59 if err != nil {
60 ExitWithError(err)
61 }
62 include = ref.Sha
63 case 1:
64 pieces := strings.SplitN(args[0], "..", 2)
65 refs, err := git.ResolveRefs(pieces)
66 if err != nil {
67 ExitWithError(err)
68 }
69 if len(refs) == 2 {
70 exclude = refs[0].Sha
71 include = refs[1].Sha
72 } else {
73 include = refs[0].Sha
74 }
75 }
76
77 if !fsckPointers && !fsckObjects {
78 fsckPointers = true
79 fsckObjects = true
80 }
81
82 ok := true
83 var corruptOids []string
84 var corruptPointers []corruptPointer
85 if fsckObjects {
86 corruptOids = doFsckObjects(include, exclude, useIndex)
87 ok = ok && len(corruptOids) == 0
88 }
89 if fsckPointers {
90 corruptPointers = doFsckPointers(include, exclude)
91 ok = ok && len(corruptPointers) == 0
92 }
93
94 if ok {
95 Print(tr.Tr.Get("Git LFS fsck OK"))
96 return
97 }
98
99 if fsckDryRun || len(corruptOids) == 0 {
100 ExitWithCode(1)
101 }
102
103 badDir := filepath.Join(cfg.LFSStorageDir(), "bad")
104 Print("objects: repair: %s", tr.Tr.Get("moving corrupt objects to %s", badDir))
105
106 if err := tools.MkdirAll(badDir, cfg); err != nil {
107 ExitWithError(err)
108 }
109
110 for _, oid := range corruptOids {
111 badFile := filepath.Join(badDir, oid)
112 srcFile := cfg.Filesystem().ObjectPathname(oid)
113 if srcFile == os.DevNull {
114 continue
115 }
116 if err := os.Rename(srcFile, badFile); err != nil {
117 if os.IsNotExist(err) {
118 continue
119 }
120 ExitWithError(err)
121 }
122 }
123 ExitWithCode(1)
124 }
125
126 // doFsckObjects checks that the objects in the given ref are correct and exist.
127 func doFsckObjects(include, exclude string, useIndex bool) []string {
128 var corruptOids []string
129 gitscanner := lfs.NewGitScanner(cfg, func(p *lfs.WrappedPointer, err error) {
130 if err == nil {
131 var pointerOk bool
132 pointerOk, err = fsckPointer(p.Name, p.Oid, p.Size)
133 if !pointerOk {
134 corruptOids = append(corruptOids, p.Oid)
135 }
136 }
137
138 if err != nil {
139 Panic(err, tr.Tr.Get("Error checking Git LFS files"))
140 }
141 })
142
143 // If 'lfs.fetchexclude' is set and 'git lfs fsck' is run after the
144 // initial fetch (i.e., has elected to fetch a subset of Git LFS
145 // objects), the "missing" ones will fail the fsck.
146 //
147 // Attach a filepathfilter to avoid _only_ the excluded paths.
148 gitscanner.Filter = filepathfilter.New(nil, cfg.FetchExcludePaths(), filepathfilter.GitIgnore, cfg.Git)
149
150 if exclude == "" {
151 if err := gitscanner.ScanRef(include, nil); err != nil {
152 ExitWithError(err)
153 }
154 } else {
155 if err := gitscanner.ScanRefRange(include, exclude, nil); err != nil {
156 ExitWithError(err)
157 }
158 }
159
160 if useIndex {
161 if err := gitscanner.ScanIndex("HEAD", "", nil); err != nil {
162 ExitWithError(err)
163 }
164 }
165
166 return corruptOids
167 }
168
169 // doFsckPointers checks that the pointers in the given ref are correct and canonical.
170 func doFsckPointers(include, exclude string) []corruptPointer {
171 var corruptPointers []corruptPointer
172 gitscanner := lfs.NewGitScanner(cfg, func(p *lfs.WrappedPointer, err error) {
173 if p != nil {
174 tracerx.Printf("Examining %v (%v)", p.Oid, p.Name)
175 if !p.Canonical {
176 cp := corruptPointer{
177 blobOid: p.Sha1,
178 lfsOid: p.Oid,
179 message: tr.Tr.Get("Pointer for %s (blob %s) was not canonical", p.Oid, p.Sha1),
180 kind: "nonCanonicalPointer",
181 }
182 Print("pointer: %s", cp.String())
183 corruptPointers = append(corruptPointers, cp)
184 }
185 } else if errors.IsPointerScanError(err) {
186 psErr, ok := err.(errors.PointerScanError)
187 if ok {
188 cp := corruptPointer{
189 treeOid: psErr.OID(),
190 path: psErr.Path(),
191 message: tr.Tr.Get("%q (treeish %s) should have been a pointer but was not", psErr.Path(), psErr.OID()),
192 kind: "unexpectedGitObject",
193 }
194 Print("pointer: %s", cp.String())
195 corruptPointers = append(corruptPointers, cp)
196 }
197 } else {
198 Panic(err, tr.Tr.Get("Error checking Git LFS files"))
199 }
200 })
201
202 if exclude == "" {
203 if err := gitscanner.ScanRefByTree(include, nil); err != nil {
204 ExitWithError(err)
205 }
206 } else {
207 if err := gitscanner.ScanRefRangeByTree(include, exclude, nil); err != nil {
208 ExitWithError(err)
209 }
210 }
211
212 return corruptPointers
213 }
214
215 func fsckPointer(name, oid string, size int64) (bool, error) {
216 path := cfg.Filesystem().ObjectPathname(oid)
217
218 tracerx.Printf("Examining %v (%v)", name, path)
219
220 f, err := os.Open(path)
221 if pErr, pOk := err.(*os.PathError); pOk {
222 // This is an empty file. No problem here.
223 if size == 0 {
224 return true, nil
225 }
226 Print("objects: openError: %s", tr.Tr.Get("%s (%s) could not be checked: %s", name, oid, pErr.Err))
227 return false, nil
228 }
229
230 if err != nil {
231 return false, err
232 }
233
234 oidHash := sha256.New()
235 _, err = io.Copy(oidHash, f)
236 f.Close()
237 if err != nil {
238 return false, err
239 }
240
241 recalculatedOid := hex.EncodeToString(oidHash.Sum(nil))
242 if recalculatedOid == oid {
243 return true, nil
244 }
245
246 Print(fmt.Sprintf("objects: corruptObject: %s", tr.Tr.Get("%s (%s) is corrupt", name, oid)))
247 return false, nil
248 }
249
250 func init() {
251 RegisterCommand("fsck", fsckCommand, func(cmd *cobra.Command) {
252 cmd.Flags().BoolVarP(&fsckDryRun, "dry-run", "d", false, "List corrupt objects without deleting them.")
253 cmd.Flags().BoolVarP(&fsckObjects, "objects", "", false, "Fsck objects.")
254 cmd.Flags().BoolVarP(&fsckPointers, "pointers", "", false, "Fsck pointers.")
255 })
256 }