2015-11-04 20:38:05 -05:00
|
|
|
package container
|
2015-07-28 14:35:24 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
"github.com/docker/docker/api/server/httputils"
|
2016-01-04 19:05:26 -05:00
|
|
|
"github.com/docker/engine-api/types"
|
2016-06-03 13:38:03 -04:00
|
|
|
"github.com/docker/engine-api/types/versions"
|
2015-09-29 17:32:07 -04:00
|
|
|
"golang.org/x/net/context"
|
2015-07-28 14:35:24 -04:00
|
|
|
)
|
|
|
|
|
2015-08-11 19:45:23 -04:00
|
|
|
// postContainersCopy is deprecated in favor of getContainersArchive.
|
2015-11-04 20:38:05 -05:00
|
|
|
func (s *containerRouter) postContainersCopy(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2016-06-03 13:38:03 -04:00
|
|
|
// Deprecated since 1.8, Errors out since 1.12
|
|
|
|
version := httputils.VersionFromContext(ctx)
|
|
|
|
if versions.GreaterThanOrEqualTo(version, "1.24") {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return nil
|
|
|
|
}
|
2015-09-23 19:42:08 -04:00
|
|
|
if err := httputils.CheckForJSON(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := types.CopyConfig{}
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&cfg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.Resource == "" {
|
|
|
|
return fmt.Errorf("Path cannot be empty")
|
|
|
|
}
|
|
|
|
|
2015-11-04 20:38:05 -05:00
|
|
|
data, err := s.backend.ContainerCopy(vars["name"], cfg.Resource)
|
2015-07-28 14:35:24 -04:00
|
|
|
if err != nil {
|
2015-12-04 02:00:08 -05:00
|
|
|
if strings.Contains(strings.ToLower(err.Error()), "no such container") {
|
2015-07-28 14:35:24 -04:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// // Encode the stat to JSON, base64 encode, and place in a header.
|
|
|
|
func setContainerPathStatHeader(stat *types.ContainerPathStat, header http.Header) error {
|
|
|
|
statJSON, err := json.Marshal(stat)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
header.Set(
|
|
|
|
"X-Docker-Container-Path-Stat",
|
|
|
|
base64.StdEncoding.EncodeToString(statJSON),
|
|
|
|
)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-04 20:38:05 -05:00
|
|
|
func (s *containerRouter) headContainersArchive(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-23 19:42:08 -04:00
|
|
|
v, err := httputils.ArchiveFormValues(r, vars)
|
2015-07-28 14:35:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-04 20:38:05 -05:00
|
|
|
stat, err := s.backend.ContainerStatPath(v.Name, v.Path)
|
2015-07-28 14:35:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return setContainerPathStatHeader(stat, w.Header())
|
|
|
|
}
|
|
|
|
|
2015-11-04 20:38:05 -05:00
|
|
|
func (s *containerRouter) getContainersArchive(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-23 19:42:08 -04:00
|
|
|
v, err := httputils.ArchiveFormValues(r, vars)
|
2015-07-28 14:35:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-04 20:38:05 -05:00
|
|
|
tarArchive, stat, err := s.backend.ContainerArchivePath(v.Name, v.Path)
|
2015-07-28 14:35:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tarArchive.Close()
|
|
|
|
|
|
|
|
if err := setContainerPathStatHeader(stat, w.Header()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/x-tar")
|
|
|
|
_, err = io.Copy(w, tarArchive)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-04 20:38:05 -05:00
|
|
|
func (s *containerRouter) putContainersArchive(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-23 19:42:08 -04:00
|
|
|
v, err := httputils.ArchiveFormValues(r, vars)
|
2015-07-28 14:35:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
noOverwriteDirNonDir := httputils.BoolValue(r, "noOverwriteDirNonDir")
|
2015-11-04 20:38:05 -05:00
|
|
|
return s.backend.ContainerExtractToDir(v.Name, v.Path, noOverwriteDirNonDir, r.Body)
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|