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