Skip to content

Commit

Permalink
Add support for global --cid-base option to more commands.
Browse files Browse the repository at this point in the history
Other related changes.

License: MIT
Signed-off-by: Kevin Atkinson <k@kevina.org>
  • Loading branch information
kevina committed Nov 22, 2018
1 parent bc37725 commit 2314b14
Show file tree
Hide file tree
Showing 19 changed files with 376 additions and 174 deletions.
8 changes: 8 additions & 0 deletions cmd/ipfs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
oldcmds "github.com/ipfs/go-ipfs/commands"
core "github.com/ipfs/go-ipfs/core"
corecmds "github.com/ipfs/go-ipfs/core/commands"
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
corehttp "github.com/ipfs/go-ipfs/core/corehttp"
loader "github.com/ipfs/go-ipfs/plugin/loader"
repo "github.com/ipfs/go-ipfs/repo"
Expand All @@ -28,6 +29,7 @@ import (
manet "gx/ipfs/QmQVUtnrNGtCRkCMpXgpApfzQjc8FDaDVxHqWH8cnZQeh5/go-multiaddr-net"
ma "gx/ipfs/QmRKLtwMw131aK7ugC3G7ybpumMz78YrJe5dzneyindvG1/go-multiaddr"
madns "gx/ipfs/QmT4zgnKCyZBpRyxzsvZqUjzUkMWLJ2pZCw7uk6M6Kto5m/go-multiaddr-dns"
cidenc "gx/ipfs/QmWf8NwKFLbTBvAvZst3bYF7WEEetzxWyMhvQ885cj9MM8/go-cidutil/cidenc"
osh "gx/ipfs/QmXuBJ7DR6k3rmUEKtvVMhwjmXDuJgXXPUt4LQXKBMsU93/go-os-helper"
"gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
"gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds/cli"
Expand Down Expand Up @@ -116,6 +118,12 @@ func mainRet() int {
}
log.Debugf("config path is %s", repoPath)

enc, err := cmdenv.ProcCidBase(req)
if err != nil {
return nil, err
}
cidenc.Default = enc

// this sets up the function that will initialize the node
// this is so that we can construct the node lazily.
return &oldcmds.Context{
Expand Down
5 changes: 0 additions & 5 deletions core/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,6 @@ You can now check what blocks have been created by:
outChan := make(chan interface{})
req := res.Request()

err := cmdenv.ProcCidBaseClientSide(req)
if err != nil {
return err
}

sizeFile, ok := req.Files.(files.SizeFile)
if ok {
// Could be slow.
Expand Down
16 changes: 4 additions & 12 deletions core/commands/cmdenv/cidbase.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ func ProcCidBase(req *cmds.Request) (cidenc.Encoder, error) {
if err != nil {
return e, err
}
if !upgradeDefined {
e.Upgrade = true
}
e.Upgrade = true
}

if upgradeDefined {
Expand All @@ -36,13 +34,7 @@ func ProcCidBase(req *cmds.Request) (cidenc.Encoder, error) {
return e, nil
}

// ProcCidBaseClientSide processes the `cid-base` and `output-cidv1`
// options and sets the default encoder based on those options
func ProcCidBaseClientSide(req *cmds.Request) error {
enc, err := ProcCidBase(req)
if err != nil {
return err
}
cidenc.Default = enc
return nil
func CidBaseDefined(req *cmds.Request) bool {
base, _ := req.Options["cid-base"].(string)
return base != ""
}
23 changes: 19 additions & 4 deletions core/commands/filestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
filestore "github.com/ipfs/go-ipfs/filestore"

cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
apicid "gx/ipfs/QmWf8NwKFLbTBvAvZst3bYF7WEEetzxWyMhvQ885cj9MM8/go-cidutil/apicid"
cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
)
Expand Down Expand Up @@ -175,6 +176,11 @@ For ERROR entries the error will also be printed to stderr.
Type: filestore.ListRes{},
}

type FilestoreDupsOutput struct {
Ref apicid.Hash
Err string
}

var dupsFileStore = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "List blocks that are both in the filestore and standard block storage.",
Expand All @@ -192,19 +198,28 @@ var dupsFileStore = &cmds.Command{
for cid := range ch {
have, err := fs.MainBlockstore().Has(cid)
if err != nil {
return res.Emit(&RefWrapper{Err: err.Error()})
return res.Emit(&FilestoreDupsOutput{Err: err.Error()})
}
if have {
if err := res.Emit(&RefWrapper{Ref: cid.String()}); err != nil {
if err := res.Emit(&FilestoreDupsOutput{Ref: apicid.FromCid(cid)}); err != nil {
return err
}
}
}

return nil
},
Encoders: refsEncoderMap,
Type: RefWrapper{},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *FilestoreDupsOutput) error {
if out.Err != "" {
return fmt.Errorf(out.Err)
}
fmt.Fprintln(w, out.Ref)

return nil
}),
},
Type: FilestoreDupsOutput{},
}

func getFilestore(env cmds.Environment) (*core.IpfsNode, *filestore.Filestore, error) {
Expand Down
10 changes: 6 additions & 4 deletions core/commands/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
uio "gx/ipfs/QmUnHNqhSB1JgzVCxL1Kz3yb4bdyB4q1Z9AD5AUBVmt3fZ/go-unixfs/io"
unixfspb "gx/ipfs/QmUnHNqhSB1JgzVCxL1Kz3yb4bdyB4q1Z9AD5AUBVmt3fZ/go-unixfs/pb"
blockservice "gx/ipfs/QmVDTbzzTwnuBwNbJdhW3u7LoBQp46bezm9yp4z1RoEepM/go-blockservice"
apicid "gx/ipfs/QmWf8NwKFLbTBvAvZst3bYF7WEEetzxWyMhvQ885cj9MM8/go-cidutil/apicid"
offline "gx/ipfs/QmYZwey1thDTynSrvd6qQkX24UpTka6TFhQ2v569UpoqxD/go-ipfs-exchange-offline"
cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
merkledag "gx/ipfs/QmcGt25mrjuB2kKW2zhPbXVZNHc4yoTDQ65NA8m6auP2f1/go-merkledag"
Expand All @@ -23,9 +24,10 @@ import (

// LsLink contains printable data for a single ipld link in ls output
type LsLink struct {
Name, Hash string
Size uint64
Type unixfspb.Data_DataType
Name string
Hash apicid.Hash
Size uint64
Type unixfspb.Data_DataType
}

// LsObject is an element of LsOutput
Expand Down Expand Up @@ -250,7 +252,7 @@ func makeLsLink(req *cmds.Request, dserv ipld.DAGService, resolve bool, link *ip
}
return &LsLink{
Name: link.Name,
Hash: link.Cid.String(),
Hash: apicid.FromCid(link.Cid),
Size: link.Size,
Type: t,
}, nil
Expand Down
57 changes: 31 additions & 26 deletions core/commands/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
bserv "gx/ipfs/QmVDTbzzTwnuBwNbJdhW3u7LoBQp46bezm9yp4z1RoEepM/go-blockservice"
apicid "gx/ipfs/QmWf8NwKFLbTBvAvZst3bYF7WEEetzxWyMhvQ885cj9MM8/go-cidutil/apicid"
"gx/ipfs/QmYMQuypUbgsdNHmuCBSUJV6wdQVsBHRivNAp3efHJwZJD/go-verifcid"
offline "gx/ipfs/QmYZwey1thDTynSrvd6qQkX24UpTka6TFhQ2v569UpoqxD/go-ipfs-exchange-offline"
cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
Expand All @@ -39,11 +40,11 @@ var PinCmd = &cmds.Command{
}

type PinOutput struct {
Pins []string
Pins []apicid.Hash
}

type AddPinOutput struct {
Pins []string
Pins []apicid.Hash
Progress int `json:",omitempty"`
}

Expand Down Expand Up @@ -92,7 +93,7 @@ var addPinCmd = &cmds.Command{
if err != nil {
return err
}
return cmds.EmitOnce(res, &AddPinOutput{Pins: cidsToStrings(added)})
return cmds.EmitOnce(res, &AddPinOutput{Pins: toAPICids(added)})
}

v := new(dag.ProgressTracker)
Expand Down Expand Up @@ -124,7 +125,7 @@ var addPinCmd = &cmds.Command{
return err
}
}
return res.Emit(&AddPinOutput{Pins: cidsToStrings(val.pins)})
return res.Emit(&AddPinOutput{Pins: toAPICids(val.pins)})
case <-ticker.C:
if err := res.Emit(&AddPinOutput{Progress: v.Value()}); err != nil {
return err
Expand Down Expand Up @@ -220,7 +221,7 @@ collected if needed. (By default, recursively. Use -r=false for direct pins.)
return err
}

return cmds.EmitOnce(res, &PinOutput{cidsToStrings(removed)})
return cmds.EmitOnce(res, &PinOutput{toAPICids(removed)})
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *PinOutput) error {
Expand Down Expand Up @@ -311,7 +312,7 @@ Example:
return err
}

var keys map[string]RefKeyObject
var keys map[apicid.Hash]RefKeyObject

if len(req.Arguments) > 0 {
keys, err = pinLsKeys(req.Context, req.Arguments, typeStr, n, api)
Expand Down Expand Up @@ -347,6 +348,10 @@ const (
pinUnpinOptionName = "unpin"
)

type UpdatePinOutput struct {
Pins []string // really paths
}

var updatePinCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Update a recursive pin",
Expand All @@ -364,7 +369,7 @@ new pin and removing the old one.
Options: []cmdkit.Option{
cmdkit.BoolOption(pinUnpinOptionName, "Remove the old pin.").WithDefault(true),
},
Type: PinOutput{},
Type: UpdatePinOutput{},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
if err != nil {
Expand All @@ -388,10 +393,10 @@ new pin and removing the old one.
return err
}

return cmds.EmitOnce(res, &PinOutput{Pins: []string{from.String(), to.String()}})
return cmds.EmitOnce(res, &UpdatePinOutput{Pins: []string{from.String(), to.String()}})
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *PinOutput) error {
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *UpdatePinOutput) error {
fmt.Fprintf(w, "updated %s to %s\n", out.Pins[0], out.Pins[1])
return nil
}),
Expand Down Expand Up @@ -452,17 +457,17 @@ type RefKeyObject struct {
}

type RefKeyList struct {
Keys map[string]RefKeyObject
Keys map[apicid.Hash]RefKeyObject
}

func pinLsKeys(ctx context.Context, args []string, typeStr string, n *core.IpfsNode, api iface.CoreAPI) (map[string]RefKeyObject, error) {
func pinLsKeys(ctx context.Context, args []string, typeStr string, n *core.IpfsNode, api iface.CoreAPI) (map[apicid.Hash]RefKeyObject, error) {

mode, ok := pin.StringToMode(typeStr)
if !ok {
return nil, fmt.Errorf("invalid pin mode '%s'", typeStr)
}

keys := make(map[string]RefKeyObject)
keys := make(map[apicid.Hash]RefKeyObject)

for _, p := range args {
pth, err := iface.ParsePath(p)
Expand All @@ -489,21 +494,21 @@ func pinLsKeys(ctx context.Context, args []string, typeStr string, n *core.IpfsN
default:
pinType = "indirect through " + pinType
}
keys[c.Cid().String()] = RefKeyObject{
keys[apicid.FromCid(c.Cid())] = RefKeyObject{
Type: pinType,
}
}

return keys, nil
}

func pinLsAll(ctx context.Context, typeStr string, n *core.IpfsNode) (map[string]RefKeyObject, error) {
func pinLsAll(ctx context.Context, typeStr string, n *core.IpfsNode) (map[apicid.Hash]RefKeyObject, error) {

keys := make(map[string]RefKeyObject)
keys := make(map[apicid.Hash]RefKeyObject)

AddToResultKeys := func(keyList []cid.Cid, typeStr string) {
for _, c := range keyList {
keys[c.String()] = RefKeyObject{
keys[apicid.FromCid(c)] = RefKeyObject{
Type: typeStr,
}
}
Expand Down Expand Up @@ -531,7 +536,7 @@ func pinLsAll(ctx context.Context, typeStr string, n *core.IpfsNode) (map[string

// PinVerifyRes is the result returned for each pin checked in "pin verify"
type PinVerifyRes struct {
Cid string
Cid apicid.Hash
PinStatus
}

Expand All @@ -543,7 +548,7 @@ type PinStatus struct {

// BadNode is used in PinVerifyRes
type BadNode struct {
Cid string
Cid apicid.Hash
Err string
}

Expand All @@ -553,7 +558,7 @@ type pinVerifyOpts struct {
}

func pinVerify(ctx context.Context, n *core.IpfsNode, opts pinVerifyOpts) <-chan interface{} {
visited := make(map[string]PinStatus)
visited := make(map[cid.Cid]PinStatus)

bs := n.Blocks.Blockstore()
DAG := dag.NewDAGService(bserv.New(bs, offline.Exchange(bs)))
Expand All @@ -562,15 +567,15 @@ func pinVerify(ctx context.Context, n *core.IpfsNode, opts pinVerifyOpts) <-chan

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

if err := verifcid.ValidateCid(root); err != nil {
status := PinStatus{Ok: false}
if opts.explain {
status.BadNodes = []BadNode{BadNode{Cid: key, Err: err.Error()}}
status.BadNodes = []BadNode{BadNode{Cid: apicid.FromCid(key), Err: err.Error()}}
}
visited[key] = status
return status
Expand All @@ -580,7 +585,7 @@ func pinVerify(ctx context.Context, n *core.IpfsNode, opts pinVerifyOpts) <-chan
if err != nil {
status := PinStatus{Ok: false}
if opts.explain {
status.BadNodes = []BadNode{BadNode{Cid: key, Err: err.Error()}}
status.BadNodes = []BadNode{BadNode{Cid: apicid.FromCid(key), Err: err.Error()}}
}
visited[key] = status
return status
Expand All @@ -606,7 +611,7 @@ func pinVerify(ctx context.Context, n *core.IpfsNode, opts pinVerifyOpts) <-chan
pinStatus := checkPin(cid)
if !pinStatus.Ok || opts.includeOk {
select {
case out <- &PinVerifyRes{cid.String(), pinStatus}:
case out <- &PinVerifyRes{apicid.FromCid(cid), pinStatus}:
case <-ctx.Done():
return
}
Expand All @@ -629,10 +634,10 @@ func (r PinVerifyRes) Format(out io.Writer) {
}
}

func cidsToStrings(cs []cid.Cid) []string {
out := make([]string, 0, len(cs))
func toAPICids(cs []cid.Cid) []apicid.Hash {
out := make([]apicid.Hash, 0, len(cs))
for _, c := range cs {
out = append(out, c.String())
out = append(out, apicid.FromCid(c))
}
return out
}
Loading

0 comments on commit 2314b14

Please sign in to comment.