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