From 60197820a48893c20098ec1b8930b4cf2c7f119d Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Sat, 5 May 2018 10:44:40 -0700 Subject: [PATCH] recover datastore on corruption (but not if we're opening it read-only) fixes ipfs/go-ipfs#4981 --- datastore.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/datastore.go b/datastore.go index 99d575f..9b0eb1f 100644 --- a/datastore.go +++ b/datastore.go @@ -8,6 +8,7 @@ import ( dsq "github.com/ipfs/go-datastore/query" "github.com/jbenet/goprocess" "github.com/syndtr/goleveldb/leveldb" + "github.com/syndtr/goleveldb/leveldb/errors" "github.com/syndtr/goleveldb/leveldb/opt" "github.com/syndtr/goleveldb/leveldb/storage" "github.com/syndtr/goleveldb/leveldb/util" @@ -38,6 +39,9 @@ func NewDatastore(path string, opts *Options) (*datastore, error) { db, err = leveldb.Open(storage.NewMemStorage(), &nopts) } else { db, err = leveldb.OpenFile(path, &nopts) + if errors.IsCorrupted(err) && !nopts.GetReadOnly() { + db, err = leveldb.RecoverFile(path, &nopts) + } } if err != nil {