mirror of
https://github.com/ipfs/kubo.git
synced 2025-12-14 13:27:45 +08:00
feat: support optional pin names (#10261)
This commit is contained in:
@@ -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)
|
||||
}
|
||||
@@ -67,7 +67,7 @@ func (api *PinAPI) Ls(ctx context.Context, opts ...caopts.PinLsOption) (<-chan c
|
||||
return nil, fmt.Errorf("invalid type '%s', must be one of {direct, indirect, recursive, all}", settings.Type)
|
||||
}
|
||||
|
||||
return api.pinLsAll(ctx, settings.Type), nil
|
||||
return api.pinLsAll(ctx, settings.Type, settings.Detailed), nil
|
||||
}
|
||||
|
||||
func (api *PinAPI) IsPinned(ctx context.Context, p path.Path, opts ...caopts.PinIsPinnedOption) (string, bool, error) {
|
||||
@@ -231,12 +231,12 @@ func (api *PinAPI) Verify(ctx context.Context) (<-chan coreiface.PinStatus, erro
|
||||
out := make(chan coreiface.PinStatus)
|
||||
go func() {
|
||||
defer close(out)
|
||||
for p := range api.pinning.RecursiveKeys(ctx) {
|
||||
for p := range api.pinning.RecursiveKeys(ctx, false) {
|
||||
var res *pinStatus
|
||||
if p.Err != nil {
|
||||
res = &pinStatus{err: p.Err}
|
||||
} else {
|
||||
res = checkPin(p.C)
|
||||
res = checkPin(p.Pin.Key)
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -271,16 +276,17 @@ func (p *pinInfo) Err() error {
|
||||
//
|
||||
// The caller must keep reading results until the channel is closed to prevent
|
||||
// leaking the goroutine that is fetching pins.
|
||||
func (api *PinAPI) pinLsAll(ctx context.Context, typeStr string) <-chan coreiface.Pin {
|
||||
func (api *PinAPI) pinLsAll(ctx context.Context, typeStr string, detailed bool) <-chan coreiface.Pin {
|
||||
out := make(chan coreiface.Pin, 1)
|
||||
|
||||
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():
|
||||
@@ -296,25 +302,25 @@ func (api *PinAPI) pinLsAll(ctx context.Context, typeStr string) <-chan coreifac
|
||||
var rkeys []cid.Cid
|
||||
var err error
|
||||
if typeStr == "recursive" || typeStr == "all" {
|
||||
for streamedCid := range api.pinning.RecursiveKeys(ctx) {
|
||||
for streamedCid := range api.pinning.RecursiveKeys(ctx, detailed) {
|
||||
if streamedCid.Err != nil {
|
||||
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" {
|
||||
for streamedCid := range api.pinning.DirectKeys(ctx) {
|
||||
for streamedCid := range api.pinning.DirectKeys(ctx, detailed) {
|
||||
if streamedCid.Err != nil {
|
||||
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
|
||||
}
|
||||
@@ -324,21 +330,21 @@ func (api *PinAPI) pinLsAll(ctx context.Context, typeStr string) <-chan coreifac
|
||||
// We need to first visit the direct pins that have priority
|
||||
// without emitting them
|
||||
|
||||
for streamedCid := range api.pinning.DirectKeys(ctx) {
|
||||
for streamedCid := range api.pinning.DirectKeys(ctx, detailed) {
|
||||
if streamedCid.Err != nil {
|
||||
out <- &pinInfo{err: streamedCid.Err}
|
||||
return
|
||||
}
|
||||
emittedSet.Add(streamedCid.C)
|
||||
emittedSet.Add(streamedCid.Pin.Key)
|
||||
}
|
||||
|
||||
for streamedCid := range api.pinning.RecursiveKeys(ctx) {
|
||||
for streamedCid := range api.pinning.RecursiveKeys(ctx, detailed) {
|
||||
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" {
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user