From d0021bb089dae12b9ab53810a0f084fd422627b5 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Fri, 22 Mar 2019 14:06:01 -0700 Subject: [PATCH] add: remove wrap logic License: MIT Signed-off-by: Steven Allen --- core/commands/add.go | 13 +++++--- core/coreapi/unixfs.go | 5 ++- core/coreunix/add.go | 72 +++++++----------------------------------- 3 files changed, 21 insertions(+), 69 deletions(-) diff --git a/core/commands/add.go b/core/commands/add.go index bdf24a6e1447..07b8ab276e80 100644 --- a/core/commands/add.go +++ b/core/commands/add.go @@ -12,7 +12,7 @@ import ( cmdkit "github.com/ipfs/go-ipfs-cmdkit" cmds "github.com/ipfs/go-ipfs-cmds" - "github.com/ipfs/go-ipfs-files" + files "github.com/ipfs/go-ipfs-files" coreiface "github.com/ipfs/interface-go-ipfs-core" "github.com/ipfs/interface-go-ipfs-core/options" mh "github.com/multiformats/go-multihash" @@ -198,7 +198,6 @@ You can now check what blocks have been created by: toadd = it.Node() name = it.Name() } - _, dir := toadd.(files.Directory) opts := []options.UnixfsAddOption{ options.Unixfs.Hash(hashFunCode), @@ -249,12 +248,16 @@ You can now check what blocks have been created by: h = enc.Encode(output.Path.Cid()) } - if !dir && name != "" { - output.Name = name - } else { + if name != "" { output.Name = path.Join(name, output.Name) } + if !wrap && output.Name == "" { + // We name the unnamed root file iff we're not + // wrapping. Don't ask me why. + output.Name = h + } + res.Emit(&AddEvent{ Name: output.Name, Hash: h, diff --git a/core/coreapi/unixfs.go b/core/coreapi/unixfs.go index ce0ecf8fe822..aacbe8abc228 100644 --- a/core/coreapi/unixfs.go +++ b/core/coreapi/unixfs.go @@ -30,7 +30,7 @@ type UnixfsAPI CoreAPI // Add builds a merkledag node from a reader, adds it to the blockstore, // and returns the key representing that node. -func (api *UnixfsAPI) Add(ctx context.Context, files files.Node, opts ...options.UnixfsAddOption) (coreiface.ResolvedPath, error) { +func (api *UnixfsAPI) Add(ctx context.Context, file files.Node, opts ...options.UnixfsAddOption) (coreiface.ResolvedPath, error) { settings, prefix, err := options.UnixfsAddOptions(opts...) if err != nil { return nil, err @@ -87,7 +87,6 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.Node, opts ...options fileAdder.Out = settings.Events fileAdder.Progress = settings.Progress } - fileAdder.Wrap = settings.Wrap fileAdder.Pin = settings.Pin && !settings.OnlyHash fileAdder.Silent = settings.Silent fileAdder.RawLeaves = settings.RawLeaves @@ -123,7 +122,7 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.Node, opts ...options fileAdder.SetMfsRoot(mr) } - nd, err := fileAdder.AddAllAndPin(files) + nd, err := fileAdder.AddAllAndPin(file) if err != nil { return nil, err } diff --git a/core/coreunix/add.go b/core/coreunix/add.go index adb3f6112d8b..a9ca0ae205d8 100644 --- a/core/coreunix/add.go +++ b/core/coreunix/add.go @@ -7,14 +7,15 @@ import ( "io" gopath "path" "strconv" + "strings" "github.com/ipfs/go-ipfs/pin" "github.com/ipfs/go-cid" bstore "github.com/ipfs/go-ipfs-blockstore" chunker "github.com/ipfs/go-ipfs-chunker" - "github.com/ipfs/go-ipfs-files" - "github.com/ipfs/go-ipfs-posinfo" + files "github.com/ipfs/go-ipfs-files" + posinfo "github.com/ipfs/go-ipfs-posinfo" ipld "github.com/ipfs/go-ipld-format" logging "github.com/ipfs/go-log" dag "github.com/ipfs/go-merkledag" @@ -31,6 +32,8 @@ var log = logging.Logger("coreunix") // how many bytes of progress to wait before sending a progress update message const progressReaderIncrement = 1024 * 256 +const rootFileName = "root" + var liveCacheSize = uint64(256 << 10) type Link struct { @@ -51,7 +54,6 @@ func NewAdder(ctx context.Context, p pin.Pinner, bs bstore.GCLocker, ds ipld.DAG Progress: false, Pin: true, Trickle: false, - Wrap: false, Chunker: "", }, nil } @@ -69,7 +71,6 @@ type Adder struct { Trickle bool RawLeaves bool Silent bool - Wrap bool NoCopy bool Chunker string root ipld.Node @@ -144,16 +145,6 @@ func (adder *Adder) curRootNode() (ipld.Node, error) { return nil, err } - // if not wrapping, AND one root file, use that hash as root. - if !adder.Wrap && len(root.Links()) == 1 { - nd, err := root.Links()[0].GetNode(adder.ctx, adder.dagService) - if err != nil { - return nil, err - } - - root = nd - } - adder.root = root return root, err } @@ -220,11 +211,6 @@ func (adder *Adder) outputDirs(path string, fsn mfs.FSNode) error { } func (adder *Adder) addNode(node ipld.Node, path string) error { - // patch it into the root - if path == "" { - path = node.Cid().String() - } - if pi, ok := node.(*posinfo.FilestoreNode); ok { node = pi.Node } @@ -266,7 +252,7 @@ func (adder *Adder) AddAllAndPin(file files.Node) (ipld.Node, error) { } }() - if err := adder.addFileNode("", file); err != nil { + if err := adder.addFileNode(rootFileName, file); err != nil { return nil, err } @@ -284,27 +270,7 @@ func (adder *Adder) AddAllAndPin(file files.Node) (ipld.Node, error) { return nil, err } - // if adding a file without wrapping, swap the root to it (when adding a - // directory, mfs root is the directory) - _, dir := file.(files.Directory) - var name string - if !adder.Wrap && !dir { - children, err := rootdir.ListNames(adder.ctx) - if err != nil { - return nil, err - } - - if len(children) == 0 { - return nil, fmt.Errorf("expected at least one child dir, got none") - } - - // Replace root with the first child - name = children[0] - root, err = rootdir.Child(name) - if err != nil { - return nil, err - } - } + root, err = rootdir.Child(rootFileName) err = mr.Close() if err != nil { @@ -316,27 +282,8 @@ func (adder *Adder) AddAllAndPin(file files.Node) (ipld.Node, error) { return nil, err } - // when adding wrapped directory, manually wrap here - if adder.Wrap && dir { - name = nd.Cid().String() - - end := unixfs.EmptyDirNode() - if err := end.AddNodeLink(nd.Cid().String(), nd); err != nil { - return nil, err - } - nd = end - - if err := adder.dagService.Add(adder.ctx, end); err != nil { - return nil, err - } - - if err := outputDagnode(adder.Out, "", nd); err != nil { - return nil, err - } - } - // output directory events - err = adder.outputDirs(name, root) + err = adder.outputDirs(rootFileName, root) if err != nil { return nil, err } @@ -463,6 +410,9 @@ func outputDagnode(out chan<- interface{}, name string, dn ipld.Node) error { return err } + name = strings.TrimPrefix(name, rootFileName) + name = strings.TrimLeft(name, "/") + out <- &coreiface.AddEvent{ Path: o.Path, Name: name,