| 1 | // Package blockstoreutil provides utility functions for Blockstores. |
| 2 | package blockstoreutil |
| 3 | |
| 4 | import ( |
| 5 | "context" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | |
| 9 | bs "github.com/ipfs/boxo/blockstore" |
| 10 | pin "github.com/ipfs/boxo/pinning/pinner" |
| 11 | cid "github.com/ipfs/go-cid" |
| 12 | format "github.com/ipfs/go-ipld-format" |
| 13 | ) |
| 14 | |
| 15 | // RemovedBlock is used to represent the result of removing a block. |
| 16 | // If a block was removed successfully, then the Error will be empty. |
| 17 | // If a block could not be removed, then Error will contain the |
| 18 | // reason the block could not be removed. If the removal was aborted |
| 19 | // due to a fatal error, Hash will be empty, Error will contain the |
| 20 | // reason, and no more results will be sent. |
| 21 | type RemovedBlock struct { |
| 22 | Hash string |
| 23 | Error error |
| 24 | } |
| 25 | |
| 26 | // RmBlocksOpts is used to wrap options for RmBlocks(). |
| 27 | type RmBlocksOpts struct { |
| 28 | Prefix string |
| 29 | Quiet bool |
| 30 | Force bool |
| 31 | } |
| 32 | |
| 33 | // RmBlocks removes the blocks provided in the cids slice. |
| 34 | // It returns a channel where objects of type RemovedBlock are placed, when |
| 35 | // not using the Quiet option. Block removal is asynchronous and will |
| 36 | // skip any pinned blocks. |
| 37 | func RmBlocks(ctx context.Context, blocks bs.GCBlockstore, pins pin.Pinner, cids []cid.Cid, opts RmBlocksOpts) (<-chan any, error) { |
| 38 | // make the channel large enough to hold any result to avoid |
| 39 | // blocking while holding the GCLock |
| 40 | out := make(chan any, len(cids)) |
| 41 | go func() { |
| 42 | defer close(out) |
| 43 | |
| 44 | unlocker := blocks.GCLock(ctx) |
| 45 | defer unlocker.Unlock(ctx) |
| 46 | |
| 47 | stillOkay := FilterPinned(ctx, pins, out, cids) |
| 48 | |
| 49 | for _, c := range stillOkay { |
| 50 | // Kept for backwards compatibility. We may want to |
| 51 | // remove this sometime in the future. |
| 52 | has, err := blocks.Has(ctx, c) |
| 53 | if err != nil { |
| 54 | out <- &RemovedBlock{Hash: c.String(), Error: err} |
| 55 | continue |
| 56 | } |
| 57 | if !has && !opts.Force { |
| 58 | out <- &RemovedBlock{Hash: c.String(), Error: format.ErrNotFound{Cid: c}} |
| 59 | continue |
| 60 | } |
| 61 | |
| 62 | err = blocks.DeleteBlock(ctx, c) |
| 63 | if err != nil { |
| 64 | out <- &RemovedBlock{Hash: c.String(), Error: err} |
| 65 | } else if !opts.Quiet { |
| 66 | out <- &RemovedBlock{Hash: c.String()} |
| 67 | } |
| 68 | } |
| 69 | }() |
| 70 | return out, nil |
| 71 | } |
| 72 | |
| 73 | // FilterPinned takes a slice of Cids and returns it with the pinned Cids |
| 74 | // removed. If a Cid is pinned, it will place RemovedBlock objects in the given |
| 75 | // out channel, with an error which indicates that the Cid is pinned. |
| 76 | // This function is used in RmBlocks to filter out any blocks which are not |
| 77 | // to be removed (because they are pinned). |
| 78 | func FilterPinned(ctx context.Context, pins pin.Pinner, out chan<- any, cids []cid.Cid) []cid.Cid { |
| 79 | stillOkay := make([]cid.Cid, 0, len(cids)) |
| 80 | res, err := pins.CheckIfPinned(ctx, cids...) |
| 81 | if err != nil { |
| 82 | out <- &RemovedBlock{Error: fmt.Errorf("pin check failed: %w", err)} |
| 83 | return nil |
| 84 | } |
| 85 | for _, r := range res { |
| 86 | if !r.Pinned() { |
| 87 | stillOkay = append(stillOkay, r.Key) |
| 88 | } else { |
| 89 | out <- &RemovedBlock{ |
| 90 | Hash: r.Key.String(), |
| 91 | Error: errors.New(r.String()), |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | return stillOkay |
| 96 | } |