copy.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package container
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "io"
  6. "net/http"
  7. "github.com/docker/docker/api/server/httputils"
  8. "github.com/docker/docker/api/types"
  9. "github.com/docker/docker/api/types/versions"
  10. "golang.org/x/net/context"
  11. )
  12. type pathError struct{}
  13. func (pathError) Error() string {
  14. return "Path cannot be empty"
  15. }
  16. func (pathError) InvalidParameter() {}
  17. // postContainersCopy is deprecated in favor of getContainersArchive.
  18. func (s *containerRouter) postContainersCopy(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  19. // Deprecated since 1.8, Errors out since 1.12
  20. version := httputils.VersionFromContext(ctx)
  21. if versions.GreaterThanOrEqualTo(version, "1.24") {
  22. w.WriteHeader(http.StatusNotFound)
  23. return nil
  24. }
  25. if err := httputils.CheckForJSON(r); err != nil {
  26. return err
  27. }
  28. cfg := types.CopyConfig{}
  29. if err := json.NewDecoder(r.Body).Decode(&cfg); err != nil {
  30. return err
  31. }
  32. if cfg.Resource == "" {
  33. return pathError{}
  34. }
  35. data, err := s.backend.ContainerCopy(vars["name"], cfg.Resource)
  36. if err != nil {
  37. return err
  38. }
  39. defer data.Close()
  40. w.Header().Set("Content-Type", "application/x-tar")
  41. _, err = io.Copy(w, data)
  42. return err
  43. }
  44. // // Encode the stat to JSON, base64 encode, and place in a header.
  45. func setContainerPathStatHeader(stat *types.ContainerPathStat, header http.Header) error {
  46. statJSON, err := json.Marshal(stat)
  47. if err != nil {
  48. return err
  49. }
  50. header.Set(
  51. "X-Docker-Container-Path-Stat",
  52. base64.StdEncoding.EncodeToString(statJSON),
  53. )
  54. return nil
  55. }
  56. func (s *containerRouter) headContainersArchive(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  57. v, err := httputils.ArchiveFormValues(r, vars)
  58. if err != nil {
  59. return err
  60. }
  61. stat, err := s.backend.ContainerStatPath(v.Name, v.Path)
  62. if err != nil {
  63. return err
  64. }
  65. return setContainerPathStatHeader(stat, w.Header())
  66. }
  67. func (s *containerRouter) getContainersArchive(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  68. v, err := httputils.ArchiveFormValues(r, vars)
  69. if err != nil {
  70. return err
  71. }
  72. tarArchive, stat, err := s.backend.ContainerArchivePath(v.Name, v.Path)
  73. if err != nil {
  74. return err
  75. }
  76. defer tarArchive.Close()
  77. if err := setContainerPathStatHeader(stat, w.Header()); err != nil {
  78. return err
  79. }
  80. w.Header().Set("Content-Type", "application/x-tar")
  81. _, err = io.Copy(w, tarArchive)
  82. return err
  83. }
  84. func (s *containerRouter) putContainersArchive(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  85. v, err := httputils.ArchiveFormValues(r, vars)
  86. if err != nil {
  87. return err
  88. }
  89. noOverwriteDirNonDir := httputils.BoolValue(r, "noOverwriteDirNonDir")
  90. copyUIDGID := httputils.BoolValue(r, "copyUIDGID")
  91. return s.backend.ContainerExtractToDir(v.Name, v.Path, copyUIDGID, noOverwriteDirNonDir, r.Body)
  92. }