Fix #983: Correctly print paths when adding

This commit is contained in:
Hector Sanjuan 2019-12-17 23:01:25 +01:00
parent 2391d2d246
commit 9c61bf6f26
3 changed files with 30 additions and 13 deletions

View File

@ -141,6 +141,15 @@ func (a *Adder) FromFiles(ctx context.Context, f files.Directory) (cid.Cid, erro
it := f.Entries()
var adderRoot ipld.Node
for it.Next() {
// Adding a folder: set a root prefix for the names in the
// output.
_, isDir := it.Node().(files.Directory)
if isDir {
ipfsAdder.OutputDirPrefix = it.Name()
} else {
ipfsAdder.OutputDirPrefix = ""
}
select {
case <-a.ctx.Done():
return cid.Undef, a.ctx.Err()

View File

@ -7,6 +7,7 @@ import (
"fmt"
"io"
gopath "path"
"path/filepath"
"github.com/ipfs/ipfs-cluster/api"
@ -59,6 +60,11 @@ type Adder struct {
CidBuilder cid.Builder
liveNodes uint64
lastFile mfs.FSNode
// Cluster: ipfs does a hack in commands/add.go to prefix the output
// path of the events from this adder with the root folder name when
// adding a folder. We are going to emit the events with the right
// file name directly here by allowing the caller to set this.
OutputDirPrefix string
}
func (adder *Adder) mfsRoot() (*mfs.Root, error) {
@ -193,7 +199,7 @@ func (adder *Adder) outputDirs(path string, fsn mfs.FSNode) error {
return err
}
return outputDagnode(adder.Out, path, nd)
return adder.outputDagnode(adder.Out, path, nd)
default:
return fmt.Errorf("unrecognized fsn type: %#v", fsn)
}
@ -239,7 +245,7 @@ func (adder *Adder) addNode(node ipld.Node, path string) error {
adder.lastFile = lastFile
if !adder.Silent {
return outputDagnode(adder.Out, path, node)
return adder.outputDagnode(adder.Out, path, node)
}
return nil
}
@ -413,8 +419,9 @@ func (adder *Adder) addDir(path string, dir files.Directory, toplevel bool) erro
}
// outputDagnode sends dagnode info over the output channel.
// Cluster: we use *api.AddedOutput instead of coreiface events.
func outputDagnode(out chan *api.AddedOutput, name string, dn ipld.Node) error {
// Cluster: we use *api.AddedOutput instead of coreiface events
// and make this an adder method to be be able to prefix.
func (adder *Adder) outputDagnode(out chan *api.AddedOutput, name string, dn ipld.Node) error {
if out == nil {
return nil
}
@ -426,7 +433,7 @@ func outputDagnode(out chan *api.AddedOutput, name string, dn ipld.Node) error {
out <- &api.AddedOutput{
Cid: dn.Cid(),
Name: name,
Name: filepath.Join(adder.OutputDirPrefix, name),
Size: s,
}

View File

@ -472,15 +472,15 @@ func statusReached(target api.TrackerStatus, gblPinInfo *api.GlobalPinInfo) (boo
}
// logic drawn from go-ipfs-cmds/cli/parse.go: appendFile
func makeSerialFile(fpath string, params *api.AddParams) (files.Node, error) {
func makeSerialFile(fpath string, params *api.AddParams) (string, files.Node, error) {
if fpath == "." {
cwd, err := os.Getwd()
if err != nil {
return nil, err
return "", nil, err
}
cwd, err = filepath.EvalSymlinks(cwd)
if err != nil {
return nil, err
return "", nil, err
}
fpath = cwd
}
@ -489,16 +489,17 @@ func makeSerialFile(fpath string, params *api.AddParams) (files.Node, error) {
stat, err := os.Lstat(fpath)
if err != nil {
return nil, err
return "", nil, err
}
if stat.IsDir() {
if !params.Recursive {
return nil, fmt.Errorf("%s is a directory, but Recursive option is not set", fpath)
return "", nil, fmt.Errorf("%s is a directory, but Recursive option is not set", fpath)
}
}
return files.NewSerialFile(fpath, params.Hidden, stat)
sf, err := files.NewSerialFile(fpath, params.Hidden, stat)
return path.Base(fpath), sf, err
}
// Add imports files to the cluster from the given paths. A path can
@ -524,7 +525,7 @@ func (c *defaultClient) Add(
close(out)
return fmt.Errorf("error parsing path: %s", err)
}
name := path.Base(p)
var name string
var addFile files.Node
if strings.HasPrefix(u.Scheme, "http") {
addFile = files.NewWebFile(u)
@ -534,7 +535,7 @@ func (c *defaultClient) Add(
close(out)
return fmt.Errorf("nocopy option is only valid for URLs")
}
addFile, err = makeSerialFile(p, params)
name, addFile, err = makeSerialFile(p, params)
if err != nil {
close(out)
return err