20 "github.com/fsouza/go-dockerclient/external/github.com/Sirupsen/logrus"
21 "github.com/fsouza/go-dockerclient/external/github.com/docker/docker/pkg/fileutils"
22 "github.com/fsouza/go-dockerclient/external/github.com/docker/docker/pkg/idtools"
23 "github.com/fsouza/go-dockerclient/external/github.com/docker/docker/pkg/ioutils"
24 "github.com/fsouza/go-dockerclient/external/github.com/docker/docker/pkg/pools"
25 "github.com/fsouza/go-dockerclient/external/github.com/docker/docker/pkg/promise"
26 "github.com/fsouza/go-dockerclient/external/github.com/docker/docker/pkg/system"
30 // Archive is a type of io.ReadCloser which has two interfaces Read and Closer.
32 // Reader is a type of io.Reader.
34 // Compression is the state represents if compressed or not.
36 // TarChownOptions wraps the chown options UID and GID.
37 TarChownOptions struct {
40 // TarOptions wraps the tar options.
43 ExcludePatterns []string
44 Compression Compression
46 UIDMaps []idtools.IDMap
47 GIDMaps []idtools.IDMap
48 ChownOpts *TarChownOptions
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
58 // Archiver allows the reuse of most utility functions of this package
59 // with a pluggable Untar function. Also, to facilitate the passing of
60 // specific id mappings for untar, an archiver can be created with maps
61 // which will then be passed to Untar operations
63 Untar func(io.Reader, string, *TarOptions) error
64 UIDMaps []idtools.IDMap
65 GIDMaps []idtools.IDMap
68 // breakoutError is used to differentiate errors related to breaking out
69 // When testing archive breakout in the unit tests, this error is expected
70 // in order for the test to pass.
75 // ErrNotImplemented is the error message of function not implemented.
76 ErrNotImplemented = errors.New("Function not implemented")
77 defaultArchiver = &Archiver{Untar: Untar, UIDMaps: nil, GIDMaps: nil}
81 // HeaderSize is the size in bytes of a tar header
86 // Uncompressed represents the uncompressed.
87 Uncompressed Compression = iota
88 // Bzip2 is bzip2 compression algorithm.
90 // Gzip is gzip compression algorithm.
92 // Xz is xz compression algorithm.
96 // IsArchive checks for the magic bytes of a tar or any supported compression
98 func IsArchive(header []byte) bool {
99 compression := DetectCompression(header)
100 if compression != Uncompressed {
103 r := tar.NewReader(bytes.NewBuffer(header))
108 // IsArchivePath checks if the (possibly compressed) file at the given path
109 // starts with a tar file header.
110 func IsArchivePath(path string) bool {
111 file, err := os.Open(path)
116 rdr, err := DecompressStream(file)
120 r := tar.NewReader(rdr)
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},
132 if len(source) < len(m) {
133 logrus.Debugf("Len too short")
136 if bytes.Compare(m, source[:len(m)]) == 0 {
143 func xzDecompress(archive io.Reader) (io.ReadCloser, <-chan struct{}, error) {
144 args := []string{"xz", "-d", "-c", "-q"}
146 return cmdStream(exec.Command(args[0], args[1:]...), archive)
149 // DecompressStream decompress the archive and returns a ReaderCloser with the decompressed archive.
150 func DecompressStream(archive io.Reader) (io.ReadCloser, error) {
151 p := pools.BufioReader32KPool
152 buf := p.Get(archive)
153 bs, err := buf.Peek(10)
154 if err != nil && err != io.EOF {
155 // Note: we'll ignore any io.EOF error because there are some odd
156 // cases where the layer.tar file will be empty (zero bytes) and
157 // that results in an io.EOF from the Peek() call. So, in those
158 // cases we'll just treat it as a non-compressed stream and
159 // that means just create an empty layer.
164 compression := DetectCompression(bs)
167 readBufWrapper := p.NewReadCloserWrapper(buf, buf)
168 return readBufWrapper, nil
170 gzReader, err := gzip.NewReader(buf)
174 readBufWrapper := p.NewReadCloserWrapper(buf, gzReader)
175 return readBufWrapper, nil
177 bz2Reader := bzip2.NewReader(buf)
178 readBufWrapper := p.NewReadCloserWrapper(buf, bz2Reader)
179 return readBufWrapper, nil
181 xzReader, chdone, err := xzDecompress(buf)
185 readBufWrapper := p.NewReadCloserWrapper(buf, xzReader)
186 return ioutils.NewReadCloserWrapper(readBufWrapper, func() error {
188 return readBufWrapper.Close()
191 return nil, fmt.Errorf("Unsupported compression format %s", (&compression).Extension())
195 // CompressStream compresses the dest with specified compression algorithm.
196 func CompressStream(dest io.WriteCloser, compression Compression) (io.WriteCloser, error) {
197 p := pools.BufioWriter32KPool
201 writeBufWrapper := p.NewWriteCloserWrapper(buf, buf)
202 return writeBufWrapper, nil
204 gzWriter := gzip.NewWriter(dest)
205 writeBufWrapper := p.NewWriteCloserWrapper(buf, gzWriter)
206 return writeBufWrapper, nil
208 // archive/bzip2 does not support writing, and there is no xz support at all
209 // However, this is not a problem as docker only currently generates gzipped tars
210 return nil, fmt.Errorf("Unsupported compression format %s", (&compression).Extension())
212 return nil, fmt.Errorf("Unsupported compression format %s", (&compression).Extension())
216 // Extension returns the extension of a file that uses the specified compression algorithm.
217 func (compression *Compression) Extension() string {
218 switch *compression {
231 type tarAppender struct {
232 TarWriter *tar.Writer
235 // for hardlink mapping
236 SeenFiles map[uint64]string
237 UIDMaps []idtools.IDMap
238 GIDMaps []idtools.IDMap
241 // canonicalTarName provides a platform-independent and consistent posix-style
242 //path for files and directories to be archived regardless of the platform.
243 func canonicalTarName(name string, isDir bool) (string, error) {
244 name, err := CanonicalTarNameForPath(name)
249 // suffix with '/' for directories
250 if isDir && !strings.HasSuffix(name, "/") {
256 func (ta *tarAppender) addTarFile(path, name string) error {
257 fi, err := os.Lstat(path)
263 if fi.Mode()&os.ModeSymlink != 0 {
264 if link, err = os.Readlink(path); err != nil {
269 hdr, err := tar.FileInfoHeader(fi, link)
273 hdr.Mode = int64(chmodTarEntry(os.FileMode(hdr.Mode)))
275 name, err = canonicalTarName(name, fi.IsDir())
277 return fmt.Errorf("tar: cannot canonicalize path: %v", err)
281 inode, err := setHeaderForSpecialDevice(hdr, ta, name, fi.Sys())
286 // if it's not a directory and has more than 1 link,
287 // it's hardlinked, so set the type flag accordingly
288 if !fi.IsDir() && hasHardlinks(fi) {
289 // a link should have a name that it links too
290 // and that linked name should be first in the tar archive
291 if oldpath, ok := ta.SeenFiles[inode]; ok {
292 hdr.Typeflag = tar.TypeLink
293 hdr.Linkname = oldpath
294 hdr.Size = 0 // This Must be here for the writer math to add up!
296 ta.SeenFiles[inode] = name
300 capability, _ := system.Lgetxattr(path, "security.capability")
301 if capability != nil {
302 hdr.Xattrs = make(map[string]string)
303 hdr.Xattrs["security.capability"] = string(capability)
306 //handle re-mapping container ID mappings back to host ID mappings before
307 //writing tar headers/files. We skip whiteout files because they were written
308 //by the kernel and already have proper ownership relative to the host
309 if !strings.HasPrefix(filepath.Base(hdr.Name), WhiteoutPrefix) && (ta.UIDMaps != nil || ta.GIDMaps != nil) {
310 uid, gid, err := getFileUIDGID(fi.Sys())
314 xUID, err := idtools.ToContainer(uid, ta.UIDMaps)
318 xGID, err := idtools.ToContainer(gid, ta.GIDMaps)
326 if err := ta.TarWriter.WriteHeader(hdr); err != nil {
330 if hdr.Typeflag == tar.TypeReg {
331 file, err := os.Open(path)
336 ta.Buffer.Reset(ta.TarWriter)
337 defer ta.Buffer.Reset(nil)
338 _, err = io.Copy(ta.Buffer, file)
343 err = ta.Buffer.Flush()
352 func createTarFile(path, extractDir string, hdr *tar.Header, reader io.Reader, Lchown bool, chownOpts *TarChownOptions) error {
353 // hdr.Mode is in linux format, which we can use for sycalls,
354 // but for os.Foo() calls we need the mode converted to os.FileMode,
355 // so use hdrInfo.Mode() (they differ for e.g. setuid bits)
356 hdrInfo := hdr.FileInfo()
358 switch hdr.Typeflag {
360 // Create directory unless it exists as a directory already.
361 // In that case we just want to merge the two
362 if fi, err := os.Lstat(path); !(err == nil && fi.IsDir()) {
363 if err := os.Mkdir(path, hdrInfo.Mode()); err != nil {
368 case tar.TypeReg, tar.TypeRegA:
369 // Source is regular file
370 file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, hdrInfo.Mode())
374 if _, err := io.Copy(file, reader); err != nil {
380 case tar.TypeBlock, tar.TypeChar, tar.TypeFifo:
381 // Handle this is an OS-specific way
382 if err := handleTarTypeBlockCharFifo(hdr, path); err != nil {
387 targetPath := filepath.Join(extractDir, hdr.Linkname)
388 // check for hardlink breakout
389 if !strings.HasPrefix(targetPath, extractDir) {
390 return breakoutError(fmt.Errorf("invalid hardlink %q -> %q", targetPath, hdr.Linkname))
392 if err := os.Link(targetPath, path); err != nil {
396 case tar.TypeSymlink:
397 // path -> hdr.Linkname = targetPath
398 // e.g. /extractDir/path/to/symlink -> ../2/file = /extractDir/path/2/file
399 targetPath := filepath.Join(filepath.Dir(path), hdr.Linkname)
401 // the reason we don't need to check symlinks in the path (with FollowSymlinkInScope) is because
402 // that symlink would first have to be created, which would be caught earlier, at this very check:
403 if !strings.HasPrefix(targetPath, extractDir) {
404 return breakoutError(fmt.Errorf("invalid symlink %q -> %q", path, hdr.Linkname))
406 if err := os.Symlink(hdr.Linkname, path); err != nil {
410 case tar.TypeXGlobalHeader:
411 logrus.Debugf("PAX Global Extended Headers found and ignored")
415 return fmt.Errorf("Unhandled tar header type %d\n", hdr.Typeflag)
418 // Lchown is not supported on Windows.
419 if Lchown && runtime.GOOS != "windows" {
420 if chownOpts == nil {
421 chownOpts = &TarChownOptions{UID: hdr.Uid, GID: hdr.Gid}
423 if err := os.Lchown(path, chownOpts.UID, chownOpts.GID); err != nil {
428 for key, value := range hdr.Xattrs {
429 if err := system.Lsetxattr(path, key, []byte(value), 0); err != nil {
434 // There is no LChmod, so ignore mode for symlink. Also, this
435 // must happen after chown, as that can modify the file mode
436 if err := handleLChmod(hdr, path, hdrInfo); err != nil {
440 aTime := hdr.AccessTime
441 if aTime.Before(hdr.ModTime) {
442 // Last access time should never be before last modified time.
446 // system.Chtimes doesn't support a NOFOLLOW flag atm
447 if hdr.Typeflag == tar.TypeLink {
448 if fi, err := os.Lstat(hdr.Linkname); err == nil && (fi.Mode()&os.ModeSymlink == 0) {
449 if err := system.Chtimes(path, aTime, hdr.ModTime); err != nil {
453 } else if hdr.Typeflag != tar.TypeSymlink {
454 if err := system.Chtimes(path, aTime, hdr.ModTime); err != nil {
458 ts := []syscall.Timespec{timeToTimespec(aTime), timeToTimespec(hdr.ModTime)}
459 if err := system.LUtimesNano(path, ts); err != nil && err != system.ErrNotSupportedPlatform {
466 // Tar creates an archive from the directory at `path`, and returns it as a
468 func Tar(path string, compression Compression) (io.ReadCloser, error) {
469 return TarWithOptions(path, &TarOptions{Compression: compression})
472 // TarWithOptions creates an archive from the directory at `path`, only including files whose relative
473 // paths are included in `options.IncludeFiles` (if non-nil) or not in `options.ExcludePatterns`.
474 func TarWithOptions(srcPath string, options *TarOptions) (io.ReadCloser, error) {
476 // Fix the source path to work with long path names. This is a no-op
477 // on platforms other than Windows.
478 srcPath = fixVolumePathPrefix(srcPath)
480 patterns, patDirs, exceptions, err := fileutils.CleanPatterns(options.ExcludePatterns)
486 pipeReader, pipeWriter := io.Pipe()
488 compressWriter, err := CompressStream(pipeWriter, options.Compression)
495 TarWriter: tar.NewWriter(compressWriter),
496 Buffer: pools.BufioWriter32KPool.Get(nil),
497 SeenFiles: make(map[uint64]string),
498 UIDMaps: options.UIDMaps,
499 GIDMaps: options.GIDMaps,
503 // Make sure to check the error on Close.
504 if err := ta.TarWriter.Close(); err != nil {
505 logrus.Debugf("Can't close tar writer: %s", err)
507 if err := compressWriter.Close(); err != nil {
508 logrus.Debugf("Can't close compress writer: %s", err)
510 if err := pipeWriter.Close(); err != nil {
511 logrus.Debugf("Can't close pipe writer: %s", err)
515 // this buffer is needed for the duration of this piped stream
516 defer pools.BufioWriter32KPool.Put(ta.Buffer)
518 // In general we log errors here but ignore them because
519 // during e.g. a diff operation the container can continue
520 // mutating the filesystem and we can see transient errors
523 stat, err := os.Lstat(srcPath)
529 // We can't later join a non-dir with any includes because the
530 // 'walk' will error if "file/." is stat-ed and "file" is not a
531 // directory. So, we must split the source path and use the
532 // basename as the include.
533 if len(options.IncludeFiles) > 0 {
534 logrus.Warn("Tar: Can't archive a file with includes")
537 dir, base := SplitPathDirEntry(srcPath)
539 options.IncludeFiles = []string{base}
542 if len(options.IncludeFiles) == 0 {
543 options.IncludeFiles = []string{"."}
546 seen := make(map[string]bool)
548 for _, include := range options.IncludeFiles {
549 rebaseName := options.RebaseNames[include]
551 walkRoot := getWalkRoot(srcPath, include)
552 filepath.Walk(walkRoot, func(filePath string, f os.FileInfo, err error) error {
554 logrus.Debugf("Tar: Can't stat file %s to tar: %s", srcPath, err)
558 relFilePath, err := filepath.Rel(srcPath, filePath)
559 if err != nil || (!options.IncludeSourceDir && relFilePath == "." && f.IsDir()) {
560 // Error getting relative path OR we are looking
561 // at the source directory path. Skip in both situations.
565 if options.IncludeSourceDir && include == "." && relFilePath != "." {
566 relFilePath = strings.Join([]string{".", relFilePath}, string(filepath.Separator))
571 // If "include" is an exact match for the current file
572 // then even if there's an "excludePatterns" pattern that
573 // matches it, don't skip it. IOW, assume an explicit 'include'
574 // is asking for that file no matter what - which is true
575 // for some files, like .dockerignore and Dockerfile (sometimes)
576 if include != relFilePath {
577 skip, err = fileutils.OptimizedMatches(relFilePath, patterns, patDirs)
579 logrus.Debugf("Error matching %s: %v", relFilePath, err)
585 if !exceptions && f.IsDir() {
586 return filepath.SkipDir
591 if seen[relFilePath] {
594 seen[relFilePath] = true
596 // Rename the base resource.
597 if rebaseName != "" {
598 var replacement string
599 if rebaseName != string(filepath.Separator) {
600 // Special case the root directory to replace with an
601 // empty string instead so that we don't end up with
602 // double slashes in the paths.
603 replacement = rebaseName
606 relFilePath = strings.Replace(relFilePath, include, replacement, 1)
609 if err := ta.addTarFile(filePath, relFilePath); err != nil {
610 logrus.Debugf("Can't add file %s to tar: %s", filePath, err)
617 return pipeReader, nil
620 // Unpack unpacks the decompressedArchive to dest with options.
621 func Unpack(decompressedArchive io.Reader, dest string, options *TarOptions) error {
622 tr := tar.NewReader(decompressedArchive)
623 trBuf := pools.BufioReader32KPool.Get(nil)
624 defer pools.BufioReader32KPool.Put(trBuf)
626 var dirs []*tar.Header
627 remappedRootUID, remappedRootGID, err := idtools.GetRootUIDGID(options.UIDMaps, options.GIDMaps)
632 // Iterate through the files in the archive.
635 hdr, err := tr.Next()
637 // end of tar archive
644 // Normalize name, for safety and for a simple is-root check
645 // This keeps "../" as-is, but normalizes "/../" to "/". Or Windows:
646 // This keeps "..\" as-is, but normalizes "\..\" to "\".
647 hdr.Name = filepath.Clean(hdr.Name)
649 for _, exclude := range options.ExcludePatterns {
650 if strings.HasPrefix(hdr.Name, exclude) {
655 // After calling filepath.Clean(hdr.Name) above, hdr.Name will now be in
656 // the filepath format for the OS on which the daemon is running. Hence
657 // the check for a slash-suffix MUST be done in an OS-agnostic way.
658 if !strings.HasSuffix(hdr.Name, string(os.PathSeparator)) {
659 // Not the root directory, ensure that the parent directory exists
660 parent := filepath.Dir(hdr.Name)
661 parentPath := filepath.Join(dest, parent)
662 if _, err := os.Lstat(parentPath); err != nil && os.IsNotExist(err) {
663 err = system.MkdirAll(parentPath, 0777)
670 path := filepath.Join(dest, hdr.Name)
671 rel, err := filepath.Rel(dest, path)
675 if strings.HasPrefix(rel, ".."+string(os.PathSeparator)) {
676 return breakoutError(fmt.Errorf("%q is outside of %q", hdr.Name, dest))
679 // If path exits we almost always just want to remove and replace it
680 // The only exception is when it is a directory *and* the file from
681 // the layer is also a directory. Then we want to merge them (i.e.
682 // just apply the metadata from the layer).
683 if fi, err := os.Lstat(path); err == nil {
684 if options.NoOverwriteDirNonDir && fi.IsDir() && hdr.Typeflag != tar.TypeDir {
685 // If NoOverwriteDirNonDir is true then we cannot replace
686 // an existing directory with a non-directory from the archive.
687 return fmt.Errorf("cannot overwrite directory %q with non-directory %q", path, dest)
690 if options.NoOverwriteDirNonDir && !fi.IsDir() && hdr.Typeflag == tar.TypeDir {
691 // If NoOverwriteDirNonDir is true then we cannot replace
692 // an existing non-directory with a directory from the archive.
693 return fmt.Errorf("cannot overwrite non-directory %q with directory %q", path, dest)
696 if fi.IsDir() && hdr.Name == "." {
700 if !(fi.IsDir() && hdr.Typeflag == tar.TypeDir) {
701 if err := os.RemoveAll(path); err != nil {
708 // if the options contain a uid & gid maps, convert header uid/gid
709 // entries using the maps such that lchown sets the proper mapped
710 // uid/gid after writing the file. We only perform this mapping if
711 // the file isn't already owned by the remapped root UID or GID, as
712 // that specific uid/gid has no mapping from container -> host, and
713 // those files already have the proper ownership for inside the
715 if hdr.Uid != remappedRootUID {
716 xUID, err := idtools.ToHost(hdr.Uid, options.UIDMaps)
722 if hdr.Gid != remappedRootGID {
723 xGID, err := idtools.ToHost(hdr.Gid, options.GIDMaps)
730 if err := createTarFile(path, dest, hdr, trBuf, !options.NoLchown, options.ChownOpts); err != nil {
734 // Directory mtimes must be handled at the end to avoid further
735 // file creation in them to modify the directory mtime
736 if hdr.Typeflag == tar.TypeDir {
737 dirs = append(dirs, hdr)
741 for _, hdr := range dirs {
742 path := filepath.Join(dest, hdr.Name)
744 if err := system.Chtimes(path, hdr.AccessTime, hdr.ModTime); err != nil {
751 // Untar reads a stream of bytes from `archive`, parses it as a tar archive,
752 // and unpacks it into the directory at `dest`.
753 // The archive may be compressed with one of the following algorithms:
754 // identity (uncompressed), gzip, bzip2, xz.
755 // FIXME: specify behavior when target path exists vs. doesn't exist.
756 func Untar(tarArchive io.Reader, dest string, options *TarOptions) error {
757 return untarHandler(tarArchive, dest, options, true)
760 // UntarUncompressed reads a stream of bytes from `archive`, parses it as a tar archive,
761 // and unpacks it into the directory at `dest`.
762 // The archive must be an uncompressed stream.
763 func UntarUncompressed(tarArchive io.Reader, dest string, options *TarOptions) error {
764 return untarHandler(tarArchive, dest, options, false)
767 // Handler for teasing out the automatic decompression
768 func untarHandler(tarArchive io.Reader, dest string, options *TarOptions, decompress bool) error {
769 if tarArchive == nil {
770 return fmt.Errorf("Empty archive")
772 dest = filepath.Clean(dest)
774 options = &TarOptions{}
776 if options.ExcludePatterns == nil {
777 options.ExcludePatterns = []string{}
782 decompressedArchive, err := DecompressStream(tarArchive)
786 defer decompressedArchive.Close()
787 r = decompressedArchive
790 return Unpack(r, dest, options)
793 // TarUntar is a convenience function which calls Tar and Untar, with the output of one piped into the other.
794 // If either Tar or Untar fails, TarUntar aborts and returns the error.
795 func (archiver *Archiver) TarUntar(src, dst string) error {
796 logrus.Debugf("TarUntar(%s %s)", src, dst)
797 archive, err := TarWithOptions(src, &TarOptions{Compression: Uncompressed})
801 defer archive.Close()
803 var options *TarOptions
804 if archiver.UIDMaps != nil || archiver.GIDMaps != nil {
805 options = &TarOptions{
806 UIDMaps: archiver.UIDMaps,
807 GIDMaps: archiver.GIDMaps,
810 return archiver.Untar(archive, dst, options)
813 // TarUntar is a convenience function which calls Tar and Untar, with the output of one piped into the other.
814 // If either Tar or Untar fails, TarUntar aborts and returns the error.
815 func TarUntar(src, dst string) error {
816 return defaultArchiver.TarUntar(src, dst)
819 // UntarPath untar a file from path to a destination, src is the source tar file path.
820 func (archiver *Archiver) UntarPath(src, dst string) error {
821 archive, err := os.Open(src)
825 defer archive.Close()
826 var options *TarOptions
827 if archiver.UIDMaps != nil || archiver.GIDMaps != nil {
828 options = &TarOptions{
829 UIDMaps: archiver.UIDMaps,
830 GIDMaps: archiver.GIDMaps,
833 return archiver.Untar(archive, dst, options)
836 // UntarPath is a convenience function which looks for an archive
837 // at filesystem path `src`, and unpacks it at `dst`.
838 func UntarPath(src, dst string) error {
839 return defaultArchiver.UntarPath(src, dst)
842 // CopyWithTar creates a tar archive of filesystem path `src`, and
843 // unpacks it at filesystem path `dst`.
844 // The archive is streamed directly with fixed buffering and no
845 // intermediary disk IO.
846 func (archiver *Archiver) CopyWithTar(src, dst string) error {
847 srcSt, err := os.Stat(src)
852 return archiver.CopyFileWithTar(src, dst)
854 // Create dst, copy src's content into it
855 logrus.Debugf("Creating dest directory: %s", dst)
856 if err := system.MkdirAll(dst, 0755); err != nil {
859 logrus.Debugf("Calling TarUntar(%s, %s)", src, dst)
860 return archiver.TarUntar(src, dst)
863 // CopyWithTar creates a tar archive of filesystem path `src`, and
864 // unpacks it at filesystem path `dst`.
865 // The archive is streamed directly with fixed buffering and no
866 // intermediary disk IO.
867 func CopyWithTar(src, dst string) error {
868 return defaultArchiver.CopyWithTar(src, dst)
871 // CopyFileWithTar emulates the behavior of the 'cp' command-line
872 // for a single file. It copies a regular file from path `src` to
873 // path `dst`, and preserves all its metadata.
874 func (archiver *Archiver) CopyFileWithTar(src, dst string) (err error) {
875 logrus.Debugf("CopyFileWithTar(%s, %s)", src, dst)
876 srcSt, err := os.Stat(src)
882 return fmt.Errorf("Can't copy a directory")
885 // Clean up the trailing slash. This must be done in an operating
886 // system specific manner.
887 if dst[len(dst)-1] == os.PathSeparator {
888 dst = filepath.Join(dst, filepath.Base(src))
890 // Create the holding directory if necessary
891 if err := system.MkdirAll(filepath.Dir(dst), 0700); err != nil {
896 errC := promise.Go(func() error {
899 srcF, err := os.Open(src)
905 hdr, err := tar.FileInfoHeader(srcSt, "")
909 hdr.Name = filepath.Base(dst)
910 hdr.Mode = int64(chmodTarEntry(os.FileMode(hdr.Mode)))
912 remappedRootUID, remappedRootGID, err := idtools.GetRootUIDGID(archiver.UIDMaps, archiver.GIDMaps)
917 // only perform mapping if the file being copied isn't already owned by the
918 // uid or gid of the remapped root in the container
919 if remappedRootUID != hdr.Uid {
920 xUID, err := idtools.ToHost(hdr.Uid, archiver.UIDMaps)
926 if remappedRootGID != hdr.Gid {
927 xGID, err := idtools.ToHost(hdr.Gid, archiver.GIDMaps)
934 tw := tar.NewWriter(w)
936 if err := tw.WriteHeader(hdr); err != nil {
939 if _, err := io.Copy(tw, srcF); err != nil {
945 if er := <-errC; err != nil {
950 err = archiver.Untar(r, filepath.Dir(dst), nil)
952 r.CloseWithError(err)
957 // CopyFileWithTar emulates the behavior of the 'cp' command-line
958 // for a single file. It copies a regular file from path `src` to
959 // path `dst`, and preserves all its metadata.
961 // Destination handling is in an operating specific manner depending
962 // where the daemon is running. If `dst` ends with a trailing slash
963 // the final destination path will be `dst/base(src)` (Linux) or
964 // `dst\base(src)` (Windows).
965 func CopyFileWithTar(src, dst string) (err error) {
966 return defaultArchiver.CopyFileWithTar(src, dst)
969 // cmdStream executes a command, and returns its stdout as a stream.
970 // If the command fails to run or doesn't complete successfully, an error
971 // will be returned, including anything written on stderr.
972 func cmdStream(cmd *exec.Cmd, input io.Reader) (io.ReadCloser, <-chan struct{}, error) {
973 chdone := make(chan struct{})
975 pipeR, pipeW := io.Pipe()
977 var errBuf bytes.Buffer
980 // Run the command and return the pipe
981 if err := cmd.Start(); err != nil {
985 // Copy stdout to the returned pipe
987 if err := cmd.Wait(); err != nil {
988 pipeW.CloseWithError(fmt.Errorf("%s: %s", err, errBuf.String()))
995 return pipeR, chdone, nil
998 // NewTempArchive reads the content of src into a temporary file, and returns the contents
999 // of that file as an archive. The archive can only be read once - as soon as reading completes,
1000 // the file will be deleted.
1001 func NewTempArchive(src Archive, dir string) (*TempArchive, error) {
1002 f, err := ioutil.TempFile(dir, "")
1006 if _, err := io.Copy(f, src); err != nil {
1009 if _, err := f.Seek(0, 0); err != nil {
1017 return &TempArchive{File: f, Size: size}, nil
1020 // TempArchive is a temporary archive. The archive can only be read once - as soon as reading completes,
1021 // the file will be deleted.
1022 type TempArchive struct {
1024 Size int64 // Pre-computed from Stat().Size() as a convenience
1029 // Close closes the underlying file if it's still open, or does a no-op
1030 // to allow callers to try to close the TempArchive multiple times safely.
1031 func (archive *TempArchive) Close() error {
1036 archive.closed = true
1038 return archive.File.Close()
1041 func (archive *TempArchive) Read(data []byte) (int, error) {
1042 n, err := archive.File.Read(data)
1043 archive.read += int64(n)
1044 if err != nil || archive.read == archive.Size {
1046 os.Remove(archive.File.Name())