Browse code

Strip leading forward slash from resource

Michael Crosby authored on 2013/07/17 23:25:49
Showing 4 changed files
... ...
@@ -878,23 +878,24 @@ func postContainersCopy(srv *Server, version float64, w http.ResponseWriter, r *
878 878
 	name := vars["name"]
879 879
 
880 880
 	copyData := &APICopy{}
881
-	if r.Header.Get("Content-Type") == "application/json" {
881
+	contentType := r.Header.Get("Content-Type")
882
+	if contentType == "application/json" {
882 883
 		if err := json.NewDecoder(r.Body).Decode(copyData); err != nil {
883 884
 			return err
884 885
 		}
885 886
 	} else {
886
-		return fmt.Errorf("Content-Type not supported: %s", r.Header.Get("Content-Type"))
887
+		return fmt.Errorf("Content-Type not supported: %s", contentType)
887 888
 	}
888 889
 
889 890
 	if copyData.Resource == "" {
890 891
 		return fmt.Errorf("Resource cannot be empty")
891 892
 	}
892 893
 	if copyData.Resource[0] == '/' {
893
-		return fmt.Errorf("Resource cannot contain a leading /")
894
+		copyData.Resource = copyData.Resource[1:]
894 895
 	}
895 896
 
896 897
 	if err := srv.ContainerCopy(name, copyData.Resource, w); err != nil {
897
-		utils.Debugf("%s", err)
898
+		utils.Debugf("%s", err.Error())
898 899
 		return err
899 900
 	}
900 901
 	return nil
... ...
@@ -1181,7 +1181,7 @@ func TestPostContainersCopy(t *testing.T) {
1181 1181
 	}
1182 1182
 
1183 1183
 	r := httptest.NewRecorder()
1184
-	copyData := APICopy{HostPath: ".", Resource: "test.txt"}
1184
+	copyData := APICopy{HostPath: ".", Resource: "/test.txt"}
1185 1185
 
1186 1186
 	jsonData, err := json.Marshal(copyData)
1187 1187
 	if err != nil {
... ...
@@ -1492,8 +1492,8 @@ func (cli *DockerCli) CmdCp(args ...string) error {
1492 1492
 		return err
1493 1493
 	}
1494 1494
 
1495
-	r := bytes.NewReader(data)
1496 1495
 	if statusCode == 200 {
1496
+		r := bytes.NewReader(data)
1497 1497
 		if err := Untar(r, copyData.HostPath); err != nil {
1498 1498
 			return err
1499 1499
 		}
... ...
@@ -1094,5 +1094,19 @@ func (container *Container) Copy(resource string) (Archive, error) {
1094 1094
 	if err := container.EnsureMounted(); err != nil {
1095 1095
 		return nil, err
1096 1096
 	}
1097
-	return TarFilter(container.RootfsPath(), Uncompressed, []string{resource})
1097
+	var filter []string
1098
+	basePath := path.Join(container.RootfsPath(), resource)
1099
+	stat, err := os.Stat(basePath)
1100
+	if err != nil {
1101
+		return nil, err
1102
+	}
1103
+	if !stat.IsDir() {
1104
+		d, f := path.Split(basePath)
1105
+		basePath = d
1106
+		filter = []string{f}
1107
+	} else {
1108
+		filter = []string{path.Base(basePath)}
1109
+		basePath = path.Dir(basePath)
1110
+	}
1111
+	return TarFilter(basePath, Uncompressed, filter)
1098 1112
 }