diff --git a/core/commands/add.go b/core/commands/add.go index b7f3d6a8437..bf0ad7e6192 100644 --- a/core/commands/add.go +++ b/core/commands/add.go @@ -17,12 +17,12 @@ import ( mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash" pb "gx/ipfs/QmPtj12fdwuAqj9sBSTNUxBNu8kCGNp8b3o8yUzMm5GHpq/pb" - cidutil "gx/ipfs/QmQJSeE3CX4zos9qeaG8EhecEK9zvrTEfTG84J8C5NVRwt/go-cidutil" offline "gx/ipfs/QmR5miWuikPxWyUrzMYJVmFUcD44pGdtc98h9Qsbp4YcJw/go-ipfs-exchange-offline" cmdkit "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit" files "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit/files" cmds "gx/ipfs/QmXTmUCBtDUrzDYVzASogLiNph7EBuYqEgPL7QoHNMzUnz/go-ipfs-cmds" - mfs "gx/ipfs/QmahrY1adY4wvtYEtoGjpZ2GUohTyukrkMkwUR9ytRjTG2/go-mfs" + mfs "gx/ipfs/QmaxgEmTteUHnA1PQrKwHqHme1WQg3Fep9AmdbUGa4t6Wf/go-mfs" + cidutil "gx/ipfs/QmdPF1WZQHFNfLdwhaShiR3e4KvFviAM58TrxVxPMhukic/go-cidutil" bstore "gx/ipfs/QmdriVJgKx4JADRgh3cYPXqXmsa1A45SvFki1nDWHhQNtC/go-ipfs-blockstore" ) @@ -367,6 +367,11 @@ You can now check what blocks have been created by: log.Warning("cannot determine size of input file") } + _, err := cmdenv.NewCidBaseHandler(req).UseGlobal().Proc() + if err != nil { + return err + } + progressBar := func(wait chan struct{}) { defer close(wait) @@ -402,8 +407,9 @@ You can now check what blocks have been created by: break LOOP } output := out.(*coreunix.AddedObject) - if len(output.Hash) > 0 { - lastHash = output.Hash + hash := output.Hash.String() + if len(hash) > 0 { + lastHash = hash if quieter { continue } @@ -413,9 +419,9 @@ You can now check what blocks have been created by: fmt.Fprintf(os.Stderr, "\033[2K\r") } if quiet { - fmt.Fprintf(os.Stdout, "%s\n", output.Hash) + fmt.Fprintf(os.Stdout, "%s\n", hash) } else { - fmt.Fprintf(os.Stdout, "added %s %s\n", output.Hash, output.Name) + fmt.Fprintf(os.Stdout, "added %s %s\n", hash, output.Name) } } else { diff --git a/core/commands/cmdenv/cidbase.go b/core/commands/cmdenv/cidbase.go new file mode 100644 index 00000000000..cf8d683be03 --- /dev/null +++ b/core/commands/cmdenv/cidbase.go @@ -0,0 +1,122 @@ +package cmdenv + +import ( + oldcmds "github.com/ipfs/go-ipfs/commands" + + cmdkit "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit" + cmds "gx/ipfs/QmXTmUCBtDUrzDYVzASogLiNph7EBuYqEgPL7QoHNMzUnz/go-ipfs-cmds" + cidenc "gx/ipfs/QmdPF1WZQHFNfLdwhaShiR3e4KvFviAM58TrxVxPMhukic/go-cidutil/cidenc" + mbase "gx/ipfs/QmekxXDhCxCJRNuzmHreuaT3BsuJcsjcXWNrtV9C8DRHtd/go-multibase" +) + +var OptionCidBase = cmdkit.StringOption("cid-base", "mbase", "Multi-base encoding used for version 1 CIDs in output.") +var OptionOutputCidV1 = cmdkit.BoolOption("output-cidv1", "Upgrade CID version 0 to version 1 in output.") + +// CidBaseHandler is a helper class to process the `--cid-base` and +// `--output-cidv1` options. In the future it may also be used to +// process relevant config settings. +// +// Several of its methods return the class itself in order to allow +// easy chaining, a typical usage would be +// `cmdenv.NewCidBaseHandler(req).UseGlobal().Proc()` or +// `cmdenv.NewCidBaseHandlerLegacy(req).Proc()`. +type CidBaseHandler struct { + base string + upgrade bool + upgradeDefined bool + args []string + enc *cidenc.Encoder +} + +// NewCidBaseHandler created a CidBaseHandler from a request +func NewCidBaseHandler(req *cmds.Request) *CidBaseHandler { + h := &CidBaseHandler{} + h.base, _ = req.Options["cid-base"].(string) + h.upgrade, h.upgradeDefined = req.Options["output-cidv1"].(bool) + h.args = req.Arguments + return h +} + +// NewCidBaseHandlerLegacy created a CidBaseHandler from a request +// using the old commands library +func NewCidBaseHandlerLegacy(req oldcmds.Request) *CidBaseHandler { + h := &CidBaseHandler{} + h.base, _, _ = req.Option("cid-base").String() + h.upgrade, h.upgradeDefined, _ = req.Option("output-cidv1").Bool() + h.args = req.Arguments() + return h +} + +// UseGlobal enables the use of the global default. This is somewhat +// of a hack and should be used with care. In particular it should +// only be used on the client side and not the server side. +func (h *CidBaseHandler) UseGlobal() *CidBaseHandler { + h.enc = &cidenc.Default + return h +} + +// Proc processes the `--cid-base` and `--output-cidv1` options. If +// UseGlobal was enabled, it will change the value of the global +// default. +func (h *CidBaseHandler) Proc() (*CidBaseHandler, error) { + e := cidenc.Default + + if h.base != "" { + var err error + e.Base, err = mbase.EncoderByName(h.base) + if err != nil { + return h, err + } + if !h.upgradeDefined { + e.Upgrade = true + } + } + + if h.upgradeDefined { + e.Upgrade = h.upgrade + } + + if h.enc == nil { + h.enc = &cidenc.Encoder{} + } + *h.enc = e + return h, nil +} + +// Encoder returns a copy of the underlying Encoder +func (h *CidBaseHandler) Encoder() cidenc.Encoder { + return *h.enc +} + +// EncoderFromPath returns a new Encoder that will format CIDs like +// the one in the path if the `--cid-base` option is not used. (If +// the `--cid-base` is used then a copy of the base encoder will be +// returned.) In particular: if the path contains a version 1 CID +// then all CIDs will be outputting using the same multibase. if the +// path contains a version 0 CID then version 0 CIDs will be outputted +// as is and version 1 cids will use the multibase from the base +// encoder +func (h *CidBaseHandler) EncoderFromPath(p string) cidenc.Encoder { + if h.base == "" { + enc, _ := cidenc.FromPath(*h.enc, p) + return enc + } else { + return *h.enc + } +} + +// EncoderWithOverride returns a new encoder that will use the setting +// from the base encoder unless it is a CID that was specified on the +// command line and the `--cid-base` option was not used. (If the +// `--cid-base` is used then a copy of the base encoder will be +// returned.) In that case the same CID string as specified on the +// command line will be used. +func (h *CidBaseHandler) EncoderWithOverride() cidenc.Interface { + if h.base == "" { + enc := cidenc.NewOverride(*h.enc) + enc.Add(h.args...) + return enc + } else { + return *h.enc + } +} diff --git a/core/commands/files.go b/core/commands/files.go index 7ad62dabfea..01e8aec0fa9 100644 --- a/core/commands/files.go +++ b/core/commands/files.go @@ -30,8 +30,9 @@ import ( cmdkit "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit" cmds "gx/ipfs/QmXTmUCBtDUrzDYVzASogLiNph7EBuYqEgPL7QoHNMzUnz/go-ipfs-cmds" logging "gx/ipfs/QmZChCsSt8DctjceaL56Eibc29CVQq4dGKRXC5JRZ6Ppae/go-log" - mfs "gx/ipfs/QmahrY1adY4wvtYEtoGjpZ2GUohTyukrkMkwUR9ytRjTG2/go-mfs" + mfs "gx/ipfs/QmaxgEmTteUHnA1PQrKwHqHme1WQg3Fep9AmdbUGa4t6Wf/go-mfs" ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format" + apicid "gx/ipfs/QmdPF1WZQHFNfLdwhaShiR3e4KvFviAM58TrxVxPMhukic/go-cidutil/apicid" ) var flog = logging.Logger("cmds/files") @@ -77,7 +78,7 @@ var hashOption = cmdkit.StringOption("hash", "Hash function to use. Will set Cid var errFormat = errors.New("format was set by multiple options. Only one format option is allowed") type statOutput struct { - Hash string + Hash apicid.Hash Size uint64 CumulativeSize uint64 Blocks int @@ -162,13 +163,18 @@ var filesStatCmd = &cmds.Command{ }, Encoders: cmds.EncoderMap{ cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error { + _, err := cmdenv.NewCidBaseHandler(req).UseGlobal().Proc() + if err != nil { + return err + } + out, ok := v.(*statOutput) if !ok { return e.TypeErr(out, v) } s, _ := statGetFormatOptions(req) - s = strings.Replace(s, "", out.Hash, -1) + s = strings.Replace(s, "", out.Hash.String(), -1) s = strings.Replace(s, "", fmt.Sprintf("%d", out.Size), -1) s = strings.Replace(s, "", fmt.Sprintf("%d", out.CumulativeSize), -1) s = strings.Replace(s, "", fmt.Sprintf("%d", out.Blocks), -1) @@ -239,7 +245,7 @@ func statNode(nd ipld.Node) (*statOutput, error) { } return &statOutput{ - Hash: c.String(), + Hash: apicid.FromCid(c), Blocks: len(nd.Links()), Size: d.FileSize(), CumulativeSize: cumulsize, @@ -247,7 +253,7 @@ func statNode(nd ipld.Node) (*statOutput, error) { }, nil case *dag.RawNode: return &statOutput{ - Hash: c.String(), + Hash: apicid.FromCid(c), Blocks: 0, Size: cumulsize, CumulativeSize: cumulsize, @@ -476,7 +482,7 @@ Examples: res.SetError(err, cmdkit.ErrNormal) return } - out.Entries[0].Hash = nd.Cid().String() + out.Entries[0].Hash = apicid.FromCid(nd.Cid()) } res.SetOutput(out) return @@ -486,6 +492,11 @@ Examples: }, Marshalers: oldcmds.MarshalerMap{ oldcmds.Text: func(res oldcmds.Response) (io.Reader, error) { + _, err := cmdenv.NewCidBaseHandlerLegacy(res.Request()).UseGlobal().Proc() + if err != nil { + return nil, err + } + v, err := unwrapOutput(res.Output()) if err != nil { return nil, err diff --git a/core/commands/filestore.go b/core/commands/filestore.go index a45e01ef93f..fa0e2a7c786 100644 --- a/core/commands/filestore.go +++ b/core/commands/filestore.go @@ -74,6 +74,11 @@ The output is: }, PostRun: cmds.PostRunMap{ cmds.CLI: func(res cmds.Response, re cmds.ResponseEmitter) error { + _, err := cmdenv.NewCidBaseHandler(res.Request()).UseGlobal().Proc() + if err != nil { + return err + } + var errors bool for { v, err := res.Next() @@ -162,6 +167,11 @@ For ERROR entries the error will also be printed to stderr. }, Marshalers: oldCmds.MarshalerMap{ oldCmds.Text: func(res oldCmds.Response) (io.Reader, error) { + _, err := cmdenv.NewCidBaseHandlerLegacy(res.Request()).UseGlobal().Proc() + if err != nil { + return nil, err + } + v, err := unwrapOutput(res.Output()) if err != nil { return nil, err @@ -198,6 +208,13 @@ var dupsFileStore = &oldCmds.Command{ return } + h, err := cmdenv.NewCidBaseHandlerLegacy(req).Proc() + if err != nil { + res.SetError(err, cmdkit.ErrNormal) + return + } + enc := h.Encoder() + out := make(chan interface{}, 128) res.SetOutput((<-chan interface{})(out)) @@ -214,7 +231,7 @@ var dupsFileStore = &oldCmds.Command{ } if have { select { - case out <- &RefWrapper{Ref: cid.String()}: + case out <- &RefWrapper{Ref: enc.Encode(cid)}: case <-req.Context().Done(): return } diff --git a/core/commands/ls.go b/core/commands/ls.go index 491ab671a4d..d4adccb1d24 100644 --- a/core/commands/ls.go +++ b/core/commands/ls.go @@ -8,6 +8,7 @@ import ( cmds "github.com/ipfs/go-ipfs/commands" 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" unixfs "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs" uio "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs/io" @@ -21,12 +22,14 @@ import ( offline "gx/ipfs/QmR5miWuikPxWyUrzMYJVmFUcD44pGdtc98h9Qsbp4YcJw/go-ipfs-exchange-offline" "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit" ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format" + apicid "gx/ipfs/QmdPF1WZQHFNfLdwhaShiR3e4KvFviAM58TrxVxPMhukic/go-cidutil/apicid" ) type LsLink struct { - Name, Hash string - Size uint64 - Type unixfspb.Data_DataType + Name string + Hash apicid.Hash + Size uint64 + Type unixfspb.Data_DataType } type LsObject struct { @@ -160,7 +163,7 @@ The JSON output contains type information. } output[i].Links[j] = LsLink{ Name: link.Name, - Hash: link.Cid.String(), + Hash: apicid.FromCid(link.Cid), Size: link.Size, Type: t, } @@ -171,6 +174,10 @@ The JSON output contains type information. }, Marshalers: cmds.MarshalerMap{ cmds.Text: func(res cmds.Response) (io.Reader, error) { + _, err := cmdenv.NewCidBaseHandlerLegacy(res.Request()).UseGlobal().Proc() + if err != nil { + return nil, err + } v, err := unwrapOutput(res.Output()) if err != nil { diff --git a/core/commands/pin.go b/core/commands/pin.go index bb6540fa5ec..c1ab54451ce 100644 --- a/core/commands/pin.go +++ b/core/commands/pin.go @@ -9,6 +9,7 @@ import ( cmds "github.com/ipfs/go-ipfs/commands" 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" corerepo "github.com/ipfs/go-ipfs/core/corerepo" pin "github.com/ipfs/go-ipfs/pin" @@ -22,6 +23,7 @@ import ( bserv "gx/ipfs/QmcRecCZWM2NZfCQrCe97Ch3Givv8KKEP82tGUDntzdLFe/go-blockservice" path "gx/ipfs/QmcjwUb36Z16NJkvDX6ccXPqsFswo6AsRXynyXcLLCphV2/go-path" resolver "gx/ipfs/QmcjwUb36Z16NJkvDX6ccXPqsFswo6AsRXynyXcLLCphV2/go-path/resolver" + apicid "gx/ipfs/QmdPF1WZQHFNfLdwhaShiR3e4KvFviAM58TrxVxPMhukic/go-cidutil/apicid" ) var PinCmd = &cmds.Command{ @@ -39,11 +41,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"` } @@ -84,7 +86,7 @@ var addPinCmd = &cmds.Command{ res.SetError(err, cmdkit.ErrNormal) return } - res.SetOutput(&AddPinOutput{Pins: cidsToStrings(added)}) + res.SetOutput(&AddPinOutput{Pins: toAPICids(added)}) return } @@ -117,7 +119,7 @@ var addPinCmd = &cmds.Command{ if pv := v.Value(); pv != 0 { out <- &AddPinOutput{Progress: v.Value()} } - out <- &AddPinOutput{Pins: cidsToStrings(val.pins)} + out <- &AddPinOutput{Pins: toAPICids(val.pins)} return case <-ticker.C: out <- &AddPinOutput{Progress: v.Value()} @@ -130,12 +132,16 @@ var addPinCmd = &cmds.Command{ }, Marshalers: cmds.MarshalerMap{ cmds.Text: func(res cmds.Response) (io.Reader, error) { + _, err := cmdenv.NewCidBaseHandlerLegacy(res.Request()).UseGlobal().Proc() + if err != nil { + return nil, err + } v, err := unwrapOutput(res.Output()) if err != nil { return nil, err } - var added []string + var added []apicid.Hash switch out := v.(type) { case *AddPinOutput: @@ -206,10 +212,15 @@ collected if needed. (By default, recursively. Use -r=false for direct pins.) return } - res.SetOutput(&PinOutput{cidsToStrings(removed)}) + res.SetOutput(&PinOutput{toAPICids(removed)}) }, Marshalers: cmds.MarshalerMap{ cmds.Text: func(res cmds.Response) (io.Reader, error) { + _, err := cmdenv.NewCidBaseHandlerLegacy(res.Request()).UseGlobal().Proc() + if err != nil { + return nil, err + } + v, err := unwrapOutput(res.Output()) if err != nil { return nil, err @@ -301,7 +312,7 @@ Example: return } - 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) @@ -318,6 +329,11 @@ Example: Type: RefKeyList{}, Marshalers: cmds.MarshalerMap{ cmds.Text: func(res cmds.Response) (io.Reader, error) { + _, err := cmdenv.NewCidBaseHandlerLegacy(res.Request()).UseGlobal().Proc() + if err != nil { + return nil, err + } + v, err := unwrapOutput(res.Output()) if err != nil { return nil, err @@ -345,6 +361,10 @@ Example: }, } +type UpdatePinOutput struct { + Pins []string // really paths +} + var updatePinCmd = &cmds.Command{ Helptext: cmdkit.HelpText{ Tagline: "Update a recursive pin", @@ -362,7 +382,7 @@ new pin and removing the old one. Options: []cmdkit.Option{ cmdkit.BoolOption("unpin", "Remove the old pin.").WithDefault(true), }, - Type: PinOutput{}, + Type: UpdatePinOutput{}, Run: func(req cmds.Request, res cmds.Response) { n, err := req.InvocContext().GetNode() if err != nil { @@ -417,7 +437,7 @@ new pin and removing the old one. return } - res.SetOutput(&PinOutput{Pins: []string{from.String(), to.String()}}) + res.SetOutput(&UpdatePinOutput{Pins: []string{from.String(), to.String()}}) }, Marshalers: cmds.MarshalerMap{ cmds.Text: func(res cmds.Response) (io.Reader, error) { @@ -425,7 +445,7 @@ new pin and removing the old one. if err != nil { return nil, err } - added, ok := v.(*PinOutput) + added, ok := v.(*UpdatePinOutput) if !ok { return nil, e.TypeErr(added, v) } @@ -470,6 +490,11 @@ var verifyPinCmd = &cmds.Command{ Type: PinVerifyRes{}, Marshalers: cmds.MarshalerMap{ cmds.Text: func(res cmds.Response) (io.Reader, error) { + _, err := cmdenv.NewCidBaseHandlerLegacy(res.Request()).UseGlobal().Proc() + if err != nil { + return nil, err + } + quiet, _, _ := res.Request().Option("quiet").Bool() out, err := unwrapOutput(res.Output()) @@ -498,17 +523,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) (map[string]RefKeyObject, error) { +func pinLsKeys(ctx context.Context, args []string, typeStr string, n *core.IpfsNode) (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) r := &resolver.Resolver{ DAG: n.DAG, @@ -540,7 +565,7 @@ func pinLsKeys(ctx context.Context, args []string, typeStr string, n *core.IpfsN default: pinType = "indirect through " + pinType } - keys[c.String()] = RefKeyObject{ + keys[apicid.FromCid(c)] = RefKeyObject{ Type: pinType, } } @@ -548,13 +573,13 @@ func pinLsKeys(ctx context.Context, args []string, typeStr string, n *core.IpfsN 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, } } @@ -582,7 +607,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 } @@ -594,7 +619,7 @@ type PinStatus struct { // BadNode is used in PinVerifyRes type BadNode struct { - Cid string + Cid apicid.Hash Err string } @@ -604,7 +629,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))) @@ -613,7 +638,7 @@ 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 } @@ -621,7 +646,7 @@ func pinVerify(ctx context.Context, n *core.IpfsNode, opts pinVerifyOpts) <-chan 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 @@ -631,7 +656,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 @@ -657,7 +682,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 } @@ -680,10 +705,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 } diff --git a/core/commands/refs.go b/core/commands/refs.go index c28af531719..8ca4beb2f0d 100644 --- a/core/commands/refs.go +++ b/core/commands/refs.go @@ -9,7 +9,9 @@ import ( cmds "github.com/ipfs/go-ipfs/commands" "github.com/ipfs/go-ipfs/core" + cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv" e "github.com/ipfs/go-ipfs/core/commands/e" + cidenc "gx/ipfs/QmdPF1WZQHFNfLdwhaShiR3e4KvFviAM58TrxVxPMhukic/go-cidutil/cidenc" cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid" cmdkit "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit" @@ -117,6 +119,13 @@ NOTE: List all references recursively by using the flag '-r'. format = " -> " } + h, err := cmdenv.NewCidBaseHandlerLegacy(req).Proc() + if err != nil { + res.SetError(err, cmdkit.ErrNormal) + return + } + enc := h.EncoderWithOverride() + objs, err := objectsForPaths(ctx, n, req.Arguments()) if err != nil { res.SetError(err, cmdkit.ErrNormal) @@ -136,6 +145,7 @@ NOTE: List all references recursively by using the flag '-r'. Unique: unique, PrintFmt: format, MaxDepth: maxDepth, + Encoder: enc, } for _, o := range objs { @@ -169,6 +179,13 @@ Displays the hashes of all local objects. return } + h, err := cmdenv.NewCidBaseHandlerLegacy(req).Proc() + if err != nil { + res.SetError(err, cmdkit.ErrNormal) + return + } + enc := h.Encoder() + // todo: make async allKeys, err := n.Blockstore.AllKeysChan(ctx) if err != nil { @@ -184,7 +201,7 @@ Displays the hashes of all local objects. for k := range allKeys { select { - case out <- &RefWrapper{Ref: k.String()}: + case out <- &RefWrapper{Ref: enc.Encode(k)}: case <-req.Context().Done(): return } @@ -197,6 +214,11 @@ Displays the hashes of all local objects. var refsMarshallerMap = cmds.MarshalerMap{ cmds.Text: func(res cmds.Response) (io.Reader, error) { + _, err := cmdenv.NewCidBaseHandlerLegacy(res.Request()).UseGlobal().Proc() + if err != nil { + return nil, err + } + v, err := unwrapOutput(res.Output()) if err != nil { return nil, err @@ -245,6 +267,7 @@ type RefWriter struct { Unique bool MaxDepth int PrintFmt string + Encoder cidenc.Interface seen map[string]int } @@ -379,11 +402,11 @@ func (rw *RefWriter) WriteEdge(from, to cid.Cid, linkname string) error { switch { case rw.PrintFmt != "": s = rw.PrintFmt - s = strings.Replace(s, "", from.String(), -1) - s = strings.Replace(s, "", to.String(), -1) + s = strings.Replace(s, "", rw.Encoder.Encode(from), -1) + s = strings.Replace(s, "", rw.Encoder.Encode(to), -1) s = strings.Replace(s, "", linkname, -1) default: - s += to.String() + s += rw.Encoder.Encode(to) } rw.out <- &RefWrapper{Ref: s} diff --git a/core/commands/resolve.go b/core/commands/resolve.go index 39963b6da95..45254cab481 100644 --- a/core/commands/resolve.go +++ b/core/commands/resolve.go @@ -89,6 +89,12 @@ Resolve the value of an IPFS DAG path: name := req.Arguments[0] recursive, _ := req.Options["recursive"].(bool) + h, err := cmdenv.NewCidBaseHandler(req).Proc() + if err != nil { + return err + } + enc := h.EncoderFromPath(name) + // the case when ipns is resolved step by step if strings.HasPrefix(name, "/ipns/") && !recursive { rc, rcok := req.Options["dht-record-count"].(uint) @@ -131,7 +137,7 @@ Resolve the value of an IPFS DAG path: c := rp.Cid() - return cmds.EmitOnce(res, &ncmd.ResolvedPath{Path: path.FromCid(c)}) + return cmds.EmitOnce(res, &ncmd.ResolvedPath{Path: path.FromString("/ipfs/" + enc.Encode(c))}) }, Encoders: cmds.EncoderMap{ cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error { diff --git a/core/commands/root.go b/core/commands/root.go index 0268136f610..5968185b2d5 100644 --- a/core/commands/root.go +++ b/core/commands/root.go @@ -7,6 +7,7 @@ import ( oldcmds "github.com/ipfs/go-ipfs/commands" lgc "github.com/ipfs/go-ipfs/commands/legacy" + cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv" dag "github.com/ipfs/go-ipfs/core/commands/dag" e "github.com/ipfs/go-ipfs/core/commands/e" name "github.com/ipfs/go-ipfs/core/commands/name" @@ -97,6 +98,9 @@ The CLI will exit with one of the following values: cmdkit.StringOption(ApiOption, "Use a specific API instance (defaults to /ip4/127.0.0.1/tcp/5001)"), // global options, added to every command + cmdenv.OptionCidBase, + cmdenv.OptionOutputCidV1, + cmds.OptionEncodingType, cmds.OptionStreamChannels, cmds.OptionTimeout, diff --git a/core/commands/tar.go b/core/commands/tar.go index a2fa3da20cc..981ea584c7e 100644 --- a/core/commands/tar.go +++ b/core/commands/tar.go @@ -6,6 +6,7 @@ import ( cmds "github.com/ipfs/go-ipfs/commands" 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" "github.com/ipfs/go-ipfs/core/coreunix" tar "github.com/ipfs/go-ipfs/tar" @@ -13,6 +14,7 @@ import ( path "gx/ipfs/QmcjwUb36Z16NJkvDX6ccXPqsFswo6AsRXynyXcLLCphV2/go-path" "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit" + apicid "gx/ipfs/QmdPF1WZQHFNfLdwhaShiR3e4KvFviAM58TrxVxPMhukic/go-cidutil/apicid" ) var TarCmd = &cmds.Command{ @@ -62,12 +64,17 @@ represent it. fi.FileName() res.SetOutput(&coreunix.AddedObject{ Name: fi.FileName(), - Hash: c.String(), + Hash: apicid.FromCid(c), }) }, Type: coreunix.AddedObject{}, Marshalers: cmds.MarshalerMap{ cmds.Text: func(res cmds.Response) (io.Reader, error) { + _, err := cmdenv.NewCidBaseHandlerLegacy(res.Request()).UseGlobal().Proc() + if err != nil { + return nil, err + } + v, err := unwrapOutput(res.Output()) if err != nil { return nil, err @@ -77,7 +84,7 @@ represent it. if !ok { return nil, e.TypeErr(o, v) } - return strings.NewReader(o.Hash + "\n"), nil + return strings.NewReader(o.Hash.String() + "\n"), nil }, }, } diff --git a/core/commands/unixfs/ls.go b/core/commands/unixfs/ls.go index 598823cfa9b..139f19d1858 100644 --- a/core/commands/unixfs/ls.go +++ b/core/commands/unixfs/ls.go @@ -9,6 +9,9 @@ import ( cmdkit "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit" + cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv" + apicid "gx/ipfs/QmdPF1WZQHFNfLdwhaShiR3e4KvFviAM58TrxVxPMhukic/go-cidutil/apicid" + cmds "github.com/ipfs/go-ipfs/commands" core "github.com/ipfs/go-ipfs/core" e "github.com/ipfs/go-ipfs/core/commands/e" @@ -26,7 +29,7 @@ type LsLink struct { } type LsObject struct { - Hash string + Hash apicid.Hash Size uint64 Type string Links []LsLink @@ -128,7 +131,7 @@ possible, please use 'ipfs ls' instead. t := unixFSNode.Type() output.Objects[hash] = &LsObject{ - Hash: c.String(), + Hash: apicid.FromCid(c), Type: t.String(), Size: unixFSNode.FileSize(), } @@ -186,6 +189,11 @@ possible, please use 'ipfs ls' instead. }, Marshalers: cmds.MarshalerMap{ cmds.Text: func(res cmds.Response) (io.Reader, error) { + _, err := cmdenv.NewCidBaseHandlerLegacy(res.Request()).UseGlobal().Proc() + if err != nil { + return nil, err + } + v, err := unwrapOutput(res.Output()) if err != nil { return nil, err diff --git a/core/core.go b/core/core.go index dc58a48338a..e17c0255f19 100644 --- a/core/core.go +++ b/core/core.go @@ -65,7 +65,7 @@ import ( dht "gx/ipfs/QmZVakpN44VAUxs9eXAuUGLFYTCGmSyqSy6hyEKfMv68ME/go-libp2p-kad-dht" dhtopts "gx/ipfs/QmZVakpN44VAUxs9eXAuUGLFYTCGmSyqSy6hyEKfMv68ME/go-libp2p-kad-dht/opts" pnet "gx/ipfs/QmZaQ3K9PRd5sYYoG1xbTGPtd3N7TYiKBRmcBUTsx8HVET/go-libp2p-pnet" - mfs "gx/ipfs/QmahrY1adY4wvtYEtoGjpZ2GUohTyukrkMkwUR9ytRjTG2/go-mfs" + mfs "gx/ipfs/QmaxgEmTteUHnA1PQrKwHqHme1WQg3Fep9AmdbUGa4t6Wf/go-mfs" peer "gx/ipfs/QmbNepETomvmXfz1X5pHNFD2QuPqnqi47dTd94QJWSorQ3/go-libp2p-peer" merkledag "gx/ipfs/QmcBoNcAP6qDjgRBew7yjvCqHq7p5jMstE44jPUBWBxzsV/go-merkledag" bserv "gx/ipfs/QmcRecCZWM2NZfCQrCe97Ch3Givv8KKEP82tGUDntzdLFe/go-blockservice" diff --git a/core/coreapi/dht.go b/core/coreapi/dht.go index 388b33b852d..0b950d09857 100644 --- a/core/coreapi/dht.go +++ b/core/coreapi/dht.go @@ -9,12 +9,12 @@ import ( caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options" cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid" - cidutil "gx/ipfs/QmQJSeE3CX4zos9qeaG8EhecEK9zvrTEfTG84J8C5NVRwt/go-cidutil" offline "gx/ipfs/QmR5miWuikPxWyUrzMYJVmFUcD44pGdtc98h9Qsbp4YcJw/go-ipfs-exchange-offline" routing "gx/ipfs/QmVBnJDKhtFXTRVjXKinqpwGu8t1DyNqPKan2iGX8PR8xG/go-libp2p-routing" peer "gx/ipfs/QmbNepETomvmXfz1X5pHNFD2QuPqnqi47dTd94QJWSorQ3/go-libp2p-peer" dag "gx/ipfs/QmcBoNcAP6qDjgRBew7yjvCqHq7p5jMstE44jPUBWBxzsV/go-merkledag" blockservice "gx/ipfs/QmcRecCZWM2NZfCQrCe97Ch3Givv8KKEP82tGUDntzdLFe/go-blockservice" + cidutil "gx/ipfs/QmdPF1WZQHFNfLdwhaShiR3e4KvFviAM58TrxVxPMhukic/go-cidutil" blockstore "gx/ipfs/QmdriVJgKx4JADRgh3cYPXqXmsa1A45SvFki1nDWHhQNtC/go-ipfs-blockstore" pstore "gx/ipfs/QmfAQMFpgDU2U4BXG64qVr8HSiictfWvkSBz7Y2oDj65st/go-libp2p-peerstore" ) diff --git a/core/corerepo/gc.go b/core/corerepo/gc.go index 032557a5f36..72e5476fd38 100644 --- a/core/corerepo/gc.go +++ b/core/corerepo/gc.go @@ -13,7 +13,7 @@ import ( humanize "gx/ipfs/QmPSBJL4momYnE7DcUyk2DVhD6rH488ZmHBGLbxNdhU44K/go-humanize" cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid" logging "gx/ipfs/QmZChCsSt8DctjceaL56Eibc29CVQq4dGKRXC5JRZ6Ppae/go-log" - mfs "gx/ipfs/QmahrY1adY4wvtYEtoGjpZ2GUohTyukrkMkwUR9ytRjTG2/go-mfs" + mfs "gx/ipfs/QmaxgEmTteUHnA1PQrKwHqHme1WQg3Fep9AmdbUGa4t6Wf/go-mfs" ) var log = logging.Logger("corerepo") diff --git a/core/coreunix/add.go b/core/coreunix/add.go index ad8450f6a2b..832335cb800 100644 --- a/core/coreunix/add.go +++ b/core/coreunix/add.go @@ -23,8 +23,9 @@ import ( files "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit/files" chunker "gx/ipfs/QmULKgr55cSWR8Kiwy3cVRcAiGVnR6EVSaB7hJcWS4138p/go-ipfs-chunker" logging "gx/ipfs/QmZChCsSt8DctjceaL56Eibc29CVQq4dGKRXC5JRZ6Ppae/go-log" - mfs "gx/ipfs/QmahrY1adY4wvtYEtoGjpZ2GUohTyukrkMkwUR9ytRjTG2/go-mfs" + mfs "gx/ipfs/QmaxgEmTteUHnA1PQrKwHqHme1WQg3Fep9AmdbUGa4t6Wf/go-mfs" ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format" + apicid "gx/ipfs/QmdPF1WZQHFNfLdwhaShiR3e4KvFviAM58TrxVxPMhukic/go-cidutil/apicid" bstore "gx/ipfs/QmdriVJgKx4JADRgh3cYPXqXmsa1A45SvFki1nDWHhQNtC/go-ipfs-blockstore" ) @@ -41,16 +42,16 @@ type Link struct { } type Object struct { - Hash string + Hash apicid.Hash Links []Link Size string } type AddedObject struct { Name string - Hash string `json:",omitempty"` - Bytes int64 `json:",omitempty"` - Size string `json:",omitempty"` + Hash apicid.Hash `json:",omitempty"` + Bytes int64 `json:",omitempty"` + Size string `json:",omitempty"` } // NewAdder Returns a new Adder used for a file add operation. @@ -564,7 +565,7 @@ func getOutput(dagnode ipld.Node) (*Object, error) { } output := &Object{ - Hash: c.String(), + Hash: apicid.FromCid(c), Size: strconv.FormatUint(s, 10), Links: make([]Link, len(dagnode.Links())), } diff --git a/core/coreunix/add_test.go b/core/coreunix/add_test.go index d2c9c7337d4..2e190f89818 100644 --- a/core/coreunix/add_test.go +++ b/core/coreunix/add_test.go @@ -23,6 +23,7 @@ import ( config "gx/ipfs/QmVBUpxsHh53rNcufqxMpLAmz37eGyLJUaexDy1W9YkiNk/go-ipfs-config" dag "gx/ipfs/QmcBoNcAP6qDjgRBew7yjvCqHq7p5jMstE44jPUBWBxzsV/go-merkledag" "gx/ipfs/QmcRecCZWM2NZfCQrCe97Ch3Givv8KKEP82tGUDntzdLFe/go-blockservice" + apicid "gx/ipfs/QmdPF1WZQHFNfLdwhaShiR3e4KvFviAM58TrxVxPMhukic/go-cidutil/apicid" blockstore "gx/ipfs/QmdriVJgKx4JADRgh3cYPXqXmsa1A45SvFki1nDWHhQNtC/go-ipfs-blockstore" ) @@ -93,7 +94,7 @@ func TestAddGCLive(t *testing.T) { }() - addedHashes := make(map[string]struct{}) + addedHashes := make(map[apicid.Hash]struct{}) select { case o := <-out: addedHashes[o.(*AddedObject).Hash] = struct{}{} @@ -132,7 +133,7 @@ func TestAddGCLive(t *testing.T) { if r.Error != nil { t.Fatal(err) } - if _, ok := addedHashes[r.KeyRemoved.String()]; ok { + if _, ok := addedHashes[apicid.FromCid(r.KeyRemoved)]; ok { t.Fatal("gc'ed a hash we just added") } } @@ -140,7 +141,7 @@ func TestAddGCLive(t *testing.T) { var last cid.Cid for a := range out { // wait for it to finish - c, err := cid.Decode(a.(*AddedObject).Hash) + c, err := a.(*AddedObject).Hash.Cid() if err != nil { t.Fatal(err) } diff --git a/exchange/reprovide/providers.go b/exchange/reprovide/providers.go index c579d0357ce..da9502402fe 100644 --- a/exchange/reprovide/providers.go +++ b/exchange/reprovide/providers.go @@ -6,9 +6,9 @@ import ( pin "github.com/ipfs/go-ipfs/pin" cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid" - cidutil "gx/ipfs/QmQJSeE3CX4zos9qeaG8EhecEK9zvrTEfTG84J8C5NVRwt/go-cidutil" merkledag "gx/ipfs/QmcBoNcAP6qDjgRBew7yjvCqHq7p5jMstE44jPUBWBxzsV/go-merkledag" ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format" + cidutil "gx/ipfs/QmdPF1WZQHFNfLdwhaShiR3e4KvFviAM58TrxVxPMhukic/go-cidutil" blocks "gx/ipfs/QmdriVJgKx4JADRgh3cYPXqXmsa1A45SvFki1nDWHhQNtC/go-ipfs-blockstore" ) diff --git a/filestore/util.go b/filestore/util.go index 6213b0f1044..cd9540d21f1 100644 --- a/filestore/util.go +++ b/filestore/util.go @@ -10,6 +10,7 @@ import ( dshelp "gx/ipfs/QmUDTSi6zJ6ACyQaKtxscCUxrg5DaXs9r4RQUPFQXGPHpo/go-ipfs-ds-help" ds "gx/ipfs/QmUyz7JTJzgegC6tiJrfby3mPhzcdswVtG4x58TQ6pq8jV/go-datastore" dsq "gx/ipfs/QmUyz7JTJzgegC6tiJrfby3mPhzcdswVtG4x58TQ6pq8jV/go-datastore/query" + apicid "gx/ipfs/QmdPF1WZQHFNfLdwhaShiR3e4KvFviAM58TrxVxPMhukic/go-cidutil/apicid" blockstore "gx/ipfs/QmdriVJgKx4JADRgh3cYPXqXmsa1A45SvFki1nDWHhQNtC/go-ipfs-blockstore" ) @@ -60,7 +61,7 @@ func (s Status) Format() string { type ListRes struct { Status Status ErrorMsg string - Key cid.Cid + Key apicid.Cid FilePath string Offset uint64 Size uint64 @@ -269,14 +270,14 @@ func mkListRes(c cid.Cid, d *pb.DataObj, err error) *ListRes { return &ListRes{ Status: status, ErrorMsg: errorMsg, - Key: c, + Key: apicid.Cid{Cid: c}, } } return &ListRes{ Status: status, ErrorMsg: errorMsg, - Key: c, + Key: apicid.Cid{Cid: c}, FilePath: d.FilePath, Size: d.Size_, Offset: d.Offset, diff --git a/fuse/ipns/ipns_unix.go b/fuse/ipns/ipns_unix.go index 61d94c593df..4f3ab3dfb92 100644 --- a/fuse/ipns/ipns_unix.go +++ b/fuse/ipns/ipns_unix.go @@ -22,7 +22,7 @@ import ( fuse "gx/ipfs/QmSJBsmLP1XMjv8hxYg2rUMdPDB7YUpyBo9idjrJ6Cmq6F/fuse" fs "gx/ipfs/QmSJBsmLP1XMjv8hxYg2rUMdPDB7YUpyBo9idjrJ6Cmq6F/fuse/fs" logging "gx/ipfs/QmZChCsSt8DctjceaL56Eibc29CVQq4dGKRXC5JRZ6Ppae/go-log" - mfs "gx/ipfs/QmahrY1adY4wvtYEtoGjpZ2GUohTyukrkMkwUR9ytRjTG2/go-mfs" + mfs "gx/ipfs/QmaxgEmTteUHnA1PQrKwHqHme1WQg3Fep9AmdbUGa4t6Wf/go-mfs" peer "gx/ipfs/QmbNepETomvmXfz1X5pHNFD2QuPqnqi47dTd94QJWSorQ3/go-libp2p-peer" ) diff --git a/package.json b/package.json index e5d03e70e4a..84e94d70333 100644 --- a/package.json +++ b/package.json @@ -536,15 +536,15 @@ }, { "author": "hsanjuan", - "hash": "QmahrY1adY4wvtYEtoGjpZ2GUohTyukrkMkwUR9ytRjTG2", + "hash": "QmaxgEmTteUHnA1PQrKwHqHme1WQg3Fep9AmdbUGa4t6Wf", "name": "go-mfs", - "version": "0.1.3" + "version": "0.1.4" }, { "author": "kevina", - "hash": "QmQJSeE3CX4zos9qeaG8EhecEK9zvrTEfTG84J8C5NVRwt", + "hash": "QmdPF1WZQHFNfLdwhaShiR3e4KvFviAM58TrxVxPMhukic", "name": "go-cidutil", - "version": "0.1.1" + "version": "0.1.2" }, { "author": "lgierth", diff --git a/test/sharness/t0040-add-and-cat.sh b/test/sharness/t0040-add-and-cat.sh index 0ee4477cdcd..480bcaecf6f 100755 --- a/test/sharness/t0040-add-and-cat.sh +++ b/test/sharness/t0040-add-and-cat.sh @@ -164,7 +164,6 @@ test_add_cat_file() { test_expect_code 1 ipfs add -Q --chunker rabin-12-512-1024 mountdir/hello.txt ' - test_expect_success "ipfs add on hidden file succeeds" ' echo "Hello Worlds!" >mountdir/.hello.txt && ipfs add mountdir/.hello.txt >actual @@ -244,6 +243,36 @@ test_add_cat_file() { echo "added QmZQWnfcqJ6hNkkPvrY9Q5X39GP3jUnUbAV4AbmbbR3Cb1 test_current_dir" > expected test_cmp expected actual ' + + # --cid-base=base32 + + test_expect_success "ipfs add --cid-base=base32 succeeds" ' + echo "Hello Worlds!" >mountdir/hello.txt && + ipfs add --cid-base=base32 mountdir/hello.txt >actual + ' + + test_expect_success "ipfs add output looks good" ' + HASH="bafybeidpq7lcjx4w5c6yr4vuthzvlav54hgxsremwk73to5ferdc2rxhai" && + echo "added $HASH hello.txt" >expected && + test_cmp expected actual + ' + + test_expect_success "ipfs add --cid-base=base32 --only-hash succeeds" ' + ipfs add --cid-base=base32 --only-hash mountdir/hello.txt > oh_actual + ' + + test_expect_success "ipfs add --only-hash output looks good" ' + test_cmp expected oh_actual + ' + + test_expect_success "ipfs cat succeeds" ' + ipfs cat "$HASH" >actual + ' + + test_expect_success "ipfs cat output looks good" ' + echo "Hello Worlds!" >expected && + test_cmp expected actual + ' } test_add_cat_5MB() { diff --git a/test/sharness/t0045-ls.sh b/test/sharness/t0045-ls.sh index 2cb46a7dde9..b9d4c0e814a 100755 --- a/test/sharness/t0045-ls.sh +++ b/test/sharness/t0045-ls.sh @@ -11,7 +11,6 @@ test_description="Test ls command" test_init_ipfs test_ls_cmd() { - test_expect_success "'ipfs add -r testData' succeeds" ' mkdir -p testData testData/d1 testData/d2 && echo "test" >testData/f1 && @@ -88,6 +87,15 @@ QmZULkCELmmk5XNfCgTnCyFgAVxBRBXyDHGGMVoLFLiXEN 14 a EOF test_cmp expected_ls_headers actual_ls_headers ' + + test_expect_success "'ipfs ls --cid-base=base32 ' succeeds" ' + ipfs ls --cid-base=base32 $(cid-fmt -v 1 -b base32 %s QmfNy183bXiRVyrhyWtq3TwHn79yHEkiAGFr18P7YNzESj QmR3jhV4XpxxPjPT3Y8vNnWvWNvakdcT3H6vqpRBsX1MLy QmSix55yz8CzWXf5ZVM9vgEvijnEeeXiTSarVtsqiiCJss) >actual_ls_base32 + ' + + test_expect_success "'ipfs ls --cid-base=base32 ' output looks good" ' + cid-fmt -b base32 -v 1 --filter %s < expected_ls > expected_ls_base32 + test_cmp expected_ls_base32 actual_ls_base32 + ' } test_ls_cmd_raw_leaves() { diff --git a/test/sharness/t0085-pins.sh b/test/sharness/t0085-pins.sh index 9fac885e887..e5dfb2718d0 100755 --- a/test/sharness/t0085-pins.sh +++ b/test/sharness/t0085-pins.sh @@ -11,15 +11,19 @@ test_description="Test ipfs pinning operations" test_pins() { EXTRA_ARGS=$1 - - test_expect_success "create some hashes" ' - HASH_A=$(echo "A" | ipfs add -q --pin=false) && - HASH_B=$(echo "B" | ipfs add -q --pin=false) && - HASH_C=$(echo "C" | ipfs add -q --pin=false) && - HASH_D=$(echo "D" | ipfs add -q --pin=false) && - HASH_E=$(echo "E" | ipfs add -q --pin=false) && - HASH_F=$(echo "F" | ipfs add -q --pin=false) && - HASH_G=$(echo "G" | ipfs add -q --pin=false) + BASE=$2 + if [ -n "$BASE" ]; then + BASE_ARGS="--cid-base=$BASE" + fi + + test_expect_success "create some hashes $BASE" ' + HASH_A=$(echo "A" | ipfs add $BASE_ARGS -q --pin=false) && + HASH_B=$(echo "B" | ipfs add $BASE_ARGS -q --pin=false) && + HASH_C=$(echo "C" | ipfs add $BASE_ARGS -q --pin=false) && + HASH_D=$(echo "D" | ipfs add $BASE_ARGS -q --pin=false) && + HASH_E=$(echo "E" | ipfs add $BASE_ARGS -q --pin=false) && + HASH_F=$(echo "F" | ipfs add $BASE_ARGS -q --pin=false) && + HASH_G=$(echo "G" | ipfs add $BASE_ARGS -q --pin=false) ' test_expect_success "put all those hashes in a file" ' @@ -32,17 +36,48 @@ test_pins() { echo $HASH_G >> hashes ' + if [ -n "$BASE" ]; then + test_expect_success "make sure hashes are in $BASE" ' + cat hashes | xargs cid-fmt %b | sort -u > actual + echo base32 > expected + test_cmp expected actual + ' + fi + test_expect_success "'ipfs pin add $EXTRA_ARGS' via stdin" ' - cat hashes | ipfs pin add $EXTRA_ARGS + cat hashes | ipfs pin add $EXTRA_ARGS $BASE_ARGS | tee actual + ' + + test_expect_success "'ipfs pin add $EXTRA_ARGS' output looks good" ' + sed -e "s/^/pinned /; s/$/ recursively/" hashes > expected && + test_cmp expected actual ' test_expect_success "see if verify works" ' ipfs pin verify ' - test_expect_success "see if verify --verbose works" ' - ipfs pin verify --verbose > verify_out && - test $(cat verify_out | wc -l) > 8 + test_expect_success "see if verify --verbose $BASE_ARGS works" ' + ipfs pin verify --verbose $BASE_ARGS > verify_out && + test $(cat verify_out | wc -l) -ge 7 && + test_should_contain "$HASH_A ok" verify_out && + test_should_contain "$HASH_B ok" verify_out && + test_should_contain "$HASH_C ok" verify_out && + test_should_contain "$HASH_D ok" verify_out && + test_should_contain "$HASH_E ok" verify_out && + test_should_contain "$HASH_F ok" verify_out && + test_should_contain "$HASH_G ok" verify_out + ' + + test_expect_success "ipfs pin ls $BASE_ARGS works" ' + ipfs pin ls $BASE_ARGS > ls_out && + test_should_contain "$HASH_A" ls_out && + test_should_contain "$HASH_B" ls_out && + test_should_contain "$HASH_C" ls_out && + test_should_contain "$HASH_D" ls_out && + test_should_contain "$HASH_E" ls_out && + test_should_contain "$HASH_F" ls_out && + test_should_contain "$HASH_G" ls_out ' test_expect_success "unpin those hashes" ' @@ -51,11 +86,11 @@ test_pins() { test_expect_success "test pin update" ' ipfs pin add "$HASH_A" && - ipfs pin ls > before_update && + ipfs pin ls $BASE_ARGS | tee before_update && test_should_contain "$HASH_A" before_update && test_must_fail grep -q "$HASH_B" before_update && ipfs pin update --unpin=true "$HASH_A" "$HASH_B" && - ipfs pin ls > after_update && + ipfs pin ls $BASE_ARGS > after_update && test_must_fail grep -q "$HASH_A" after_update && test_should_contain "$HASH_B" after_update && ipfs pin rm "$HASH_B" @@ -122,6 +157,7 @@ test_init_ipfs test_pins test_pins --progress +test_pins '' base32 test_pins_error_reporting test_pins_error_reporting --progress @@ -135,6 +171,7 @@ test_launch_ipfs_daemon --offline test_pins test_pins --progress +test_pins '' base32 test_pins_error_reporting test_pins_error_reporting --progress diff --git a/test/sharness/t0095-refs.sh b/test/sharness/t0095-refs.sh index 67dcdfaba85..dfa3c61bb95 100755 --- a/test/sharness/t0095-refs.sh +++ b/test/sharness/t0095-refs.sh @@ -173,4 +173,44 @@ EOF test_cmp refsr.txt expected.txt ' +test_expect_success "ipfs refs -r -l " ' + cat < expected.txt +bafybeicvusi42o2fcso5ybswnsticebaetge4ispea3i4ayj4bgmy432va -> QmdytmR4wULMd3SLo6ePF4s3WcRHWcpnJZ7bHhoj3QB13v +bafybeicvusi42o2fcso5ybswnsticebaetge4ispea3i4ayj4bgmy432va -> QmNkQvpiyAEtbeLviC7kqfifYoK1GXPcsSxTpP1yS3ykLa +QmNkQvpiyAEtbeLviC7kqfifYoK1GXPcsSxTpP1yS3ykLa -> QmdytmR4wULMd3SLo6ePF4s3WcRHWcpnJZ7bHhoj3QB13v +QmNkQvpiyAEtbeLviC7kqfifYoK1GXPcsSxTpP1yS3ykLa -> uAXASID8NjbmooGbiMTtNz1kVElOi4R4h3R2oKMEQu1lWaqBZ +uAXASID8NjbmooGbiMTtNz1kVElOi4R4h3R2oKMEQu1lWaqBZ -> QmSFxnK675wQ9Kc1uqWKyJUaNxvSc2BP5DbXCD3x93oq61 +bafybeicvusi42o2fcso5ybswnsticebaetge4ispea3i4ayj4bgmy432va -> QmXXazTjeNCKFnpW1D65vTKsTs8fbgkCWTv8Em4pdK2coH +QmXXazTjeNCKFnpW1D65vTKsTs8fbgkCWTv8Em4pdK2coH -> QmNkQvpiyAEtbeLviC7kqfifYoK1GXPcsSxTpP1yS3ykLa +QmNkQvpiyAEtbeLviC7kqfifYoK1GXPcsSxTpP1yS3ykLa -> QmdytmR4wULMd3SLo6ePF4s3WcRHWcpnJZ7bHhoj3QB13v +QmNkQvpiyAEtbeLviC7kqfifYoK1GXPcsSxTpP1yS3ykLa -> uAXASID8NjbmooGbiMTtNz1kVElOi4R4h3R2oKMEQu1lWaqBZ +uAXASID8NjbmooGbiMTtNz1kVElOi4R4h3R2oKMEQu1lWaqBZ -> QmSFxnK675wQ9Kc1uqWKyJUaNxvSc2BP5DbXCD3x93oq61 +bafybeicvusi42o2fcso5ybswnsticebaetge4ispea3i4ayj4bgmy432va -> uAXASID8NjbmooGbiMTtNz1kVElOi4R4h3R2oKMEQu1lWaqBZ +uAXASID8NjbmooGbiMTtNz1kVElOi4R4h3R2oKMEQu1lWaqBZ -> QmSFxnK675wQ9Kc1uqWKyJUaNxvSc2BP5DbXCD3x93oq61 +uAXASID8NjbmooGbiMTtNz1kVElOi4R4h3R2oKMEQu1lWaqBZ -> QmSFxnK675wQ9Kc1uqWKyJUaNxvSc2BP5DbXCD3x93oq61 +EOF + ipfs refs -r -e bafybeicvusi42o2fcso5ybswnsticebaetge4ispea3i4ayj4bgmy432va uAXASID8NjbmooGbiMTtNz1kVElOi4R4h3R2oKMEQu1lWaqBZ > refsr.txt + test_cmp refsr.txt expected.txt +' + +test_expect_success "ipfs refs --output-cidv1 -r -l " ' + cat < expected.txt +bafybeicvusi42o2fcso5ybswnsticebaetge4ispea3i4ayj4bgmy432va -> zdj7Wm598r8nwoxVkkGXqc17Shc8pcjEAEvAsX7xWCPrqjuDr +bafybeicvusi42o2fcso5ybswnsticebaetge4ispea3i4ayj4bgmy432va -> zdj7WVqfJFnpdiVUMerSw5Ni3ZEB1WSz4okjmMzVBQeFVYeWW +zdj7WVqfJFnpdiVUMerSw5Ni3ZEB1WSz4okjmMzVBQeFVYeWW -> zdj7Wm598r8nwoxVkkGXqc17Shc8pcjEAEvAsX7xWCPrqjuDr +zdj7WVqfJFnpdiVUMerSw5Ni3ZEB1WSz4okjmMzVBQeFVYeWW -> uAXASID8NjbmooGbiMTtNz1kVElOi4R4h3R2oKMEQu1lWaqBZ +uAXASID8NjbmooGbiMTtNz1kVElOi4R4h3R2oKMEQu1lWaqBZ -> zdj7WZMD9k9xZR122uweaTwqgK91B7tKZbWwY13s1ScxVNjFw +bafybeicvusi42o2fcso5ybswnsticebaetge4ispea3i4ayj4bgmy432va -> zdj7WecqMtoVqfv8W8Rjx3hnqAS65L7KEALNnKeuZTVSkbWyD +zdj7WecqMtoVqfv8W8Rjx3hnqAS65L7KEALNnKeuZTVSkbWyD -> zdj7WVqfJFnpdiVUMerSw5Ni3ZEB1WSz4okjmMzVBQeFVYeWW +zdj7WVqfJFnpdiVUMerSw5Ni3ZEB1WSz4okjmMzVBQeFVYeWW -> zdj7Wm598r8nwoxVkkGXqc17Shc8pcjEAEvAsX7xWCPrqjuDr +zdj7WVqfJFnpdiVUMerSw5Ni3ZEB1WSz4okjmMzVBQeFVYeWW -> uAXASID8NjbmooGbiMTtNz1kVElOi4R4h3R2oKMEQu1lWaqBZ +uAXASID8NjbmooGbiMTtNz1kVElOi4R4h3R2oKMEQu1lWaqBZ -> zdj7WZMD9k9xZR122uweaTwqgK91B7tKZbWwY13s1ScxVNjFw +bafybeicvusi42o2fcso5ybswnsticebaetge4ispea3i4ayj4bgmy432va -> uAXASID8NjbmooGbiMTtNz1kVElOi4R4h3R2oKMEQu1lWaqBZ +uAXASID8NjbmooGbiMTtNz1kVElOi4R4h3R2oKMEQu1lWaqBZ -> zdj7WZMD9k9xZR122uweaTwqgK91B7tKZbWwY13s1ScxVNjFw +uAXASID8NjbmooGbiMTtNz1kVElOi4R4h3R2oKMEQu1lWaqBZ -> zdj7WZMD9k9xZR122uweaTwqgK91B7tKZbWwY13s1ScxVNjFw +EOF + ipfs refs --output-cidv1 -r -e bafybeicvusi42o2fcso5ybswnsticebaetge4ispea3i4ayj4bgmy432va uAXASID8NjbmooGbiMTtNz1kVElOi4R4h3R2oKMEQu1lWaqBZ > refsr.txt + test_cmp refsr.txt expected.txt +' + test_done diff --git a/test/sharness/t0160-resolve.sh b/test/sharness/t0160-resolve.sh index dacd05d9f84..3261632c2dd 100755 --- a/test/sharness/t0160-resolve.sh +++ b/test/sharness/t0160-resolve.sh @@ -12,6 +12,9 @@ test_expect_success "resolve: prepare files" ' a_hash=$(ipfs add -q -r a | tail -n1) && b_hash=$(ipfs add -q -r a/b | tail -n1) && c_hash=$(ipfs add -q -r a/b/c | tail -n1) + a_hash_b32=$(cid-fmt -v 1 -b b %s $a_hash) + b_hash_b32=$(cid-fmt -v 1 -b b %s $b_hash) + c_hash_b32=$(cid-fmt -v 1 -b b %s $c_hash) ' test_resolve_setup_name() { @@ -41,9 +44,10 @@ test_resolve_setup_name_fail() { test_resolve() { src=$1 dst=$2 + extra=$3 test_expect_success "resolve succeeds: $src" ' - ipfs resolve -r "$src" >actual + ipfs resolve $extra -r "$src" >actual ' test_expect_success "resolved correctly: $src -> $dst" ' @@ -53,7 +57,6 @@ test_resolve() { } test_resolve_cmd() { - test_resolve "/ipfs/$a_hash" "/ipfs/$a_hash" test_resolve "/ipfs/$a_hash/b" "/ipfs/$b_hash" test_resolve "/ipfs/$a_hash/b/c" "/ipfs/$c_hash" @@ -72,6 +75,30 @@ test_resolve_cmd() { test_resolve "/ipns/$id_hash" "/ipfs/$c_hash" } +test_resolve_cmd_b32() { + # no flags needed, base should be preserved + + test_resolve "/ipfs/$a_hash_b32" "/ipfs/$a_hash_b32" + test_resolve "/ipfs/$a_hash_b32/b" "/ipfs/$b_hash_b32" + test_resolve "/ipfs/$a_hash_b32/b/c" "/ipfs/$c_hash_b32" + test_resolve "/ipfs/$b_hash_b32/c" "/ipfs/$c_hash_b32" + + # flags needed passed in path does not contain cid to derive base + + test_resolve_setup_name "/ipfs/$a_hash_b32" + test_resolve "/ipns/$id_hash" "/ipfs/$a_hash_b32" --cid-base=base32 + test_resolve "/ipns/$id_hash/b" "/ipfs/$b_hash_b32" --cid-base=base32 + test_resolve "/ipns/$id_hash/b/c" "/ipfs/$c_hash_b32" --cid-base=base32 + + test_resolve_setup_name "/ipfs/$b_hash_b32" --cid-base=base32 + test_resolve "/ipns/$id_hash" "/ipfs/$b_hash_b32" --cid-base=base32 + test_resolve "/ipns/$id_hash/c" "/ipfs/$c_hash_b32" --cid-base=base32 + + test_resolve_setup_name "/ipfs/$c_hash_b32" + test_resolve "/ipns/$id_hash" "/ipfs/$c_hash_b32" --cid-base=base32 +} + + #todo remove this once the online resolve is fixed test_resolve_fail() { src=$1 @@ -108,6 +135,7 @@ test_resolve_cmd_fail() { # should work offline test_resolve_cmd +test_resolve_cmd_b32 # should work online test_launch_ipfs_daemon diff --git a/test/sharness/t0250-files-api.sh b/test/sharness/t0250-files-api.sh index 7b97f31ded6..79d3e570b09 100755 --- a/test/sharness/t0250-files-api.sh +++ b/test/sharness/t0250-files-api.sh @@ -202,6 +202,12 @@ test_files_api() { test_cmp ls_l_expected ls_l_actual ' + test_expect_success "file has correct hash and size listed with -l --cid-base=base32" ' + echo "file1 `cid-fmt -v 1 -b base32 %s $FILE1` 4" > ls_l_expected && + ipfs files ls --cid-base=base32 -l /cats/file1 > ls_l_actual && + test_cmp ls_l_expected ls_l_actual + ' + test_expect_success "file shows up with the correct name" ' echo "file1" > ls_l_expected && ipfs files ls /cats/file1 > ls_l_actual && @@ -221,6 +227,19 @@ test_files_api() { test_cmp file1stat_expect file1stat_actual ' + test_expect_success "can stat file with --cid-base=base32 $EXTRA" ' + ipfs files stat --cid-base=base32 /cats/file1 > file1stat_orig + ' + + test_expect_success "stat output looks good with --cid-base=base32" ' + grep -v CumulativeSize: file1stat_orig > file1stat_actual && + echo `cid-fmt -v 1 -b base32 %s $FILE1` > file1stat_expect && + echo "Size: 4" >> file1stat_expect && + echo "ChildBlocks: 0" >> file1stat_expect && + echo "Type: file" >> file1stat_expect && + test_cmp file1stat_expect file1stat_actual + ' + test_expect_success "can read file $EXTRA" ' ipfs files read /cats/file1 > file1out ' diff --git a/thirdparty/cidv0v1/blockstore.go b/thirdparty/cidv0v1/blockstore.go index 2cb19a4f9a0..1fa13ef348e 100644 --- a/thirdparty/cidv0v1/blockstore.go +++ b/thirdparty/cidv0v1/blockstore.go @@ -2,8 +2,8 @@ package cidv0v1 import ( cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid" - mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash" blocks "gx/ipfs/QmRcHuYzAyswytBuMF78rj3LTChYszomRFXNg4685ZN1WM/go-block-format" + cidutil "gx/ipfs/QmdPF1WZQHFNfLdwhaShiR3e4KvFviAM58TrxVxPMhukic/go-cidutil" bs "gx/ipfs/QmdriVJgKx4JADRgh3cYPXqXmsa1A45SvFki1nDWHhQNtC/go-ipfs-blockstore" ) @@ -20,7 +20,7 @@ func (b *blockstore) Has(c cid.Cid) (bool, error) { if have || err != nil { return have, err } - c1 := tryOtherCidVersion(c) + c1 := cidutil.TryOtherCidVersion(c) if !c1.Defined() { return false, nil } @@ -35,7 +35,7 @@ func (b *blockstore) Get(c cid.Cid) (blocks.Block, error) { if err != bs.ErrNotFound { return nil, err } - c1 := tryOtherCidVersion(c) + c1 := cidutil.TryOtherCidVersion(c) if !c1.Defined() { return nil, bs.ErrNotFound } @@ -65,23 +65,9 @@ func (b *blockstore) GetSize(c cid.Cid) (int, error) { if err != bs.ErrNotFound { return -1, err } - c1 := tryOtherCidVersion(c) + c1 := cidutil.TryOtherCidVersion(c) if !c1.Defined() { return -1, bs.ErrNotFound } return b.Blockstore.GetSize(c1) } - -func tryOtherCidVersion(c cid.Cid) cid.Cid { - prefix := c.Prefix() - if prefix.Codec != cid.DagProtobuf || prefix.MhType != mh.SHA2_256 || prefix.MhLength != 32 { - return cid.Undef - } - var c1 cid.Cid - if prefix.Version == 0 { - c1 = cid.NewCidV1(cid.DagProtobuf, c.Hash()) - } else { - c1 = cid.NewCidV0(c.Hash()) - } - return c1 -}