Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add global option to specify the multibase encoding. #5289

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions core/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,12 @@ You can now check what blocks have been created by:
return
}

base, _, err := HandleCidBase(req, env)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

fileAdder.Out = outChan
fileAdder.Chunker = chunker
fileAdder.Progress = progress
Expand All @@ -280,6 +286,7 @@ You can now check what blocks have been created by:
fileAdder.RawLeaves = rawblks
fileAdder.NoCopy = nocopy
fileAdder.Prefix = &prefix
fileAdder.Base = base

if hash {
md := dagtest.Mock()
Expand Down
23 changes: 16 additions & 7 deletions core/commands/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
humanize "gx/ipfs/QmPSBJL4momYnE7DcUyk2DVhD6rH488ZmHBGLbxNdhU44K/go-humanize"
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
offline "gx/ipfs/QmS6mo1dPpHdYsVkm27BRZDLxpKBCiJKUH8fHX15XFfMez/go-ipfs-exchange-offline"
mbase "gx/ipfs/QmSbvata2WqNkqGtZNg8MR3SKwnB8iQ7vTPJgWqB8bC5kR/go-multibase"
cid "gx/ipfs/QmYVNvtQkeZ6AKSwDrjQTs432QtL6umrrK41EBq3cu7iSP/go-cid"
ipld "gx/ipfs/QmZtNq8dArGfnpCZfx2pUNY7UcjGhVp5qqwQ4hH6mpTMRQ/go-ipld-format"
logging "gx/ipfs/QmcVVHfdyv15GVPk7NrxdWjh2hLVccXnoD8j2tyQShiXJb/go-log"
Expand Down Expand Up @@ -128,6 +129,12 @@ var filesStatCmd = &cmds.Command{

withLocal, _ := req.Options["with-local"].(bool)

base, _, err := HandleCidBase(req, env)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

var dagserv ipld.DAGService
if withLocal {
// an offline DAGService will not fetch from the network
Expand All @@ -145,7 +152,7 @@ var filesStatCmd = &cmds.Command{
return
}

o, err := statNode(nd)
o, err := statNode(nd, base)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
Expand Down Expand Up @@ -217,7 +224,7 @@ func statGetFormatOptions(req *cmds.Request) (string, error) {
}
}

func statNode(nd ipld.Node) (*statOutput, error) {
func statNode(nd ipld.Node, base mbase.Encoder) (*statOutput, error) {
c := nd.Cid()

cumulsize, err := nd.Size()
Expand All @@ -243,15 +250,15 @@ func statNode(nd ipld.Node) (*statOutput, error) {
}

return &statOutput{
Hash: c.String(),
Hash: c.Encode(base),
Blocks: len(nd.Links()),
Size: d.GetFilesize(),
CumulativeSize: cumulsize,
Type: ndtype,
}, nil
case *dag.RawNode:
return &statOutput{
Hash: c.String(),
Hash: c.Encode(base),
Blocks: 0,
Size: cumulsize,
CumulativeSize: cumulsize,
Expand Down Expand Up @@ -437,11 +444,13 @@ Examples:

long, _, _ := req.Option("l").Bool()

base, _, ctx, err := HandleCidBaseOld(req, req.Context())

switch fsn := fsn.(type) {
case *mfs.Directory:
if !long {
var output []mfs.NodeListing
names, err := fsn.ListNames(req.Context())
names, err := fsn.ListNames(ctx)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
Expand All @@ -454,7 +463,7 @@ Examples:
}
res.SetOutput(&filesLsOutput{output})
} else {
listing, err := fsn.List(req.Context())
listing, err := fsn.List(ctx)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
Expand All @@ -480,7 +489,7 @@ Examples:
res.SetError(err, cmdkit.ErrNormal)
return
}
out.Entries[0].Hash = nd.Cid().String()
out.Entries[0].Hash = nd.Cid().Encode(base)
}
res.SetOutput(out)
return
Expand Down
16 changes: 12 additions & 4 deletions core/commands/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
blockservice "github.com/ipfs/go-ipfs/blockservice"
cmds "github.com/ipfs/go-ipfs/commands"
core "github.com/ipfs/go-ipfs/core"
misc "github.com/ipfs/go-ipfs/core/misc"
e "github.com/ipfs/go-ipfs/core/commands/e"
merkledag "github.com/ipfs/go-ipfs/merkledag"
path "github.com/ipfs/go-ipfs/path"
Expand Down Expand Up @@ -77,6 +78,12 @@ The JSON output contains type information.
return
}

_, _, ctx, err := HandleCidBaseOld(req, req.Context())
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

dserv := nd.DAG
if !resolve {
offlineexch := offline.Exchange(nd.Blockstore)
Expand All @@ -99,7 +106,7 @@ The JSON output contains type information.
ResolveOnce: uio.ResolveUnixfsOnce,
}

dagnode, err := core.Resolve(req.Context(), nd.Namesys, r, p)
dagnode, err := core.Resolve(ctx, nd.Namesys, r, p)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
Expand All @@ -120,7 +127,7 @@ The JSON output contains type information.
if dir == nil {
links = dagnode.Links()
} else {
links, err = dir.Links(req.Context())
links, err = dir.Links(ctx)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
Expand All @@ -140,7 +147,7 @@ The JSON output contains type information.
// No need to check with raw leaves
t = unixfspb.Data_File
case cid.DagProtobuf:
linkNode, err := link.GetNode(req.Context(), dserv)
linkNode, err := link.GetNode(ctx, dserv)
if err == ipld.ErrNotFound && !resolve {
// not an error
linkNode = nil
Expand All @@ -158,9 +165,10 @@ The JSON output contains type information.
t = d.GetType()
}
}
base := misc.GetCidBase(ctx, paths[i])
output[i].Links[j] = LsLink{
Name: link.Name,
Hash: link.Cid.String(),
Hash: link.Cid.Encode(base),
Size: link.Size,
Type: t,
}
Expand Down
24 changes: 15 additions & 9 deletions core/commands/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,14 @@ var addPinCmd = &cmds.Command{
}
showProgress, _, _ := req.Option("progress").Bool()

_, _, ctx, err := HandleCidBase(req, ctx)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

if !showProgress {
added, err := corerepo.Pin(n, req.Context(), req.Arguments(), recursive)
added, err := corerepo.Pin(n, ctx, req.Arguments(), recursive)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
Expand All @@ -92,7 +98,7 @@ var addPinCmd = &cmds.Command{
out := make(chan interface{})
res.SetOutput((<-chan interface{})(out))
v := new(dag.ProgressTracker)
ctx := v.DeriveContext(req.Context())
ctx := v.DeriveContext(ctx)

type pinResult struct {
pins []*cid.Cid
Expand Down Expand Up @@ -201,7 +207,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, ctx, req.Arguments(), recursive)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
Expand Down Expand Up @@ -305,9 +311,9 @@ Example:
var keys map[string]RefKeyObject

if len(req.Arguments()) > 0 {
keys, err = pinLsKeys(req.Context(), req.Arguments(), typeStr, n)
keys, err = pinLsKeys(ctx, req.Arguments(), typeStr, n)
} else {
keys, err = pinLsAll(req.Context(), typeStr, n)
keys, err = pinLsAll(ctx, typeStr, n)
}

if err != nil {
Expand Down Expand Up @@ -394,19 +400,19 @@ new pin and removing the old one.
ResolveOnce: uio.ResolveUnixfsOnce,
}

fromc, err := core.ResolveToCid(req.Context(), n.Namesys, r, from)
fromc, err := core.ResolveToCid(ctx, n.Namesys, r, from)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

toc, err := core.ResolveToCid(req.Context(), n.Namesys, r, to)
toc, err := core.ResolveToCid(ctx, n.Namesys, r, to)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

err = n.Pinning.Update(req.Context(), fromc, toc, unpin)
err = n.Pinning.Update(ctx, fromc, toc, unpin)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
Expand Down Expand Up @@ -454,7 +460,7 @@ var verifyPinCmd = &cmds.Command{
explain: !quiet,
includeOk: verbose,
}
out := pinVerify(req.Context(), n, opts)
out := pinVerify(ctx, n, opts)

res.SetOutput(out)
},
Expand Down
23 changes: 19 additions & 4 deletions core/commands/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"strings"

misc "github.com/ipfs/go-ipfs/core/misc"
cmds "github.com/ipfs/go-ipfs/commands"
"github.com/ipfs/go-ipfs/core"
e "github.com/ipfs/go-ipfs/core/commands/e"
Expand Down Expand Up @@ -106,6 +107,12 @@ NOTE: List all references recursively by using the flag '-r'.
format = "<src> -> <dst>"
}

_, _, ctx, err = HandleCidBaseOld(req, ctx)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

objs, err := objectsForPaths(ctx, n, req.Arguments())
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
Expand Down Expand Up @@ -158,6 +165,12 @@ Displays the hashes of all local objects.
return
}

base, _, ctx, err := HandleCidBaseOld(req, req.Context())
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

// todo: make async
allKeys, err := n.Blockstore.AllKeysChan(ctx)
if err != nil {
Expand All @@ -173,7 +186,7 @@ Displays the hashes of all local objects.

for k := range allKeys {
select {
case out <- &RefWrapper{Ref: k.String()}:
case out <- &RefWrapper{Ref: k.Encode(base)}:
case <-req.Context().Done():
return
}
Expand Down Expand Up @@ -323,15 +336,17 @@ func (rw *RefWriter) WriteEdge(from, to *cid.Cid, linkname string) error {
}
}

base := misc.GetCidBase(rw.Ctx, "")

var s string
switch {
case rw.PrintFmt != "":
s = rw.PrintFmt
s = strings.Replace(s, "<src>", from.String(), -1)
s = strings.Replace(s, "<dst>", to.String(), -1)
s = strings.Replace(s, "<src>", from.Encode(base), -1)
s = strings.Replace(s, "<dst>", to.Encode(base), -1)
s = strings.Replace(s, "<linkname>", linkname, -1)
default:
s += to.String()
s += to.Encode(base)
}

rw.out <- &RefWrapper{Ref: s}
Expand Down
15 changes: 12 additions & 3 deletions core/commands/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
cmds "github.com/ipfs/go-ipfs/commands"
"github.com/ipfs/go-ipfs/core"
e "github.com/ipfs/go-ipfs/core/commands/e"
misc "github.com/ipfs/go-ipfs/core/misc"
ns "github.com/ipfs/go-ipfs/namesys"
nsopts "github.com/ipfs/go-ipfs/namesys/opts"
path "github.com/ipfs/go-ipfs/path"
Expand Down Expand Up @@ -87,6 +88,12 @@ Resolve the value of an IPFS DAG path:
name := req.Arguments()[0]
recursive, _, _ := req.Option("recursive").Bool()

_, _, ctx, err := HandleCidBaseOld(req, req.Context())
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

// the case when ipns is resolved step by step
if strings.HasPrefix(name, "/ipns/") && !recursive {
rc, rcok, _ := req.Option("dht-record-count").Int()
Expand All @@ -107,7 +114,7 @@ Resolve the value of an IPFS DAG path:
}
ropts = append(ropts, nsopts.DhtTimeout(d))
}
p, err := n.Namesys.Resolve(req.Context(), name, ropts...)
p, err := n.Namesys.Resolve(ctx, name, ropts...)
// ErrResolveRecursion is fine
if err != nil && err != ns.ErrResolveRecursion {
res.SetError(err, cmdkit.ErrNormal)
Expand All @@ -124,15 +131,17 @@ Resolve the value of an IPFS DAG path:
return
}

node, err := core.Resolve(req.Context(), n.Namesys, n.Resolver, p)
node, err := core.Resolve(ctx, n.Namesys, n.Resolver, p)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

c := node.Cid()
base := misc.GetCidBase(ctx, name)
pathStr := "/ipfs/" + c.Encode(base)

res.SetOutput(&ResolvedPath{path.FromCid(c)})
res.SetOutput(&ResolvedPath{path.FromString(pathStr)})
},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
Expand Down
Loading