diff --git a/pkg/database/keyvalue/block/database_test.go b/pkg/database/keyvalue/block/database_test.go index 996ff17e7..d2b6722c6 100644 --- a/pkg/database/keyvalue/block/database_test.go +++ b/pkg/database/keyvalue/block/database_test.go @@ -11,6 +11,7 @@ import ( "fmt" "math/big" "os" + "path/filepath" "strings" "testing" "time" @@ -92,7 +93,7 @@ func TestDelete(t *testing.T) { func newOpener(t testing.TB) kvtest.Opener { path := t.TempDir() return func() (keyvalue.Beginner, error) { - return Open(path) + return Open(filepath.Join(path, "test.db")) } } diff --git a/pkg/database/keyvalue/block/index_file.go b/pkg/database/keyvalue/block/index_file.go index 355dcca2c..3c8072bcc 100644 --- a/pkg/database/keyvalue/block/index_file.go +++ b/pkg/database/keyvalue/block/index_file.go @@ -10,7 +10,9 @@ import ( stdbin "encoding/binary" stderr "errors" "fmt" + "io/fs" "os" + "path/filepath" "sort" "sync/atomic" "unsafe" @@ -28,6 +30,12 @@ type indexFile struct { } func newIndexFile(name string, level int) (_ *indexFile, err error) { + // Ensure the directory exists + err = os.Mkdir(filepath.Dir(name), 0700) + if err != nil && !errors.Is(err, fs.ErrExist) { + return nil, err + } + f := new(indexFile) f.level = level f.file, err = ioutil.OpenMappedFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600) diff --git a/pkg/database/keyvalue/block/index_file_tree.go b/pkg/database/keyvalue/block/index_file_tree.go index ccaded1c4..ddaa74ea8 100644 --- a/pkg/database/keyvalue/block/index_file_tree.go +++ b/pkg/database/keyvalue/block/index_file_tree.go @@ -8,6 +8,7 @@ package block import ( "bytes" + "io/fs" "os" "path/filepath" "regexp" @@ -53,7 +54,13 @@ func openIndexFileTree(cfg *config) (_ *indexFileTree, err error) { defer closeIfError(&err, s) entries, err := os.ReadDir(cfg.path) - if err != nil { + switch { + case err == nil, + errors.Is(err, fs.ErrNotExist): + // Directory exists, or doesn't + + default: + // Some other error return nil, err }