remove.go 3.36 KB
Newer Older
1 2
// Package blockstoreutil provides utility functions for Blockstores.
package blockstoreutil
3 4 5 6 7

import (
	"fmt"
	"io"

8
	cid "gx/ipfs/QmNw61A6sJoXMeP37mJRtQZdNhj5e3FdjoTN3v4FyE96Gk/go-cid"
Łukasz Magiera's avatar
Łukasz Magiera committed
9
	ds "gx/ipfs/QmSiN66ybp5udnQnvhb6euiWiiQWdGvwMhAWa95cC1DTCV/go-datastore"
10 11 12

	bs "github.com/ipfs/go-ipfs/blocks/blockstore"
	"github.com/ipfs/go-ipfs/pin"
13 14
)

15 16 17 18 19 20
// RemovedBlock is used to respresent the result of removing a block.
// If a block was removed successfully than the Error string will be
// empty.  If a block could not be removed than Error will contain the
// reason the block could not be removed.  If the removal was aborted
// due to a fatal error Hash will be be empty, Error will contain the
// reason, and no more results will be sent.
21 22 23 24 25
type RemovedBlock struct {
	Hash  string `json:",omitempty"`
	Error string `json:",omitempty"`
}

26
// RmBlocksOpts is used to wrap options for RmBlocks().
27 28 29 30 31 32
type RmBlocksOpts struct {
	Prefix string
	Quiet  bool
	Force  bool
}

33 34 35 36
// RmBlocks removes the blocks provided in the cids slice.
// It returns a channel where objects of type RemovedBlock are placed, when
// not using the Quiet option. Block removal is asynchronous and will
// skip any pinned blocks.
37 38
func RmBlocks(blocks bs.GCBlockstore, pins pin.Pinner, cids []*cid.Cid, opts RmBlocksOpts) (<-chan interface{}, error) {
	// make the channel large enough to hold any result to avoid
39
	// blocking while holding the GCLock
40
	out := make(chan interface{}, len(cids))
41 42 43 44 45 46
	go func() {
		defer close(out)

		unlocker := blocks.GCLock()
		defer unlocker.Unlock()

47
		stillOkay := FilterPinned(pins, out, cids)
48 49

		for _, c := range stillOkay {
50
			err := blocks.DeleteBlock(c)
51 52 53 54 55 56 57 58 59
			if err != nil && opts.Force && (err == bs.ErrNotFound || err == ds.ErrNotFound) {
				// ignore non-existent blocks
			} else if err != nil {
				out <- &RemovedBlock{Hash: c.String(), Error: err.Error()}
			} else if !opts.Quiet {
				out <- &RemovedBlock{Hash: c.String()}
			}
		}
	}()
60
	return out, nil
61 62
}

63 64 65 66 67
// FilterPinned takes a slice of Cids and returns it with the pinned Cids
// removed. If a Cid is pinned, it will place RemovedBlock objects in the given
// out channel, with an error which indicates that the Cid is pinned.
// This function is used in RmBlocks to filter out any blocks which are not
// to be removed (because they are pinned).
68
func FilterPinned(pins pin.Pinner, out chan<- interface{}, cids []*cid.Cid) []*cid.Cid {
69 70 71
	stillOkay := make([]*cid.Cid, 0, len(cids))
	res, err := pins.CheckIfPinned(cids...)
	if err != nil {
72 73
		out <- &RemovedBlock{Error: fmt.Sprintf("pin check failed: %s", err)}
		return nil
74 75 76 77 78 79 80 81 82 83 84
	}
	for _, r := range res {
		if !r.Pinned() {
			stillOkay = append(stillOkay, r.Key)
		} else {
			out <- &RemovedBlock{
				Hash:  r.Key.String(),
				Error: r.String(),
			}
		}
	}
85
	return stillOkay
86 87
}

88 89 90
// ProcRmOutput takes the channel returned by RmBlocks and writes
// to stdout/stderr according to the RemovedBlock objects received in
// that channel.
91
func ProcRmOutput(in <-chan interface{}, sout io.Writer, serr io.Writer) error {
92 93 94 95
	someFailed := false
	for res := range in {
		r := res.(*RemovedBlock)
		if r.Hash == "" && r.Error != "" {
96
			return fmt.Errorf("aborted: %s", r.Error)
97 98 99 100 101 102 103 104
		} else if r.Error != "" {
			someFailed = true
			fmt.Fprintf(serr, "cannot remove %s: %s\n", r.Hash, r.Error)
		} else {
			fmt.Fprintf(sout, "removed %s\n", r.Hash)
		}
	}
	if someFailed {
105
		return fmt.Errorf("some blocks not removed")
106 107 108
	}
	return nil
}