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