github.com/zhouyu0/docker-note@v0.0.0-20190722021225-b8d3825084db/builder/dockerfile/copy.go (about)

     1  package dockerfile // import "github.com/docker/docker/builder/dockerfile"
     2  
     3  import (
     4  	"archive/tar"
     5  	"fmt"
     6  	"io"
     7  	"mime"
     8  	"net/http"
     9  	"net/url"
    10  	"os"
    11  	"path/filepath"
    12  	"runtime"
    13  	"sort"
    14  	"strings"
    15  	"time"
    16  
    17  	"github.com/docker/docker/builder"
    18  	"github.com/docker/docker/builder/remotecontext"
    19  	"github.com/docker/docker/pkg/archive"
    20  	"github.com/docker/docker/pkg/containerfs"
    21  	"github.com/docker/docker/pkg/idtools"
    22  	"github.com/docker/docker/pkg/ioutils"
    23  	"github.com/docker/docker/pkg/progress"
    24  	"github.com/docker/docker/pkg/streamformatter"
    25  	"github.com/docker/docker/pkg/system"
    26  	"github.com/docker/docker/pkg/urlutil"
    27  	specs "github.com/opencontainers/image-spec/specs-go/v1"
    28  	"github.com/pkg/errors"
    29  )
    30  
    31  const unnamedFilename = "__unnamed__"
    32  
    33  type pathCache interface {
    34  	Load(key interface{}) (value interface{}, ok bool)
    35  	Store(key, value interface{})
    36  }
    37  
    38  // copyInfo is a data object which stores the metadata about each source file in
    39  // a copyInstruction
    40  type copyInfo struct {
    41  	root         containerfs.ContainerFS
    42  	path         string
    43  	hash         string
    44  	noDecompress bool
    45  }
    46  
    47  func (c copyInfo) fullPath() (string, error) {
    48  	return c.root.ResolveScopedPath(c.path, true)
    49  }
    50  
    51  func newCopyInfoFromSource(source builder.Source, path string, hash string) copyInfo {
    52  	return copyInfo{root: source.Root(), path: path, hash: hash}
    53  }
    54  
    55  func newCopyInfos(copyInfos ...copyInfo) []copyInfo {
    56  	return copyInfos
    57  }
    58  
    59  // copyInstruction is a fully parsed COPY or ADD command that is passed to
    60  // Builder.performCopy to copy files into the image filesystem
    61  type copyInstruction struct {
    62  	cmdName                 string
    63  	infos                   []copyInfo
    64  	dest                    string
    65  	chownStr                string
    66  	allowLocalDecompression bool
    67  }
    68  
    69  // copier reads a raw COPY or ADD command, fetches remote sources using a downloader,
    70  // and creates a copyInstruction
    71  type copier struct {
    72  	imageSource *imageMount
    73  	source      builder.Source
    74  	pathCache   pathCache
    75  	download    sourceDownloader
    76  	platform    *specs.Platform
    77  	// for cleanup. TODO: having copier.cleanup() is error prone and hard to
    78  	// follow. Code calling performCopy should manage the lifecycle of its params.
    79  	// Copier should take override source as input, not imageMount.
    80  	activeLayer builder.RWLayer
    81  	tmpPaths    []string
    82  }
    83  
    84  func copierFromDispatchRequest(req dispatchRequest, download sourceDownloader, imageSource *imageMount) copier {
    85  	platform := req.builder.platform
    86  	if platform == nil {
    87  		// May be nil if not explicitly set in API/dockerfile
    88  		platform = &specs.Platform{}
    89  	}
    90  	if platform.OS == "" {
    91  		// Default to the dispatch requests operating system if not explicit in API/dockerfile
    92  		platform.OS = req.state.operatingSystem
    93  	}
    94  	if platform.OS == "" {
    95  		// This is a failsafe just in case. Shouldn't be hit.
    96  		platform.OS = runtime.GOOS
    97  	}
    98  
    99  	return copier{
   100  		source:      req.source,
   101  		pathCache:   req.builder.pathCache,
   102  		download:    download,
   103  		imageSource: imageSource,
   104  		platform:    platform,
   105  	}
   106  
   107  }
   108  
   109  func (o *copier) createCopyInstruction(args []string, cmdName string) (copyInstruction, error) {
   110  	inst := copyInstruction{cmdName: cmdName}
   111  	last := len(args) - 1
   112  
   113  	// Work in platform-specific filepath semantics
   114  	// TODO: This OS switch for paths is NOT correct and should not be supported.
   115  	// Maintained for backwards compatibility
   116  	pathOS := runtime.GOOS
   117  	if o.platform != nil {
   118  		pathOS = o.platform.OS
   119  	}
   120  	inst.dest = fromSlash(args[last], pathOS)
   121  	separator := string(separator(pathOS))
   122  	infos, err := o.getCopyInfosForSourcePaths(args[0:last], inst.dest)
   123  	if err != nil {
   124  		return inst, errors.Wrapf(err, "%s failed", cmdName)
   125  	}
   126  	if len(infos) > 1 && !strings.HasSuffix(inst.dest, separator) {
   127  		return inst, errors.Errorf("When using %s with more than one source file, the destination must be a directory and end with a /", cmdName)
   128  	}
   129  	inst.infos = infos
   130  	return inst, nil
   131  }
   132  
   133  // getCopyInfosForSourcePaths iterates over the source files and calculate the info
   134  // needed to copy (e.g. hash value if cached)
   135  // The dest is used in case source is URL (and ends with "/")
   136  func (o *copier) getCopyInfosForSourcePaths(sources []string, dest string) ([]copyInfo, error) {
   137  	var infos []copyInfo
   138  	for _, orig := range sources {
   139  		subinfos, err := o.getCopyInfoForSourcePath(orig, dest)
   140  		if err != nil {
   141  			return nil, err
   142  		}
   143  		infos = append(infos, subinfos...)
   144  	}
   145  
   146  	if len(infos) == 0 {
   147  		return nil, errors.New("no source files were specified")
   148  	}
   149  	return infos, nil
   150  }
   151  
   152  func (o *copier) getCopyInfoForSourcePath(orig, dest string) ([]copyInfo, error) {
   153  	if !urlutil.IsURL(orig) {
   154  		return o.calcCopyInfo(orig, true)
   155  	}
   156  
   157  	remote, path, err := o.download(orig)
   158  	if err != nil {
   159  		return nil, err
   160  	}
   161  	// If path == "" then we are unable to determine filename from src
   162  	// We have to make sure dest is available
   163  	if path == "" {
   164  		if strings.HasSuffix(dest, "/") {
   165  			return nil, errors.Errorf("cannot determine filename for source %s", orig)
   166  		}
   167  		path = unnamedFilename
   168  	}
   169  	o.tmpPaths = append(o.tmpPaths, remote.Root().Path())
   170  
   171  	hash, err := remote.Hash(path)
   172  	ci := newCopyInfoFromSource(remote, path, hash)
   173  	ci.noDecompress = true // data from http shouldn't be extracted even on ADD
   174  	return newCopyInfos(ci), err
   175  }
   176  
   177  // Cleanup removes any temporary directories created as part of downloading
   178  // remote files.
   179  func (o *copier) Cleanup() {
   180  	for _, path := range o.tmpPaths {
   181  		os.RemoveAll(path)
   182  	}
   183  	o.tmpPaths = []string{}
   184  	if o.activeLayer != nil {
   185  		o.activeLayer.Release()
   186  		o.activeLayer = nil
   187  	}
   188  }
   189  
   190  // TODO: allowWildcards can probably be removed by refactoring this function further.
   191  func (o *copier) calcCopyInfo(origPath string, allowWildcards bool) ([]copyInfo, error) {
   192  	imageSource := o.imageSource
   193  
   194  	// TODO: do this when creating copier. Requires validateCopySourcePath
   195  	// (and other below) to be aware of the difference sources. Why is it only
   196  	// done on image Source?
   197  	if imageSource != nil && o.activeLayer == nil {
   198  		// this needs to be protected against repeated calls as wildcard copy
   199  		// will call it multiple times for a single COPY
   200  		var err error
   201  		rwLayer, err := imageSource.NewRWLayer()
   202  		if err != nil {
   203  			return nil, err
   204  		}
   205  		o.activeLayer = rwLayer
   206  
   207  		o.source, err = remotecontext.NewLazySource(rwLayer.Root())
   208  		if err != nil {
   209  			return nil, errors.Wrapf(err, "failed to create context for copy from %s", rwLayer.Root().Path())
   210  		}
   211  	}
   212  
   213  	if o.source == nil {
   214  		return nil, errors.Errorf("missing build context")
   215  	}
   216  
   217  	root := o.source.Root()
   218  
   219  	if err := validateCopySourcePath(imageSource, origPath, root.OS()); err != nil {
   220  		return nil, err
   221  	}
   222  
   223  	// Work in source OS specific filepath semantics
   224  	// For LCOW, this is NOT the daemon OS.
   225  	origPath = root.FromSlash(origPath)
   226  	origPath = strings.TrimPrefix(origPath, string(root.Separator()))
   227  	origPath = strings.TrimPrefix(origPath, "."+string(root.Separator()))
   228  
   229  	// Deal with wildcards
   230  	if allowWildcards && containsWildcards(origPath, root.OS()) {
   231  		return o.copyWithWildcards(origPath)
   232  	}
   233  
   234  	if imageSource != nil && imageSource.ImageID() != "" {
   235  		// return a cached copy if one exists
   236  		if h, ok := o.pathCache.Load(imageSource.ImageID() + origPath); ok {
   237  			return newCopyInfos(newCopyInfoFromSource(o.source, origPath, h.(string))), nil
   238  		}
   239  	}
   240  
   241  	// Deal with the single file case
   242  	copyInfo, err := copyInfoForFile(o.source, origPath)
   243  	switch {
   244  	case err != nil:
   245  		return nil, err
   246  	case copyInfo.hash != "":
   247  		o.storeInPathCache(imageSource, origPath, copyInfo.hash)
   248  		return newCopyInfos(copyInfo), err
   249  	}
   250  
   251  	// TODO: remove, handle dirs in Hash()
   252  	subfiles, err := walkSource(o.source, origPath)
   253  	if err != nil {
   254  		return nil, err
   255  	}
   256  
   257  	hash := hashStringSlice("dir", subfiles)
   258  	o.storeInPathCache(imageSource, origPath, hash)
   259  	return newCopyInfos(newCopyInfoFromSource(o.source, origPath, hash)), nil
   260  }
   261  
   262  func containsWildcards(name, platform string) bool {
   263  	isWindows := platform == "windows"
   264  	for i := 0; i < len(name); i++ {
   265  		ch := name[i]
   266  		if ch == '\\' && !isWindows {
   267  			i++
   268  		} else if ch == '*' || ch == '?' || ch == '[' {
   269  			return true
   270  		}
   271  	}
   272  	return false
   273  }
   274  
   275  func (o *copier) storeInPathCache(im *imageMount, path string, hash string) {
   276  	if im != nil {
   277  		o.pathCache.Store(im.ImageID()+path, hash)
   278  	}
   279  }
   280  
   281  func (o *copier) copyWithWildcards(origPath string) ([]copyInfo, error) {
   282  	root := o.source.Root()
   283  	var copyInfos []copyInfo
   284  	if err := root.Walk(root.Path(), func(path string, info os.FileInfo, err error) error {
   285  		if err != nil {
   286  			return err
   287  		}
   288  		rel, err := remotecontext.Rel(root, path)
   289  		if err != nil {
   290  			return err
   291  		}
   292  
   293  		if rel == "." {
   294  			return nil
   295  		}
   296  		if match, _ := root.Match(origPath, rel); !match {
   297  			return nil
   298  		}
   299  
   300  		// Note we set allowWildcards to false in case the name has
   301  		// a * in it
   302  		subInfos, err := o.calcCopyInfo(rel, false)
   303  		if err != nil {
   304  			return err
   305  		}
   306  		copyInfos = append(copyInfos, subInfos...)
   307  		return nil
   308  	}); err != nil {
   309  		return nil, err
   310  	}
   311  	return copyInfos, nil
   312  }
   313  
   314  func copyInfoForFile(source builder.Source, path string) (copyInfo, error) {
   315  	fi, err := remotecontext.StatAt(source, path)
   316  	if err != nil {
   317  		return copyInfo{}, err
   318  	}
   319  
   320  	if fi.IsDir() {
   321  		return copyInfo{}, nil
   322  	}
   323  	hash, err := source.Hash(path)
   324  	if err != nil {
   325  		return copyInfo{}, err
   326  	}
   327  	return newCopyInfoFromSource(source, path, "file:"+hash), nil
   328  }
   329  
   330  // TODO: dedupe with copyWithWildcards()
   331  func walkSource(source builder.Source, origPath string) ([]string, error) {
   332  	fp, err := remotecontext.FullPath(source, origPath)
   333  	if err != nil {
   334  		return nil, err
   335  	}
   336  	// Must be a dir
   337  	var subfiles []string
   338  	err = source.Root().Walk(fp, func(path string, info os.FileInfo, err error) error {
   339  		if err != nil {
   340  			return err
   341  		}
   342  		rel, err := remotecontext.Rel(source.Root(), path)
   343  		if err != nil {
   344  			return err
   345  		}
   346  		if rel == "." {
   347  			return nil
   348  		}
   349  		hash, err := source.Hash(rel)
   350  		if err != nil {
   351  			return nil
   352  		}
   353  		// we already checked handleHash above
   354  		subfiles = append(subfiles, hash)
   355  		return nil
   356  	})
   357  	if err != nil {
   358  		return nil, err
   359  	}
   360  
   361  	sort.Strings(subfiles)
   362  	return subfiles, nil
   363  }
   364  
   365  type sourceDownloader func(string) (builder.Source, string, error)
   366  
   367  func newRemoteSourceDownloader(output, stdout io.Writer) sourceDownloader {
   368  	return func(url string) (builder.Source, string, error) {
   369  		return downloadSource(output, stdout, url)
   370  	}
   371  }
   372  
   373  func errOnSourceDownload(_ string) (builder.Source, string, error) {
   374  	return nil, "", errors.New("source can't be a URL for COPY")
   375  }
   376  
   377  func getFilenameForDownload(path string, resp *http.Response) string {
   378  	// Guess filename based on source
   379  	if path != "" && !strings.HasSuffix(path, "/") {
   380  		if filename := filepath.Base(filepath.FromSlash(path)); filename != "" {
   381  			return filename
   382  		}
   383  	}
   384  
   385  	// Guess filename based on Content-Disposition
   386  	if contentDisposition := resp.Header.Get("Content-Disposition"); contentDisposition != "" {
   387  		if _, params, err := mime.ParseMediaType(contentDisposition); err == nil {
   388  			if params["filename"] != "" && !strings.HasSuffix(params["filename"], "/") {
   389  				if filename := filepath.Base(filepath.FromSlash(params["filename"])); filename != "" {
   390  					return filename
   391  				}
   392  			}
   393  		}
   394  	}
   395  	return ""
   396  }
   397  
   398  func downloadSource(output io.Writer, stdout io.Writer, srcURL string) (remote builder.Source, p string, err error) {
   399  	u, err := url.Parse(srcURL)
   400  	if err != nil {
   401  		return
   402  	}
   403  
   404  	resp, err := remotecontext.GetWithStatusError(srcURL)
   405  	if err != nil {
   406  		return
   407  	}
   408  
   409  	filename := getFilenameForDownload(u.Path, resp)
   410  
   411  	// Prepare file in a tmp dir
   412  	tmpDir, err := ioutils.TempDir("", "docker-remote")
   413  	if err != nil {
   414  		return
   415  	}
   416  	defer func() {
   417  		if err != nil {
   418  			os.RemoveAll(tmpDir)
   419  		}
   420  	}()
   421  	// If filename is empty, the returned filename will be "" but
   422  	// the tmp filename will be created as "__unnamed__"
   423  	tmpFileName := filename
   424  	if filename == "" {
   425  		tmpFileName = unnamedFilename
   426  	}
   427  	tmpFileName = filepath.Join(tmpDir, tmpFileName)
   428  	tmpFile, err := os.OpenFile(tmpFileName, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
   429  	if err != nil {
   430  		return
   431  	}
   432  
   433  	progressOutput := streamformatter.NewJSONProgressOutput(output, true)
   434  	progressReader := progress.NewProgressReader(resp.Body, progressOutput, resp.ContentLength, "", "Downloading")
   435  	// Download and dump result to tmp file
   436  	// TODO: add filehash directly
   437  	if _, err = io.Copy(tmpFile, progressReader); err != nil {
   438  		tmpFile.Close()
   439  		return
   440  	}
   441  	// TODO: how important is this random blank line to the output?
   442  	fmt.Fprintln(stdout)
   443  
   444  	// Set the mtime to the Last-Modified header value if present
   445  	// Otherwise just remove atime and mtime
   446  	mTime := time.Time{}
   447  
   448  	lastMod := resp.Header.Get("Last-Modified")
   449  	if lastMod != "" {
   450  		// If we can't parse it then just let it default to 'zero'
   451  		// otherwise use the parsed time value
   452  		if parsedMTime, err := http.ParseTime(lastMod); err == nil {
   453  			mTime = parsedMTime
   454  		}
   455  	}
   456  
   457  	tmpFile.Close()
   458  
   459  	if err = system.Chtimes(tmpFileName, mTime, mTime); err != nil {
   460  		return
   461  	}
   462  
   463  	lc, err := remotecontext.NewLazySource(containerfs.NewLocalContainerFS(tmpDir))
   464  	return lc, filename, err
   465  }
   466  
   467  type copyFileOptions struct {
   468  	decompress bool
   469  	identity   idtools.Identity
   470  	archiver   Archiver
   471  }
   472  
   473  type copyEndpoint struct {
   474  	driver containerfs.Driver
   475  	path   string
   476  }
   477  
   478  func performCopyForInfo(dest copyInfo, source copyInfo, options copyFileOptions) error {
   479  	srcPath, err := source.fullPath()
   480  	if err != nil {
   481  		return err
   482  	}
   483  
   484  	destPath, err := dest.fullPath()
   485  	if err != nil {
   486  		return err
   487  	}
   488  
   489  	archiver := options.archiver
   490  
   491  	srcEndpoint := &copyEndpoint{driver: source.root, path: srcPath}
   492  	destEndpoint := &copyEndpoint{driver: dest.root, path: destPath}
   493  
   494  	src, err := source.root.Stat(srcPath)
   495  	if err != nil {
   496  		return errors.Wrapf(err, "source path not found")
   497  	}
   498  	if src.IsDir() {
   499  		return copyDirectory(archiver, srcEndpoint, destEndpoint, options.identity)
   500  	}
   501  	if options.decompress && isArchivePath(source.root, srcPath) && !source.noDecompress {
   502  		return archiver.UntarPath(srcPath, destPath)
   503  	}
   504  
   505  	destExistsAsDir, err := isExistingDirectory(destEndpoint)
   506  	if err != nil {
   507  		return err
   508  	}
   509  	// dest.path must be used because destPath has already been cleaned of any
   510  	// trailing slash
   511  	if endsInSlash(dest.root, dest.path) || destExistsAsDir {
   512  		// source.path must be used to get the correct filename when the source
   513  		// is a symlink
   514  		destPath = dest.root.Join(destPath, source.root.Base(source.path))
   515  		destEndpoint = &copyEndpoint{driver: dest.root, path: destPath}
   516  	}
   517  	return copyFile(archiver, srcEndpoint, destEndpoint, options.identity)
   518  }
   519  
   520  func isArchivePath(driver containerfs.ContainerFS, path string) bool {
   521  	file, err := driver.Open(path)
   522  	if err != nil {
   523  		return false
   524  	}
   525  	defer file.Close()
   526  	rdr, err := archive.DecompressStream(file)
   527  	if err != nil {
   528  		return false
   529  	}
   530  	r := tar.NewReader(rdr)
   531  	_, err = r.Next()
   532  	return err == nil
   533  }
   534  
   535  func copyDirectory(archiver Archiver, source, dest *copyEndpoint, identity idtools.Identity) error {
   536  	destExists, err := isExistingDirectory(dest)
   537  	if err != nil {
   538  		return errors.Wrapf(err, "failed to query destination path")
   539  	}
   540  
   541  	if err := archiver.CopyWithTar(source.path, dest.path); err != nil {
   542  		return errors.Wrapf(err, "failed to copy directory")
   543  	}
   544  	// TODO: @gupta-ak. Investigate how LCOW permission mappings will work.
   545  	return fixPermissions(source.path, dest.path, identity, !destExists)
   546  }
   547  
   548  func copyFile(archiver Archiver, source, dest *copyEndpoint, identity idtools.Identity) error {
   549  	if runtime.GOOS == "windows" && dest.driver.OS() == "linux" {
   550  		// LCOW
   551  		if err := dest.driver.MkdirAll(dest.driver.Dir(dest.path), 0755); err != nil {
   552  			return errors.Wrapf(err, "failed to create new directory")
   553  		}
   554  	} else {
   555  		if err := idtools.MkdirAllAndChownNew(filepath.Dir(dest.path), 0755, identity); err != nil {
   556  			// Normal containers
   557  			return errors.Wrapf(err, "failed to create new directory")
   558  		}
   559  	}
   560  
   561  	if err := archiver.CopyFileWithTar(source.path, dest.path); err != nil {
   562  		return errors.Wrapf(err, "failed to copy file")
   563  	}
   564  	// TODO: @gupta-ak. Investigate how LCOW permission mappings will work.
   565  	return fixPermissions(source.path, dest.path, identity, false)
   566  }
   567  
   568  func endsInSlash(driver containerfs.Driver, path string) bool {
   569  	return strings.HasSuffix(path, string(driver.Separator()))
   570  }
   571  
   572  // isExistingDirectory returns true if the path exists and is a directory
   573  func isExistingDirectory(point *copyEndpoint) (bool, error) {
   574  	destStat, err := point.driver.Stat(point.path)
   575  	switch {
   576  	case os.IsNotExist(err):
   577  		return false, nil
   578  	case err != nil:
   579  		return false, err
   580  	}
   581  	return destStat.IsDir(), nil
   582  }