@@ -174,20 +174,20 @@ func (p *pinner) SetAutosync(auto bool) bool {
174
174
}
175
175
176
176
// Pin the given node, optionally recursive
177
- func (p * pinner ) Pin (ctx context.Context , node ipld.Node , recurse bool ) error {
177
+ func (p * pinner ) Pin (ctx context.Context , node ipld.Node , recurse bool , name string ) error {
178
178
err := p .dserv .Add (ctx , node )
179
179
if err != nil {
180
180
return err
181
181
}
182
182
183
183
if recurse {
184
- return p .doPinRecursive (ctx , node .Cid (), true )
184
+ return p .doPinRecursive (ctx , node .Cid (), true , name )
185
185
} else {
186
- return p .doPinDirect (ctx , node .Cid ())
186
+ return p .doPinDirect (ctx , node .Cid (), name )
187
187
}
188
188
}
189
189
190
- func (p * pinner ) doPinRecursive (ctx context.Context , c cid.Cid , fetch bool ) error {
190
+ func (p * pinner ) doPinRecursive (ctx context.Context , c cid.Cid , fetch bool , name string ) error {
191
191
cidKey := c .KeyString ()
192
192
193
193
p .lock .Lock ()
@@ -243,14 +243,14 @@ func (p *pinner) doPinRecursive(ctx context.Context, c cid.Cid, fetch bool) erro
243
243
}
244
244
}
245
245
246
- _ , err = p .addPin (ctx , c , ipfspinner .Recursive , "" )
246
+ _ , err = p .addPin (ctx , c , ipfspinner .Recursive , name )
247
247
if err != nil {
248
248
return err
249
249
}
250
250
return p .flushPins (ctx , false )
251
251
}
252
252
253
- func (p * pinner ) doPinDirect (ctx context.Context , c cid.Cid ) error {
253
+ func (p * pinner ) doPinDirect (ctx context.Context , c cid.Cid , name string ) error {
254
254
cidKey := c .KeyString ()
255
255
256
256
p .lock .Lock ()
@@ -264,7 +264,7 @@ func (p *pinner) doPinDirect(ctx context.Context, c cid.Cid) error {
264
264
return fmt .Errorf ("%s already pinned recursively" , c .String ())
265
265
}
266
266
267
- _ , err = p .addPin (ctx , c , ipfspinner .Direct , "" )
267
+ _ , err = p .addPin (ctx , c , ipfspinner .Direct , name )
268
268
if err != nil {
269
269
return err
270
270
}
@@ -665,17 +665,17 @@ func (p *pinner) loadPin(ctx context.Context, pid string) (*pin, error) {
665
665
}
666
666
667
667
// DirectKeys returns a slice containing the directly pinned keys
668
- func (p * pinner ) DirectKeys (ctx context.Context ) <- chan ipfspinner.StreamedCid {
669
- return p .streamIndex (ctx , p .cidDIndex )
668
+ func (p * pinner ) DirectKeys (ctx context.Context , detailed bool ) <- chan ipfspinner.StreamedPin {
669
+ return p .streamIndex (ctx , p .cidDIndex , detailed )
670
670
}
671
671
672
672
// RecursiveKeys returns a slice containing the recursively pinned keys
673
- func (p * pinner ) RecursiveKeys (ctx context.Context ) <- chan ipfspinner.StreamedCid {
674
- return p .streamIndex (ctx , p .cidRIndex )
673
+ func (p * pinner ) RecursiveKeys (ctx context.Context , detailed bool ) <- chan ipfspinner.StreamedPin {
674
+ return p .streamIndex (ctx , p .cidRIndex , detailed )
675
675
}
676
676
677
- func (p * pinner ) streamIndex (ctx context.Context , index dsindex.Indexer ) <- chan ipfspinner.StreamedCid {
678
- out := make (chan ipfspinner.StreamedCid )
677
+ func (p * pinner ) streamIndex (ctx context.Context , index dsindex.Indexer , detailed bool ) <- chan ipfspinner.StreamedPin {
678
+ out := make (chan ipfspinner.StreamedPin )
679
679
680
680
go func () {
681
681
defer close (out )
@@ -688,21 +688,37 @@ func (p *pinner) streamIndex(ctx context.Context, index dsindex.Indexer) <-chan
688
688
err := index .ForEach (ctx , "" , func (key , value string ) bool {
689
689
c , err := cid .Cast ([]byte (key ))
690
690
if err != nil {
691
- out <- ipfspinner.StreamedCid {Err : err }
691
+ out <- ipfspinner.StreamedPin {Err : err }
692
692
return false
693
693
}
694
+
695
+ var pin ipfspinner.Pinned
696
+ if detailed {
697
+ pp , err := p .loadPin (ctx , value )
698
+ if err != nil {
699
+ out <- ipfspinner.StreamedPin {Err : err }
700
+ return false
701
+ }
702
+
703
+ pin .Key = pp .Cid
704
+ pin .Mode = pp .Mode
705
+ pin .Name = pp .Name
706
+ } else {
707
+ pin .Key = c
708
+ }
709
+
694
710
if ! cidSet .Has (c ) {
695
711
select {
696
712
case <- ctx .Done ():
697
713
return false
698
- case out <- ipfspinner.StreamedCid { C : c }:
714
+ case out <- ipfspinner.StreamedPin { Pin : pin }:
699
715
}
700
716
cidSet .Add (c )
701
717
}
702
718
return true
703
719
})
704
720
if err != nil {
705
- out <- ipfspinner.StreamedCid {Err : err }
721
+ out <- ipfspinner.StreamedPin {Err : err }
706
722
}
707
723
}()
708
724
@@ -711,8 +727,8 @@ func (p *pinner) streamIndex(ctx context.Context, index dsindex.Indexer) <-chan
711
727
712
728
// InternalPins returns all cids kept pinned for the internal state of the
713
729
// pinner
714
- func (p * pinner ) InternalPins (ctx context.Context ) <- chan ipfspinner.StreamedCid {
715
- c := make (chan ipfspinner.StreamedCid )
730
+ func (p * pinner ) InternalPins (ctx context.Context , detailed bool ) <- chan ipfspinner.StreamedPin {
731
+ c := make (chan ipfspinner.StreamedPin )
716
732
close (c )
717
733
return c
718
734
}
@@ -725,11 +741,11 @@ func (p *pinner) Update(ctx context.Context, from, to cid.Cid, unpin bool) error
725
741
p .lock .Lock ()
726
742
defer p .lock .Unlock ()
727
743
728
- found , err := p .cidRIndex .HasAny (ctx , from .KeyString ())
744
+ fromValues , err := p .cidRIndex .Search (ctx , from .KeyString ())
729
745
if err != nil {
730
746
return err
731
747
}
732
- if ! found {
748
+ if len ( fromValues ) != 1 {
733
749
return errors .New ("'from' cid was not recursively pinned already" )
734
750
}
735
751
@@ -739,11 +755,11 @@ func (p *pinner) Update(ctx context.Context, from, to cid.Cid, unpin bool) error
739
755
}
740
756
741
757
// Check if the `to` cid is already recursively pinned
742
- found , err = p .cidRIndex .HasAny (ctx , to .KeyString ())
758
+ toFound , err : = p .cidRIndex .HasAny (ctx , to .KeyString ())
743
759
if err != nil {
744
760
return err
745
761
}
746
- if found {
762
+ if toFound {
747
763
return errors .New ("'to' cid was already recursively pinned" )
748
764
}
749
765
@@ -756,7 +772,13 @@ func (p *pinner) Update(ctx context.Context, from, to cid.Cid, unpin bool) error
756
772
return err
757
773
}
758
774
759
- _ , err = p .addPin (ctx , to , ipfspinner .Recursive , "" )
775
+ // Get pin information so that we can keep the name.
776
+ pin , err := p .loadPin (ctx , fromValues [0 ])
777
+ if err != nil {
778
+ return err
779
+ }
780
+
781
+ _ , err = p .addPin (ctx , to , ipfspinner .Recursive , pin .Name )
760
782
if err != nil {
761
783
return err
762
784
}
@@ -809,13 +831,13 @@ func (p *pinner) Flush(ctx context.Context) error {
809
831
810
832
// PinWithMode allows the user to have fine grained control over pin
811
833
// counts
812
- func (p * pinner ) PinWithMode (ctx context.Context , c cid.Cid , mode ipfspinner.Mode ) error {
834
+ func (p * pinner ) PinWithMode (ctx context.Context , c cid.Cid , mode ipfspinner.Mode , name string ) error {
813
835
// TODO: remove his to support multiple pins per CID
814
836
switch mode {
815
837
case ipfspinner .Recursive :
816
- return p .doPinRecursive (ctx , c , false )
838
+ return p .doPinRecursive (ctx , c , false , name )
817
839
case ipfspinner .Direct :
818
- return p .doPinDirect (ctx , c )
840
+ return p .doPinDirect (ctx , c , name )
819
841
default :
820
842
return errors .New ("unrecognized pin mode" )
821
843
}
0 commit comments