Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

coreapi unixfs: change Wrap logic to make more sense #6019

Merged
merged 14 commits into from
Mar 23, 2019
41 changes: 29 additions & 12 deletions core/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import (
"fmt"
"io"
"os"
"path"
"strings"

"github.com/ipfs/go-ipfs/core/commands/cmdenv"

cmdkit "github.com/ipfs/go-ipfs-cmdkit"
cmds "github.com/ipfs/go-ipfs-cmds"
"github.com/ipfs/go-ipfs-files"
coreiface "github.com/ipfs/interface-go-ipfs-core"
options "github.com/ipfs/interface-go-ipfs-core/options"
"github.com/ipfs/interface-go-ipfs-core/options"
mh "github.com/multiformats/go-multihash"
pb "gopkg.in/cheggaaa/pb.v1"
)
Expand All @@ -34,8 +36,6 @@ const (
progressOptionName = "progress"
trickleOptionName = "trickle"
wrapOptionName = "wrap-with-directory"
stdinPathName = "stdin-name"
hiddenOptionName = "hidden"
onlyHashOptionName = "only-hash"
chunkerOptionName = "chunker"
pinOptionName = "pin"
Expand Down Expand Up @@ -112,15 +112,15 @@ You can now check what blocks have been created by:
Options: []cmdkit.Option{
cmds.OptionRecursivePath, // a builtin option that allows recursive paths (-r, --recursive)
cmds.OptionDerefArgs, // a builtin option that resolves passed in filesystem links (--dereference-args)
cmds.OptionStdinName, // a builtin option that optionally allows wrapping stdin into a named file
cmds.OptionHidden,
cmdkit.BoolOption(quietOptionName, "q", "Write minimal output."),
cmdkit.BoolOption(quieterOptionName, "Q", "Write only final hash."),
cmdkit.BoolOption(silentOptionName, "Write no output."),
cmdkit.BoolOption(progressOptionName, "p", "Stream progress data."),
cmdkit.BoolOption(trickleOptionName, "t", "Use trickle-dag format for dag generation."),
cmdkit.BoolOption(onlyHashOptionName, "n", "Only chunk and hash - do not write to disk."),
cmdkit.BoolOption(wrapOptionName, "w", "Wrap files with a directory object."),
cmdkit.StringOption(stdinPathName, "Assign a name if the file source is stdin."),
cmdkit.BoolOption(hiddenOptionName, "H", "Include files that are hidden. Only takes effect on recursive add."),
cmdkit.StringOption(chunkerOptionName, "s", "Chunking algorithm, size-[bytes] or rabin-[min]-[avg]-[max]").WithDefault("size-262144"),
cmdkit.BoolOption(pinOptionName, "Pin this object when adding.").WithDefault(true),
cmdkit.BoolOption(rawLeavesOptionName, "Use raw blocks for leaf nodes. (experimental)"),
Expand Down Expand Up @@ -160,7 +160,6 @@ You can now check what blocks have been created by:
trickle, _ := req.Options[trickleOptionName].(bool)
wrap, _ := req.Options[wrapOptionName].(bool)
hash, _ := req.Options[onlyHashOptionName].(bool)
hidden, _ := req.Options[hiddenOptionName].(bool)
silent, _ := req.Options[silentOptionName].(bool)
chunker, _ := req.Options[chunkerOptionName].(string)
dopin, _ := req.Options[pinOptionName].(bool)
Expand All @@ -171,7 +170,6 @@ You can now check what blocks have been created by:
hashFunStr, _ := req.Options[hashOptionName].(string)
inline, _ := req.Options[inlineOptionName].(bool)
inlineLimit, _ := req.Options[inlineLimitOptionName].(int)
pathName, _ := req.Options[stdinPathName].(string)

hashFunCode, ok := mh.Names[strings.ToLower(hashFunStr)]
if !ok {
Expand All @@ -185,6 +183,23 @@ You can now check what blocks have been created by:

events := make(chan interface{}, adderOutChanSize)

var toadd files.Node = req.Files
name := ""
if !wrap {
it := req.Files.Entries()
if !it.Next() {
err := it.Err()
if err == nil {
return fmt.Errorf("expected a file argument")
}
return err
}

toadd = it.Node()
name = it.Name()
}
_, dir := toadd.(files.Directory)

opts := []options.UnixfsAddOption{
options.Unixfs.Hash(hashFunCode),

Expand All @@ -198,10 +213,6 @@ You can now check what blocks have been created by:
options.Unixfs.FsCache(fscache),
options.Unixfs.Nocopy(nocopy),

options.Unixfs.Wrap(wrap),
options.Unixfs.Hidden(hidden),
options.Unixfs.StdinName(pathName),

options.Unixfs.Progress(progress),
options.Unixfs.Silent(silent),
options.Unixfs.Events(events),
Expand All @@ -224,7 +235,7 @@ You can now check what blocks have been created by:
var err error
defer func() { errCh <- err }()
defer close(events)
_, err = api.Unixfs().Add(req.Context, req.Files, opts...)
_, err = api.Unixfs().Add(req.Context, toadd, opts...)
}()

for event := range events {
Expand All @@ -238,6 +249,12 @@ You can now check what blocks have been created by:
h = enc.Encode(output.Path.Cid())
}

if !dir && name != "" {
output.Name = name
} else {
output.Name = path.Join(name, output.Name)
}

res.Emit(&AddEvent{
Name: output.Name,
Hash: h,
Expand Down
2 changes: 0 additions & 2 deletions core/coreapi/unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,11 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.Node, opts ...options
fileAdder.Out = settings.Events
fileAdder.Progress = settings.Progress
}
fileAdder.Hidden = settings.Hidden
fileAdder.Wrap = settings.Wrap
fileAdder.Pin = settings.Pin && !settings.OnlyHash
fileAdder.Silent = settings.Silent
fileAdder.RawLeaves = settings.RawLeaves
fileAdder.NoCopy = settings.NoCopy
fileAdder.Name = settings.StdinName
fileAdder.CidBuilder = prefix

switch settings.Layout {
Expand Down
5 changes: 2 additions & 3 deletions core/corehttp/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
files "github.com/ipfs/go-ipfs-files"
path "github.com/ipfs/go-path"
iface "github.com/ipfs/interface-go-ipfs-core"
"github.com/ipfs/interface-go-ipfs-core/options"
nsopts "github.com/ipfs/interface-go-ipfs-core/options/namesys"
ci "github.com/libp2p/go-libp2p-crypto"
id "github.com/libp2p/go-libp2p/p2p/protocol/identify"
Expand Down Expand Up @@ -253,7 +252,7 @@ func TestIPNSHostnameRedirect(t *testing.T) {
}),
})

k, err := api.Unixfs().Add(ctx, f1, options.Unixfs.Wrap(true))
k, err := api.Unixfs().Add(ctx, f1)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -346,7 +345,7 @@ func TestIPNSHostnameBacklinks(t *testing.T) {
})

// create /ipns/example.net/foo/
k, err := api.Unixfs().Add(ctx, f1, options.Unixfs.Wrap(true))
k, err := api.Unixfs().Add(ctx, f1)
if err != nil {
t.Fatal(err)
}
Expand Down
Loading