Skip to content

Commit 83e4e2d

Browse files
authored
Make fsck behave correctly when faced with a zero-sized tree. (#753)
1 parent da6c09b commit 83e4e2d

2 files changed

Lines changed: 23 additions & 7 deletions

File tree

cmd/fsck/main.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,21 @@ func main() {
4747
if err != nil {
4848
klog.Exitf("Invalid --storage_url %q: %v", *storageURL, err)
4949
}
50-
src, err := client.NewHTTPFetcher(logURL, nil)
51-
if err != nil {
52-
klog.Exitf("Failed to create HTTP fetcher: %v", err)
53-
}
54-
if *bearerToken != "" {
55-
src.SetAuthorizationHeader(fmt.Sprintf("Bearer %s", *bearerToken))
50+
var src fsck.Fetcher
51+
52+
if logURL.Scheme == "file" {
53+
src = &client.FileFetcher{
54+
Root: logURL.Path,
55+
}
56+
} else {
57+
httpSrc, err := client.NewHTTPFetcher(logURL, nil)
58+
if err != nil {
59+
klog.Exitf("Failed to create HTTP fetcher: %v", err)
60+
}
61+
if *bearerToken != "" {
62+
httpSrc.SetAuthorizationHeader(fmt.Sprintf("Bearer %s", *bearerToken))
63+
}
64+
src = httpSrc
5665
}
5766
v := verifierFromFlags()
5867
if *origin == "" {

fsck/fsck.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func Check(ctx context.Context, origin string, verifier note.Verifier, f Fetcher
101101
}
102102

103103
// Finally, check that the claimed root hash matches what we calculated.
104-
gotRoot, err := fTree.tree.GetRootHash(nil)
104+
gotRoot, err := fTree.GetRootHash()
105105
switch {
106106
case err != nil:
107107
klog.Exitf("Failed to calculate root: %v", err)
@@ -160,6 +160,13 @@ func (f *fsckTree) AppendBundle(ri layout.RangeInfo, data []byte) error {
160160
return nil
161161
}
162162

163+
func (f *fsckTree) GetRootHash() ([]byte, error) {
164+
if f.tree.End() == 0 {
165+
return rfc6962.DefaultHasher.EmptyRoot(), nil
166+
}
167+
return f.tree.GetRootHash(nil)
168+
}
169+
163170
// visit is used to populate the derived tiles as we consume entries from the log we're checking.
164171
func (f *fsckTree) visit(id compact.NodeID, h []byte) {
165172
// We're only storing the lowest level of hash in the tiles, so early-out in other cases.

0 commit comments

Comments
 (0)