container_copy.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 := fmt.Sprintf("/containers/%s/archive", containerID)
  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. func (cli *Client) CopyToContainer(ctx context.Context, container, path string, content io.Reader, options types.CopyToContainerOptions) error {
  28. query := url.Values{}
  29. query.Set("path", filepath.ToSlash(path)) // Normalize the paths used in the API.
  30. // Do not allow for an existing directory to be overwritten by a non-directory and vice versa.
  31. if !options.AllowOverwriteDirWithFile {
  32. query.Set("noOverwriteDirNonDir", "true")
  33. }
  34. if options.CopyUIDGID {
  35. query.Set("copyUIDGID", "true")
  36. }
  37. apiPath := fmt.Sprintf("/containers/%s/archive", container)
  38. response, err := cli.putRaw(ctx, apiPath, query, content, nil)
  39. if err != nil {
  40. return err
  41. }
  42. defer ensureReaderClosed(response)
  43. if response.statusCode != http.StatusOK {
  44. return fmt.Errorf("unexpected status code from daemon: %d", response.statusCode)
  45. }
  46. return nil
  47. }
  48. // CopyFromContainer gets the content from the container and returns it as a Reader
  49. // to manipulate it in the host. It's up to the caller to close the reader.
  50. func (cli *Client) CopyFromContainer(ctx context.Context, container, srcPath string) (io.ReadCloser, types.ContainerPathStat, error) {
  51. query := make(url.Values, 1)
  52. query.Set("path", filepath.ToSlash(srcPath)) // Normalize the paths used in the API.
  53. apiPath := fmt.Sprintf("/containers/%s/archive", container)
  54. response, err := cli.get(ctx, apiPath, query, nil)
  55. if err != nil {
  56. return nil, types.ContainerPathStat{}, err
  57. }
  58. if response.statusCode != http.StatusOK {
  59. return nil, types.ContainerPathStat{}, fmt.Errorf("unexpected status code from daemon: %d", response.statusCode)
  60. }
  61. // In order to get the copy behavior right, we need to know information
  62. // about both the source and the destination. The response headers include
  63. // stat info about the source that we can use in deciding exactly how to
  64. // copy it locally. Along with the stat info about the local destination,
  65. // we have everything we need to handle the multiple possibilities there
  66. // can be when copying a file/dir from one location to another file/dir.
  67. stat, err := getContainerPathStatFromHeader(response.header)
  68. if err != nil {
  69. return nil, stat, fmt.Errorf("unable to get resource stat from response: %s", err)
  70. }
  71. return response.body, stat, err
  72. }
  73. func getContainerPathStatFromHeader(header http.Header) (types.ContainerPathStat, error) {
  74. var stat types.ContainerPathStat
  75. encodedStat := header.Get("X-Docker-Container-Path-Stat")
  76. statDecoder := base64.NewDecoder(base64.StdEncoding, strings.NewReader(encodedStat))
  77. err := json.NewDecoder(statDecoder).Decode(&stat)
  78. if err != nil {
  79. err = fmt.Errorf("unable to decode container path stat header: %s", err)
  80. }
  81. return stat, err
  82. }