github.com/endophage/docker@v1.4.2-0.20161027011718-242853499895/pkg/archive/archive.go (about) 1 package archive 2 3 import ( 4 "archive/tar" 5 "bufio" 6 "bytes" 7 "compress/bzip2" 8 "compress/gzip" 9 "errors" 10 "fmt" 11 "io" 12 "io/ioutil" 13 "os" 14 "os/exec" 15 "path/filepath" 16 "runtime" 17 "strings" 18 "syscall" 19 20 "github.com/Sirupsen/logrus" 21 "github.com/docker/docker/pkg/fileutils" 22 "github.com/docker/docker/pkg/idtools" 23 "github.com/docker/docker/pkg/ioutils" 24 "github.com/docker/docker/pkg/pools" 25 "github.com/docker/docker/pkg/promise" 26 "github.com/docker/docker/pkg/system" 27 ) 28 29 type ( 30 // Compression is the state represents if compressed or not. 31 Compression int 32 // WhiteoutFormat is the format of whiteouts unpacked 33 WhiteoutFormat int 34 // TarChownOptions wraps the chown options UID and GID. 35 TarChownOptions struct { 36 UID, GID int 37 } 38 39 // TarOptions wraps the tar options. 40 TarOptions struct { 41 IncludeFiles []string 42 ExcludePatterns []string 43 Compression Compression 44 NoLchown bool 45 UIDMaps []idtools.IDMap 46 GIDMaps []idtools.IDMap 47 ChownOpts *TarChownOptions 48 IncludeSourceDir bool 49 // WhiteoutFormat is the expected on disk format for whiteout files. 50 // This format will be converted to the standard format on pack 51 // and from the standard format on unpack. 52 WhiteoutFormat WhiteoutFormat 53 // When unpacking, specifies whether overwriting a directory with a 54 // non-directory is allowed and vice versa. 55 NoOverwriteDirNonDir bool 56 // For each include when creating an archive, the included name will be 57 // replaced with the matching name from this map. 58 RebaseNames map[string]string 59 InUserNS bool 60 } 61 62 // Archiver allows the reuse of most utility functions of this package 63 // with a pluggable Untar function. Also, to facilitate the passing of 64 // specific id mappings for untar, an archiver can be created with maps 65 // which will then be passed to Untar operations 66 Archiver struct { 67 Untar func(io.Reader, string, *TarOptions) error 68 UIDMaps []idtools.IDMap 69 GIDMaps []idtools.IDMap 70 } 71 72 // breakoutError is used to differentiate errors related to breaking out 73 // When testing archive breakout in the unit tests, this error is expected 74 // in order for the test to pass. 75 breakoutError error 76 ) 77 78 var ( 79 // ErrNotImplemented is the error message of function not implemented. 80 ErrNotImplemented = errors.New("Function not implemented") 81 defaultArchiver = &Archiver{Untar: Untar, UIDMaps: nil, GIDMaps: nil} 82 ) 83 84 const ( 85 // HeaderSize is the size in bytes of a tar header 86 HeaderSize = 512 87 ) 88 89 const ( 90 // Uncompressed represents the uncompressed. 91 Uncompressed Compression = iota 92 // Bzip2 is bzip2 compression algorithm. 93 Bzip2 94 // Gzip is gzip compression algorithm. 95 Gzip 96 // Xz is xz compression algorithm. 97 Xz 98 ) 99 100 const ( 101 // AUFSWhiteoutFormat is the default format for whiteouts 102 AUFSWhiteoutFormat WhiteoutFormat = iota 103 // OverlayWhiteoutFormat formats whiteout according to the overlay 104 // standard. 105 OverlayWhiteoutFormat 106 ) 107 108 // IsArchive checks for the magic bytes of a tar or any supported compression 109 // algorithm. 110 func IsArchive(header []byte) bool { 111 compression := DetectCompression(header) 112 if compression != Uncompressed { 113 return true 114 } 115 r := tar.NewReader(bytes.NewBuffer(header)) 116 _, err := r.Next() 117 return err == nil 118 } 119 120 // IsArchivePath checks if the (possibly compressed) file at the given path 121 // starts with a tar file header. 122 func IsArchivePath(path string) bool { 123 file, err := os.Open(path) 124 if err != nil { 125 return false 126 } 127 defer file.Close() 128 rdr, err := DecompressStream(file) 129 if err != nil { 130 return false 131 } 132 r := tar.NewReader(rdr) 133 _, err = r.Next() 134 return err == nil 135 } 136 137 // DetectCompression detects the compression algorithm of the source. 138 func DetectCompression(source []byte) Compression { 139 for compression, m := range map[Compression][]byte{ 140 Bzip2: {0x42, 0x5A, 0x68}, 141 Gzip: {0x1F, 0x8B, 0x08}, 142 Xz: {0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00}, 143 } { 144 if len(source) < len(m) { 145 logrus.Debug("Len too short") 146 continue 147 } 148 if bytes.Compare(m, source[:len(m)]) == 0 { 149 return compression 150 } 151 } 152 return Uncompressed 153 } 154 155 func xzDecompress(archive io.Reader) (io.ReadCloser, <-chan struct{}, error) { 156 args := []string{"xz", "-d", "-c", "-q"} 157 158 return cmdStream(exec.Command(args[0], args[1:]...), archive) 159 } 160 161 // DecompressStream decompresses the archive and returns a ReaderCloser with the decompressed archive. 162 func DecompressStream(archive io.Reader) (io.ReadCloser, error) { 163 p := pools.BufioReader32KPool 164 buf := p.Get(archive) 165 bs, err := buf.Peek(10) 166 if err != nil && err != io.EOF { 167 // Note: we'll ignore any io.EOF error because there are some odd 168 // cases where the layer.tar file will be empty (zero bytes) and 169 // that results in an io.EOF from the Peek() call. So, in those 170 // cases we'll just treat it as a non-compressed stream and 171 // that means just create an empty layer. 172 // See Issue 18170 173 return nil, err 174 } 175 176 compression := DetectCompression(bs) 177 switch compression { 178 case Uncompressed: 179 readBufWrapper := p.NewReadCloserWrapper(buf, buf) 180 return readBufWrapper, nil 181 case Gzip: 182 gzReader, err := gzip.NewReader(buf) 183 if err != nil { 184 return nil, err 185 } 186 readBufWrapper := p.NewReadCloserWrapper(buf, gzReader) 187 return readBufWrapper, nil 188 case Bzip2: 189 bz2Reader := bzip2.NewReader(buf) 190 readBufWrapper := p.NewReadCloserWrapper(buf, bz2Reader) 191 return readBufWrapper, nil 192 case Xz: 193 xzReader, chdone, err := xzDecompress(buf) 194 if err != nil { 195 return nil, err 196 } 197 readBufWrapper := p.NewReadCloserWrapper(buf, xzReader) 198 return ioutils.NewReadCloserWrapper(readBufWrapper, func() error { 199 <-chdone 200 return readBufWrapper.Close() 201 }), nil 202 default: 203 return nil, fmt.Errorf("Unsupported compression format %s", (&compression).Extension()) 204 } 205 } 206 207 // CompressStream compresseses the dest with specified compression algorithm. 208 func CompressStream(dest io.Writer, compression Compression) (io.WriteCloser, error) { 209 p := pools.BufioWriter32KPool 210 buf := p.Get(dest) 211 switch compression { 212 case Uncompressed: 213 writeBufWrapper := p.NewWriteCloserWrapper(buf, buf) 214 return writeBufWrapper, nil 215 case Gzip: 216 gzWriter := gzip.NewWriter(dest) 217 writeBufWrapper := p.NewWriteCloserWrapper(buf, gzWriter) 218 return writeBufWrapper, nil 219 case Bzip2, Xz: 220 // archive/bzip2 does not support writing, and there is no xz support at all 221 // However, this is not a problem as docker only currently generates gzipped tars 222 return nil, fmt.Errorf("Unsupported compression format %s", (&compression).Extension()) 223 default: 224 return nil, fmt.Errorf("Unsupported compression format %s", (&compression).Extension()) 225 } 226 } 227 228 // Extension returns the extension of a file that uses the specified compression algorithm. 229 func (compression *Compression) Extension() string { 230 switch *compression { 231 case Uncompressed: 232 return "tar" 233 case Bzip2: 234 return "tar.bz2" 235 case Gzip: 236 return "tar.gz" 237 case Xz: 238 return "tar.xz" 239 } 240 return "" 241 } 242 243 type tarWhiteoutConverter interface { 244 ConvertWrite(*tar.Header, string, os.FileInfo) error 245 ConvertRead(*tar.Header, string) (bool, error) 246 } 247 248 type tarAppender struct { 249 TarWriter *tar.Writer 250 Buffer *bufio.Writer 251 252 // for hardlink mapping 253 SeenFiles map[uint64]string 254 UIDMaps []idtools.IDMap 255 GIDMaps []idtools.IDMap 256 257 // For packing and unpacking whiteout files in the 258 // non standard format. The whiteout files defined 259 // by the AUFS standard are used as the tar whiteout 260 // standard. 261 WhiteoutConverter tarWhiteoutConverter 262 } 263 264 // canonicalTarName provides a platform-independent and consistent posix-style 265 //path for files and directories to be archived regardless of the platform. 266 func canonicalTarName(name string, isDir bool) (string, error) { 267 name, err := CanonicalTarNameForPath(name) 268 if err != nil { 269 return "", err 270 } 271 272 // suffix with '/' for directories 273 if isDir && !strings.HasSuffix(name, "/") { 274 name += "/" 275 } 276 return name, nil 277 } 278 279 // addTarFile adds to the tar archive a file from `path` as `name` 280 func (ta *tarAppender) addTarFile(path, name string) error { 281 fi, err := os.Lstat(path) 282 if err != nil { 283 return err 284 } 285 286 link := "" 287 if fi.Mode()&os.ModeSymlink != 0 { 288 if link, err = os.Readlink(path); err != nil { 289 return err 290 } 291 } 292 293 hdr, err := tar.FileInfoHeader(fi, link) 294 if err != nil { 295 return err 296 } 297 hdr.Mode = int64(chmodTarEntry(os.FileMode(hdr.Mode))) 298 299 name, err = canonicalTarName(name, fi.IsDir()) 300 if err != nil { 301 return fmt.Errorf("tar: cannot canonicalize path: %v", err) 302 } 303 hdr.Name = name 304 305 inode, err := setHeaderForSpecialDevice(hdr, ta, name, fi.Sys()) 306 if err != nil { 307 return err 308 } 309 310 // if it's not a directory and has more than 1 link, 311 // it's hardlinked, so set the type flag accordingly 312 if !fi.IsDir() && hasHardlinks(fi) { 313 // a link should have a name that it links too 314 // and that linked name should be first in the tar archive 315 if oldpath, ok := ta.SeenFiles[inode]; ok { 316 hdr.Typeflag = tar.TypeLink 317 hdr.Linkname = oldpath 318 hdr.Size = 0 // This Must be here for the writer math to add up! 319 } else { 320 ta.SeenFiles[inode] = name 321 } 322 } 323 324 capability, _ := system.Lgetxattr(path, "security.capability") 325 if capability != nil { 326 hdr.Xattrs = make(map[string]string) 327 hdr.Xattrs["security.capability"] = string(capability) 328 } 329 330 //handle re-mapping container ID mappings back to host ID mappings before 331 //writing tar headers/files. We skip whiteout files because they were written 332 //by the kernel and already have proper ownership relative to the host 333 if !strings.HasPrefix(filepath.Base(hdr.Name), WhiteoutPrefix) && (ta.UIDMaps != nil || ta.GIDMaps != nil) { 334 uid, gid, err := getFileUIDGID(fi.Sys()) 335 if err != nil { 336 return err 337 } 338 xUID, err := idtools.ToContainer(uid, ta.UIDMaps) 339 if err != nil { 340 return err 341 } 342 xGID, err := idtools.ToContainer(gid, ta.GIDMaps) 343 if err != nil { 344 return err 345 } 346 hdr.Uid = xUID 347 hdr.Gid = xGID 348 } 349 350 if ta.WhiteoutConverter != nil { 351 if err := ta.WhiteoutConverter.ConvertWrite(hdr, path, fi); err != nil { 352 return err 353 } 354 } 355 356 if err := ta.TarWriter.WriteHeader(hdr); err != nil { 357 return err 358 } 359 360 if hdr.Typeflag == tar.TypeReg && hdr.Size > 0 { 361 file, err := os.Open(path) 362 if err != nil { 363 return err 364 } 365 366 ta.Buffer.Reset(ta.TarWriter) 367 defer ta.Buffer.Reset(nil) 368 _, err = io.Copy(ta.Buffer, file) 369 file.Close() 370 if err != nil { 371 return err 372 } 373 err = ta.Buffer.Flush() 374 if err != nil { 375 return err 376 } 377 } 378 379 return nil 380 } 381 382 func createTarFile(path, extractDir string, hdr *tar.Header, reader io.Reader, Lchown bool, chownOpts *TarChownOptions, inUserns bool) error { 383 // hdr.Mode is in linux format, which we can use for sycalls, 384 // but for os.Foo() calls we need the mode converted to os.FileMode, 385 // so use hdrInfo.Mode() (they differ for e.g. setuid bits) 386 hdrInfo := hdr.FileInfo() 387 388 switch hdr.Typeflag { 389 case tar.TypeDir: 390 // Create directory unless it exists as a directory already. 391 // In that case we just want to merge the two 392 if fi, err := os.Lstat(path); !(err == nil && fi.IsDir()) { 393 if err := os.Mkdir(path, hdrInfo.Mode()); err != nil { 394 return err 395 } 396 } 397 398 case tar.TypeReg, tar.TypeRegA: 399 // Source is regular file 400 file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, hdrInfo.Mode()) 401 if err != nil { 402 return err 403 } 404 if _, err := io.Copy(file, reader); err != nil { 405 file.Close() 406 return err 407 } 408 file.Close() 409 410 case tar.TypeBlock, tar.TypeChar: 411 if inUserns { // cannot create devices in a userns 412 return nil 413 } 414 // Handle this is an OS-specific way 415 if err := handleTarTypeBlockCharFifo(hdr, path); err != nil { 416 return err 417 } 418 419 case tar.TypeFifo: 420 // Handle this is an OS-specific way 421 if err := handleTarTypeBlockCharFifo(hdr, path); err != nil { 422 return err 423 } 424 425 case tar.TypeLink: 426 targetPath := filepath.Join(extractDir, hdr.Linkname) 427 // check for hardlink breakout 428 if !strings.HasPrefix(targetPath, extractDir) { 429 return breakoutError(fmt.Errorf("invalid hardlink %q -> %q", targetPath, hdr.Linkname)) 430 } 431 if err := os.Link(targetPath, path); err != nil { 432 return err 433 } 434 435 case tar.TypeSymlink: 436 // path -> hdr.Linkname = targetPath 437 // e.g. /extractDir/path/to/symlink -> ../2/file = /extractDir/path/2/file 438 targetPath := filepath.Join(filepath.Dir(path), hdr.Linkname) 439 440 // the reason we don't need to check symlinks in the path (with FollowSymlinkInScope) is because 441 // that symlink would first have to be created, which would be caught earlier, at this very check: 442 if !strings.HasPrefix(targetPath, extractDir) { 443 return breakoutError(fmt.Errorf("invalid symlink %q -> %q", path, hdr.Linkname)) 444 } 445 if err := os.Symlink(hdr.Linkname, path); err != nil { 446 return err 447 } 448 449 case tar.TypeXGlobalHeader: 450 logrus.Debug("PAX Global Extended Headers found and ignored") 451 return nil 452 453 default: 454 return fmt.Errorf("Unhandled tar header type %d\n", hdr.Typeflag) 455 } 456 457 // Lchown is not supported on Windows. 458 if Lchown && runtime.GOOS != "windows" { 459 if chownOpts == nil { 460 chownOpts = &TarChownOptions{UID: hdr.Uid, GID: hdr.Gid} 461 } 462 if err := os.Lchown(path, chownOpts.UID, chownOpts.GID); err != nil { 463 return err 464 } 465 } 466 467 var errors []string 468 for key, value := range hdr.Xattrs { 469 if err := system.Lsetxattr(path, key, []byte(value), 0); err != nil { 470 if err == syscall.ENOTSUP { 471 // We ignore errors here because not all graphdrivers support 472 // xattrs *cough* old versions of AUFS *cough*. However only 473 // ENOTSUP should be emitted in that case, otherwise we still 474 // bail. 475 errors = append(errors, err.Error()) 476 continue 477 } 478 return err 479 } 480 481 } 482 483 if len(errors) > 0 { 484 logrus.WithFields(logrus.Fields{ 485 "errors": errors, 486 }).Warn("ignored xattrs in archive: underlying filesystem doesn't support them") 487 } 488 489 // There is no LChmod, so ignore mode for symlink. Also, this 490 // must happen after chown, as that can modify the file mode 491 if err := handleLChmod(hdr, path, hdrInfo); err != nil { 492 return err 493 } 494 495 aTime := hdr.AccessTime 496 if aTime.Before(hdr.ModTime) { 497 // Last access time should never be before last modified time. 498 aTime = hdr.ModTime 499 } 500 501 // system.Chtimes doesn't support a NOFOLLOW flag atm 502 if hdr.Typeflag == tar.TypeLink { 503 if fi, err := os.Lstat(hdr.Linkname); err == nil && (fi.Mode()&os.ModeSymlink == 0) { 504 if err := system.Chtimes(path, aTime, hdr.ModTime); err != nil { 505 return err 506 } 507 } 508 } else if hdr.Typeflag != tar.TypeSymlink { 509 if err := system.Chtimes(path, aTime, hdr.ModTime); err != nil { 510 return err 511 } 512 } else { 513 ts := []syscall.Timespec{timeToTimespec(aTime), timeToTimespec(hdr.ModTime)} 514 if err := system.LUtimesNano(path, ts); err != nil && err != system.ErrNotSupportedPlatform { 515 return err 516 } 517 } 518 return nil 519 } 520 521 // Tar creates an archive from the directory at `path`, and returns it as a 522 // stream of bytes. 523 func Tar(path string, compression Compression) (io.ReadCloser, error) { 524 return TarWithOptions(path, &TarOptions{Compression: compression}) 525 } 526 527 // TarWithOptions creates an archive from the directory at `path`, only including files whose relative 528 // paths are included in `options.IncludeFiles` (if non-nil) or not in `options.ExcludePatterns`. 529 func TarWithOptions(srcPath string, options *TarOptions) (io.ReadCloser, error) { 530 531 // Fix the source path to work with long path names. This is a no-op 532 // on platforms other than Windows. 533 srcPath = fixVolumePathPrefix(srcPath) 534 535 patterns, patDirs, exceptions, err := fileutils.CleanPatterns(options.ExcludePatterns) 536 537 if err != nil { 538 return nil, err 539 } 540 541 pipeReader, pipeWriter := io.Pipe() 542 543 compressWriter, err := CompressStream(pipeWriter, options.Compression) 544 if err != nil { 545 return nil, err 546 } 547 548 go func() { 549 ta := &tarAppender{ 550 TarWriter: tar.NewWriter(compressWriter), 551 Buffer: pools.BufioWriter32KPool.Get(nil), 552 SeenFiles: make(map[uint64]string), 553 UIDMaps: options.UIDMaps, 554 GIDMaps: options.GIDMaps, 555 WhiteoutConverter: getWhiteoutConverter(options.WhiteoutFormat), 556 } 557 558 defer func() { 559 // Make sure to check the error on Close. 560 if err := ta.TarWriter.Close(); err != nil { 561 logrus.Errorf("Can't close tar writer: %s", err) 562 } 563 if err := compressWriter.Close(); err != nil { 564 logrus.Errorf("Can't close compress writer: %s", err) 565 } 566 if err := pipeWriter.Close(); err != nil { 567 logrus.Errorf("Can't close pipe writer: %s", err) 568 } 569 }() 570 571 // this buffer is needed for the duration of this piped stream 572 defer pools.BufioWriter32KPool.Put(ta.Buffer) 573 574 // In general we log errors here but ignore them because 575 // during e.g. a diff operation the container can continue 576 // mutating the filesystem and we can see transient errors 577 // from this 578 579 stat, err := os.Lstat(srcPath) 580 if err != nil { 581 return 582 } 583 584 if !stat.IsDir() { 585 // We can't later join a non-dir with any includes because the 586 // 'walk' will error if "file/." is stat-ed and "file" is not a 587 // directory. So, we must split the source path and use the 588 // basename as the include. 589 if len(options.IncludeFiles) > 0 { 590 logrus.Warn("Tar: Can't archive a file with includes") 591 } 592 593 dir, base := SplitPathDirEntry(srcPath) 594 srcPath = dir 595 options.IncludeFiles = []string{base} 596 } 597 598 if len(options.IncludeFiles) == 0 { 599 options.IncludeFiles = []string{"."} 600 } 601 602 seen := make(map[string]bool) 603 604 for _, include := range options.IncludeFiles { 605 rebaseName := options.RebaseNames[include] 606 607 walkRoot := getWalkRoot(srcPath, include) 608 filepath.Walk(walkRoot, func(filePath string, f os.FileInfo, err error) error { 609 if err != nil { 610 logrus.Errorf("Tar: Can't stat file %s to tar: %s", srcPath, err) 611 return nil 612 } 613 614 relFilePath, err := filepath.Rel(srcPath, filePath) 615 if err != nil || (!options.IncludeSourceDir && relFilePath == "." && f.IsDir()) { 616 // Error getting relative path OR we are looking 617 // at the source directory path. Skip in both situations. 618 return nil 619 } 620 621 if options.IncludeSourceDir && include == "." && relFilePath != "." { 622 relFilePath = strings.Join([]string{".", relFilePath}, string(filepath.Separator)) 623 } 624 625 skip := false 626 627 // If "include" is an exact match for the current file 628 // then even if there's an "excludePatterns" pattern that 629 // matches it, don't skip it. IOW, assume an explicit 'include' 630 // is asking for that file no matter what - which is true 631 // for some files, like .dockerignore and Dockerfile (sometimes) 632 if include != relFilePath { 633 skip, err = fileutils.OptimizedMatches(relFilePath, patterns, patDirs) 634 if err != nil { 635 logrus.Errorf("Error matching %s: %v", relFilePath, err) 636 return err 637 } 638 } 639 640 if skip { 641 // If we want to skip this file and its a directory 642 // then we should first check to see if there's an 643 // excludes pattern (eg !dir/file) that starts with this 644 // dir. If so then we can't skip this dir. 645 646 // Its not a dir then so we can just return/skip. 647 if !f.IsDir() { 648 return nil 649 } 650 651 // No exceptions (!...) in patterns so just skip dir 652 if !exceptions { 653 return filepath.SkipDir 654 } 655 656 dirSlash := relFilePath + string(filepath.Separator) 657 658 for _, pat := range patterns { 659 if pat[0] != '!' { 660 continue 661 } 662 pat = pat[1:] + string(filepath.Separator) 663 if strings.HasPrefix(pat, dirSlash) { 664 // found a match - so can't skip this dir 665 return nil 666 } 667 } 668 669 // No matching exclusion dir so just skip dir 670 return filepath.SkipDir 671 } 672 673 if seen[relFilePath] { 674 return nil 675 } 676 seen[relFilePath] = true 677 678 // Rename the base resource. 679 if rebaseName != "" { 680 var replacement string 681 if rebaseName != string(filepath.Separator) { 682 // Special case the root directory to replace with an 683 // empty string instead so that we don't end up with 684 // double slashes in the paths. 685 replacement = rebaseName 686 } 687 688 relFilePath = strings.Replace(relFilePath, include, replacement, 1) 689 } 690 691 if err := ta.addTarFile(filePath, relFilePath); err != nil { 692 logrus.Errorf("Can't add file %s to tar: %s", filePath, err) 693 // if pipe is broken, stop writing tar stream to it 694 if err == io.ErrClosedPipe { 695 return err 696 } 697 } 698 return nil 699 }) 700 } 701 }() 702 703 return pipeReader, nil 704 } 705 706 // Unpack unpacks the decompressedArchive to dest with options. 707 func Unpack(decompressedArchive io.Reader, dest string, options *TarOptions) error { 708 tr := tar.NewReader(decompressedArchive) 709 trBuf := pools.BufioReader32KPool.Get(nil) 710 defer pools.BufioReader32KPool.Put(trBuf) 711 712 var dirs []*tar.Header 713 remappedRootUID, remappedRootGID, err := idtools.GetRootUIDGID(options.UIDMaps, options.GIDMaps) 714 if err != nil { 715 return err 716 } 717 whiteoutConverter := getWhiteoutConverter(options.WhiteoutFormat) 718 719 // Iterate through the files in the archive. 720 loop: 721 for { 722 hdr, err := tr.Next() 723 if err == io.EOF { 724 // end of tar archive 725 break 726 } 727 if err != nil { 728 return err 729 } 730 731 // Normalize name, for safety and for a simple is-root check 732 // This keeps "../" as-is, but normalizes "/../" to "/". Or Windows: 733 // This keeps "..\" as-is, but normalizes "\..\" to "\". 734 hdr.Name = filepath.Clean(hdr.Name) 735 736 for _, exclude := range options.ExcludePatterns { 737 if strings.HasPrefix(hdr.Name, exclude) { 738 continue loop 739 } 740 } 741 742 // After calling filepath.Clean(hdr.Name) above, hdr.Name will now be in 743 // the filepath format for the OS on which the daemon is running. Hence 744 // the check for a slash-suffix MUST be done in an OS-agnostic way. 745 if !strings.HasSuffix(hdr.Name, string(os.PathSeparator)) { 746 // Not the root directory, ensure that the parent directory exists 747 parent := filepath.Dir(hdr.Name) 748 parentPath := filepath.Join(dest, parent) 749 if _, err := os.Lstat(parentPath); err != nil && os.IsNotExist(err) { 750 err = idtools.MkdirAllNewAs(parentPath, 0777, remappedRootUID, remappedRootGID) 751 if err != nil { 752 return err 753 } 754 } 755 } 756 757 path := filepath.Join(dest, hdr.Name) 758 rel, err := filepath.Rel(dest, path) 759 if err != nil { 760 return err 761 } 762 if strings.HasPrefix(rel, ".."+string(os.PathSeparator)) { 763 return breakoutError(fmt.Errorf("%q is outside of %q", hdr.Name, dest)) 764 } 765 766 // If path exits we almost always just want to remove and replace it 767 // The only exception is when it is a directory *and* the file from 768 // the layer is also a directory. Then we want to merge them (i.e. 769 // just apply the metadata from the layer). 770 if fi, err := os.Lstat(path); err == nil { 771 if options.NoOverwriteDirNonDir && fi.IsDir() && hdr.Typeflag != tar.TypeDir { 772 // If NoOverwriteDirNonDir is true then we cannot replace 773 // an existing directory with a non-directory from the archive. 774 return fmt.Errorf("cannot overwrite directory %q with non-directory %q", path, dest) 775 } 776 777 if options.NoOverwriteDirNonDir && !fi.IsDir() && hdr.Typeflag == tar.TypeDir { 778 // If NoOverwriteDirNonDir is true then we cannot replace 779 // an existing non-directory with a directory from the archive. 780 return fmt.Errorf("cannot overwrite non-directory %q with directory %q", path, dest) 781 } 782 783 if fi.IsDir() && hdr.Name == "." { 784 continue 785 } 786 787 if !(fi.IsDir() && hdr.Typeflag == tar.TypeDir) { 788 if err := os.RemoveAll(path); err != nil { 789 return err 790 } 791 } 792 } 793 trBuf.Reset(tr) 794 795 // if the options contain a uid & gid maps, convert header uid/gid 796 // entries using the maps such that lchown sets the proper mapped 797 // uid/gid after writing the file. We only perform this mapping if 798 // the file isn't already owned by the remapped root UID or GID, as 799 // that specific uid/gid has no mapping from container -> host, and 800 // those files already have the proper ownership for inside the 801 // container. 802 if hdr.Uid != remappedRootUID { 803 xUID, err := idtools.ToHost(hdr.Uid, options.UIDMaps) 804 if err != nil { 805 return err 806 } 807 hdr.Uid = xUID 808 } 809 if hdr.Gid != remappedRootGID { 810 xGID, err := idtools.ToHost(hdr.Gid, options.GIDMaps) 811 if err != nil { 812 return err 813 } 814 hdr.Gid = xGID 815 } 816 817 if whiteoutConverter != nil { 818 writeFile, err := whiteoutConverter.ConvertRead(hdr, path) 819 if err != nil { 820 return err 821 } 822 if !writeFile { 823 continue 824 } 825 } 826 827 if err := createTarFile(path, dest, hdr, trBuf, !options.NoLchown, options.ChownOpts, options.InUserNS); err != nil { 828 return err 829 } 830 831 // Directory mtimes must be handled at the end to avoid further 832 // file creation in them to modify the directory mtime 833 if hdr.Typeflag == tar.TypeDir { 834 dirs = append(dirs, hdr) 835 } 836 } 837 838 for _, hdr := range dirs { 839 path := filepath.Join(dest, hdr.Name) 840 841 if err := system.Chtimes(path, hdr.AccessTime, hdr.ModTime); err != nil { 842 return err 843 } 844 } 845 return nil 846 } 847 848 // Untar reads a stream of bytes from `archive`, parses it as a tar archive, 849 // and unpacks it into the directory at `dest`. 850 // The archive may be compressed with one of the following algorithms: 851 // identity (uncompressed), gzip, bzip2, xz. 852 // FIXME: specify behavior when target path exists vs. doesn't exist. 853 func Untar(tarArchive io.Reader, dest string, options *TarOptions) error { 854 return untarHandler(tarArchive, dest, options, true) 855 } 856 857 // UntarUncompressed reads a stream of bytes from `archive`, parses it as a tar archive, 858 // and unpacks it into the directory at `dest`. 859 // The archive must be an uncompressed stream. 860 func UntarUncompressed(tarArchive io.Reader, dest string, options *TarOptions) error { 861 return untarHandler(tarArchive, dest, options, false) 862 } 863 864 // Handler for teasing out the automatic decompression 865 func untarHandler(tarArchive io.Reader, dest string, options *TarOptions, decompress bool) error { 866 if tarArchive == nil { 867 return fmt.Errorf("Empty archive") 868 } 869 dest = filepath.Clean(dest) 870 if options == nil { 871 options = &TarOptions{} 872 } 873 if options.ExcludePatterns == nil { 874 options.ExcludePatterns = []string{} 875 } 876 877 r := tarArchive 878 if decompress { 879 decompressedArchive, err := DecompressStream(tarArchive) 880 if err != nil { 881 return err 882 } 883 defer decompressedArchive.Close() 884 r = decompressedArchive 885 } 886 887 return Unpack(r, dest, options) 888 } 889 890 // TarUntar is a convenience function which calls Tar and Untar, with the output of one piped into the other. 891 // If either Tar or Untar fails, TarUntar aborts and returns the error. 892 func (archiver *Archiver) TarUntar(src, dst string) error { 893 logrus.Debugf("TarUntar(%s %s)", src, dst) 894 archive, err := TarWithOptions(src, &TarOptions{Compression: Uncompressed}) 895 if err != nil { 896 return err 897 } 898 defer archive.Close() 899 900 var options *TarOptions 901 if archiver.UIDMaps != nil || archiver.GIDMaps != nil { 902 options = &TarOptions{ 903 UIDMaps: archiver.UIDMaps, 904 GIDMaps: archiver.GIDMaps, 905 } 906 } 907 return archiver.Untar(archive, dst, options) 908 } 909 910 // TarUntar is a convenience function which calls Tar and Untar, with the output of one piped into the other. 911 // If either Tar or Untar fails, TarUntar aborts and returns the error. 912 func TarUntar(src, dst string) error { 913 return defaultArchiver.TarUntar(src, dst) 914 } 915 916 // UntarPath untar a file from path to a destination, src is the source tar file path. 917 func (archiver *Archiver) UntarPath(src, dst string) error { 918 archive, err := os.Open(src) 919 if err != nil { 920 return err 921 } 922 defer archive.Close() 923 var options *TarOptions 924 if archiver.UIDMaps != nil || archiver.GIDMaps != nil { 925 options = &TarOptions{ 926 UIDMaps: archiver.UIDMaps, 927 GIDMaps: archiver.GIDMaps, 928 } 929 } 930 return archiver.Untar(archive, dst, options) 931 } 932 933 // UntarPath is a convenience function which looks for an archive 934 // at filesystem path `src`, and unpacks it at `dst`. 935 func UntarPath(src, dst string) error { 936 return defaultArchiver.UntarPath(src, dst) 937 } 938 939 // CopyWithTar creates a tar archive of filesystem path `src`, and 940 // unpacks it at filesystem path `dst`. 941 // The archive is streamed directly with fixed buffering and no 942 // intermediary disk IO. 943 func (archiver *Archiver) CopyWithTar(src, dst string) error { 944 srcSt, err := os.Stat(src) 945 if err != nil { 946 return err 947 } 948 if !srcSt.IsDir() { 949 return archiver.CopyFileWithTar(src, dst) 950 } 951 952 // if this archiver is set up with ID mapping we need to create 953 // the new destination directory with the remapped root UID/GID pair 954 // as owner 955 rootUID, rootGID, err := idtools.GetRootUIDGID(archiver.UIDMaps, archiver.GIDMaps) 956 if err != nil { 957 return err 958 } 959 // Create dst, copy src's content into it 960 logrus.Debugf("Creating dest directory: %s", dst) 961 if err := idtools.MkdirAllNewAs(dst, 0755, rootUID, rootGID); err != nil { 962 return err 963 } 964 logrus.Debugf("Calling TarUntar(%s, %s)", src, dst) 965 return archiver.TarUntar(src, dst) 966 } 967 968 // CopyWithTar creates a tar archive of filesystem path `src`, and 969 // unpacks it at filesystem path `dst`. 970 // The archive is streamed directly with fixed buffering and no 971 // intermediary disk IO. 972 func CopyWithTar(src, dst string) error { 973 return defaultArchiver.CopyWithTar(src, dst) 974 } 975 976 // CopyFileWithTar emulates the behavior of the 'cp' command-line 977 // for a single file. It copies a regular file from path `src` to 978 // path `dst`, and preserves all its metadata. 979 func (archiver *Archiver) CopyFileWithTar(src, dst string) (err error) { 980 logrus.Debugf("CopyFileWithTar(%s, %s)", src, dst) 981 srcSt, err := os.Stat(src) 982 if err != nil { 983 return err 984 } 985 986 if srcSt.IsDir() { 987 return fmt.Errorf("Can't copy a directory") 988 } 989 990 // Clean up the trailing slash. This must be done in an operating 991 // system specific manner. 992 if dst[len(dst)-1] == os.PathSeparator { 993 dst = filepath.Join(dst, filepath.Base(src)) 994 } 995 // Create the holding directory if necessary 996 if err := system.MkdirAll(filepath.Dir(dst), 0700); err != nil { 997 return err 998 } 999 1000 r, w := io.Pipe() 1001 errC := promise.Go(func() error { 1002 defer w.Close() 1003 1004 srcF, err := os.Open(src) 1005 if err != nil { 1006 return err 1007 } 1008 defer srcF.Close() 1009 1010 hdr, err := tar.FileInfoHeader(srcSt, "") 1011 if err != nil { 1012 return err 1013 } 1014 hdr.Name = filepath.Base(dst) 1015 hdr.Mode = int64(chmodTarEntry(os.FileMode(hdr.Mode))) 1016 1017 remappedRootUID, remappedRootGID, err := idtools.GetRootUIDGID(archiver.UIDMaps, archiver.GIDMaps) 1018 if err != nil { 1019 return err 1020 } 1021 1022 // only perform mapping if the file being copied isn't already owned by the 1023 // uid or gid of the remapped root in the container 1024 if remappedRootUID != hdr.Uid { 1025 xUID, err := idtools.ToHost(hdr.Uid, archiver.UIDMaps) 1026 if err != nil { 1027 return err 1028 } 1029 hdr.Uid = xUID 1030 } 1031 if remappedRootGID != hdr.Gid { 1032 xGID, err := idtools.ToHost(hdr.Gid, archiver.GIDMaps) 1033 if err != nil { 1034 return err 1035 } 1036 hdr.Gid = xGID 1037 } 1038 1039 tw := tar.NewWriter(w) 1040 defer tw.Close() 1041 if err := tw.WriteHeader(hdr); err != nil { 1042 return err 1043 } 1044 if _, err := io.Copy(tw, srcF); err != nil { 1045 return err 1046 } 1047 return nil 1048 }) 1049 defer func() { 1050 if er := <-errC; err == nil && er != nil { 1051 err = er 1052 } 1053 }() 1054 1055 err = archiver.Untar(r, filepath.Dir(dst), nil) 1056 if err != nil { 1057 r.CloseWithError(err) 1058 } 1059 return err 1060 } 1061 1062 // CopyFileWithTar emulates the behavior of the 'cp' command-line 1063 // for a single file. It copies a regular file from path `src` to 1064 // path `dst`, and preserves all its metadata. 1065 // 1066 // Destination handling is in an operating specific manner depending 1067 // where the daemon is running. If `dst` ends with a trailing slash 1068 // the final destination path will be `dst/base(src)` (Linux) or 1069 // `dst\base(src)` (Windows). 1070 func CopyFileWithTar(src, dst string) (err error) { 1071 return defaultArchiver.CopyFileWithTar(src, dst) 1072 } 1073 1074 // cmdStream executes a command, and returns its stdout as a stream. 1075 // If the command fails to run or doesn't complete successfully, an error 1076 // will be returned, including anything written on stderr. 1077 func cmdStream(cmd *exec.Cmd, input io.Reader) (io.ReadCloser, <-chan struct{}, error) { 1078 chdone := make(chan struct{}) 1079 cmd.Stdin = input 1080 pipeR, pipeW := io.Pipe() 1081 cmd.Stdout = pipeW 1082 var errBuf bytes.Buffer 1083 cmd.Stderr = &errBuf 1084 1085 // Run the command and return the pipe 1086 if err := cmd.Start(); err != nil { 1087 return nil, nil, err 1088 } 1089 1090 // Copy stdout to the returned pipe 1091 go func() { 1092 if err := cmd.Wait(); err != nil { 1093 pipeW.CloseWithError(fmt.Errorf("%s: %s", err, errBuf.String())) 1094 } else { 1095 pipeW.Close() 1096 } 1097 close(chdone) 1098 }() 1099 1100 return pipeR, chdone, nil 1101 } 1102 1103 // NewTempArchive reads the content of src into a temporary file, and returns the contents 1104 // of that file as an archive. The archive can only be read once - as soon as reading completes, 1105 // the file will be deleted. 1106 func NewTempArchive(src io.Reader, dir string) (*TempArchive, error) { 1107 f, err := ioutil.TempFile(dir, "") 1108 if err != nil { 1109 return nil, err 1110 } 1111 if _, err := io.Copy(f, src); err != nil { 1112 return nil, err 1113 } 1114 if _, err := f.Seek(0, 0); err != nil { 1115 return nil, err 1116 } 1117 st, err := f.Stat() 1118 if err != nil { 1119 return nil, err 1120 } 1121 size := st.Size() 1122 return &TempArchive{File: f, Size: size}, nil 1123 } 1124 1125 // TempArchive is a temporary archive. The archive can only be read once - as soon as reading completes, 1126 // the file will be deleted. 1127 type TempArchive struct { 1128 *os.File 1129 Size int64 // Pre-computed from Stat().Size() as a convenience 1130 read int64 1131 closed bool 1132 } 1133 1134 // Close closes the underlying file if it's still open, or does a no-op 1135 // to allow callers to try to close the TempArchive multiple times safely. 1136 func (archive *TempArchive) Close() error { 1137 if archive.closed { 1138 return nil 1139 } 1140 1141 archive.closed = true 1142 1143 return archive.File.Close() 1144 } 1145 1146 func (archive *TempArchive) Read(data []byte) (int, error) { 1147 n, err := archive.File.Read(data) 1148 archive.read += int64(n) 1149 if err != nil || archive.read == archive.Size { 1150 archive.Close() 1151 os.Remove(archive.File.Name()) 1152 } 1153 return n, err 1154 }