Skip to content

Commit 8a5dd4c

Browse files
committed
fix missed error check
1 parent 24c4098 commit 8a5dd4c

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

cmd/ipfs/daemon.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,10 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
476476

477477
// Add any files downloaded by migration.
478478
if keepMigrations {
479-
addMigrations(cctx.Context(), node, fetcher, pinMigrations)
479+
err = addMigrations(cctx.Context(), node, fetcher, pinMigrations)
480+
if err != nil {
481+
fmt.Fprintln(os.Stderr, "Could not add migragion to IPFS:", err)
482+
}
480483
fetcher.Close()
481484
os.RemoveAll(migrations.DownloadDirectory)
482485
migrations.DownloadDirectory = ""

cmd/ipfs/migration.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/ipfs/go-ipfs/core/coreapi"
1717
"github.com/ipfs/go-ipfs/repo/fsrepo/migrations"
1818
"github.com/ipfs/go-ipfs/repo/fsrepo/migrations/ipfsfetcher"
19+
coreiface "github.com/ipfs/interface-go-ipfs-core"
1920
"github.com/ipfs/interface-go-ipfs-core/options"
2021
ipath "github.com/ipfs/interface-go-ipfs-core/path"
2122
"github.com/libp2p/go-libp2p-core/peer"
@@ -174,9 +175,9 @@ func addMigrationPaths(ctx context.Context, node *core.IpfsNode, peerInfo peer.A
174175

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

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

195196
// Add migration files
196197
for _, ipfsPath := range paths {
197-
nd, err := ufs.Get(ctx, ipfsPath)
198+
err = ipfsGet(ctx, ufs, ipfsPath)
198199
if err != nil {
199200
return err
200201
}
202+
}
201203

202-
fnd, ok := nd.(files.File)
203-
if !ok {
204-
return fmt.Errorf("not a file node: %q", ipfsPath)
205-
}
206-
io.Copy(ioutil.Discard, fnd)
207-
nd.Close()
208-
fmt.Printf("Added migration file: %q\n", ipfsPath)
204+
return nil
205+
}
206+
207+
func ipfsGet(ctx context.Context, ufs coreiface.UnixfsAPI, ipfsPath ipath.Path) error {
208+
nd, err := ufs.Get(ctx, ipfsPath)
209+
if err != nil {
210+
return err
209211
}
212+
defer nd.Close()
210213

214+
fnd, ok := nd.(files.File)
215+
if !ok {
216+
return fmt.Errorf("not a file node: %q", ipfsPath)
217+
}
218+
_, err = io.Copy(ioutil.Discard, fnd)
219+
if err != nil {
220+
return fmt.Errorf("could not read migration: %w", err)
221+
}
222+
fmt.Printf("Added migration file: %q\n", ipfsPath)
211223
return nil
212224
}
213225

226+
// parsePeers parses multiaddr strings in the form:
227+
// /<ip-proto>/<ip-addr>/<transport>/<port>/p2p/<node-id>,..
228+
// and parses them into a list of peer.AddrInfo, in no particular order
214229
func parsePeers(migrationPeers string) ([]peer.AddrInfo, error) {
215230
var peers []string
216231
for _, p := range strings.Split(migrationPeers, ",") {

cmd/ipfs/migration_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,15 @@ func TestParsePeers(t *testing.T) {
6868
t.Fatal("expected 2 peers, got:", len(peers))
6969
}
7070

71-
if peers[0].ID.String() != "12D3KooWGC6TvWhfajpgX6wvJHMYvKpDMXPb3ZnCZ6dMoaMtimQ5" {
72-
t.Fatal("wrong peer id:", peers[0].ID)
73-
}
74-
if peers[0].Addrs[0].String() != "/ip4/127.0.0.1/tcp/4001" {
75-
t.Fatal("wrong peer addr")
76-
}
77-
if peers[1].ID.String() != "12D3KooWGC6TvWhfagifX6wvJHMYvKpDMXPb3ZnCZ6dMoaMtimQ5" {
78-
t.Fatal("wrong peer id:", peers[1].ID)
79-
}
80-
if peers[1].Addrs[0].String() != "/ip4/127.0.0.1/udp/4001/quic" {
81-
t.Fatal("wrong peer addr")
71+
for i := range peers {
72+
pid := peers[i].ID.String()
73+
if pid != "12D3KooWGC6TvWhfajpgX6wvJHMYvKpDMXPb3ZnCZ6dMoaMtimQ5" &&
74+
pid != "12D3KooWGC6TvWhfagifX6wvJHMYvKpDMXPb3ZnCZ6dMoaMtimQ5" {
75+
t.Fatal("wrong peer id:", pid)
76+
}
77+
addr := peers[i].Addrs[0].String()
78+
if addr != "/ip4/127.0.0.1/tcp/4001" && addr != "/ip4/127.0.0.1/udp/4001/quic" {
79+
t.Fatal("wrong peer addr:", addr)
80+
}
8281
}
8382
}

0 commit comments

Comments
 (0)