Skip to content

Commit c7eddab

Browse files
Merge pull request #4451 from ipfs/fix/better-pinning
Pinning fixes
2 parents bee2ba3 + 9a335ce commit c7eddab

File tree

6 files changed

+20
-43
lines changed

6 files changed

+20
-43
lines changed

assets/assets.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,6 @@ func addAssetList(nd *core.IpfsNode, l []string) (*cid.Cid, error) {
8080
return nil, err
8181
}
8282

83-
dcid, err := nd.DAG.Add(dir)
84-
if err != nil {
85-
return nil, fmt.Errorf("assets: DAG.Add(dir) failed: %s", err)
86-
}
87-
8883
if err := nd.Pinning.Pin(nd.Context(), dir, true); err != nil {
8984
return nil, fmt.Errorf("assets: Pinning on init-docu failed: %s", err)
9085
}
@@ -93,5 +88,5 @@ func addAssetList(nd *core.IpfsNode, l []string) (*cid.Cid, error) {
9388
return nil, fmt.Errorf("assets: Pinning flush failed: %s", err)
9489
}
9590

96-
return dcid, nil
91+
return dir.Cid(), nil
9792
}

cmd/ipfs/init.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,5 +260,5 @@ func initializeIpnsKeyspace(repoRoot string) error {
260260
return err
261261
}
262262

263-
return namesys.InitializeKeyspace(ctx, nd.DAG, nd.Namesys, nd.Pinning, nd.PrivateKey)
263+
return namesys.InitializeKeyspace(ctx, nd.Namesys, nd.Pinning, nd.PrivateKey)
264264
}

core/corerepo/pinning.go

+7-19
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,17 @@ import (
2222
uio "github.com/ipfs/go-ipfs/unixfs/io"
2323

2424
cid "gx/ipfs/QmNp85zy9RLrQ5oQD4hPyS39ezrrXpcaa7R4Y9kxdWQLLQ/go-cid"
25-
node "gx/ipfs/QmPN7cwmpcc4DWXb4KTB9dNAJgjuPY69h3npsMfhRrQL9c/go-ipld-format"
2625
)
2726

2827
func Pin(n *core.IpfsNode, ctx context.Context, paths []string, recursive bool) ([]*cid.Cid, error) {
29-
dagnodes := make([]node.Node, 0)
28+
out := make([]*cid.Cid, len(paths))
3029

3130
r := &path.Resolver{
3231
DAG: n.DAG,
3332
ResolveOnce: uio.ResolveUnixfsOnce,
3433
}
3534

36-
for _, fpath := range paths {
35+
for i, fpath := range paths {
3736
p, err := path.ParsePath(fpath)
3837
if err != nil {
3938
return nil, err
@@ -43,20 +42,11 @@ func Pin(n *core.IpfsNode, ctx context.Context, paths []string, recursive bool)
4342
if err != nil {
4443
return nil, fmt.Errorf("pin: %s", err)
4544
}
46-
dagnodes = append(dagnodes, dagnode)
47-
}
48-
49-
var out []*cid.Cid
50-
for _, dagnode := range dagnodes {
51-
c := dagnode.Cid()
52-
53-
ctx, cancel := context.WithCancel(ctx)
54-
defer cancel()
55-
err := n.Pinning.Pin(ctx, dagnode, recursive)
45+
err = n.Pinning.Pin(ctx, dagnode, recursive)
5646
if err != nil {
5747
return nil, fmt.Errorf("pin: %s", err)
5848
}
59-
out = append(out, c)
49+
out[i] = dagnode.Cid()
6050
}
6151

6252
err := n.Pinning.Flush()
@@ -68,14 +58,14 @@ func Pin(n *core.IpfsNode, ctx context.Context, paths []string, recursive bool)
6858
}
6959

7060
func Unpin(n *core.IpfsNode, ctx context.Context, paths []string, recursive bool) ([]*cid.Cid, error) {
71-
var unpinned []*cid.Cid
61+
unpinned := make([]*cid.Cid, len(paths))
7262

7363
r := &path.Resolver{
7464
DAG: n.DAG,
7565
ResolveOnce: uio.ResolveUnixfsOnce,
7666
}
7767

78-
for _, p := range paths {
68+
for i, p := range paths {
7969
p, err := path.ParsePath(p)
8070
if err != nil {
8171
return nil, err
@@ -86,13 +76,11 @@ func Unpin(n *core.IpfsNode, ctx context.Context, paths []string, recursive bool
8676
return nil, err
8777
}
8878

89-
ctx, cancel := context.WithCancel(ctx)
90-
defer cancel()
9179
err = n.Pinning.Unpin(ctx, k, recursive)
9280
if err != nil {
9381
return nil, err
9482
}
95-
unpinned = append(unpinned, k)
83+
unpinned[i] = k
9684
}
9785

9886
err := n.Pinning.Flush()

fuse/ipns/common.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,12 @@ import (
1313
// InitializeKeyspace sets the ipns record for the given key to
1414
// point to an empty directory.
1515
func InitializeKeyspace(n *core.IpfsNode, key ci.PrivKey) error {
16-
emptyDir := ft.EmptyDirNode()
17-
nodek, err := n.DAG.Add(emptyDir)
18-
if err != nil {
19-
return err
20-
}
21-
2216
ctx, cancel := context.WithCancel(n.Context())
2317
defer cancel()
2418

25-
err = n.Pinning.Pin(ctx, emptyDir, false)
19+
emptyDir := ft.EmptyDirNode()
20+
21+
err := n.Pinning.Pin(ctx, emptyDir, false)
2622
if err != nil {
2723
return err
2824
}
@@ -34,5 +30,5 @@ func InitializeKeyspace(n *core.IpfsNode, key ci.PrivKey) error {
3430

3531
pub := nsys.NewRoutingPublisher(n.Routing, n.Repo.Datastore())
3632

37-
return pub.Publish(ctx, key, path.FromCid(nodek))
33+
return pub.Publish(ctx, key, path.FromCid(emptyDir.Cid()))
3834
}

namesys/publisher.go

+3-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"fmt"
88
"time"
99

10-
dag "github.com/ipfs/go-ipfs/merkledag"
1110
pb "github.com/ipfs/go-ipfs/namesys/pb"
1211
path "github.com/ipfs/go-ipfs/path"
1312
pin "github.com/ipfs/go-ipfs/pin"
@@ -321,16 +320,12 @@ func ValidateIpnsRecord(k string, val []byte) error {
321320
// InitializeKeyspace sets the ipns record for the given key to
322321
// point to an empty directory.
323322
// TODO: this doesnt feel like it belongs here
324-
func InitializeKeyspace(ctx context.Context, ds dag.DAGService, pub Publisher, pins pin.Pinner, key ci.PrivKey) error {
323+
func InitializeKeyspace(ctx context.Context, pub Publisher, pins pin.Pinner, key ci.PrivKey) error {
325324
emptyDir := ft.EmptyDirNode()
326-
nodek, err := ds.Add(emptyDir)
327-
if err != nil {
328-
return err
329-
}
330325

331326
// pin recursively because this might already be pinned
332327
// and doing a direct pin would throw an error in that case
333-
err = pins.Pin(ctx, emptyDir, true)
328+
err := pins.Pin(ctx, emptyDir, true)
334329
if err != nil {
335330
return err
336331
}
@@ -340,7 +335,7 @@ func InitializeKeyspace(ctx context.Context, ds dag.DAGService, pub Publisher, p
340335
return err
341336
}
342337

343-
return pub.Publish(ctx, key, path.FromCid(nodek))
338+
return pub.Publish(ctx, key, path.FromCid(emptyDir.Cid()))
344339
}
345340

346341
func IpnsKeysForID(id peer.ID) (name, ipns string) {

pin/pin.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,10 @@ func NewPinner(dstore ds.Datastore, serv, internal mdag.DAGService) Pinner {
172172
func (p *pinner) Pin(ctx context.Context, node node.Node, recurse bool) error {
173173
p.lock.Lock()
174174
defer p.lock.Unlock()
175-
c := node.Cid()
175+
c, err := p.dserv.Add(node)
176+
if err != nil {
177+
return err
178+
}
176179

177180
if recurse {
178181
if p.recursePin.Has(c) {

0 commit comments

Comments
 (0)