Skip to content

Commit 109b1e0

Browse files
author
anencore94
committed
make cp commmand allow target path as absolutepath
- only absolute path is allowed Signed-off-by: anencore94 <anencore94@kaist.ac.kr>
1 parent 76f3c72 commit 109b1e0

File tree

3 files changed

+40
-14
lines changed

3 files changed

+40
-14
lines changed

cmd/minikube/cmd/cp.go

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2016 The Kubernetes Authors All rights reserved.
2+
Copyright 2021 The Kubernetes Authors All rights reserved.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -17,11 +17,13 @@ limitations under the License.
1717
package cmd
1818

1919
import (
20-
"os"
21-
2220
"github.com/pkg/errors"
2321
"github.com/spf13/cobra"
2422

23+
"os"
24+
pt "path"
25+
"path/filepath"
26+
2527
"k8s.io/minikube/pkg/minikube/assets"
2628
"k8s.io/minikube/pkg/minikube/exit"
2729
"k8s.io/minikube/pkg/minikube/mustload"
@@ -37,25 +39,27 @@ var (
3739

3840
// cpCmd represents the cp command, similar to docker cp
3941
var cpCmd = &cobra.Command{
40-
Use: "cp <source file path> <target file path followed by '/home/docker/'>",
42+
Use: "cp <source file path> <target file absolute path>",
4143
Short: "Copy the specified file into minikube",
42-
Long: "Copy the specified file into minikube, it will be saved at path \"/home/docker/<target file path>\" in your minikube.\n" +
43-
"Example Command : \"minikube cp a.txt b.txt\"\n",
44+
Long: "Copy the specified file into minikube, it will be saved at path <target file absolute path> in your minikube.\n" +
45+
"Example Command : \"minikube cp a.txt /home/docker/b.txt\"\n",
4446
Run: func(cmd *cobra.Command, args []string) {
45-
// validate args
4647
if len(args) != 2 {
4748
exit.Message(reason.Usage, `Please specify the path to copy:
48-
minikube cp <source file path> <target file path> (example: "minikube cp a/b/c/d.txt a.txt")`)
49+
minikube cp <source file path> <target file absolute path> (example: "minikube cp a/b.txt /copied.txt")`)
4950
}
51+
5052
srcPath = args[0]
5153
dstPath = args[1]
54+
validateArgs(srcPath, dstPath)
5255

5356
co := mustload.Running(ClusterFlagValue())
54-
fa, err := assets.NewFileAsset(srcPath, "/home/docker/", dstPath, "0644")
57+
fa, err := assets.NewFileAsset(srcPath, pt.Dir(dstPath), pt.Base(dstPath), "0644")
5558
if err != nil {
5659
out.ErrLn("%v", errors.Wrap(err, "getting file asset"))
5760
os.Exit(1)
5861
}
62+
5963
if err = co.CP.Runner.Copy(fa); err != nil {
6064
out.ErrLn("%v", errors.Wrap(err, "copying file"))
6165
os.Exit(1)
@@ -65,3 +69,25 @@ var cpCmd = &cobra.Command{
6569

6670
func init() {
6771
}
72+
73+
func validateArgs(srcPath string, dstPath string) {
74+
if srcPath == "" {
75+
exit.Message(reason.Usage, "Source {{.path}} can not be empty", out.V{"path": srcPath})
76+
}
77+
78+
if dstPath == "" {
79+
exit.Message(reason.Usage, "Target {{.path}} can not be empty", out.V{"path": dstPath})
80+
}
81+
82+
if _, err := os.Stat(srcPath); err != nil {
83+
if os.IsNotExist(err) {
84+
exit.Message(reason.HostPathMissing, "Cannot find directory {{.path}} for copy", out.V{"path": srcPath})
85+
} else {
86+
exit.Error(reason.HostPathStat, "stat failed", err)
87+
}
88+
}
89+
90+
if !filepath.IsAbs(dstPath) {
91+
exit.Message(reason.Usage, `<target file absolute path> must be an absolute Path. Relative Path is not allowed (example: "/home/docker/copied.txt")`)
92+
}
93+
}

site/content/en/docs/commands/cp.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ Copy the specified file into minikube
1111

1212
### Synopsis
1313

14-
Copy the specified file into minikube, it will be saved at path "/home/docker/<target file path>" in your minikube.
15-
Example Command : "minikube cp a.txt b.txt"
14+
Copy the specified file into minikube, it will be saved at path <target file absolute path> in your minikube.
15+
Example Command : "minikube cp a.txt /home/docker/b.txt"
1616

1717

1818
```shell
19-
minikube cp <source file path> <target file path followed by '/home/docker/'> [flags]
19+
minikube cp <source file path> <target file absolute path> [flags]
2020
```
2121

2222
### Options inherited from parent commands

test/integration/functional_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,7 @@ func validateCpCmd(ctx context.Context, t *testing.T, profile string) {
11511151
}
11521152

11531153
cpPath := filepath.Join(*testdataDir, "cp-test.txt")
1154-
rr, err := Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "cp", cpPath, "hello_cp.txt"))
1154+
rr, err := Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "cp", cpPath, "/home/docker/hello_cp.txt"))
11551155
if ctx.Err() == context.DeadlineExceeded {
11561156
t.Errorf("failed to run command by deadline. exceeded timeout : %s", rr.Command())
11571157
}
@@ -1160,7 +1160,7 @@ func validateCpCmd(ctx context.Context, t *testing.T, profile string) {
11601160
}
11611161

11621162
expected := "Test file for checking file cp process"
1163-
rr, err = Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "ssh", "cat hello_cp.txt"))
1163+
rr, err = Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "ssh", "cat /home/docker/hello_cp.txt"))
11641164
if ctx.Err() == context.DeadlineExceeded {
11651165
t.Errorf("failed to run command by deadline. exceeded timeout : %s", rr.Command())
11661166
}

0 commit comments

Comments
 (0)