Skip to content

Commit

Permalink
Handle creating the directory
Browse files Browse the repository at this point in the history
  • Loading branch information
firelizzard18 committed Jun 24, 2024
1 parent eeda55c commit a53a90c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
3 changes: 2 additions & 1 deletion pkg/database/keyvalue/block/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"math/big"
"os"
"path/filepath"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -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"))
}
}

Expand Down
8 changes: 8 additions & 0 deletions pkg/database/keyvalue/block/index_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
stdbin "encoding/binary"
stderr "errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"sort"
"sync/atomic"
"unsafe"
Expand All @@ -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)
Expand Down
9 changes: 8 additions & 1 deletion pkg/database/keyvalue/block/index_file_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package block

import (
"bytes"
"io/fs"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit a53a90c

Please sign in to comment.