Skip to content

Commit

Permalink
fix missed error check
Browse files Browse the repository at this point in the history
  • Loading branch information
gammazero committed Apr 14, 2021
1 parent 24c4098 commit e3eba81
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
5 changes: 4 additions & 1 deletion cmd/ipfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,10 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment

// Add any files downloaded by migration.
if keepMigrations {
addMigrations(cctx.Context(), node, fetcher, pinMigrations)
err = addMigrations(cctx.Context(), node, fetcher, pinMigrations)
if err != nil {
fmt.Fprintln(os.Stderr, "Could not add migragion to IPFS:", err)
}
fetcher.Close()
os.RemoveAll(migrations.DownloadDirectory)
migrations.DownloadDirectory = ""
Expand Down
35 changes: 25 additions & 10 deletions cmd/ipfs/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/ipfs/go-ipfs/core/coreapi"
"github.com/ipfs/go-ipfs/repo/fsrepo/migrations"
"github.com/ipfs/go-ipfs/repo/fsrepo/migrations/ipfsfetcher"
coreiface "github.com/ipfs/interface-go-ipfs-core"
"github.com/ipfs/interface-go-ipfs-core/options"
ipath "github.com/ipfs/interface-go-ipfs-core/path"
"github.com/libp2p/go-libp2p-core/peer"
Expand Down Expand Up @@ -174,9 +175,9 @@ func addMigrationPaths(ctx context.Context, node *core.IpfsNode, peerInfo peer.A

// Connect to temp node
if err := ipfs.Swarm().Connect(ctx, peerInfo); err != nil {
return fmt.Errorf("cound not connec to migration peer %q: %s", peerInfo.ID, err)
return fmt.Errorf("could not connect to migration peer %q: %s", peerInfo.ID, err)
}
fmt.Printf("conneced to migration peer %q\n", peerInfo)
fmt.Printf("connected to migration peer %q\n", peerInfo)

if pin {
pinApi := ipfs.Pin()
Expand All @@ -194,23 +195,37 @@ func addMigrationPaths(ctx context.Context, node *core.IpfsNode, peerInfo peer.A

// Add migration files
for _, ipfsPath := range paths {
nd, err := ufs.Get(ctx, ipfsPath)
err = ipfsGet(ctx, ufs, ipfsPath)
if err != nil {
return err
}
}

fnd, ok := nd.(files.File)
if !ok {
return fmt.Errorf("not a file node: %q", ipfsPath)
}
io.Copy(ioutil.Discard, fnd)
nd.Close()
fmt.Printf("Added migration file: %q\n", ipfsPath)
return nil
}

func ipfsGet(ctx context.Context, ufs coreiface.UnixfsAPI, ipfsPath ipath.Path) error {
nd, err := ufs.Get(ctx, ipfsPath)
if err != nil {
return err
}
defer nd.Close()

fnd, ok := nd.(files.File)
if !ok {
return fmt.Errorf("not a file node: %q", ipfsPath)
}
_, err = io.Copy(ioutil.Discard, fnd)
if err != nil {
return fmt.Errorf("could not read migration: %w", err)
}
fmt.Printf("Added migration file: %q\n", ipfsPath)
return nil
}

// parsePeers parses multiaddr strings in the form:
// /<ip-proto>/<ip-addr>/<transport>/<port>/p2p/<node-id>,..
// and parses them into a list of peer.AddrInfo, in no particular order
func parsePeers(migrationPeers string) ([]peer.AddrInfo, error) {
var peers []string
for _, p := range strings.Split(migrationPeers, ",") {
Expand Down
23 changes: 12 additions & 11 deletions cmd/ipfs/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,17 @@ func TestParsePeers(t *testing.T) {
t.Fatal("expected 2 peers, got:", len(peers))
}

if peers[0].ID.String() != "12D3KooWGC6TvWhfajpgX6wvJHMYvKpDMXPb3ZnCZ6dMoaMtimQ5" {
t.Fatal("wrong peer id:", peers[0].ID)
}
if peers[0].Addrs[0].String() != "/ip4/127.0.0.1/tcp/4001" {
t.Fatal("wrong peer addr")
}
if peers[1].ID.String() != "12D3KooWGC6TvWhfagifX6wvJHMYvKpDMXPb3ZnCZ6dMoaMtimQ5" {
t.Fatal("wrong peer id:", peers[1].ID)
}
if peers[1].Addrs[0].String() != "/ip4/127.0.0.1/udp/4001/quic" {
t.Fatal("wrong peer addr")
for i := range peers {
pid := peers[i].ID.String()
if pid != "12D3KooWGC6TvWhfajpgX6wvJHMYvKpDMXPb3ZnCZ6dMoaMtimQ5" &&
pid != "12D3KooWGC6TvWhfagifX6wvJHMYvKpDMXPb3ZnCZ6dMoaMtimQ5" {
t.Fatal("wrong peer id:", pid)
}
addr := peers[i].Addrs[0].String()
if addr != "/ip4/127.0.0.1/tcp/4001" && addr != "/ip4/127.0.0.1/udp/4001/quic" {
t.Fatal("wrong peer addr:", addr)
}
}
}

func Test

0 comments on commit e3eba81

Please sign in to comment.