From 2cfd73c879764224ba4364162cb4cc13126fe83c Mon Sep 17 00:00:00 2001 From: vyzo Date: Mon, 26 Jul 2021 09:46:21 +0300 Subject: [PATCH] manually compute size when badger is being stupid --- blockstore/badger/blockstore.go | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/blockstore/badger/blockstore.go b/blockstore/badger/blockstore.go index 91de96b0e..f8d077760 100644 --- a/blockstore/badger/blockstore.go +++ b/blockstore/badger/blockstore.go @@ -4,6 +4,8 @@ import ( "context" "fmt" "io" + "os" + "path/filepath" "runtime" "sync" @@ -84,7 +86,8 @@ type Blockstore struct { state int viewers sync.WaitGroup - DB *badger.DB + DB *badger.DB + opts Options prefixing bool prefix []byte @@ -110,7 +113,7 @@ func Open(opts Options) (*Blockstore, error) { return nil, fmt.Errorf("failed to open badger blockstore: %w", err) } - bs := &Blockstore{DB: db} + bs := &Blockstore{DB: db, opts: opts} if p := opts.Prefix; p != "" { bs.prefixing = true bs.prefix = []byte(p) @@ -200,7 +203,27 @@ func (b *Blockstore) Size() (int64, error) { defer b.viewers.Done() lsm, vlog := b.DB.Size() - return lsm + vlog, nil + size := lsm + vlog + + if size == 0 { + // badger reports a 0 size on symlinked directories... sigh + dir := b.opts.Dir + entries, err := os.ReadDir(dir) + if err != nil { + return 0, err + } + + for _, e := range entries { + path := filepath.Join(dir, e.Name()) + finfo, err := os.Stat(path) + if err != nil { + return 0, err + } + size += finfo.Size() + } + } + + return size, nil } // View implements blockstore.Viewer, which leverages zero-copy read-only