mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Strip leading forward slash from resource
This commit is contained in:
parent
5b8cfbe15c
commit
d94b186080
4 changed files with 22 additions and 7 deletions
9
api.go
9
api.go
|
@ -878,23 +878,24 @@ func postContainersCopy(srv *Server, version float64, w http.ResponseWriter, r *
|
||||||
name := vars["name"]
|
name := vars["name"]
|
||||||
|
|
||||||
copyData := &APICopy{}
|
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 {
|
if err := json.NewDecoder(r.Body).Decode(copyData); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else {
|
} 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 == "" {
|
if copyData.Resource == "" {
|
||||||
return fmt.Errorf("Resource cannot be empty")
|
return fmt.Errorf("Resource cannot be empty")
|
||||||
}
|
}
|
||||||
if copyData.Resource[0] == '/' {
|
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 {
|
if err := srv.ContainerCopy(name, copyData.Resource, w); err != nil {
|
||||||
utils.Debugf("%s", err)
|
utils.Debugf("%s", err.Error())
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -1181,7 +1181,7 @@ func TestPostContainersCopy(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
r := httptest.NewRecorder()
|
r := httptest.NewRecorder()
|
||||||
copyData := APICopy{HostPath: ".", Resource: "test.txt"}
|
copyData := APICopy{HostPath: ".", Resource: "/test.txt"}
|
||||||
|
|
||||||
jsonData, err := json.Marshal(copyData)
|
jsonData, err := json.Marshal(copyData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -1492,8 +1492,8 @@ func (cli *DockerCli) CmdCp(args ...string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
r := bytes.NewReader(data)
|
|
||||||
if statusCode == 200 {
|
if statusCode == 200 {
|
||||||
|
r := bytes.NewReader(data)
|
||||||
if err := Untar(r, copyData.HostPath); err != nil {
|
if err := Untar(r, copyData.HostPath); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
16
container.go
16
container.go
|
@ -1094,5 +1094,19 @@ func (container *Container) Copy(resource string) (Archive, error) {
|
||||||
if err := container.EnsureMounted(); err != nil {
|
if err := container.EnsureMounted(); err != nil {
|
||||||
return nil, err
|
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)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue