From 0740274b7c17dfb1857cdeb92494852c84dd4b22 Mon Sep 17 00:00:00 2001 From: vyzo Date: Wed, 28 Jul 2021 11:56:23 +0300 Subject: [PATCH] make relative links when the canonical and new db paths are in the same directory --- blockstore/badger/blockstore.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/blockstore/badger/blockstore.go b/blockstore/badger/blockstore.go index 49951db6e..3d1862283 100644 --- a/blockstore/badger/blockstore.go +++ b/blockstore/badger/blockstore.go @@ -355,7 +355,7 @@ func (b *Blockstore) movingGC() error { panic(fmt.Errorf("error renaming old badger db dir from %s to %s: %w; USER ACTION REQUIRED", dbPath, backupPath, err)) //nolint } - if err = os.Symlink(newPath, dbPath); err != nil { + if err = b.symlink(newPath, dbPath); err != nil { // same here; the db path is pointing to the void. panic and let the user fix. panic(fmt.Errorf("error symlinking new badger db dir from %s to %s: %w; USER ACTION REQUIRED", newPath, dbPath, err)) //nolint } @@ -367,6 +367,18 @@ func (b *Blockstore) movingGC() error { return nil } +// symlink creates a symlink from path to linkPath; the link is relative if the two are +// in the same directory +func (b *Blockstore) symlink(path, linkTo string) error { + pathDir := filepath.Dir(path) + linkDir := filepath.Dir(linkTo) + if pathDir == linkDir { + path = filepath.Base(path) + } + + return os.Symlink(path, linkTo) +} + // doCopy copies a badger blockstore to another, with an optional filter; if the filter // is not nil, then only cids that satisfy the filter will be copied. func (b *Blockstore) doCopy(from, to *badger.DB) error {