mirror of https://github.com/k3s-io/k3s
refactors to kubernetes CP command
parent
f9a243ca99
commit
be929daa25
|
@ -500,16 +500,13 @@ func linkJoin(base, link string) string {
|
|||
if filepath.IsAbs(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,
|
||||
// false otherwise.
|
||||
func isDestRelative(base, dest string) bool {
|
||||
fullPath := dest
|
||||
if !filepath.IsAbs(dest) {
|
||||
fullPath = filepath.Join(base, dest)
|
||||
}
|
||||
relative, err := filepath.Rel(base, fullPath)
|
||||
if err != nil {
|
||||
return false
|
||||
|
|
|
@ -200,12 +200,12 @@ func TestIsDestRelative(t *testing.T) {
|
|||
}{
|
||||
{
|
||||
base: "/dir",
|
||||
dest: "../link",
|
||||
dest: "/dir/../link",
|
||||
relative: false,
|
||||
},
|
||||
{
|
||||
base: "/dir",
|
||||
dest: "../../link",
|
||||
dest: "/dir/../../link",
|
||||
relative: false,
|
||||
},
|
||||
{
|
||||
|
@ -213,21 +213,6 @@ func TestIsDestRelative(t *testing.T) {
|
|||
dest: "/link",
|
||||
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",
|
||||
dest: "/dir/link",
|
||||
|
@ -239,8 +224,18 @@ func TestIsDestRelative(t *testing.T) {
|
|||
relative: true,
|
||||
},
|
||||
{
|
||||
base: "/dir",
|
||||
dest: "/dir/../../link",
|
||||
base: "dir",
|
||||
dest: "dir/link",
|
||||
relative: true,
|
||||
},
|
||||
{
|
||||
base: "dir",
|
||||
dest: "dir/int/../link",
|
||||
relative: true,
|
||||
},
|
||||
{
|
||||
base: "dir",
|
||||
dest: "dir/../../link",
|
||||
relative: false,
|
||||
},
|
||||
}
|
||||
|
@ -792,9 +787,10 @@ func TestUntar(t *testing.T) {
|
|||
}
|
||||
return expected + suffix
|
||||
}
|
||||
mkBacktickExpectation := func(expected, suffix string) string {
|
||||
dir, _ := filepath.Split(filepath.Clean(expected))
|
||||
if len(strings.Split(dir, string(os.PathSeparator))) <= 1 {
|
||||
mkBacktickExpectation := func(path, expected, suffix string) string {
|
||||
linkTarget := filepath.Join(backtick(path), "link-target")
|
||||
baseDir := filepath.Join(testdir, dest)
|
||||
if !isDestRelative(baseDir, linkJoin(filepath.Join(baseDir, path), linkTarget)) {
|
||||
return ""
|
||||
}
|
||||
return expected + suffix
|
||||
|
@ -812,7 +808,7 @@ func TestUntar(t *testing.T) {
|
|||
}, file{
|
||||
path: f.path + "-outerlink",
|
||||
linkTarget: filepath.Join(backtick(f.path), "link-target"),
|
||||
expected: mkBacktickExpectation(f.expected, "-outerlink"),
|
||||
expected: mkBacktickExpectation(f.path, f.expected, "-outerlink"),
|
||||
}, file{
|
||||
path: f.path + "-outerlink-abs",
|
||||
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
|
||||
func backtick(target string) string {
|
||||
rel, _ := filepath.Rel(filepath.Dir(target), "../")
|
||||
rel := filepath.Join(filepath.Dir(target), "../")
|
||||
return rel
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue