etcd: Use source filepath base in CopyFs destination path

This patch corrects the destination path in CopyFs to use the source's
base filepath, instead of the entire source path. Now copying /foo/bar
to /baz results in /baz/bar instead of /baz/foo/bar. This commit also adds
a test to verify this behaviour.
This commit is contained in:
Jonathan Gold
2018-12-08 17:43:05 -05:00
parent 70ac38e66c
commit 086a89fad6
2 changed files with 58 additions and 4 deletions

View File

@@ -227,3 +227,59 @@ func TestFs3(t *testing.T) {
} }
t.Logf("tree2: \n%s", tree2) t.Logf("tree2: \n%s", tree2)
} }
func TestFs4(t *testing.T) {
etcdClient := &etcd.ClientEtcd{
Seeds: []string{"localhost:2379"}, // endpoints
}
if err := etcdClient.Connect(); err != nil {
t.Logf("client connection error: %+v", err)
return
}
defer etcdClient.Destroy()
etcdFs := &etcdfs.Fs{
Client: etcdClient.GetClient(),
Metadata: superblock,
DataPrefix: etcdfs.DefaultDataPrefix,
}
etcdFs.Mkdir("/tmp", umask)
etcdFs.Mkdir("/tmp/foo", umask)
etcdFs.Mkdir("/tmp/foo/bar", umask)
tree, err := util.FsTree(etcdFs, "/")
if err != nil {
t.Errorf("tree error: %+v", err)
return
}
t.Logf("tree: \n%s", tree)
var memFs = afero.NewMemMapFs()
if err := util.CopyFs(etcdFs, memFs, "/tmp/foo/bar", "/", false); err != nil {
t.Errorf("CopyFs error: %+v", err)
return
}
if err := util.CopyFs(etcdFs, memFs, "/tmp/foo/bar", "/baz/", false); err != nil {
t.Errorf("CopyFs error: %+v", err)
return
}
tree2, err := util.FsTree(memFs, "/")
if err != nil {
t.Errorf("tree2 error: %+v", err)
return
}
t.Logf("tree2: \n%s", tree2)
if _, err := memFs.Stat("/bar"); err != nil {
t.Errorf("Stat error: %+v", err)
return
}
if _, err := memFs.Stat("/baz/bar"); err != nil {
t.Errorf("Stat error: %+v", err)
return
}
}

View File

@@ -22,6 +22,7 @@ import (
"io" "io"
"os" "os"
"path" "path"
"path/filepath"
"github.com/spf13/afero" "github.com/spf13/afero"
) )
@@ -100,10 +101,7 @@ func CopyFs(srcFs, dstFs afero.Fs, src, dst string, force bool) error {
} }
//perm := info.Perm() //perm := info.Perm()
perm := info.Mode() // TODO: is this correct? perm := info.Mode() // TODO: is this correct?
p := name p := path.Join(dst, filepath.Base(name))
if dst != "/" {
p = path.Join(dst, name) // relative dest in dst
}
if info.IsDir() { if info.IsDir() {
err := dstFs.Mkdir(p, perm) err := dstFs.Mkdir(p, perm)
if os.IsExist(err) && (name == "/" || force) { if os.IsExist(err) && (name == "/" || force) {