Skip to content

Commit

Permalink
add: remove wrap logic
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Steven Allen <steven@stebalien.com>
  • Loading branch information
Stebalien committed Mar 22, 2019
1 parent cbc1e6e commit d0021bb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 69 deletions.
13 changes: 8 additions & 5 deletions core/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions core/coreapi/unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
72 changes: 11 additions & 61 deletions core/coreunix/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -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
}
Expand All @@ -69,7 +71,6 @@ type Adder struct {
Trickle bool
RawLeaves bool
Silent bool
Wrap bool
NoCopy bool
Chunker string
root ipld.Node
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand All @@ -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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit d0021bb

Please sign in to comment.