container_copy.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package client
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "net/url"
  9. "path/filepath"
  10. "strings"
  11. "golang.org/x/net/context"
  12. "github.com/docker/docker/api/types"
  13. )
  14. // ContainerStatPath returns Stat information about a path inside the container filesystem.
  15. func (cli *Client) ContainerStatPath(ctx context.Context, containerID, path string) (types.ContainerPathStat, error) {
  16. query := url.Values{}
  17. query.Set("path", filepath.ToSlash(path)) // Normalize the paths used in the API.
  18. urlStr := "/containers/" + containerID + "/archive"
  19. response, err := cli.head(ctx, urlStr, query, nil)
  20. if err != nil {
  21. return types.ContainerPathStat{}, err
  22. }
  23. defer ensureReaderClosed(response)
  24. return getContainerPathStatFromHeader(response.header)
  25. }
  26. // CopyToContainer copies content into the container filesystem.
  27. // Note that `content` must be a Reader for a TAR
  28. func (cli *Client) CopyToContainer(ctx context.Context, container, path string, content io.Reader, options types.CopyToContainerOptions) error {
  29. query := url.Values{}
  30. query.Set("path", filepath.ToSlash(path)) // Normalize the paths used in the API.
  31. // Do not allow for an existing directory to be overwritten by a non-directory and vice versa.
  32. if !options.AllowOverwriteDirWithFile {
  33. query.Set("noOverwriteDirNonDir", "true")
  34. }
  35. if options.CopyUIDGID {
  36. query.Set("copyUIDGID", "true")
  37. }
  38. apiPath := "/containers/" + container + "/archive"
  39. response, err := cli.putRaw(ctx, apiPath, query, content, nil)
  40. if err != nil {
  41. return err
  42. }
  43. defer ensureReaderClosed(response)
  44. if response.statusCode != http.StatusOK {
  45. return fmt.Errorf("unexpected status code from daemon: %d", response.statusCode)
  46. }
  47. return nil
  48. }
  49. // CopyFromContainer gets the content from the container and returns it as a Reader
  50. // to manipulate it in the host. It's up to the caller to close the reader.
  51. func (cli *Client) CopyFromContainer(ctx context.Context, container, srcPath string) (io.ReadCloser, types.ContainerPathStat, error) {
  52. query := make(url.Values, 1)
  53. query.Set("path", filepath.ToSlash(srcPath)) // Normalize the paths used in the API.
  54. apiPath := "/containers/" + container + "/archive"
  55. response, err := cli.get(ctx, apiPath, query, nil)
  56. if err != nil {
  57. return nil, types.ContainerPathStat{}, err
  58. }
  59. if response.statusCode != http.StatusOK {
  60. return nil, types.ContainerPathStat{}, fmt.Errorf("unexpected status code from daemon: %d", response.statusCode)
  61. }
  62. // In order to get the copy behavior right, we need to know information
  63. // about both the source and the destination. The response headers include
  64. // stat info about the source that we can use in deciding exactly how to
  65. // copy it locally. Along with the stat info about the local destination,
  66. // we have everything we need to handle the multiple possibilities there
  67. // can be when copying a file/dir from one location to another file/dir.
  68. stat, err := getContainerPathStatFromHeader(response.header)
  69. if err != nil {
  70. return nil, stat, fmt.Errorf("unable to get resource stat from response: %s", err)
  71. }
  72. return response.body, stat, err
  73. }
  74. func getContainerPathStatFromHeader(header http.Header) (types.ContainerPathStat, error) {
  75. var stat types.ContainerPathStat
  76. encodedStat := header.Get("X-Docker-Container-Path-Stat")
  77. statDecoder := base64.NewDecoder(base64.StdEncoding, strings.NewReader(encodedStat))
  78. err := json.NewDecoder(statDecoder).Decode(&stat)
  79. if err != nil {
  80. err = fmt.Errorf("unable to decode container path stat header: %s", err)
  81. }
  82. return stat, err
  83. }