Strip leading forward slash from resource

This commit is contained in:
Michael Crosby 2013-07-17 05:25:49 -09:00
parent 5b8cfbe15c
commit d94b186080
4 changed files with 22 additions and 7 deletions

9
api.go
View File

@ -878,23 +878,24 @@ func postContainersCopy(srv *Server, version float64, w http.ResponseWriter, r *
name := vars["name"]
copyData := &APICopy{}
if r.Header.Get("Content-Type") == "application/json" {
contentType := r.Header.Get("Content-Type")
if contentType == "application/json" {
if err := json.NewDecoder(r.Body).Decode(copyData); err != nil {
return err
}
} else {
return fmt.Errorf("Content-Type not supported: %s", r.Header.Get("Content-Type"))
return fmt.Errorf("Content-Type not supported: %s", contentType)
}
if copyData.Resource == "" {
return fmt.Errorf("Resource cannot be empty")
}
if copyData.Resource[0] == '/' {
return fmt.Errorf("Resource cannot contain a leading /")
copyData.Resource = copyData.Resource[1:]
}
if err := srv.ContainerCopy(name, copyData.Resource, w); err != nil {
utils.Debugf("%s", err)
utils.Debugf("%s", err.Error())
return err
}
return nil

View File

@ -1181,7 +1181,7 @@ func TestPostContainersCopy(t *testing.T) {
}
r := httptest.NewRecorder()
copyData := APICopy{HostPath: ".", Resource: "test.txt"}
copyData := APICopy{HostPath: ".", Resource: "/test.txt"}
jsonData, err := json.Marshal(copyData)
if err != nil {

View File

@ -1492,8 +1492,8 @@ func (cli *DockerCli) CmdCp(args ...string) error {
return err
}
r := bytes.NewReader(data)
if statusCode == 200 {
r := bytes.NewReader(data)
if err := Untar(r, copyData.HostPath); err != nil {
return err
}

View File

@ -1094,5 +1094,19 @@ func (container *Container) Copy(resource string) (Archive, error) {
if err := container.EnsureMounted(); err != nil {
return nil, err
}
return TarFilter(container.RootfsPath(), Uncompressed, []string{resource})
var filter []string
basePath := path.Join(container.RootfsPath(), resource)
stat, err := os.Stat(basePath)
if err != nil {
return nil, err
}
if !stat.IsDir() {
d, f := path.Split(basePath)
basePath = d
filter = []string{f}
} else {
filter = []string{path.Base(basePath)}
basePath = path.Dir(basePath)
}
return TarFilter(basePath, Uncompressed, filter)
}