pin.go 4.2 KB
Newer Older
Łukasz Magiera's avatar
Łukasz Magiera committed
1 2 3 4
package coreapi

import (
	"context"
Łukasz Magiera's avatar
Łukasz Magiera committed
5
	"fmt"
Łukasz Magiera's avatar
Łukasz Magiera committed
6 7 8

	coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
	caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
Łukasz Magiera's avatar
Łukasz Magiera committed
9 10 11 12 13
	corerepo "github.com/ipfs/go-ipfs/core/corerepo"
	merkledag "github.com/ipfs/go-ipfs/merkledag"
	pin "github.com/ipfs/go-ipfs/pin"

	cid "gx/ipfs/QmeSrf6pzut73u6zLQkRFQ3ygt3k6XFT2kjdYP8Tnkwwyg/go-cid"
Łukasz Magiera's avatar
Łukasz Magiera committed
14 15 16 17 18 19 20
)

type PinAPI struct {
	*CoreAPI
	*caopts.PinOptions
}

Łukasz Magiera's avatar
Łukasz Magiera committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
func (api *PinAPI) Add(ctx context.Context, p coreiface.Path, opts ...caopts.PinAddOption) error {
	settings, err := caopts.PinAddOptions(opts...)
	if err != nil {
		return err
	}

	defer api.node.Blockstore.PinLock().Unlock()

	_, err = corerepo.Pin(api.node, ctx, []string{p.String()}, settings.Recursive)
	if err != nil {
		return err
	}

	return nil
}

func (api *PinAPI) Ls(ctx context.Context, opts ...caopts.PinLsOption) ([]coreiface.Pin, error) {
	settings, err := caopts.PinLsOptions(opts...)
	if err != nil {
		return nil, err
	}

	switch settings.Type {
	case "all", "direct", "indirect", "recursive":
	default:
		return nil, fmt.Errorf("invalid type '%s', must be one of {direct, indirect, recursive, all}", settings.Type)
	}

	return pinLsAll(settings.Type, ctx, api.node.Pinning, api.node.DAG)
}

func (api *PinAPI) Rm(ctx context.Context, p coreiface.Path) error {
	_, err := corerepo.Unpin(api.node, ctx, []string{p.String()}, true)
	if err != nil {
		return err
	}

	return nil
}

func (api *PinAPI) Update(ctx context.Context, from coreiface.Path, to coreiface.Path, opts ...caopts.PinUpdateOption) error {
	settings, err := caopts.PinUpdateOptions(opts...)
	if err != nil {
		return err
	}

	return api.node.Pinning.Update(ctx, from.Cid(), to.Cid(), settings.Unpin)
}

type pinStatus struct {
	cid      *cid.Cid
	ok       bool
	badNodes []coreiface.BadPinNode
}

// BadNode is used in PinVerifyRes
type badNode struct {
	cid *cid.Cid
	err error
}

func (s *pinStatus) Ok() bool {
Łukasz Magiera's avatar
Łukasz Magiera committed
83
	return s.ok
Łukasz Magiera's avatar
Łukasz Magiera committed
84 85
}

Łukasz Magiera's avatar
Łukasz Magiera committed
86 87
func (s *pinStatus) BadNodes() []coreiface.BadPinNode {
	return s.badNodes
Łukasz Magiera's avatar
Łukasz Magiera committed
88 89
}

Łukasz Magiera's avatar
Łukasz Magiera committed
90 91
func (n *badNode) Path() coreiface.Path {
	return ParseCid(n.cid)
Łukasz Magiera's avatar
Łukasz Magiera committed
92 93
}

Łukasz Magiera's avatar
Łukasz Magiera committed
94 95
func (n *badNode) Err() error {
	return n.err
Łukasz Magiera's avatar
Łukasz Magiera committed
96 97
}

Łukasz Magiera's avatar
Łukasz Magiera committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
func (api *PinAPI) Verify(ctx context.Context) (<-chan coreiface.PinStatus, error) {
	visited := make(map[string]*pinStatus)
	getLinks := api.node.DAG.GetOfflineLinkService().GetLinks
	recPins := api.node.Pinning.RecursiveKeys()

	var checkPin func(root *cid.Cid) *pinStatus
	checkPin = func(root *cid.Cid) *pinStatus {
		key := root.String()
		if status, ok := visited[key]; ok {
			return status
		}

		links, err := getLinks(ctx, root)
		if err != nil {
			status := &pinStatus{ok: false, cid: root}
			status.badNodes = []coreiface.BadPinNode{&badNode{cid: root, err: err}}
			visited[key] = status
			return status
		}

		status := &pinStatus{ok: true, cid: root}
		for _, lnk := range links {
			res := checkPin(lnk.Cid)
			if !res.ok {
				status.ok = false
				status.badNodes = append(status.badNodes, res.badNodes...)
			}
		}

		visited[key] = status
		return status
	}

	out := make(chan coreiface.PinStatus)
	go func() {
		defer close(out)
		for _, c := range recPins {
			out <- checkPin(c)
		}
	}()

	return out, nil
}

type pinInfo struct {
	pinType string
	object  *cid.Cid
}

func (p *pinInfo) Path() coreiface.Path {
	return ParseCid(p.object)
}

func (p *pinInfo) Type() string {
	return p.pinType
}

func pinLsAll(typeStr string, ctx context.Context, pinning pin.Pinner, dag merkledag.DAGService) ([]coreiface.Pin, error) {

	keys := make(map[string]*pinInfo)

	AddToResultKeys := func(keyList []*cid.Cid, typeStr string) {
		for _, c := range keyList {
			keys[c.String()] = &pinInfo{
				pinType: typeStr,
				object:  c,
			}
		}
	}

	if typeStr == "direct" || typeStr == "all" {
		AddToResultKeys(pinning.DirectKeys(), "direct")
	}
	if typeStr == "indirect" || typeStr == "all" {
		set := cid.NewSet()
		for _, k := range pinning.RecursiveKeys() {
			err := merkledag.EnumerateChildren(ctx, dag.GetLinks, k, set.Visit)
			if err != nil {
				return nil, err
			}
		}
		AddToResultKeys(set.Keys(), "indirect")
	}
	if typeStr == "recursive" || typeStr == "all" {
		AddToResultKeys(pinning.RecursiveKeys(), "recursive")
	}

	out := make([]coreiface.Pin, 0, len(keys))
	for _, v := range keys {
		out = append(out, v)
	}

	return out, nil
Łukasz Magiera's avatar
Łukasz Magiera committed
191
}