Skip to content

Commit

Permalink
Fix error caused by double-locking repo (#117)
Browse files Browse the repository at this point in the history
* ipfs-10-to-11: Fix error caused by double-locking repo
* ipfs-10-to-11: Move fsrepo.Open to higher in call stack
  • Loading branch information
gammazero authored Jan 15, 2021
1 parent c3f148e commit c07535c
Showing 1 changed file with 27 additions and 35 deletions.
62 changes: 27 additions & 35 deletions ipfs-10-to-11/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/ipfs/fs-repo-migrations/ipfs-10-to-11/_vendor/github.com/ipfs/go-merkledag"

migrate "github.com/ipfs/fs-repo-migrations/go-migrate"
lock "github.com/ipfs/fs-repo-migrations/ipfs-1-to-2/repolock"
mfsr "github.com/ipfs/fs-repo-migrations/mfsr"
log "github.com/ipfs/fs-repo-migrations/stump"
)
Expand All @@ -37,14 +36,8 @@ func (m Migration) Apply(opts migrate.Options) error {
log.Verbose = opts.Verbose
log.Log("applying %s repo migration", m.Versions())

log.VLog("locking repo at %q", opts.Path)
lk, err := lock.Lock2(opts.Path)
err := setupPlugins(opts.Path)
if err != nil {
return err
}
defer lk.Close()

if err = setupPlugins(opts.Path); err != nil {
log.Error("failed to setup plugins", err.Error())
return err
}
Expand All @@ -56,7 +49,18 @@ func (m Migration) Apply(opts migrate.Options) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

if err = transferPins(ctx, opts.Path); err != nil {
if !fsrepo.IsInitialized(opts.Path) {
return fmt.Errorf("ipfs repo %q not initialized", opts.Path)
}

log.VLog(" - opening datastore at %q", opts.Path)
r, err := fsrepo.Open(opts.Path)
if err != nil {
return fmt.Errorf("cannot open datastore: %v", err)
}
defer r.Close()

if err = transferPins(ctx, r); err != nil {
log.Error("failed to transfer pins:", err.Error())
return err
}
Expand All @@ -75,21 +79,27 @@ func (m Migration) Revert(opts migrate.Options) error {
log.Verbose = opts.Verbose
log.Log("reverting migration")

lk, err := lock.Lock2(opts.Path)
err := setupPlugins(opts.Path)
if err != nil {
log.Error("failed to setup plugins", err.Error())
return err
}
defer lk.Close()

if err = setupPlugins(opts.Path); err != nil {
log.Error("failed to setup plugins", err.Error())
return err
if !fsrepo.IsInitialized(opts.Path) {
return fmt.Errorf("ipfs repo %q not initialized", opts.Path)
}

log.VLog(" - opening datastore at %q", opts.Path)
r, err := fsrepo.Open(opts.Path)
if err != nil {
return fmt.Errorf("cannot open datastore: %v", err)
}
defer r.Close()

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

if err = revertPins(ctx, opts.Path); err != nil {
if err = revertPins(ctx, r); err != nil {
return err
}

Expand Down Expand Up @@ -165,20 +175,9 @@ func makeStore(r repo.Repo) (datastore.Datastore, format.DAGService, format.DAGS
return dstore, syncDs, syncInternalDag, nil
}

func transferPins(ctx context.Context, repopath string) error {
func transferPins(ctx context.Context, r repo.Repo) error {
log.Log("> Upgrading pinning to use datastore")

if !fsrepo.IsInitialized(repopath) {
return fmt.Errorf("ipfs repo %q not initialized", repopath)
}

log.VLog(" - opening datastore at %q", repopath)
r, err := fsrepo.Open(repopath)
if err != nil {
return fmt.Errorf("cannot open datastore: %v", err)
}
defer r.Close()

dstore, dserv, internalDag, err := makeStore(r)
if err != nil {
return err
Expand All @@ -195,16 +194,9 @@ func transferPins(ctx context.Context, repopath string) error {
return nil
}

func revertPins(ctx context.Context, repopath string) error {
func revertPins(ctx context.Context, r repo.Repo) error {
log.Log("> Reverting pinning to use ipld storage")

log.VLog(" - opening datastore at %q", repopath)
r, err := fsrepo.Open(repopath)
if err != nil {
return fmt.Errorf("cannot open datastore: %v", err)
}
defer r.Close()

dstore, dserv, internalDag, err := makeStore(r)
if err != nil {
return err
Expand Down

0 comments on commit c07535c

Please sign in to comment.