1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Removed engine from /copy

* Client and server use types.CopyConfig
* API calls container.Copy directly

Signed-off-by: Alexander Morozov <lk4d4@docker.com>
This commit is contained in:
Alexander Morozov 2015-04-09 13:05:31 -07:00
parent 6fb8bd0450
commit ccdef895de
5 changed files with 37 additions and 59 deletions

View file

@ -5,7 +5,7 @@ import (
"io" "io"
"strings" "strings"
"github.com/docker/docker/engine" "github.com/docker/docker/api/types"
"github.com/docker/docker/pkg/archive" "github.com/docker/docker/pkg/archive"
flag "github.com/docker/docker/pkg/mflag" flag "github.com/docker/docker/pkg/mflag"
) )
@ -21,17 +21,16 @@ func (cli *DockerCli) CmdCp(args ...string) error {
cmd.ParseFlags(args, true) cmd.ParseFlags(args, true)
var copyData engine.Env
info := strings.Split(cmd.Arg(0), ":") info := strings.Split(cmd.Arg(0), ":")
if len(info) != 2 { if len(info) != 2 {
return fmt.Errorf("Error: Path not specified") return fmt.Errorf("Error: Path not specified")
} }
copyData.Set("Resource", info[1]) cfg := &types.CopyConfig{
copyData.Set("HostPath", cmd.Arg(1)) Resource: info[1],
}
stream, statusCode, err := cli.call("POST", "/containers/"+info[0]+"/copy", copyData, nil) stream, statusCode, err := cli.call("POST", "/containers/"+info[0]+"/copy", cfg, nil)
if stream != nil { if stream != nil {
defer stream.Close() defer stream.Close()
} }
@ -42,13 +41,12 @@ func (cli *DockerCli) CmdCp(args ...string) error {
return err return err
} }
hostPath := cmd.Arg(1)
if statusCode == 200 { if statusCode == 200 {
dest := copyData.Get("HostPath") if hostPath == "-" {
if dest == "-" {
_, err = io.Copy(cli.out, stream) _, err = io.Copy(cli.out, stream)
} else { } else {
err = archive.Untar(stream, dest, &archive.TarOptions{NoLchown: true}) err = archive.Untar(stream, hostPath, &archive.TarOptions{NoLchown: true})
} }
if err != nil { if err != nil {
return err return err

View file

@ -1203,37 +1203,48 @@ func postContainersCopy(eng *engine.Engine, version version.Version, w http.Resp
return fmt.Errorf("Missing parameter") return fmt.Errorf("Missing parameter")
} }
var copyData engine.Env
if err := checkForJson(r); err != nil { if err := checkForJson(r); err != nil {
return err return err
} }
if err := copyData.Decode(r.Body); err != nil { cfg := types.CopyConfig{}
if err := json.NewDecoder(r.Body).Decode(&cfg); err != nil {
return err return err
} }
if copyData.Get("Resource") == "" { if cfg.Resource == "" {
return fmt.Errorf("Path cannot be empty") return fmt.Errorf("Path cannot be empty")
} }
origResource := copyData.Get("Resource") res := cfg.Resource
if copyData.Get("Resource")[0] == '/' { if res[0] == '/' {
copyData.Set("Resource", copyData.Get("Resource")[1:]) res = res[1:]
} }
job := eng.Job("container_copy", vars["name"], copyData.Get("Resource")) cont, err := getDaemon(eng).Get(vars["name"])
job.Stdout.Add(w) if err != nil {
w.Header().Set("Content-Type", "application/x-tar")
if err := job.Run(); err != nil {
logrus.Errorf("%v", err) logrus.Errorf("%v", err)
if strings.Contains(strings.ToLower(err.Error()), "no such id") { if strings.Contains(strings.ToLower(err.Error()), "no such id") {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
} else if strings.Contains(err.Error(), "no such file or directory") { return nil
return fmt.Errorf("Could not find the file %s in container %s", origResource, vars["name"])
} }
} }
data, err := cont.Copy(res)
if err != nil {
logrus.Errorf("%v", err)
if os.IsNotExist(err) {
return fmt.Errorf("Could not find the file %s in container %s", cfg.Resource, vars["name"])
}
return err
}
defer data.Close()
w.Header().Set("Content-Type", "application/x-tar")
if _, err := io.Copy(w, data); err != nil {
return err
}
return nil return nil
} }

View file

@ -98,3 +98,8 @@ type Container struct {
Labels map[string]string `json:",omitempty"` Labels map[string]string `json:",omitempty"`
Status string `json:",omitempty"` Status string `json:",omitempty"`
} }
// POST "/containers/"+containerID+"/copy"
type CopyConfig struct {
Resource string
}

View file

@ -1,35 +0,0 @@
package daemon
import (
"fmt"
"io"
"github.com/docker/docker/engine"
)
func (daemon *Daemon) ContainerCopy(job *engine.Job) error {
if len(job.Args) != 2 {
return fmt.Errorf("Usage: %s CONTAINER RESOURCE\n", job.Name)
}
var (
name = job.Args[0]
resource = job.Args[1]
)
container, err := daemon.Get(name)
if err != nil {
return err
}
data, err := container.Copy(resource)
if err != nil {
return err
}
defer data.Close()
if _, err := io.Copy(job.Stdout, data); err != nil {
return err
}
return nil
}

View file

@ -117,7 +117,6 @@ type Daemon struct {
func (daemon *Daemon) Install(eng *engine.Engine) error { func (daemon *Daemon) Install(eng *engine.Engine) error {
for name, method := range map[string]engine.Handler{ for name, method := range map[string]engine.Handler{
"commit": daemon.ContainerCommit, "commit": daemon.ContainerCommit,
"container_copy": daemon.ContainerCopy,
"container_inspect": daemon.ContainerInspect, "container_inspect": daemon.ContainerInspect,
"container_stats": daemon.ContainerStats, "container_stats": daemon.ContainerStats,
"create": daemon.ContainerCreate, "create": daemon.ContainerCreate,