Skip to content

Commit

Permalink
"filestore rm": Do not delete blocks that are shared with another pin…
Browse files Browse the repository at this point in the history
…ned node.

License: MIT
Signed-off-by: Kevin Atkinson <[email protected]>
  • Loading branch information
kevina committed May 16, 2016
1 parent 283746b commit dcdc14e
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions filestore/util/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@ type DeleteOpts struct {
IgnorePins bool
}

type delInfo int

const (
DirectlySpecified delInfo = 1
IndirectlySpecified delInfo = 2
)

func Delete(req cmds.Request, out io.Writer, node *core.IpfsNode, fs *Datastore, opts DeleteOpts, keyList ...k.Key) error {
keys := make(map[k.Key]struct{})
keys := make(map[k.Key]delInfo)
for _, k := range keyList {
keys[k] = struct{}{}
keys[k] = DirectlySpecified
}

//
Expand Down Expand Up @@ -64,24 +71,30 @@ func Delete(req cmds.Request, out io.Writer, node *core.IpfsNode, fs *Datastore,
pinned := make(map[k.Key]pin.PinMode)
if !opts.IgnorePins {
walkPins(node.Pinning, fs, node.Blockstore, func(key k.Key, mode pin.PinMode) bool {
_, ok := keys[key]
dm, ok := keys[key]
if !ok {
// Hack to make sure mangled hashes are unpinned
// (see issue #2601)
_, ok = keys[k.KeyFromDsKey(key.DsKey())]
dm, ok = keys[k.KeyFromDsKey(key.DsKey())]
}
if ok {
if mode == pin.NotPinned {
if mode == pin.NotPinned && dm == DirectlySpecified {
// an indirect pin
fmt.Fprintf(out, "%s: indirectly pinned\n", key)
errors = true
if !opts.Force {
errors = true
}
return true
} else if mode == pin.NotPinned && dm == IndirectlySpecified {
fmt.Fprintf(out, "skipping indirectly pinned block: %s\n", key)
delete(keys, key)
return true
} else {
pinned[key] = mode
return false
}
} else {
if opts.Force {
if opts.Force && opts.Direct {
// do not recurse and thus do not check indirect pins
return false
} else {
Expand Down Expand Up @@ -130,7 +143,7 @@ func Delete(req cmds.Request, out io.Writer, node *core.IpfsNode, fs *Datastore,
return nil
}

func getChildren(out io.Writer, node *node.Node, fs *Datastore, bs b.Blockstore, keys map[k.Key]struct{}) error {
func getChildren(out io.Writer, node *node.Node, fs *Datastore, bs b.Blockstore, keys map[k.Key]delInfo) error {
errors := false
for _, link := range node.Links {
key := k.Key(link.Hash)
Expand All @@ -142,7 +155,7 @@ func getChildren(out io.Writer, node *node.Node, fs *Datastore, bs b.Blockstore,
fmt.Fprintf(out, "%s: error retrieving key", key)
errors = true
}
keys[key] = struct{}{}
keys[key] = IndirectlySpecified
if n != nil {
err := getChildren(out, n, fs, bs, keys)
if err != nil {
Expand Down

0 comments on commit dcdc14e

Please sign in to comment.