diff --git a/core/commands/add.go b/core/commands/add.go index e60df3d1e2c..1bab5472f9e 100644 --- a/core/commands/add.go +++ b/core/commands/add.go @@ -158,7 +158,7 @@ You can now refer to the added file in a gateway, like so: rawblks, rbset, _ := req.Option(rawLeavesOptionName).Bool() nocopy, _, _ := req.Option(noCopyOptionName).Bool() fscache, _, _ := req.Option(fstoreCacheOptionName).Bool() - cidVerNum, _, _ := req.Option(cidVersionOptionName).Int() + cidVer, _, _ := req.Option(cidVersionOptionName).Int() if nocopy && !cfg.Experimental.FilestoreEnabled { res.SetError(errors.New("filestore is not enabled, see https://git.io/vy4XN"), @@ -175,11 +175,11 @@ You can now refer to the added file in a gateway, like so: return } - if cidVerNum >= 1 && !rbset { + if cidVer >= 1 && !rbset { rawblks = true } - cidVer, err := dag.NewCidVersion(cidVerNum) + prefix, err := dag.PrefixForCidVersion(cidVer) if err != nil { res.SetError(err, cmds.ErrNormal) return @@ -232,7 +232,7 @@ You can now refer to the added file in a gateway, like so: fileAdder.RawLeaves = rawblks fileAdder.NoCopy = nocopy - fileAdder.SetCidVersion(cidVer) + fileAdder.SetPrefix(prefix) if hash { md := dagtest.Mock() diff --git a/core/coreunix/add.go b/core/coreunix/add.go index 25905980da8..64bc53cbc96 100644 --- a/core/coreunix/add.go +++ b/core/coreunix/add.go @@ -110,12 +110,12 @@ type Adder struct { mr *mfs.Root unlocker bs.Unlocker tempRoot *cid.Cid - cidVersion dag.CidVersion + prefix cid.Prefix } -func (adder *Adder) SetCidVersion(cidVer dag.CidVersion) { - adder.mr.SetCidVersion(cidVer) - adder.cidVersion = cidVer +func (adder *Adder) SetPrefix(prefix cid.Prefix) { + adder.mr.SetPrefix(prefix) + adder.prefix = prefix } func (adder *Adder) SetMfsRoot(r *mfs.Root) { @@ -130,11 +130,11 @@ func (adder Adder) add(reader io.Reader) (node.Node, error) { } params := ihelper.DagBuilderParams{ - Dagserv: adder.dagService, - RawLeaves: adder.RawLeaves, - Maxlinks: ihelper.DefaultLinksPerBlock, - NoCopy: adder.NoCopy, - CidVersion: adder.cidVersion, + Dagserv: adder.dagService, + RawLeaves: adder.RawLeaves, + Maxlinks: ihelper.DefaultLinksPerBlock, + NoCopy: adder.NoCopy, + Prefix: adder.prefix, } if adder.Trickle { @@ -402,7 +402,7 @@ func (adder *Adder) addFile(file files.File) error { } dagnode := dag.NodeWithData(sdata) - dagnode.SetCidVersion(adder.cidVersion) + dagnode.SetPrefix(adder.prefix) _, err = adder.dagService.Add(dagnode) if err != nil { return err diff --git a/importer/helpers/dagbuilder.go b/importer/helpers/dagbuilder.go index 4529e040267..f0c7c37e9f5 100644 --- a/importer/helpers/dagbuilder.go +++ b/importer/helpers/dagbuilder.go @@ -9,22 +9,23 @@ import ( dag "github.com/ipfs/go-ipfs/merkledag" ft "github.com/ipfs/go-ipfs/unixfs" + cid "gx/ipfs/QmV5gPoRsjN1Gid3LMdNZTyfCtP2DsvqEbMAmz82RmmiGk/go-cid" node "gx/ipfs/QmYDscK7dmdo2GZ9aumS8s5auUUAH5mR1jvj5pYhWusfK7/go-ipld-node" ) // DagBuilderHelper wraps together a bunch of objects needed to // efficiently create unixfs dag trees type DagBuilderHelper struct { - dserv dag.DAGService - spl chunk.Splitter - recvdErr error - rawLeaves bool - nextData []byte // the next item to return. - maxlinks int - batch *dag.Batch - fullPath string - stat os.FileInfo - cidVersion dag.CidVersion + dserv dag.DAGService + spl chunk.Splitter + recvdErr error + rawLeaves bool + nextData []byte // the next item to return. + maxlinks int + batch *dag.Batch + fullPath string + stat os.FileInfo + prefix cid.Prefix } type DagBuilderParams struct { @@ -35,8 +36,8 @@ type DagBuilderParams struct { // instead of using the unixfs TRaw type RawLeaves bool - // CID Version to use - CidVersion dag.CidVersion + // CID Prefix to use + Prefix cid.Prefix // DAGService to write blocks to (required) Dagserv dag.DAGService @@ -50,12 +51,12 @@ type DagBuilderParams struct { // from chunks object func (dbp *DagBuilderParams) New(spl chunk.Splitter) *DagBuilderHelper { db := &DagBuilderHelper{ - dserv: dbp.Dagserv, - spl: spl, - rawLeaves: dbp.RawLeaves, - cidVersion: dbp.CidVersion, - maxlinks: dbp.Maxlinks, - batch: dbp.Dagserv.Batch(), + dserv: dbp.Dagserv, + spl: spl, + rawLeaves: dbp.RawLeaves, + prefix: dbp.Prefix, + maxlinks: dbp.Maxlinks, + batch: dbp.Dagserv.Batch(), } if fi, ok := spl.Reader().(files.FileInfo); dbp.NoCopy && ok { db.fullPath = fi.AbsPath() @@ -115,7 +116,7 @@ func (db *DagBuilderHelper) NewUnixfsNode() *UnixfsNode { node: new(dag.ProtoNode), ufmt: &ft.FSNode{Type: ft.TFile}, } - n.SetCidVersion(db.cidVersion) + n.SetPrefix(db.prefix) return n } @@ -125,7 +126,7 @@ func (db *DagBuilderHelper) NewUnixfsBlock() *UnixfsNode { node: new(dag.ProtoNode), ufmt: &ft.FSNode{Type: ft.TRaw}, } - n.SetCidVersion(db.cidVersion) + n.SetPrefix(db.prefix) return n } diff --git a/importer/helpers/helpers.go b/importer/helpers/helpers.go index 0253e9d41f7..c7963035ae5 100644 --- a/importer/helpers/helpers.go +++ b/importer/helpers/helpers.go @@ -10,6 +10,7 @@ import ( pi "github.com/ipfs/go-ipfs/thirdparty/posinfo" ft "github.com/ipfs/go-ipfs/unixfs" + cid "gx/ipfs/QmV5gPoRsjN1Gid3LMdNZTyfCtP2DsvqEbMAmz82RmmiGk/go-cid" node "gx/ipfs/QmYDscK7dmdo2GZ9aumS8s5auUUAH5mR1jvj5pYhWusfK7/go-ipld-node" ) @@ -61,8 +62,8 @@ func NewUnixfsNodeFromDag(nd *dag.ProtoNode) (*UnixfsNode, error) { }, nil } -func (n *UnixfsNode) SetCidVersion(v dag.CidVersion) { - n.node.SetCidVersion(v) +func (n *UnixfsNode) SetPrefix(prefix cid.Prefix) { + n.node.SetPrefix(prefix) } func (n *UnixfsNode) NumChildren() int { diff --git a/merkledag/cidversion.go b/merkledag/cidversion.go deleted file mode 100644 index 329ae1bdaf6..00000000000 --- a/merkledag/cidversion.go +++ /dev/null @@ -1,23 +0,0 @@ -package merkledag - -import ( - "fmt" -) - -// CidVersion is a a cid version number. Do not set directly, instead -// use NewCidVersion to create. It is safe to panic if it is invalid. -type CidVersion uint64 - -const ( - CID0 CidVersion = 0 - CID1 CidVersion = 1 -) - -func NewCidVersion(version int) (CidVersion, error) { - switch version { - case 0, 1: - return CidVersion(version), nil - default: - return CidVersion(0), fmt.Errorf("unknown CID version: %d", version) - } -} diff --git a/merkledag/node.go b/merkledag/node.go index 16c9b1342b0..e05747da274 100644 --- a/merkledag/node.go +++ b/merkledag/node.go @@ -42,16 +42,20 @@ var v1CidPrefix = cid.Prefix{ Version: 1, } -func (n *ProtoNode) SetCidVersion(v CidVersion) { - switch v { +func PrefixForCidVersion(version int) (cid.Prefix, error) { + switch version { case 0: - n.Prefix = v0CidPrefix + return v0CidPrefix, nil case 1: - n.Prefix = v1CidPrefix + return v1CidPrefix, nil default: - // should not happen - panic("unhanded prefix version") + return cid.Prefix{}, fmt.Errorf("unknown CID version: %d", version) } +} + +func (n *ProtoNode) SetPrefix(prefix cid.Prefix) { + n.Prefix = prefix + n.Prefix.Codec = cid.DagProtobuf n.encoded = nil n.cached = nil } diff --git a/mfs/dir.go b/mfs/dir.go index 6cbea3bce83..aaee3c10ac1 100644 --- a/mfs/dir.go +++ b/mfs/dir.go @@ -14,6 +14,7 @@ import ( ft "github.com/ipfs/go-ipfs/unixfs" ufspb "github.com/ipfs/go-ipfs/unixfs/pb" + cid "gx/ipfs/QmV5gPoRsjN1Gid3LMdNZTyfCtP2DsvqEbMAmz82RmmiGk/go-cid" node "gx/ipfs/QmYDscK7dmdo2GZ9aumS8s5auUUAH5mR1jvj5pYhWusfK7/go-ipld-node" ) @@ -50,8 +51,8 @@ func NewDirectory(ctx context.Context, name string, node *dag.ProtoNode, parent } } -func (d *Directory) SetCidVersion(cidVer dag.CidVersion) { - d.node.SetCidVersion(cidVer) +func (d *Directory) SetPrefix(prefix cid.Prefix) { + d.node.SetPrefix(prefix) } // closeChild updates the child by the given name to the dag node 'nd' diff --git a/mfs/ops.go b/mfs/ops.go index 8132b834ac3..0806d33f942 100644 --- a/mfs/ops.go +++ b/mfs/ops.go @@ -134,7 +134,7 @@ func Mkdir(r *Root, pth string, mkparents bool, flush bool) error { if err != nil { return err } - mkd.SetCidVersion(r.CidVersion) + mkd.SetPrefix(r.Prefix) fsn = mkd } else if err != nil { return err @@ -153,7 +153,7 @@ func Mkdir(r *Root, pth string, mkparents bool, flush bool) error { return err } } - final.SetCidVersion(r.CidVersion) + final.SetPrefix(r.Prefix) if flush { err := final.Flush() diff --git a/mfs/system.go b/mfs/system.go index 4e860d7f646..572b39d9e15 100644 --- a/mfs/system.go +++ b/mfs/system.go @@ -61,8 +61,8 @@ type Root struct { Type string - // CID version to use for any children created - CidVersion dag.CidVersion + // Prefix to use for any children created + Prefix cid.Prefix } type PubFunc func(context.Context, *cid.Cid) error @@ -107,9 +107,9 @@ func NewRoot(parent context.Context, ds dag.DAGService, node *dag.ProtoNode, pf // SetCidVersion sets the CidVersion of the root node, and any // children created. To just set the CID version of future children, // set CidVersion directly -func (kr *Root) SetCidVersion(cidVer dag.CidVersion) { - kr.node.SetCidVersion(cidVer) - kr.CidVersion = cidVer +func (kr *Root) SetPrefix(prefix cid.Prefix) { + kr.node.SetPrefix(prefix) + kr.Prefix = prefix } func (kr *Root) GetValue() FSNode {