1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-19 18:05:32 +08:00

Cleanup instances of manual resolver construction

License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
This commit is contained in:
Łukasz Magiera
2018-09-19 23:40:45 +02:00
parent e93aa25e97
commit 1ed555b87f
14 changed files with 171 additions and 191 deletions

View File

@ -6,9 +6,8 @@ import (
"io"
"os"
core "github.com/ipfs/go-ipfs/core"
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
coreunix "github.com/ipfs/go-ipfs/core/coreunix"
"github.com/ipfs/go-ipfs/core/coreapi/interface"
"gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
cmds "gx/ipfs/QmXTmUCBtDUrzDYVzASogLiNph7EBuYqEgPL7QoHNMzUnz/go-ipfs-cmds"
@ -35,6 +34,11 @@ var CatCmd = &cmds.Command{
return err
}
api, err := cmdenv.GetApi(env)
if err != nil {
return err
}
if !node.OnlineMode() {
if err := node.SetupOfflineRouting(); err != nil {
return err
@ -62,7 +66,7 @@ var CatCmd = &cmds.Command{
return err
}
readers, length, err := cat(req.Context, node, req.Arguments, int64(offset), int64(max))
readers, length, err := cat(req.Context, api, req.Arguments, int64(offset), int64(max))
if err != nil {
return err
}
@ -115,14 +119,19 @@ var CatCmd = &cmds.Command{
},
}
func cat(ctx context.Context, node *core.IpfsNode, paths []string, offset int64, max int64) ([]io.Reader, uint64, error) {
func cat(ctx context.Context, api iface.CoreAPI, paths []string, offset int64, max int64) ([]io.Reader, uint64, error) {
readers := make([]io.Reader, 0, len(paths))
length := uint64(0)
if max == 0 {
return nil, 0, nil
}
for _, fpath := range paths {
read, err := coreunix.Cat(ctx, node, fpath)
for _, p := range paths {
fpath, err := iface.ParsePath(p)
if err != nil {
return nil, 0, err
}
read, err := api.Unixfs().Cat(ctx, fpath)
if err != nil {
return nil, 0, err
}

View File

@ -16,21 +16,19 @@ import (
core "github.com/ipfs/go-ipfs/core"
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
e "github.com/ipfs/go-ipfs/core/commands/e"
ft "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs"
uio "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs/io"
dag "gx/ipfs/QmcBoNcAP6qDjgRBew7yjvCqHq7p5jMstE44jPUBWBxzsV/go-merkledag"
bservice "gx/ipfs/QmcRecCZWM2NZfCQrCe97Ch3Givv8KKEP82tGUDntzdLFe/go-blockservice"
path "gx/ipfs/QmcjwUb36Z16NJkvDX6ccXPqsFswo6AsRXynyXcLLCphV2/go-path"
resolver "gx/ipfs/QmcjwUb36Z16NJkvDX6ccXPqsFswo6AsRXynyXcLLCphV2/go-path/resolver"
"github.com/ipfs/go-ipfs/core/coreapi/interface"
humanize "gx/ipfs/QmPSBJL4momYnE7DcUyk2DVhD6rH488ZmHBGLbxNdhU44K/go-humanize"
cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
offline "gx/ipfs/QmR5miWuikPxWyUrzMYJVmFUcD44pGdtc98h9Qsbp4YcJw/go-ipfs-exchange-offline"
cmdkit "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
ft "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs"
cmds "gx/ipfs/QmXTmUCBtDUrzDYVzASogLiNph7EBuYqEgPL7QoHNMzUnz/go-ipfs-cmds"
logging "gx/ipfs/QmZChCsSt8DctjceaL56Eibc29CVQq4dGKRXC5JRZ6Ppae/go-log"
mfs "gx/ipfs/QmahrY1adY4wvtYEtoGjpZ2GUohTyukrkMkwUR9ytRjTG2/go-mfs"
dag "gx/ipfs/QmcBoNcAP6qDjgRBew7yjvCqHq7p5jMstE44jPUBWBxzsV/go-merkledag"
bservice "gx/ipfs/QmcRecCZWM2NZfCQrCe97Ch3Givv8KKEP82tGUDntzdLFe/go-blockservice"
ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format"
)
@ -120,6 +118,11 @@ var filesStatCmd = &cmds.Command{
return err
}
api, err := cmdenv.GetApi(env)
if err != nil {
return err
}
path, err := checkPath(req.Arguments[0])
if err != nil {
return err
@ -138,7 +141,7 @@ var filesStatCmd = &cmds.Command{
dagserv = node.DAG
}
nd, err := getNodeFromPath(req.Context, node, dagserv, path)
nd, err := getNodeFromPath(req.Context, node, api, path)
if err != nil {
return err
}
@ -305,6 +308,12 @@ var filesCpCmd = &oldcmds.Command{
return
}
api, err := req.InvocContext().GetApi()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
flush, _, _ := req.Option("flush").Bool()
src, err := checkPath(req.Arguments()[0])
@ -324,7 +333,7 @@ var filesCpCmd = &oldcmds.Command{
dst += gopath.Base(src)
}
nd, err := getNodeFromPath(req.Context(), node, node.DAG, src)
nd, err := getNodeFromPath(req.Context(), node, api, src)
if err != nil {
res.SetError(fmt.Errorf("cp: cannot get node from path %s: %s", src, err), cmdkit.ErrNormal)
return
@ -348,20 +357,15 @@ var filesCpCmd = &oldcmds.Command{
},
}
func getNodeFromPath(ctx context.Context, node *core.IpfsNode, dagservice ipld.DAGService, p string) (ipld.Node, error) {
func getNodeFromPath(ctx context.Context, node *core.IpfsNode, api iface.CoreAPI, p string) (ipld.Node, error) {
switch {
case strings.HasPrefix(p, "/ipfs/"):
np, err := path.ParsePath(p)
np, err := iface.ParsePath(p)
if err != nil {
return nil, err
}
resolver := &resolver.Resolver{
DAG: dagservice,
ResolveOnce: uio.ResolveUnixfsOnce,
}
return core.Resolve(ctx, node.Namesys, resolver, np)
return api.ResolveNode(ctx, np)
default:
fsn, err := mfs.Lookup(node.FilesRoot, p)
if err != nil {

View File

@ -7,19 +7,17 @@ import (
"text/tabwriter"
cmds "github.com/ipfs/go-ipfs/commands"
core "github.com/ipfs/go-ipfs/core"
e "github.com/ipfs/go-ipfs/core/commands/e"
iface "github.com/ipfs/go-ipfs/core/coreapi/interface"
cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
offline "gx/ipfs/QmR5miWuikPxWyUrzMYJVmFUcD44pGdtc98h9Qsbp4YcJw/go-ipfs-exchange-offline"
"gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
unixfs "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs"
uio "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs/io"
unixfspb "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs/pb"
merkledag "gx/ipfs/QmcBoNcAP6qDjgRBew7yjvCqHq7p5jMstE44jPUBWBxzsV/go-merkledag"
blockservice "gx/ipfs/QmcRecCZWM2NZfCQrCe97Ch3Givv8KKEP82tGUDntzdLFe/go-blockservice"
path "gx/ipfs/QmcjwUb36Z16NJkvDX6ccXPqsFswo6AsRXynyXcLLCphV2/go-path"
resolver "gx/ipfs/QmcjwUb36Z16NJkvDX6ccXPqsFswo6AsRXynyXcLLCphV2/go-path/resolver"
cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
offline "gx/ipfs/QmR5miWuikPxWyUrzMYJVmFUcD44pGdtc98h9Qsbp4YcJw/go-ipfs-exchange-offline"
"gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format"
)
@ -65,6 +63,12 @@ The JSON output contains type information.
return
}
api, err := req.InvocContext().GetApi()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
// get options early -> exit early in case of error
if _, _, err := req.Option("headers").Bool(); err != nil {
res.SetError(err, cmdkit.ErrNormal)
@ -88,18 +92,13 @@ The JSON output contains type information.
var dagnodes []ipld.Node
for _, fpath := range paths {
p, err := path.ParsePath(fpath)
p, err := iface.ParsePath(fpath)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
r := &resolver.Resolver{
DAG: nd.DAG,
ResolveOnce: uio.ResolveUnixfsOnce,
}
dagnode, err := core.Resolve(req.Context(), nd.Namesys, r, p)
dagnode, err := api.ResolveNode(req.Context(), p)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return

View File

@ -10,9 +10,10 @@ import (
cmds "github.com/ipfs/go-ipfs/commands"
core "github.com/ipfs/go-ipfs/core"
e "github.com/ipfs/go-ipfs/core/commands/e"
iface "github.com/ipfs/go-ipfs/core/coreapi/interface"
options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
corerepo "github.com/ipfs/go-ipfs/core/corerepo"
pin "github.com/ipfs/go-ipfs/pin"
uio "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs/io"
cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
offline "gx/ipfs/QmR5miWuikPxWyUrzMYJVmFUcD44pGdtc98h9Qsbp4YcJw/go-ipfs-exchange-offline"
@ -20,8 +21,6 @@ import (
"gx/ipfs/QmVkMRSkXrpjqrroEXWuYBvDBnXCdMMY6gsKicBGVGUqKT/go-verifcid"
dag "gx/ipfs/QmcBoNcAP6qDjgRBew7yjvCqHq7p5jMstE44jPUBWBxzsV/go-merkledag"
bserv "gx/ipfs/QmcRecCZWM2NZfCQrCe97Ch3Givv8KKEP82tGUDntzdLFe/go-blockservice"
path "gx/ipfs/QmcjwUb36Z16NJkvDX6ccXPqsFswo6AsRXynyXcLLCphV2/go-path"
resolver "gx/ipfs/QmcjwUb36Z16NJkvDX6ccXPqsFswo6AsRXynyXcLLCphV2/go-path/resolver"
)
var PinCmd = &cmds.Command{
@ -68,6 +67,12 @@ var addPinCmd = &cmds.Command{
return
}
api, err := req.InvocContext().GetApi()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
defer n.Blockstore.PinLock().Unlock()
// set recursive flag
@ -79,7 +84,7 @@ var addPinCmd = &cmds.Command{
showProgress, _, _ := req.Option("progress").Bool()
if !showProgress {
added, err := corerepo.Pin(n, req.Context(), req.Arguments(), recursive)
added, err := corerepo.Pin(n, api, req.Context(), req.Arguments(), recursive)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
@ -99,7 +104,7 @@ var addPinCmd = &cmds.Command{
}
ch := make(chan pinResult, 1)
go func() {
added, err := corerepo.Pin(n, ctx, req.Arguments(), recursive)
added, err := corerepo.Pin(n, api, ctx, req.Arguments(), recursive)
ch <- pinResult{pins: added, err: err}
}()
@ -193,6 +198,12 @@ collected if needed. (By default, recursively. Use -r=false for direct pins.)
return
}
api, err := req.InvocContext().GetApi()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
// set recursive flag
recursive, _, err := req.Option("recursive").Bool()
if err != nil {
@ -200,7 +211,7 @@ collected if needed. (By default, recursively. Use -r=false for direct pins.)
return
}
removed, err := corerepo.Unpin(n, req.Context(), req.Arguments(), recursive)
removed, err := corerepo.Unpin(n, api, req.Context(), req.Arguments(), recursive)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
@ -287,6 +298,12 @@ Example:
return
}
api, err := req.InvocContext().GetApi()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
typeStr, _, err := req.Option("type").String()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
@ -304,7 +321,7 @@ Example:
var keys map[string]RefKeyObject
if len(req.Arguments()) > 0 {
keys, err = pinLsKeys(req.Context(), req.Arguments(), typeStr, n)
keys, err = pinLsKeys(req.Context(), req.Arguments(), typeStr, n, api)
} else {
keys, err = pinLsAll(req.Context(), typeStr, n)
}
@ -364,7 +381,7 @@ new pin and removing the old one.
},
Type: PinOutput{},
Run: func(req cmds.Request, res cmds.Response) {
n, err := req.InvocContext().GetNode()
api, err := req.InvocContext().GetApi()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
@ -376,42 +393,19 @@ new pin and removing the old one.
return
}
from, err := path.ParsePath(req.Arguments()[0])
from, err := iface.ParsePath(req.Arguments()[0])
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
to, err := path.ParsePath(req.Arguments()[1])
to, err := iface.ParsePath(req.Arguments()[1])
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
r := &resolver.Resolver{
DAG: n.DAG,
ResolveOnce: uio.ResolveUnixfsOnce,
}
fromc, err := core.ResolveToCid(req.Context(), n.Namesys, r, from)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
toc, err := core.ResolveToCid(req.Context(), n.Namesys, r, to)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
err = n.Pinning.Update(req.Context(), fromc, toc, unpin)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
err = n.Pinning.Flush()
err = api.Pin().Update(req.Context(), from, to, options.Pin.Unpin(unpin))
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
@ -501,7 +495,7 @@ type RefKeyList struct {
Keys map[string]RefKeyObject
}
func pinLsKeys(ctx context.Context, args []string, typeStr string, n *core.IpfsNode) (map[string]RefKeyObject, error) {
func pinLsKeys(ctx context.Context, args []string, typeStr string, n *core.IpfsNode, api iface.CoreAPI) (map[string]RefKeyObject, error) {
mode, ok := pin.StringToMode(typeStr)
if !ok {
@ -510,23 +504,18 @@ func pinLsKeys(ctx context.Context, args []string, typeStr string, n *core.IpfsN
keys := make(map[string]RefKeyObject)
r := &resolver.Resolver{
DAG: n.DAG,
ResolveOnce: uio.ResolveUnixfsOnce,
}
for _, p := range args {
pth, err := path.ParsePath(p)
pth, err := iface.ParsePath(p)
if err != nil {
return nil, err
}
c, err := core.ResolveToCid(ctx, n.Namesys, r, pth)
c, err := api.ResolvePath(ctx, pth)
if err != nil {
return nil, err
}
pinType, pinned, err := n.Pinning.IsPinnedWithType(c, mode)
pinType, pinned, err := n.Pinning.IsPinnedWithType(c.Cid(), mode)
if err != nil {
return nil, err
}

View File

@ -7,16 +7,13 @@ import (
"sort"
"text/tabwriter"
cmdkit "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
cmds "github.com/ipfs/go-ipfs/commands"
core "github.com/ipfs/go-ipfs/core"
e "github.com/ipfs/go-ipfs/core/commands/e"
iface "github.com/ipfs/go-ipfs/core/coreapi/interface"
cmdkit "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
unixfs "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs"
uio "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs/io"
merkledag "gx/ipfs/QmcBoNcAP6qDjgRBew7yjvCqHq7p5jMstE44jPUBWBxzsV/go-merkledag"
path "gx/ipfs/QmcjwUb36Z16NJkvDX6ccXPqsFswo6AsRXynyXcLLCphV2/go-path"
resolver "gx/ipfs/QmcjwUb36Z16NJkvDX6ccXPqsFswo6AsRXynyXcLLCphV2/go-path/resolver"
)
type LsLink struct {
@ -82,6 +79,12 @@ possible, please use 'ipfs ls' instead.
return
}
api, err := req.InvocContext().GetApi()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
paths := req.Arguments()
output := LsOutput{
@ -89,15 +92,16 @@ possible, please use 'ipfs ls' instead.
Objects: map[string]*LsObject{},
}
for _, fpath := range paths {
for _, p := range paths {
ctx := req.Context()
resolver := &resolver.Resolver{
DAG: node.DAG,
ResolveOnce: uio.ResolveUnixfsOnce,
fpath, err := iface.ParsePath(p)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
merkleNode, err := core.Resolve(ctx, node.Namesys, resolver, path.Path(fpath))
merkleNode, err := api.ResolveNode(ctx, fpath)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
@ -106,7 +110,7 @@ possible, please use 'ipfs ls' instead.
c := merkleNode.Cid()
hash := c.String()
output.Arguments[fpath] = hash
output.Arguments[p] = hash
if _, ok := output.Objects[hash]; ok {
// duplicate argument for an already-listed node

View File

@ -1,10 +1,20 @@
package iface
import (
"context"
"io"
)
type Reader interface {
io.ReadSeeker
io.Closer
ReadSeekCloser
Size() uint64
CtxReadFull(context.Context, []byte) (int, error)
}
// A ReadSeekCloser implements interfaces to read, copy, seek and close.
type ReadSeekCloser interface {
io.Reader
io.Seeker
io.Closer
io.WriterTo
}

View File

@ -29,12 +29,12 @@ func (api *PinAPI) Add(ctx context.Context, p coreiface.Path, opts ...caopts.Pin
return err
}
_, err = corerepo.Pin(api.node, ctx, []string{rp.Cid().String()}, settings.Recursive)
_, err = corerepo.Pin(api.node, api.core(), ctx, []string{rp.Cid().String()}, settings.Recursive)
if err != nil {
return err
}
return nil
return api.node.Pinning.Flush()
}
func (api *PinAPI) Ls(ctx context.Context, opts ...caopts.PinLsOption) ([]coreiface.Pin, error) {
@ -53,12 +53,12 @@ func (api *PinAPI) Ls(ctx context.Context, opts ...caopts.PinLsOption) ([]coreif
}
func (api *PinAPI) Rm(ctx context.Context, p coreiface.Path) error {
_, err := corerepo.Unpin(api.node, ctx, []string{p.String()}, true)
_, err := corerepo.Unpin(api.node, api.core(), ctx, []string{p.String()}, true)
if err != nil {
return err
}
return nil
return api.node.Pinning.Flush()
}
func (api *PinAPI) Update(ctx context.Context, from coreiface.Path, to coreiface.Path, opts ...caopts.PinUpdateOption) error {
@ -77,7 +77,12 @@ func (api *PinAPI) Update(ctx context.Context, from coreiface.Path, to coreiface
return err
}
return api.node.Pinning.Update(ctx, fp.Cid(), tp.Cid(), settings.Unpin)
err = api.node.Pinning.Update(ctx, fp.Cid(), tp.Cid(), settings.Unpin)
if err != nil {
return err
}
return api.node.Pinning.Flush()
}
type pinStatus struct {

View File

@ -18,28 +18,21 @@ import (
"fmt"
"github.com/ipfs/go-ipfs/core"
uio "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs/io"
path "gx/ipfs/QmcjwUb36Z16NJkvDX6ccXPqsFswo6AsRXynyXcLLCphV2/go-path"
resolver "gx/ipfs/QmcjwUb36Z16NJkvDX6ccXPqsFswo6AsRXynyXcLLCphV2/go-path/resolver"
"github.com/ipfs/go-ipfs/core/coreapi/interface"
cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
"gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
)
func Pin(n *core.IpfsNode, ctx context.Context, paths []string, recursive bool) ([]cid.Cid, error) {
func Pin(n *core.IpfsNode, api iface.CoreAPI, ctx context.Context, paths []string, recursive bool) ([]cid.Cid, error) {
out := make([]cid.Cid, len(paths))
r := &resolver.Resolver{
DAG: n.DAG,
ResolveOnce: uio.ResolveUnixfsOnce,
}
for i, fpath := range paths {
p, err := path.ParsePath(fpath)
p, err := iface.ParsePath(fpath)
if err != nil {
return nil, err
}
dagnode, err := core.Resolve(ctx, n.Namesys, r, p)
dagnode, err := api.ResolveNode(ctx, p)
if err != nil {
return nil, fmt.Errorf("pin: %s", err)
}
@ -58,30 +51,25 @@ func Pin(n *core.IpfsNode, ctx context.Context, paths []string, recursive bool)
return out, nil
}
func Unpin(n *core.IpfsNode, ctx context.Context, paths []string, recursive bool) ([]cid.Cid, error) {
func Unpin(n *core.IpfsNode, api iface.CoreAPI, ctx context.Context, paths []string, recursive bool) ([]cid.Cid, error) {
unpinned := make([]cid.Cid, len(paths))
r := &resolver.Resolver{
DAG: n.DAG,
ResolveOnce: uio.ResolveUnixfsOnce,
}
for i, p := range paths {
p, err := path.ParsePath(p)
p, err := iface.ParsePath(p)
if err != nil {
return nil, err
}
k, err := core.ResolveToCid(ctx, n.Namesys, r, p)
k, err := api.ResolvePath(ctx, p)
if err != nil {
return nil, err
}
err = n.Pinning.Unpin(ctx, k, recursive)
err = n.Pinning.Unpin(ctx, k.Cid(), recursive)
if err != nil {
return nil, err
}
unpinned[i] = k
unpinned[i] = k.Cid()
}
err := n.Pinning.Flush()

View File

@ -1,24 +0,0 @@
package coreunix
import (
"context"
core "github.com/ipfs/go-ipfs/core"
uio "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs/io"
path "gx/ipfs/QmcjwUb36Z16NJkvDX6ccXPqsFswo6AsRXynyXcLLCphV2/go-path"
resolver "gx/ipfs/QmcjwUb36Z16NJkvDX6ccXPqsFswo6AsRXynyXcLLCphV2/go-path/resolver"
)
func Cat(ctx context.Context, n *core.IpfsNode, pstr string) (uio.DagReader, error) {
r := &resolver.Resolver{
DAG: n.DAG,
ResolveOnce: uio.ResolveUnixfsOnce,
}
dagNode, err := core.Resolve(ctx, n.Namesys, r, path.Path(pstr))
if err != nil {
return nil, err
}
return uio.NewDagReader(ctx, dagNode, n.DAG)
}

View File

@ -6,11 +6,10 @@ import (
"strings"
namesys "github.com/ipfs/go-ipfs/namesys"
logging "gx/ipfs/QmZChCsSt8DctjceaL56Eibc29CVQq4dGKRXC5JRZ6Ppae/go-log"
path "gx/ipfs/QmcjwUb36Z16NJkvDX6ccXPqsFswo6AsRXynyXcLLCphV2/go-path"
resolver "gx/ipfs/QmcjwUb36Z16NJkvDX6ccXPqsFswo6AsRXynyXcLLCphV2/go-path/resolver"
cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
logging "gx/ipfs/QmZChCsSt8DctjceaL56Eibc29CVQq4dGKRXC5JRZ6Ppae/go-log"
ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format"
)
@ -74,37 +73,3 @@ func Resolve(ctx context.Context, nsys namesys.NameSystem, r *resolver.Resolver,
// ok, we have an IPFS path now (or what we'll treat as one)
return r.ResolvePath(ctx, p)
}
// ResolveToCid resolves a path to a cid.
//
// It first checks if the path is already in the form of just a cid (<cid> or
// /ipfs/<cid>) and returns immediately if so. Otherwise, it falls back onto
// Resolve to perform resolution of the dagnode being referenced.
func ResolveToCid(ctx context.Context, nsys namesys.NameSystem, r *resolver.Resolver, p path.Path) (cid.Cid, error) {
// If the path is simply a cid, parse and return it. Parsed paths are already
// normalized (read: prepended with /ipfs/ if needed), so segment[1] should
// always be the key.
if p.IsJustAKey() {
return cid.Decode(p.Segments()[1])
}
// Fall back onto regular dagnode resolution. Retrieve the second-to-last
// segment of the path and resolve its link to the last segment.
head, tail, err := p.PopLastSegment()
if err != nil {
return cid.Cid{}, err
}
dagnode, err := Resolve(ctx, nsys, r, head)
if err != nil {
return cid.Cid{}, err
}
// Extract and return the cid of the link to the target dag node.
link, _, err := dagnode.ResolveLink([]string{tail})
if err != nil {
return cid.Cid{}, err
}
return link.Cid, nil
}

View File

@ -14,16 +14,17 @@ import (
"testing"
core "github.com/ipfs/go-ipfs/core"
coreunix "github.com/ipfs/go-ipfs/core/coreunix"
coreapi "github.com/ipfs/go-ipfs/core/coreapi"
iface "github.com/ipfs/go-ipfs/core/coreapi/interface"
coremock "github.com/ipfs/go-ipfs/core/mock"
importer "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs/importer"
uio "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs/io"
dag "gx/ipfs/QmcBoNcAP6qDjgRBew7yjvCqHq7p5jMstE44jPUBWBxzsV/go-merkledag"
ci "gx/ipfs/QmNfQbgBfARAtrYsBguChX6VJ5nbjeoYy1KdC36aaYWqG8/go-testutil/ci"
u "gx/ipfs/QmPdKqUcHGFdeSpvjVoaTRPPstGif9GBZb5Q56RVw9o69A/go-ipfs-util"
fstest "gx/ipfs/QmSJBsmLP1XMjv8hxYg2rUMdPDB7YUpyBo9idjrJ6Cmq6F/fuse/fs/fstestutil"
importer "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs/importer"
uio "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs/io"
chunker "gx/ipfs/QmULKgr55cSWR8Kiwy3cVRcAiGVnR6EVSaB7hJcWS4138p/go-ipfs-chunker"
dag "gx/ipfs/QmcBoNcAP6qDjgRBew7yjvCqHq7p5jMstE44jPUBWBxzsV/go-merkledag"
ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format"
)
@ -116,6 +117,8 @@ func TestIpfsStressRead(t *testing.T) {
nd, mnt := setupIpfsTest(t, nil)
defer mnt.Close()
api := coreapi.NewCoreAPI(nd)
var nodes []ipld.Node
var paths []string
@ -165,14 +168,14 @@ func TestIpfsStressRead(t *testing.T) {
defer wg.Done()
for i := 0; i < 2000; i++ {
item := paths[rand.Intn(len(paths))]
fname := path.Join(mnt.Dir, item)
item, _ := iface.ParsePath(paths[rand.Intn(len(paths))])
fname := path.Join(mnt.Dir, item.String())
rbuf, err := ioutil.ReadFile(fname)
if err != nil {
errs <- err
}
read, err := coreunix.Cat(nd.Context(), nd, item)
read, err := api.Unixfs().Cat(nd.Context(), item)
if err != nil {
errs <- err
}

View File

@ -12,6 +12,8 @@ import (
"time"
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/coreapi"
"github.com/ipfs/go-ipfs/core/coreapi/interface"
coreunix "github.com/ipfs/go-ipfs/core/coreunix"
mock "github.com/ipfs/go-ipfs/core/mock"
"github.com/ipfs/go-ipfs/thirdparty/unit"
@ -118,6 +120,8 @@ func DirectAddCat(data []byte, conf testutil.LatencyConfig) error {
}
defer catter.Close()
catterApi := coreapi.NewCoreAPI(catter)
err = mn.LinkAll()
if err != nil {
return err
@ -138,7 +142,12 @@ func DirectAddCat(data []byte, conf testutil.LatencyConfig) error {
return err
}
readerCatted, err := coreunix.Cat(ctx, catter, added)
ap, err := iface.ParsePath(added)
if err != nil {
return err
}
readerCatted, err := catterApi.Unixfs().Cat(ctx, ap)
if err != nil {
return err
}

View File

@ -9,6 +9,8 @@ import (
"testing"
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/coreapi"
"github.com/ipfs/go-ipfs/core/coreapi/interface"
coreunix "github.com/ipfs/go-ipfs/core/coreunix"
mock "github.com/ipfs/go-ipfs/core/mock"
"github.com/ipfs/go-ipfs/thirdparty/unit"
@ -64,6 +66,8 @@ func benchCat(b *testing.B, data []byte, conf testutil.LatencyConfig) error {
}
defer catter.Close()
catterApi := coreapi.NewCoreAPI(catter)
err = mn.LinkAll()
if err != nil {
return err
@ -84,8 +88,13 @@ func benchCat(b *testing.B, data []byte, conf testutil.LatencyConfig) error {
return err
}
ap, err := iface.ParsePath(added)
if err != nil {
return err
}
b.StartTimer()
readerCatted, err := coreunix.Cat(ctx, catter, added)
readerCatted, err := catterApi.Unixfs().Cat(ctx, ap)
if err != nil {
return err
}

View File

@ -10,6 +10,8 @@ import (
"time"
core "github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/coreapi"
"github.com/ipfs/go-ipfs/core/coreapi/interface"
coreunix "github.com/ipfs/go-ipfs/core/coreunix"
mock "github.com/ipfs/go-ipfs/core/mock"
"github.com/ipfs/go-ipfs/thirdparty/unit"
@ -100,6 +102,9 @@ func RunThreeLeggedCat(data []byte, conf testutil.LatencyConfig) error {
return err
}
defer catter.Close()
catterApi := coreapi.NewCoreAPI(catter)
mn.LinkAll()
bis := bootstrap.Peerstore.PeerInfo(bootstrap.PeerHost.ID())
@ -116,7 +121,12 @@ func RunThreeLeggedCat(data []byte, conf testutil.LatencyConfig) error {
return err
}
readerCatted, err := coreunix.Cat(ctx, catter, added)
ap, err := iface.ParsePath(added)
if err != nil {
return err
}
readerCatted, err := catterApi.Unixfs().Cat(ctx, ap)
if err != nil {
return err
}