Skip to content

Commit

Permalink
fix: add leveldb recover ability (#352)
Browse files Browse the repository at this point in the history
While fixing #349, the leveldb supports a Recover() method in case the
manifest is broken. I validated that the Recover would have recovered
the manifest file using the db file (that still existed). Add a test
that validates if the manifest is truncated, it can be recovered.

Signed-off-by: Mike Marchetti <[email protected]>
  • Loading branch information
mfmarche authored Aug 4, 2022
1 parent 768b63a commit 1b070ee
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
5 changes: 5 additions & 0 deletions client/leveldbstore/leveldbstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"

"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/errors"
"github.com/syndtr/goleveldb/leveldb/storage"

tuf_client "github.com/theupdateframework/go-tuf/client"
Expand All @@ -16,6 +17,10 @@ func FileLocalStore(path string) (tuf_client.LocalStore, error) {
}

db, err := leveldb.Open(fd, nil)
if err != nil && errors.IsCorrupted(err) {
db, err = leveldb.Recover(fd, nil)
}

return &fileLocalStore{fd: fd, db: db}, err
}

Expand Down
40 changes: 40 additions & 0 deletions client/leveldbstore/leveldbstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

. "gopkg.in/check.v1"
"os"
)

type LocalStoreSuite struct{}
Expand Down Expand Up @@ -81,3 +82,42 @@ func (LocalStoreSuite) TestDeleteMeta(c *C) {
c.Fatalf("Metadata is not deleted!")
}
}

func (LocalStoreSuite) TestCorruptManifest(c *C) {
tmp := c.MkDir()
path := filepath.Join(tmp, "tuf.db")

store, err := FileLocalStore(path)
c.Assert(err, IsNil)

// now break the manifest file
err = os.Truncate(filepath.Join(path, "MANIFEST-000000"), 1)
c.Assert(err, IsNil)
err = store.Close()
c.Assert(err, IsNil)

store, err = FileLocalStore(path)
c.Assert(err, IsNil)

type meta map[string]json.RawMessage

assertGet := func(expected meta) {
actual, err := store.GetMeta()
c.Assert(err, IsNil)
c.Assert(meta(actual), DeepEquals, expected)
}

// initial GetMeta should return empty meta
assertGet(meta{})

// SetMeta should persist
rootJSON := []byte(`{"_type":"Root"}`)
c.Assert(store.SetMeta("root.json", rootJSON), IsNil)
assertGet(meta{"root.json": rootJSON})

store.DeleteMeta("root.json")
m, _ := store.GetMeta()
if _, ok := m["root.json"]; ok {
c.Fatalf("Metadata is not deleted!")
}
}

0 comments on commit 1b070ee

Please sign in to comment.