refactors to kubernetes CP command

This commit is contained in:
M00nF1sh 2019-06-24 11:16:00 -07:00
parent 7e6b70fbb5
commit bad1802892
2 changed files with 21 additions and 28 deletions

View File

@ -500,16 +500,13 @@ func linkJoin(base, link string) string {
if filepath.IsAbs(link) { if filepath.IsAbs(link) {
return link return link
} }
return filepath.Join(base, link) return filepath.Join(filepath.Dir(base), link)
} }
// isDestRelative returns true if dest is pointing outside the base directory, // isDestRelative returns true if dest is pointing outside the base directory,
// false otherwise. // false otherwise.
func isDestRelative(base, dest string) bool { func isDestRelative(base, dest string) bool {
fullPath := dest fullPath := dest
if !filepath.IsAbs(dest) {
fullPath = filepath.Join(base, dest)
}
relative, err := filepath.Rel(base, fullPath) relative, err := filepath.Rel(base, fullPath)
if err != nil { if err != nil {
return false return false

View File

@ -200,12 +200,12 @@ func TestIsDestRelative(t *testing.T) {
}{ }{
{ {
base: "/dir", base: "/dir",
dest: "../link", dest: "/dir/../link",
relative: false, relative: false,
}, },
{ {
base: "/dir", base: "/dir",
dest: "../../link", dest: "/dir/../../link",
relative: false, relative: false,
}, },
{ {
@ -213,21 +213,6 @@ func TestIsDestRelative(t *testing.T) {
dest: "/link", dest: "/link",
relative: false, relative: false,
}, },
{
base: "/dir",
dest: "link",
relative: true,
},
{
base: "/dir",
dest: "int/file/link",
relative: true,
},
{
base: "/dir",
dest: "int/../link",
relative: true,
},
{ {
base: "/dir", base: "/dir",
dest: "/dir/link", dest: "/dir/link",
@ -239,8 +224,18 @@ func TestIsDestRelative(t *testing.T) {
relative: true, relative: true,
}, },
{ {
base: "/dir", base: "dir",
dest: "/dir/../../link", dest: "dir/link",
relative: true,
},
{
base: "dir",
dest: "dir/int/../link",
relative: true,
},
{
base: "dir",
dest: "dir/../../link",
relative: false, relative: false,
}, },
} }
@ -792,9 +787,10 @@ func TestUntar(t *testing.T) {
} }
return expected + suffix return expected + suffix
} }
mkBacktickExpectation := func(expected, suffix string) string { mkBacktickExpectation := func(path, expected, suffix string) string {
dir, _ := filepath.Split(filepath.Clean(expected)) linkTarget := filepath.Join(backtick(path), "link-target")
if len(strings.Split(dir, string(os.PathSeparator))) <= 1 { baseDir := filepath.Join(testdir, dest)
if !isDestRelative(baseDir, linkJoin(filepath.Join(baseDir, path), linkTarget)) {
return "" return ""
} }
return expected + suffix return expected + suffix
@ -812,7 +808,7 @@ func TestUntar(t *testing.T) {
}, file{ }, file{
path: f.path + "-outerlink", path: f.path + "-outerlink",
linkTarget: filepath.Join(backtick(f.path), "link-target"), linkTarget: filepath.Join(backtick(f.path), "link-target"),
expected: mkBacktickExpectation(f.expected, "-outerlink"), expected: mkBacktickExpectation(f.path, f.expected, "-outerlink"),
}, file{ }, file{
path: f.path + "-outerlink-abs", path: f.path + "-outerlink-abs",
linkTarget: filepath.Join(testdir, "link-target"), linkTarget: filepath.Join(testdir, "link-target"),
@ -918,7 +914,7 @@ func TestUntar(t *testing.T) {
// backtick returns a path to one directory up from the target // backtick returns a path to one directory up from the target
func backtick(target string) string { func backtick(target string) string {
rel, _ := filepath.Rel(filepath.Dir(target), "../") rel := filepath.Join(filepath.Dir(target), "../")
return rel return rel
} }