Skip to content

Commit

Permalink
feat: support for optional pin names
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Jan 3, 2024
1 parent 765cffe commit f98d02c
Show file tree
Hide file tree
Showing 20 changed files with 78 additions and 45 deletions.
8 changes: 7 additions & 1 deletion client/rpc/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type pinRefKeyList struct {
type pin struct {
path path.ImmutablePath
typ string
name string
err error
}

Expand All @@ -37,6 +38,10 @@ func (p pin) Path() path.ImmutablePath {
return p.path
}

func (p pin) Name() string {
return p.name
}

func (p pin) Type() string {
return p.typ
}
Expand All @@ -53,6 +58,7 @@ func (api *PinAPI) Add(ctx context.Context, p path.Path, opts ...caopts.PinAddOp

type pinLsObject struct {
Cid string
Name string
Type string
}

Expand Down Expand Up @@ -102,7 +108,7 @@ func (api *PinAPI) Ls(ctx context.Context, opts ...caopts.PinLsOption) (<-chan i
}

select {
case ch <- pin{typ: out.Type, path: path.FromCid(c)}:
case ch <- pin{typ: out.Type, name: out.Name, path: path.FromCid(c)}:
case <-ctx.Done():
return
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/ipfs/kubo/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func initializeIpnsKeyspace(repoRoot string) error {

// pin recursively because this might already be pinned
// and doing a direct pin would throw an error in that case
err = nd.Pinning.Pin(ctx, emptyDir, true)
err = nd.Pinning.Pin(ctx, emptyDir, true, "")
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion core/commands/dag/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func dagImport(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment
ret.PinErrorMsg = err.Error()
} else if nd, err := blockDecoder.DecodeNode(req.Context, block); err != nil {
ret.PinErrorMsg = err.Error()
} else if err := node.Pinning.Pin(req.Context, nd, true); err != nil {
} else if err := node.Pinning.Pin(req.Context, nd, true, ""); err != nil {
ret.PinErrorMsg = err.Error()
} else if err := node.Pinning.Flush(req.Context); err != nil {
ret.PinErrorMsg = err.Error()
Expand Down
29 changes: 19 additions & 10 deletions core/commands/pin/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ var addPinCmd = &cmds.Command{
Options: []cmds.Option{
cmds.BoolOption(pinRecursiveOptionName, "r", "Recursively pin the object linked to by the specified object(s).").WithDefault(true),
cmds.BoolOption(pinProgressOptionName, "Show progress"),
cmds.StringOption(pinNameOptionName, "n", "An optional name for the pin(s)."),
},
Type: AddPinOutput{},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
Expand All @@ -75,6 +76,7 @@ var addPinCmd = &cmds.Command{

// set recursive flag
recursive, _ := req.Options[pinRecursiveOptionName].(bool)
name, _ := req.Options[pinNameOptionName].(string)
showProgress, _ := req.Options[pinProgressOptionName].(bool)

if err := req.ParseBodyArgs(); err != nil {
Expand All @@ -87,7 +89,7 @@ var addPinCmd = &cmds.Command{
}

if !showProgress {
added, err := pinAddMany(req.Context, api, enc, req.Arguments, recursive)
added, err := pinAddMany(req.Context, api, enc, req.Arguments, recursive, name)
if err != nil {
return err
}
Expand All @@ -105,7 +107,7 @@ var addPinCmd = &cmds.Command{

ch := make(chan pinResult, 1)
go func() {
added, err := pinAddMany(ctx, api, enc, req.Arguments, recursive)
added, err := pinAddMany(ctx, api, enc, req.Arguments, recursive, name)
ch <- pinResult{pins: added, err: err}
}()

Expand Down Expand Up @@ -181,7 +183,7 @@ var addPinCmd = &cmds.Command{
},
}

func pinAddMany(ctx context.Context, api coreiface.CoreAPI, enc cidenc.Encoder, paths []string, recursive bool) ([]string, error) {
func pinAddMany(ctx context.Context, api coreiface.CoreAPI, enc cidenc.Encoder, paths []string, recursive bool, name string) ([]string, error) {
added := make([]string, len(paths))
for i, b := range paths {
p, err := cmdutils.PathOrCidPath(b)
Expand All @@ -194,7 +196,7 @@ func pinAddMany(ctx context.Context, api coreiface.CoreAPI, enc cidenc.Encoder,
return nil, err
}

if err := api.Pin().Add(ctx, rp, options.Pin.Recursive(recursive)); err != nil {
if err := api.Pin().Add(ctx, rp, options.Pin.Recursive(recursive), options.Pin.Name(name)); err != nil {
return nil, err
}
added[i] = enc.Encode(rp.RootCid())
Expand Down Expand Up @@ -356,7 +358,7 @@ Example:
lgcList := map[string]PinLsType{}
if !stream {
emit = func(v PinLsOutputWrapper) error {
lgcList[v.PinLsObject.Cid] = PinLsType{Type: v.PinLsObject.Type}
lgcList[v.PinLsObject.Cid] = PinLsType{Type: v.PinLsObject.Type, Label: v.PinLsObject.Name}
return nil
}
} else {
Expand Down Expand Up @@ -402,17 +404,21 @@ Example:
if stream {
if quiet {
fmt.Fprintf(w, "%s\n", out.PinLsObject.Cid)
} else {
} else if out.PinLsObject.Name == "" {
fmt.Fprintf(w, "%s %s\n", out.PinLsObject.Cid, out.PinLsObject.Type)
} else {
fmt.Fprintf(w, "%s %s %s\n", out.PinLsObject.Cid, out.PinLsObject.Type, out.PinLsObject.Name)
}
return nil
}

for k, v := range out.PinLsList.Keys {
if quiet {
fmt.Fprintf(w, "%s\n", k)
} else {
} else if v.Label == "" {
fmt.Fprintf(w, "%s %s\n", k, v.Type)
} else {
fmt.Fprintf(w, "%s %s %s\n", k, v.Type, v.Label)
}
}

Expand All @@ -436,12 +442,14 @@ type PinLsList struct {

// PinLsType contains the type of a pin
type PinLsType struct {
Type string
Type string
Label string
}

// PinLsObject contains the description of a pin
type PinLsObject struct {
Cid string `json:",omitempty"`
Name string `json:",omitempty"`
Type string `json:",omitempty"`
}

Expand Down Expand Up @@ -532,6 +540,7 @@ func pinLsAll(req *cmds.Request, typeStr string, api coreiface.CoreAPI, emit fun
err = emit(PinLsOutputWrapper{
PinLsObject: PinLsObject{
Type: p.Type(),
Name: p.Name(),
Cid: enc.Encode(p.Path().RootCid()),
},
})
Expand Down Expand Up @@ -753,10 +762,10 @@ func pinVerify(ctx context.Context, n *core.IpfsNode, opts pinVerifyOpts, enc ci
out <- PinVerifyRes{Err: p.Err.Error()}
return
}
pinStatus := checkPin(p.C)
pinStatus := checkPin(p.Pin.Key)
if !pinStatus.Ok || opts.includeOk {
select {
case out <- PinVerifyRes{Cid: enc.Encode(p.C), PinStatus: pinStatus}:
case out <- PinVerifyRes{Cid: enc.Encode(p.Pin.Key), PinStatus: pinStatus}:
case <-ctx.Done():
return
}
Expand Down
2 changes: 1 addition & 1 deletion core/coreapi/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Bloc
}

if settings.Pin {
if err = api.pinning.PinWithMode(ctx, b.Cid(), pin.Recursive); err != nil {
if err = api.pinning.PinWithMode(ctx, b.Cid(), pin.Recursive, ""); err != nil {
return nil, err
}
if err := api.pinning.Flush(ctx); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions core/coreapi/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (adder *pinningAdder) Add(ctx context.Context, nd ipld.Node) error {
return err
}

if err := adder.pinning.PinWithMode(ctx, nd.Cid(), pin.Recursive); err != nil {
if err := adder.pinning.PinWithMode(ctx, nd.Cid(), pin.Recursive, ""); err != nil {
return err
}

Expand All @@ -51,7 +51,7 @@ func (adder *pinningAdder) AddMany(ctx context.Context, nds []ipld.Node) error {
for _, nd := range nds {
c := nd.Cid()
if cids.Visit(c) {
if err := adder.pinning.PinWithMode(ctx, c, pin.Recursive); err != nil {
if err := adder.pinning.PinWithMode(ctx, c, pin.Recursive, ""); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/coreapi/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (api *ObjectAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Obj
}

if options.Pin {
if err := api.pinning.PinWithMode(ctx, dagnode.Cid(), pin.Recursive); err != nil {
if err := api.pinning.PinWithMode(ctx, dagnode.Cid(), pin.Recursive, ""); err != nil {
return path.ImmutablePath{}, err
}

Expand Down
26 changes: 16 additions & 10 deletions core/coreapi/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (api *PinAPI) Add(ctx context.Context, p path.Path, opts ...caopts.PinAddOp

defer api.blockstore.PinLock(ctx).Unlock(ctx)

err = api.pinning.Pin(ctx, dagNode, settings.Recursive)
err = api.pinning.Pin(ctx, dagNode, settings.Recursive, settings.Name)
if err != nil {
return fmt.Errorf("pin: %s", err)
}
Expand Down Expand Up @@ -236,7 +236,7 @@ func (api *PinAPI) Verify(ctx context.Context) (<-chan coreiface.PinStatus, erro
if p.Err != nil {
res = &pinStatus{err: p.Err}
} else {
res = checkPin(p.C)
res = checkPin(p.Pin.Key)
}
select {
case <-ctx.Done():
Expand All @@ -252,6 +252,7 @@ func (api *PinAPI) Verify(ctx context.Context) (<-chan coreiface.PinStatus, erro
type pinInfo struct {
pinType string
path path.ImmutablePath
name string
err error
}

Expand All @@ -263,6 +264,10 @@ func (p *pinInfo) Type() string {
return p.pinType
}

func (p *pinInfo) Name() string {
return p.name
}

func (p *pinInfo) Err() error {
return p.err
}
Expand All @@ -276,11 +281,12 @@ func (api *PinAPI) pinLsAll(ctx context.Context, typeStr string) <-chan coreifac

emittedSet := cid.NewSet()

AddToResultKeys := func(c cid.Cid, typeStr string) error {
AddToResultKeys := func(c cid.Cid, name, typeStr string) error {
if emittedSet.Visit(c) {
select {
case out <- &pinInfo{
pinType: typeStr,
name: name,
path: path.FromCid(c),
}:
case <-ctx.Done():
Expand All @@ -301,11 +307,11 @@ func (api *PinAPI) pinLsAll(ctx context.Context, typeStr string) <-chan coreifac
out <- &pinInfo{err: streamedCid.Err}
return
}
if err = AddToResultKeys(streamedCid.C, "recursive"); err != nil {
if err = AddToResultKeys(streamedCid.Pin.Key, streamedCid.Pin.Name, "recursive"); err != nil {
out <- &pinInfo{err: err}
return
}
rkeys = append(rkeys, streamedCid.C)
rkeys = append(rkeys, streamedCid.Pin.Key)
}
}
if typeStr == "direct" || typeStr == "all" {
Expand All @@ -314,7 +320,7 @@ func (api *PinAPI) pinLsAll(ctx context.Context, typeStr string) <-chan coreifac
out <- &pinInfo{err: streamedCid.Err}
return
}
if err = AddToResultKeys(streamedCid.C, "direct"); err != nil {
if err = AddToResultKeys(streamedCid.Pin.Key, streamedCid.Pin.Name, "direct"); err != nil {
out <- &pinInfo{err: err}
return
}
Expand All @@ -329,16 +335,16 @@ func (api *PinAPI) pinLsAll(ctx context.Context, typeStr string) <-chan coreifac
out <- &pinInfo{err: streamedCid.Err}
return
}
emittedSet.Add(streamedCid.C)
emittedSet.Add(streamedCid.Pin.Key)
}

for streamedCid := range api.pinning.RecursiveKeys(ctx) {
if streamedCid.Err != nil {
out <- &pinInfo{err: streamedCid.Err}
return
}
emittedSet.Add(streamedCid.C)
rkeys = append(rkeys, streamedCid.C)
emittedSet.Add(streamedCid.Pin.Key)
rkeys = append(rkeys, streamedCid.Pin.Key)
}
}
if typeStr == "indirect" || typeStr == "all" {
Expand All @@ -353,7 +359,7 @@ func (api *PinAPI) pinLsAll(ctx context.Context, typeStr string) <-chan coreifac
if emittedSet.Has(c) {
return true // skipped
}
err := AddToResultKeys(c, "indirect")
err := AddToResultKeys(c, "", "indirect")
if err != nil {
out <- &pinInfo{err: err}
return false
Expand Down
9 changes: 9 additions & 0 deletions core/coreiface/options/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "fmt"
// PinAddSettings represent the settings for PinAPI.Add
type PinAddSettings struct {
Recursive bool
Name string
}

// PinLsSettings represent the settings for PinAPI.Ls
Expand Down Expand Up @@ -263,6 +264,14 @@ func (pinOpts) Recursive(recursive bool) PinAddOption {
}
}

// Name is an option for Pin.Add which specifies an optional name to add to the pin.
func (pinOpts) Name(name string) PinAddOption {
return func(settings *PinAddSettings) error {
settings.Name = name
return nil
}
}

// RmRecursive is an option for Pin.Rm which specifies whether to recursively
// unpin the object linked to by the specified object(s). This does not remove
// indirect pins referenced by other recursive pins.
Expand Down
3 changes: 3 additions & 0 deletions core/coreiface/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ type Pin interface {
// Path to the pinned object
Path() path.ImmutablePath

// Name is the name of the pin.
Name() string

// Type of the pin
Type() string

Expand Down
2 changes: 1 addition & 1 deletion core/coreunix/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (adder *Adder) PinRoot(ctx context.Context, root ipld.Node) error {
adder.tempRoot = rnk
}

err = adder.pinning.PinWithMode(ctx, rnk, pin.Recursive)
err = adder.pinning.PinWithMode(ctx, rnk, pin.Recursive, "")
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/kubo-as-a-library/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ go 1.20
replace github.com/ipfs/kubo => ./../../..

require (
github.com/ipfs/boxo v0.16.0
github.com/ipfs/boxo v0.16.1-0.20240102112808-a73b1877dbf8
github.com/ipfs/kubo v0.0.0-00010101000000-000000000000
github.com/libp2p/go-libp2p v0.32.2
github.com/multiformats/go-multiaddr v0.12.0
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/kubo-as-a-library/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,8 @@ github.com/ipfs-shipyard/nopfs/ipfs v0.13.2-0.20231027223058-cde3b5ba964c h1:7Uy
github.com/ipfs-shipyard/nopfs/ipfs v0.13.2-0.20231027223058-cde3b5ba964c/go.mod h1:6EekK/jo+TynwSE/ZOiOJd4eEvRXoavEC3vquKtv4yI=
github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs=
github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0=
github.com/ipfs/boxo v0.16.0 h1:A9dUmef5a+mEFki6kbyG7el5gl65CiUBzrDeZxzTWKY=
github.com/ipfs/boxo v0.16.0/go.mod h1:jAgpNQn7T7BnibUeReXcKU9Ha1xmYNyOlwVEl193ow0=
github.com/ipfs/boxo v0.16.1-0.20240102112808-a73b1877dbf8 h1:J7v8ilV3rJDSFuEIIfQfEwqYBrdoksTenVRdpUzrpiU=
github.com/ipfs/boxo v0.16.1-0.20240102112808-a73b1877dbf8/go.mod h1:jAgpNQn7T7BnibUeReXcKU9Ha1xmYNyOlwVEl193ow0=
github.com/ipfs/go-bitfield v1.1.0 h1:fh7FIo8bSwaJEh6DdTWbCeZ1eqOaOkKFI74SCnsWbGA=
github.com/ipfs/go-bitfield v1.1.0/go.mod h1:paqf1wjq/D2BBmzfTVFlJQ9IlFOZpg422HL0HqsGWHU=
github.com/ipfs/go-block-format v0.0.2/go.mod h1:AWR46JfpcObNfg3ok2JHDUfdiHRgWhJgCQF+KIgOPJY=
Expand Down
2 changes: 1 addition & 1 deletion fuse/ipns/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func InitializeKeyspace(n *core.IpfsNode, key ci.PrivKey) error {

emptyDir := ft.EmptyDirNode()

err := n.Pinning.Pin(ctx, emptyDir, false)
err := n.Pinning.Pin(ctx, emptyDir, false, "")
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit f98d02c

Please sign in to comment.