diff --git a/core/commands/add.go b/core/commands/add.go index c4ac6703c9f..175f6cdf42d 100644 --- a/core/commands/add.go +++ b/core/commands/add.go @@ -236,7 +236,7 @@ You can now refer to the added file in a gateway, like so: fileAdder.Silent = silent fileAdder.RawLeaves = rawblks fileAdder.NoCopy = nocopy - fileAdder.Prefix = prefix + fileAdder.Prefix = &prefix if hash { md := dagtest.Mock() diff --git a/core/coreunix/add.go b/core/coreunix/add.go index cc9ba0dc71d..d1cdf5fad29 100644 --- a/core/coreunix/add.go +++ b/core/coreunix/add.go @@ -103,7 +103,7 @@ type Adder struct { mroot *mfs.Root unlocker bs.Unlocker tempRoot *cid.Cid - Prefix cid.Prefix + Prefix *cid.Prefix } func (adder *Adder) mfsRoot() (*mfs.Root, error) { @@ -113,6 +113,7 @@ func (adder *Adder) mfsRoot() (*mfs.Root, error) { rnode := unixfs.EmptyDirNode() rnode.SetPrefix(adder.Prefix) mr, err := mfs.NewRoot(adder.ctx, adder.dagService, rnode, nil) + mr.Prefix = adder.Prefix if err != nil { return nil, err } diff --git a/importer/helpers/dagbuilder.go b/importer/helpers/dagbuilder.go index f0c7c37e9f5..39839d3515a 100644 --- a/importer/helpers/dagbuilder.go +++ b/importer/helpers/dagbuilder.go @@ -25,7 +25,7 @@ type DagBuilderHelper struct { batch *dag.Batch fullPath string stat os.FileInfo - prefix cid.Prefix + prefix *cid.Prefix } type DagBuilderParams struct { @@ -36,8 +36,8 @@ type DagBuilderParams struct { // instead of using the unixfs TRaw type RawLeaves bool - // CID Prefix to use - Prefix cid.Prefix + // CID Prefix to use if set + Prefix *cid.Prefix // DAGService to write blocks to (required) Dagserv dag.DAGService diff --git a/importer/helpers/helpers.go b/importer/helpers/helpers.go index c7963035ae5..17b08e54ef4 100644 --- a/importer/helpers/helpers.go +++ b/importer/helpers/helpers.go @@ -62,7 +62,7 @@ func NewUnixfsNodeFromDag(nd *dag.ProtoNode) (*UnixfsNode, error) { }, nil } -func (n *UnixfsNode) SetPrefix(prefix cid.Prefix) { +func (n *UnixfsNode) SetPrefix(prefix *cid.Prefix) { n.node.SetPrefix(prefix) } diff --git a/merkledag/node.go b/merkledag/node.go index 6a533fa4219..526dad4138a 100644 --- a/merkledag/node.go +++ b/merkledag/node.go @@ -53,10 +53,17 @@ func PrefixForCidVersion(version int) (cid.Prefix, error) { } } -func (n *ProtoNode) SetPrefix(prefix cid.Prefix) { - n.Prefix = prefix - n.encoded = nil - n.cached = nil +// SetPrefix sets the prefix if it is non nil, if prefix is nil then +// it resets it the default value +func (n *ProtoNode) SetPrefix(prefix *cid.Prefix) { + if prefix == nil { + n.Prefix = v0CidPrefix + } else { + n.Prefix = *prefix + n.Prefix.Codec = cid.DagProtobuf + n.encoded = nil + n.cached = nil + } } type LinkSlice []*node.Link @@ -287,7 +294,7 @@ func (n *ProtoNode) Cid() *cid.Cid { } if n.Prefix.Codec == 0 { - n.Prefix = v0CidPrefix + n.SetPrefix(nil) } c, err := n.Prefix.Sum(n.RawData()) diff --git a/mfs/dir.go b/mfs/dir.go index beb7a2eed4e..47d8260dd5b 100644 --- a/mfs/dir.go +++ b/mfs/dir.go @@ -58,7 +58,7 @@ func NewDirectory(ctx context.Context, name string, node node.Node, parent child }, nil } -func (d *Directory) SetPrefix(prefix cid.Prefix) { +func (d *Directory) SetPrefix(prefix *cid.Prefix) { d.dirbuilder.SetPrefix(prefix) } diff --git a/mfs/system.go b/mfs/system.go index cf8498e1013..c542663a26c 100644 --- a/mfs/system.go +++ b/mfs/system.go @@ -63,7 +63,7 @@ type Root struct { Type string // Prefix to use for any children created - Prefix cid.Prefix + Prefix *cid.Prefix } type PubFunc func(context.Context, *cid.Cid) error diff --git a/unixfs/io/dirbuilder.go b/unixfs/io/dirbuilder.go index 6b3fb83ca5a..72a93d856e6 100644 --- a/unixfs/io/dirbuilder.go +++ b/unixfs/io/dirbuilder.go @@ -81,7 +81,7 @@ func NewDirectoryFromNode(dserv mdag.DAGService, nd node.Node) (*Directory, erro } // SetPrefix sets the prefix of the root node -func (d *Directory) SetPrefix(prefix cid.Prefix) { +func (d *Directory) SetPrefix(prefix *cid.Prefix) { if d.dirnode != nil { d.dirnode.SetPrefix(prefix) }