mirror of
https://github.com/ipfs/kubo.git
synced 2025-09-16 12:07:11 +08:00

This commit adds a new set of sharness tests for pinning, and addresses bugs that were pointed out by said tests. test/sharness: added more pinning tests Pinning is currently broken. See issue #1051. This commit introduces a few more pinning tests. These are by no means exhaustive, but definitely surface the present problems going on. I believe these tests are correct, but not sure. Pushing them as failing so that pinning is fixed in this PR. make pinning and merkledag.Get take contexts improve 'add' commands usage of pinning FIXUP: fix 'pin lists look good' ipfs-pin-stat simple script to help check pinning This is a simple shell script to help check pinning. We ought to strive towards making adding commands this easy. The http api is great and powerful, but our setup right now gets in the way. Perhaps we can clean up that area. updated t0081-repo-pinning - fixed a couple bugs with the tests - made it a bit clearer (still a lot going on) - the remaining tests are correct and highlight a problem with pinning. Namely, that recursive pinning is buggy. At least: towards the end of the test, $HASH_DIR4 and $HASH_FILE4 should be pinned indirectly, but they're not. And thus get gc-ed out. There may be other problems too. cc @whyrusleeping fix grep params for context deadline check fix bugs in pin and pin tests check for block local before checking recursive pin
169 lines
3.8 KiB
Go
169 lines
3.8 KiB
Go
package coreunix
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
"io/ioutil"
|
|
"os"
|
|
gopath "path"
|
|
"time"
|
|
|
|
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
|
|
|
|
"github.com/ipfs/go-ipfs/commands/files"
|
|
core "github.com/ipfs/go-ipfs/core"
|
|
importer "github.com/ipfs/go-ipfs/importer"
|
|
chunk "github.com/ipfs/go-ipfs/importer/chunk"
|
|
merkledag "github.com/ipfs/go-ipfs/merkledag"
|
|
"github.com/ipfs/go-ipfs/pin"
|
|
"github.com/ipfs/go-ipfs/thirdparty/eventlog"
|
|
unixfs "github.com/ipfs/go-ipfs/unixfs"
|
|
)
|
|
|
|
var log = eventlog.Logger("coreunix")
|
|
|
|
// Add builds a merkledag from the a reader, pinning all objects to the local
|
|
// datastore. Returns a key representing the root node.
|
|
func Add(n *core.IpfsNode, r io.Reader) (string, error) {
|
|
// TODO more attractive function signature importer.BuildDagFromReader
|
|
dagNode, err := importer.BuildDagFromReader(
|
|
r,
|
|
n.DAG,
|
|
n.Pinning.GetManual(), // Fix this interface
|
|
chunk.DefaultSplitter,
|
|
)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if err := n.Pinning.Flush(); err != nil {
|
|
return "", err
|
|
}
|
|
k, err := dagNode.Key()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return k.String(), nil
|
|
}
|
|
|
|
// AddR recursively adds files in |path|.
|
|
func AddR(n *core.IpfsNode, root string) (key string, err error) {
|
|
f, err := os.Open(root)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer f.Close()
|
|
ff, err := files.NewSerialFile(root, f)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
dagnode, err := addFile(n, ff)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
k, err := dagnode.Key()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return k.String(), nil
|
|
}
|
|
|
|
// AddWrapped adds data from a reader, and wraps it with a directory object
|
|
// to preserve the filename.
|
|
// Returns the path of the added file ("<dir hash>/filename"), the DAG node of
|
|
// the directory, and and error if any.
|
|
func AddWrapped(n *core.IpfsNode, r io.Reader, filename string) (string, *merkledag.Node, error) {
|
|
file := files.NewReaderFile(filename, ioutil.NopCloser(r), nil)
|
|
dir := files.NewSliceFile("", []files.File{file})
|
|
dagnode, err := addDir(n, dir)
|
|
if err != nil {
|
|
return "", nil, err
|
|
}
|
|
k, err := dagnode.Key()
|
|
if err != nil {
|
|
return "", nil, err
|
|
}
|
|
return gopath.Join(k.String(), filename), dagnode, nil
|
|
}
|
|
|
|
func add(n *core.IpfsNode, readers []io.Reader) ([]*merkledag.Node, error) {
|
|
mp, ok := n.Pinning.(pin.ManualPinner)
|
|
if !ok {
|
|
return nil, errors.New("invalid pinner type! expected manual pinner")
|
|
}
|
|
dagnodes := make([]*merkledag.Node, 0)
|
|
for _, reader := range readers {
|
|
node, err := importer.BuildDagFromReader(reader, n.DAG, mp, chunk.DefaultSplitter)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
dagnodes = append(dagnodes, node)
|
|
}
|
|
err := n.Pinning.Flush()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return dagnodes, nil
|
|
}
|
|
|
|
func addNode(n *core.IpfsNode, node *merkledag.Node) error {
|
|
err := n.DAG.AddRecursive(node) // add the file to the graph + local storage
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.TODO(), time.Minute)
|
|
defer cancel()
|
|
err = n.Pinning.Pin(ctx, node, true) // ensure we keep it
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func addFile(n *core.IpfsNode, file files.File) (*merkledag.Node, error) {
|
|
if file.IsDirectory() {
|
|
return addDir(n, file)
|
|
}
|
|
|
|
dns, err := add(n, []io.Reader{file})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return dns[len(dns)-1], nil // last dag node is the file.
|
|
}
|
|
|
|
func addDir(n *core.IpfsNode, dir files.File) (*merkledag.Node, error) {
|
|
|
|
tree := &merkledag.Node{Data: unixfs.FolderPBData()}
|
|
|
|
Loop:
|
|
for {
|
|
file, err := dir.NextFile()
|
|
switch {
|
|
case err != nil && err != io.EOF:
|
|
return nil, err
|
|
case err == io.EOF:
|
|
break Loop
|
|
}
|
|
|
|
node, err := addFile(n, file)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
_, name := gopath.Split(file.FileName())
|
|
|
|
err = tree.AddNodeLink(name, node)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
err := addNode(n, tree)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return tree, nil
|
|
}
|