github.com/xuyutom/docker@v1.6.0/pkg/archive/archive.go (about) 1 package archive 2 3 import ( 4 "bufio" 5 "bytes" 6 "compress/bzip2" 7 "compress/gzip" 8 "errors" 9 "fmt" 10 "io" 11 "io/ioutil" 12 "os" 13 "os/exec" 14 "path" 15 "path/filepath" 16 "strings" 17 "syscall" 18 19 "github.com/docker/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar" 20 21 log "github.com/Sirupsen/logrus" 22 "github.com/docker/docker/pkg/fileutils" 23 "github.com/docker/docker/pkg/pools" 24 "github.com/docker/docker/pkg/promise" 25 "github.com/docker/docker/pkg/system" 26 ) 27 28 type ( 29 Archive io.ReadCloser 30 ArchiveReader io.Reader 31 Compression int 32 TarOptions struct { 33 IncludeFiles []string 34 ExcludePatterns []string 35 Compression Compression 36 NoLchown bool 37 Name string 38 } 39 40 // Archiver allows the reuse of most utility functions of this package 41 // with a pluggable Untar function. 42 Archiver struct { 43 Untar func(io.Reader, string, *TarOptions) error 44 } 45 46 // breakoutError is used to differentiate errors related to breaking out 47 // When testing archive breakout in the unit tests, this error is expected 48 // in order for the test to pass. 49 breakoutError error 50 ) 51 52 var ( 53 ErrNotImplemented = errors.New("Function not implemented") 54 defaultArchiver = &Archiver{Untar} 55 ) 56 57 const ( 58 Uncompressed Compression = iota 59 Bzip2 60 Gzip 61 Xz 62 ) 63 64 func IsArchive(header []byte) bool { 65 compression := DetectCompression(header) 66 if compression != Uncompressed { 67 return true 68 } 69 r := tar.NewReader(bytes.NewBuffer(header)) 70 _, err := r.Next() 71 return err == nil 72 } 73 74 func DetectCompression(source []byte) Compression { 75 for compression, m := range map[Compression][]byte{ 76 Bzip2: {0x42, 0x5A, 0x68}, 77 Gzip: {0x1F, 0x8B, 0x08}, 78 Xz: {0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00}, 79 } { 80 if len(source) < len(m) { 81 log.Debugf("Len too short") 82 continue 83 } 84 if bytes.Compare(m, source[:len(m)]) == 0 { 85 return compression 86 } 87 } 88 return Uncompressed 89 } 90 91 func xzDecompress(archive io.Reader) (io.ReadCloser, error) { 92 args := []string{"xz", "-d", "-c", "-q"} 93 94 return CmdStream(exec.Command(args[0], args[1:]...), archive) 95 } 96 97 func DecompressStream(archive io.Reader) (io.ReadCloser, error) { 98 p := pools.BufioReader32KPool 99 buf := p.Get(archive) 100 bs, err := buf.Peek(10) 101 if err != nil { 102 return nil, err 103 } 104 105 compression := DetectCompression(bs) 106 switch compression { 107 case Uncompressed: 108 readBufWrapper := p.NewReadCloserWrapper(buf, buf) 109 return readBufWrapper, nil 110 case Gzip: 111 gzReader, err := gzip.NewReader(buf) 112 if err != nil { 113 return nil, err 114 } 115 readBufWrapper := p.NewReadCloserWrapper(buf, gzReader) 116 return readBufWrapper, nil 117 case Bzip2: 118 bz2Reader := bzip2.NewReader(buf) 119 readBufWrapper := p.NewReadCloserWrapper(buf, bz2Reader) 120 return readBufWrapper, nil 121 case Xz: 122 xzReader, err := xzDecompress(buf) 123 if err != nil { 124 return nil, err 125 } 126 readBufWrapper := p.NewReadCloserWrapper(buf, xzReader) 127 return readBufWrapper, nil 128 default: 129 return nil, fmt.Errorf("Unsupported compression format %s", (&compression).Extension()) 130 } 131 } 132 133 func CompressStream(dest io.WriteCloser, compression Compression) (io.WriteCloser, error) { 134 p := pools.BufioWriter32KPool 135 buf := p.Get(dest) 136 switch compression { 137 case Uncompressed: 138 writeBufWrapper := p.NewWriteCloserWrapper(buf, buf) 139 return writeBufWrapper, nil 140 case Gzip: 141 gzWriter := gzip.NewWriter(dest) 142 writeBufWrapper := p.NewWriteCloserWrapper(buf, gzWriter) 143 return writeBufWrapper, nil 144 case Bzip2, Xz: 145 // archive/bzip2 does not support writing, and there is no xz support at all 146 // However, this is not a problem as docker only currently generates gzipped tars 147 return nil, fmt.Errorf("Unsupported compression format %s", (&compression).Extension()) 148 default: 149 return nil, fmt.Errorf("Unsupported compression format %s", (&compression).Extension()) 150 } 151 } 152 153 func (compression *Compression) Extension() string { 154 switch *compression { 155 case Uncompressed: 156 return "tar" 157 case Bzip2: 158 return "tar.bz2" 159 case Gzip: 160 return "tar.gz" 161 case Xz: 162 return "tar.xz" 163 } 164 return "" 165 } 166 167 type tarAppender struct { 168 TarWriter *tar.Writer 169 Buffer *bufio.Writer 170 171 // for hardlink mapping 172 SeenFiles map[uint64]string 173 } 174 175 // canonicalTarName provides a platform-independent and consistent posix-style 176 //path for files and directories to be archived regardless of the platform. 177 func canonicalTarName(name string, isDir bool) (string, error) { 178 name, err := CanonicalTarNameForPath(name) 179 if err != nil { 180 return "", err 181 } 182 183 // suffix with '/' for directories 184 if isDir && !strings.HasSuffix(name, "/") { 185 name += "/" 186 } 187 return name, nil 188 } 189 190 func (ta *tarAppender) addTarFile(path, name string) error { 191 fi, err := os.Lstat(path) 192 if err != nil { 193 return err 194 } 195 196 link := "" 197 if fi.Mode()&os.ModeSymlink != 0 { 198 if link, err = os.Readlink(path); err != nil { 199 return err 200 } 201 } 202 203 hdr, err := tar.FileInfoHeader(fi, link) 204 if err != nil { 205 return err 206 } 207 hdr.Mode = int64(chmodTarEntry(os.FileMode(hdr.Mode))) 208 209 name, err = canonicalTarName(name, fi.IsDir()) 210 if err != nil { 211 return fmt.Errorf("tar: cannot canonicalize path: %v", err) 212 } 213 hdr.Name = name 214 215 nlink, inode, err := setHeaderForSpecialDevice(hdr, ta, name, fi.Sys()) 216 if err != nil { 217 return err 218 } 219 220 // if it's a regular file and has more than 1 link, 221 // it's hardlinked, so set the type flag accordingly 222 if fi.Mode().IsRegular() && nlink > 1 { 223 // a link should have a name that it links too 224 // and that linked name should be first in the tar archive 225 if oldpath, ok := ta.SeenFiles[inode]; ok { 226 hdr.Typeflag = tar.TypeLink 227 hdr.Linkname = oldpath 228 hdr.Size = 0 // This Must be here for the writer math to add up! 229 } else { 230 ta.SeenFiles[inode] = name 231 } 232 } 233 234 capability, _ := system.Lgetxattr(path, "security.capability") 235 if capability != nil { 236 hdr.Xattrs = make(map[string]string) 237 hdr.Xattrs["security.capability"] = string(capability) 238 } 239 240 if err := ta.TarWriter.WriteHeader(hdr); err != nil { 241 return err 242 } 243 244 if hdr.Typeflag == tar.TypeReg { 245 file, err := os.Open(path) 246 if err != nil { 247 return err 248 } 249 250 ta.Buffer.Reset(ta.TarWriter) 251 defer ta.Buffer.Reset(nil) 252 _, err = io.Copy(ta.Buffer, file) 253 file.Close() 254 if err != nil { 255 return err 256 } 257 err = ta.Buffer.Flush() 258 if err != nil { 259 return err 260 } 261 } 262 263 return nil 264 } 265 266 func createTarFile(path, extractDir string, hdr *tar.Header, reader io.Reader, Lchown bool) error { 267 // hdr.Mode is in linux format, which we can use for sycalls, 268 // but for os.Foo() calls we need the mode converted to os.FileMode, 269 // so use hdrInfo.Mode() (they differ for e.g. setuid bits) 270 hdrInfo := hdr.FileInfo() 271 272 switch hdr.Typeflag { 273 case tar.TypeDir: 274 // Create directory unless it exists as a directory already. 275 // In that case we just want to merge the two 276 if fi, err := os.Lstat(path); !(err == nil && fi.IsDir()) { 277 if err := os.Mkdir(path, hdrInfo.Mode()); err != nil { 278 return err 279 } 280 } 281 282 case tar.TypeReg, tar.TypeRegA: 283 // Source is regular file 284 file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, hdrInfo.Mode()) 285 if err != nil { 286 return err 287 } 288 if _, err := io.Copy(file, reader); err != nil { 289 file.Close() 290 return err 291 } 292 file.Close() 293 294 case tar.TypeBlock, tar.TypeChar, tar.TypeFifo: 295 mode := uint32(hdr.Mode & 07777) 296 switch hdr.Typeflag { 297 case tar.TypeBlock: 298 mode |= syscall.S_IFBLK 299 case tar.TypeChar: 300 mode |= syscall.S_IFCHR 301 case tar.TypeFifo: 302 mode |= syscall.S_IFIFO 303 } 304 305 if err := system.Mknod(path, mode, int(system.Mkdev(hdr.Devmajor, hdr.Devminor))); err != nil { 306 return err 307 } 308 309 case tar.TypeLink: 310 targetPath := filepath.Join(extractDir, hdr.Linkname) 311 // check for hardlink breakout 312 if !strings.HasPrefix(targetPath, extractDir) { 313 return breakoutError(fmt.Errorf("invalid hardlink %q -> %q", targetPath, hdr.Linkname)) 314 } 315 if err := os.Link(targetPath, path); err != nil { 316 return err 317 } 318 319 case tar.TypeSymlink: 320 // path -> hdr.Linkname = targetPath 321 // e.g. /extractDir/path/to/symlink -> ../2/file = /extractDir/path/2/file 322 targetPath := filepath.Join(filepath.Dir(path), hdr.Linkname) 323 324 // the reason we don't need to check symlinks in the path (with FollowSymlinkInScope) is because 325 // that symlink would first have to be created, which would be caught earlier, at this very check: 326 if !strings.HasPrefix(targetPath, extractDir) { 327 return breakoutError(fmt.Errorf("invalid symlink %q -> %q", path, hdr.Linkname)) 328 } 329 if err := os.Symlink(hdr.Linkname, path); err != nil { 330 return err 331 } 332 333 case tar.TypeXGlobalHeader: 334 log.Debugf("PAX Global Extended Headers found and ignored") 335 return nil 336 337 default: 338 return fmt.Errorf("Unhandled tar header type %d\n", hdr.Typeflag) 339 } 340 341 if err := os.Lchown(path, hdr.Uid, hdr.Gid); err != nil && Lchown { 342 return err 343 } 344 345 for key, value := range hdr.Xattrs { 346 if err := system.Lsetxattr(path, key, []byte(value), 0); err != nil { 347 return err 348 } 349 } 350 351 // There is no LChmod, so ignore mode for symlink. Also, this 352 // must happen after chown, as that can modify the file mode 353 if hdr.Typeflag != tar.TypeSymlink { 354 if err := os.Chmod(path, hdrInfo.Mode()); err != nil { 355 return err 356 } 357 } 358 359 ts := []syscall.Timespec{timeToTimespec(hdr.AccessTime), timeToTimespec(hdr.ModTime)} 360 // syscall.UtimesNano doesn't support a NOFOLLOW flag atm, and 361 if hdr.Typeflag != tar.TypeSymlink { 362 if err := system.UtimesNano(path, ts); err != nil && err != system.ErrNotSupportedPlatform { 363 return err 364 } 365 } else { 366 if err := system.LUtimesNano(path, ts); err != nil && err != system.ErrNotSupportedPlatform { 367 return err 368 } 369 } 370 return nil 371 } 372 373 // Tar creates an archive from the directory at `path`, and returns it as a 374 // stream of bytes. 375 func Tar(path string, compression Compression) (io.ReadCloser, error) { 376 return TarWithOptions(path, &TarOptions{Compression: compression}) 377 } 378 379 func escapeName(name string) string { 380 escaped := make([]byte, 0) 381 for i, c := range []byte(name) { 382 if i == 0 && c == '/' { 383 continue 384 } 385 // all printable chars except "-" which is 0x2d 386 if (0x20 <= c && c <= 0x7E) && c != 0x2d { 387 escaped = append(escaped, c) 388 } else { 389 escaped = append(escaped, fmt.Sprintf("\\%03o", c)...) 390 } 391 } 392 return string(escaped) 393 } 394 395 // TarWithOptions creates an archive from the directory at `path`, only including files whose relative 396 // paths are included in `options.IncludeFiles` (if non-nil) or not in `options.ExcludePatterns`. 397 func TarWithOptions(srcPath string, options *TarOptions) (io.ReadCloser, error) { 398 pipeReader, pipeWriter := io.Pipe() 399 400 compressWriter, err := CompressStream(pipeWriter, options.Compression) 401 if err != nil { 402 return nil, err 403 } 404 405 go func() { 406 ta := &tarAppender{ 407 TarWriter: tar.NewWriter(compressWriter), 408 Buffer: pools.BufioWriter32KPool.Get(nil), 409 SeenFiles: make(map[uint64]string), 410 } 411 // this buffer is needed for the duration of this piped stream 412 defer pools.BufioWriter32KPool.Put(ta.Buffer) 413 414 // In general we log errors here but ignore them because 415 // during e.g. a diff operation the container can continue 416 // mutating the filesystem and we can see transient errors 417 // from this 418 419 if options.IncludeFiles == nil { 420 options.IncludeFiles = []string{"."} 421 } 422 423 seen := make(map[string]bool) 424 425 var renamedRelFilePath string // For when tar.Options.Name is set 426 for _, include := range options.IncludeFiles { 427 filepath.Walk(filepath.Join(srcPath, include), func(filePath string, f os.FileInfo, err error) error { 428 if err != nil { 429 log.Debugf("Tar: Can't stat file %s to tar: %s", srcPath, err) 430 return nil 431 } 432 433 relFilePath, err := filepath.Rel(srcPath, filePath) 434 if err != nil || (relFilePath == "." && f.IsDir()) { 435 // Error getting relative path OR we are looking 436 // at the root path. Skip in both situations. 437 return nil 438 } 439 440 skip := false 441 442 // If "include" is an exact match for the current file 443 // then even if there's an "excludePatterns" pattern that 444 // matches it, don't skip it. IOW, assume an explicit 'include' 445 // is asking for that file no matter what - which is true 446 // for some files, like .dockerignore and Dockerfile (sometimes) 447 if include != relFilePath { 448 skip, err = fileutils.Matches(relFilePath, options.ExcludePatterns) 449 if err != nil { 450 log.Debugf("Error matching %s", relFilePath, err) 451 return err 452 } 453 } 454 455 if skip { 456 if f.IsDir() { 457 return filepath.SkipDir 458 } 459 return nil 460 } 461 462 if seen[relFilePath] { 463 return nil 464 } 465 seen[relFilePath] = true 466 467 // Rename the base resource 468 if options.Name != "" && filePath == srcPath+"/"+filepath.Base(relFilePath) { 469 renamedRelFilePath = relFilePath 470 } 471 // Set this to make sure the items underneath also get renamed 472 if options.Name != "" { 473 relFilePath = strings.Replace(relFilePath, renamedRelFilePath, options.Name, 1) 474 } 475 476 if err := ta.addTarFile(filePath, relFilePath); err != nil { 477 log.Debugf("Can't add file %s to tar: %s", filePath, err) 478 } 479 return nil 480 }) 481 } 482 483 // Make sure to check the error on Close. 484 if err := ta.TarWriter.Close(); err != nil { 485 log.Debugf("Can't close tar writer: %s", err) 486 } 487 if err := compressWriter.Close(); err != nil { 488 log.Debugf("Can't close compress writer: %s", err) 489 } 490 if err := pipeWriter.Close(); err != nil { 491 log.Debugf("Can't close pipe writer: %s", err) 492 } 493 }() 494 495 return pipeReader, nil 496 } 497 498 func Unpack(decompressedArchive io.Reader, dest string, options *TarOptions) error { 499 tr := tar.NewReader(decompressedArchive) 500 trBuf := pools.BufioReader32KPool.Get(nil) 501 defer pools.BufioReader32KPool.Put(trBuf) 502 503 var dirs []*tar.Header 504 505 // Iterate through the files in the archive. 506 loop: 507 for { 508 hdr, err := tr.Next() 509 if err == io.EOF { 510 // end of tar archive 511 break 512 } 513 if err != nil { 514 return err 515 } 516 517 // Normalize name, for safety and for a simple is-root check 518 // This keeps "../" as-is, but normalizes "/../" to "/" 519 hdr.Name = filepath.Clean(hdr.Name) 520 521 for _, exclude := range options.ExcludePatterns { 522 if strings.HasPrefix(hdr.Name, exclude) { 523 continue loop 524 } 525 } 526 527 if !strings.HasSuffix(hdr.Name, "/") { 528 // Not the root directory, ensure that the parent directory exists 529 parent := filepath.Dir(hdr.Name) 530 parentPath := filepath.Join(dest, parent) 531 if _, err := os.Lstat(parentPath); err != nil && os.IsNotExist(err) { 532 err = os.MkdirAll(parentPath, 0777) 533 if err != nil { 534 return err 535 } 536 } 537 } 538 539 path := filepath.Join(dest, hdr.Name) 540 rel, err := filepath.Rel(dest, path) 541 if err != nil { 542 return err 543 } 544 if strings.HasPrefix(rel, "../") { 545 return breakoutError(fmt.Errorf("%q is outside of %q", hdr.Name, dest)) 546 } 547 548 // If path exits we almost always just want to remove and replace it 549 // The only exception is when it is a directory *and* the file from 550 // the layer is also a directory. Then we want to merge them (i.e. 551 // just apply the metadata from the layer). 552 if fi, err := os.Lstat(path); err == nil { 553 if fi.IsDir() && hdr.Name == "." { 554 continue 555 } 556 if !(fi.IsDir() && hdr.Typeflag == tar.TypeDir) { 557 if err := os.RemoveAll(path); err != nil { 558 return err 559 } 560 } 561 } 562 trBuf.Reset(tr) 563 if err := createTarFile(path, dest, hdr, trBuf, !options.NoLchown); err != nil { 564 return err 565 } 566 567 // Directory mtimes must be handled at the end to avoid further 568 // file creation in them to modify the directory mtime 569 if hdr.Typeflag == tar.TypeDir { 570 dirs = append(dirs, hdr) 571 } 572 } 573 574 for _, hdr := range dirs { 575 path := filepath.Join(dest, hdr.Name) 576 ts := []syscall.Timespec{timeToTimespec(hdr.AccessTime), timeToTimespec(hdr.ModTime)} 577 if err := syscall.UtimesNano(path, ts); err != nil { 578 return err 579 } 580 } 581 return nil 582 } 583 584 // Untar reads a stream of bytes from `archive`, parses it as a tar archive, 585 // and unpacks it into the directory at `dest`. 586 // The archive may be compressed with one of the following algorithms: 587 // identity (uncompressed), gzip, bzip2, xz. 588 // FIXME: specify behavior when target path exists vs. doesn't exist. 589 func Untar(archive io.Reader, dest string, options *TarOptions) error { 590 if archive == nil { 591 return fmt.Errorf("Empty archive") 592 } 593 dest = filepath.Clean(dest) 594 if options == nil { 595 options = &TarOptions{} 596 } 597 if options.ExcludePatterns == nil { 598 options.ExcludePatterns = []string{} 599 } 600 decompressedArchive, err := DecompressStream(archive) 601 if err != nil { 602 return err 603 } 604 defer decompressedArchive.Close() 605 return Unpack(decompressedArchive, dest, options) 606 } 607 608 func (archiver *Archiver) TarUntar(src, dst string) error { 609 log.Debugf("TarUntar(%s %s)", src, dst) 610 archive, err := TarWithOptions(src, &TarOptions{Compression: Uncompressed}) 611 if err != nil { 612 return err 613 } 614 defer archive.Close() 615 return archiver.Untar(archive, dst, nil) 616 } 617 618 // TarUntar is a convenience function which calls Tar and Untar, with the output of one piped into the other. 619 // If either Tar or Untar fails, TarUntar aborts and returns the error. 620 func TarUntar(src, dst string) error { 621 return defaultArchiver.TarUntar(src, dst) 622 } 623 624 func (archiver *Archiver) UntarPath(src, dst string) error { 625 archive, err := os.Open(src) 626 if err != nil { 627 return err 628 } 629 defer archive.Close() 630 if err := archiver.Untar(archive, dst, nil); err != nil { 631 return err 632 } 633 return nil 634 } 635 636 // UntarPath is a convenience function which looks for an archive 637 // at filesystem path `src`, and unpacks it at `dst`. 638 func UntarPath(src, dst string) error { 639 return defaultArchiver.UntarPath(src, dst) 640 } 641 642 func (archiver *Archiver) CopyWithTar(src, dst string) error { 643 srcSt, err := os.Stat(src) 644 if err != nil { 645 return err 646 } 647 if !srcSt.IsDir() { 648 return archiver.CopyFileWithTar(src, dst) 649 } 650 // Create dst, copy src's content into it 651 log.Debugf("Creating dest directory: %s", dst) 652 if err := os.MkdirAll(dst, 0755); err != nil && !os.IsExist(err) { 653 return err 654 } 655 log.Debugf("Calling TarUntar(%s, %s)", src, dst) 656 return archiver.TarUntar(src, dst) 657 } 658 659 // CopyWithTar creates a tar archive of filesystem path `src`, and 660 // unpacks it at filesystem path `dst`. 661 // The archive is streamed directly with fixed buffering and no 662 // intermediary disk IO. 663 func CopyWithTar(src, dst string) error { 664 return defaultArchiver.CopyWithTar(src, dst) 665 } 666 667 func (archiver *Archiver) CopyFileWithTar(src, dst string) (err error) { 668 log.Debugf("CopyFileWithTar(%s, %s)", src, dst) 669 srcSt, err := os.Stat(src) 670 if err != nil { 671 return err 672 } 673 if srcSt.IsDir() { 674 return fmt.Errorf("Can't copy a directory") 675 } 676 // Clean up the trailing / 677 if dst[len(dst)-1] == '/' { 678 dst = path.Join(dst, filepath.Base(src)) 679 } 680 // Create the holding directory if necessary 681 if err := os.MkdirAll(filepath.Dir(dst), 0700); err != nil && !os.IsExist(err) { 682 return err 683 } 684 685 r, w := io.Pipe() 686 errC := promise.Go(func() error { 687 defer w.Close() 688 689 srcF, err := os.Open(src) 690 if err != nil { 691 return err 692 } 693 defer srcF.Close() 694 695 hdr, err := tar.FileInfoHeader(srcSt, "") 696 if err != nil { 697 return err 698 } 699 hdr.Name = filepath.Base(dst) 700 hdr.Mode = int64(chmodTarEntry(os.FileMode(hdr.Mode))) 701 702 tw := tar.NewWriter(w) 703 defer tw.Close() 704 if err := tw.WriteHeader(hdr); err != nil { 705 return err 706 } 707 if _, err := io.Copy(tw, srcF); err != nil { 708 return err 709 } 710 return nil 711 }) 712 defer func() { 713 if er := <-errC; err != nil { 714 err = er 715 } 716 }() 717 return archiver.Untar(r, filepath.Dir(dst), nil) 718 } 719 720 // CopyFileWithTar emulates the behavior of the 'cp' command-line 721 // for a single file. It copies a regular file from path `src` to 722 // path `dst`, and preserves all its metadata. 723 // 724 // If `dst` ends with a trailing slash '/', the final destination path 725 // will be `dst/base(src)`. 726 func CopyFileWithTar(src, dst string) (err error) { 727 return defaultArchiver.CopyFileWithTar(src, dst) 728 } 729 730 // CmdStream executes a command, and returns its stdout as a stream. 731 // If the command fails to run or doesn't complete successfully, an error 732 // will be returned, including anything written on stderr. 733 func CmdStream(cmd *exec.Cmd, input io.Reader) (io.ReadCloser, error) { 734 if input != nil { 735 stdin, err := cmd.StdinPipe() 736 if err != nil { 737 return nil, err 738 } 739 // Write stdin if any 740 go func() { 741 io.Copy(stdin, input) 742 stdin.Close() 743 }() 744 } 745 stdout, err := cmd.StdoutPipe() 746 if err != nil { 747 return nil, err 748 } 749 stderr, err := cmd.StderrPipe() 750 if err != nil { 751 return nil, err 752 } 753 pipeR, pipeW := io.Pipe() 754 errChan := make(chan []byte) 755 // Collect stderr, we will use it in case of an error 756 go func() { 757 errText, e := ioutil.ReadAll(stderr) 758 if e != nil { 759 errText = []byte("(...couldn't fetch stderr: " + e.Error() + ")") 760 } 761 errChan <- errText 762 }() 763 // Copy stdout to the returned pipe 764 go func() { 765 _, err := io.Copy(pipeW, stdout) 766 if err != nil { 767 pipeW.CloseWithError(err) 768 } 769 errText := <-errChan 770 if err := cmd.Wait(); err != nil { 771 pipeW.CloseWithError(fmt.Errorf("%s: %s", err, errText)) 772 } else { 773 pipeW.Close() 774 } 775 }() 776 // Run the command and return the pipe 777 if err := cmd.Start(); err != nil { 778 return nil, err 779 } 780 return pipeR, nil 781 } 782 783 // NewTempArchive reads the content of src into a temporary file, and returns the contents 784 // of that file as an archive. The archive can only be read once - as soon as reading completes, 785 // the file will be deleted. 786 func NewTempArchive(src Archive, dir string) (*TempArchive, error) { 787 f, err := ioutil.TempFile(dir, "") 788 if err != nil { 789 return nil, err 790 } 791 if _, err := io.Copy(f, src); err != nil { 792 return nil, err 793 } 794 if err = f.Sync(); err != nil { 795 return nil, err 796 } 797 if _, err := f.Seek(0, 0); err != nil { 798 return nil, err 799 } 800 st, err := f.Stat() 801 if err != nil { 802 return nil, err 803 } 804 size := st.Size() 805 return &TempArchive{File: f, Size: size}, nil 806 } 807 808 type TempArchive struct { 809 *os.File 810 Size int64 // Pre-computed from Stat().Size() as a convenience 811 read int64 812 closed bool 813 } 814 815 // Close closes the underlying file if it's still open, or does a no-op 816 // to allow callers to try to close the TempArchive multiple times safely. 817 func (archive *TempArchive) Close() error { 818 if archive.closed { 819 return nil 820 } 821 822 archive.closed = true 823 824 return archive.File.Close() 825 } 826 827 func (archive *TempArchive) Read(data []byte) (int, error) { 828 n, err := archive.File.Read(data) 829 archive.read += int64(n) 830 if err != nil || archive.read == archive.Size { 831 archive.Close() 832 os.Remove(archive.File.Name()) 833 } 834 return n, err 835 }