From c03859c1b53645257028fa71340656c9bf3dbb8b Mon Sep 17 00:00:00 2001 From: vyzo Date: Tue, 27 Jul 2021 10:05:35 +0300 Subject: [PATCH] resolve symlinks when constructing the new db path so that the new path is adjacent to the old path, allowing the user to symlink the db in a different file system. --- blockstore/badger/blockstore.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/blockstore/badger/blockstore.go b/blockstore/badger/blockstore.go index 1ef622f39..83c5012c1 100644 --- a/blockstore/badger/blockstore.go +++ b/blockstore/badger/blockstore.go @@ -247,7 +247,7 @@ func (b *Blockstore) movingGC() error { b.moveCond.Broadcast() b.moveMx.Unlock() - path := fmt.Sprintf("%s.%d", b.opts.Dir, time.Now().Unix()) + var path string defer func() { b.lockMove() @@ -276,6 +276,23 @@ func (b *Blockstore) movingGC() error { } }() + // we resolve symlinks to create the new path in the adjacent to the old path. + // this allows the user to symlink the db directory into a separate filesystem. + basePath := b.opts.Dir + linkPath, err := filepath.EvalSymlinks(basePath) + if err != nil { + return fmt.Errorf("error resolving symlink %s: %w", basePath, err) + } + + if basePath == linkPath { + path = basePath + } else { + name := filepath.Base(basePath) + dir := filepath.Dir(linkPath) + path = filepath.Join(dir, name) + } + path = fmt.Sprintf("%s.%d", path, time.Now().Unix()) + log.Infof("moving blockstore from %s to %s", b.opts.Dir, path) opts := b.opts