github.com/rsampaio/docker@v0.7.2-0.20150827203920-fdc73cc3fc31/pkg/archive/diff.go (about)

     1  package archive
     2  
     3  import (
     4  	"archive/tar"
     5  	"fmt"
     6  	"io"
     7  	"io/ioutil"
     8  	"os"
     9  	"path/filepath"
    10  	"runtime"
    11  	"strings"
    12  	"syscall"
    13  
    14  	"github.com/Sirupsen/logrus"
    15  	"github.com/docker/docker/pkg/pools"
    16  	"github.com/docker/docker/pkg/system"
    17  )
    18  
    19  // UnpackLayer unpack `layer` to a `dest`. The stream `layer` can be
    20  // compressed or uncompressed.
    21  // Returns the size in bytes of the contents of the layer.
    22  func UnpackLayer(dest string, layer Reader) (size int64, err error) {
    23  	tr := tar.NewReader(layer)
    24  	trBuf := pools.BufioReader32KPool.Get(tr)
    25  	defer pools.BufioReader32KPool.Put(trBuf)
    26  
    27  	var dirs []*tar.Header
    28  
    29  	aufsTempdir := ""
    30  	aufsHardlinks := make(map[string]*tar.Header)
    31  
    32  	// Iterate through the files in the archive.
    33  	for {
    34  		hdr, err := tr.Next()
    35  		if err == io.EOF {
    36  			// end of tar archive
    37  			break
    38  		}
    39  		if err != nil {
    40  			return 0, err
    41  		}
    42  
    43  		size += hdr.Size
    44  
    45  		// Normalize name, for safety and for a simple is-root check
    46  		hdr.Name = filepath.Clean(hdr.Name)
    47  
    48  		// Windows does not support filenames with colons in them. Ignore
    49  		// these files. This is not a problem though (although it might
    50  		// appear that it is). Let's suppose a client is running docker pull.
    51  		// The daemon it points to is Windows. Would it make sense for the
    52  		// client to be doing a docker pull Ubuntu for example (which has files
    53  		// with colons in the name under /usr/share/man/man3)? No, absolutely
    54  		// not as it would really only make sense that they were pulling a
    55  		// Windows image. However, for development, it is necessary to be able
    56  		// to pull Linux images which are in the repository.
    57  		//
    58  		// TODO Windows. Once the registry is aware of what images are Windows-
    59  		// specific or Linux-specific, this warning should be changed to an error
    60  		// to cater for the situation where someone does manage to upload a Linux
    61  		// image but have it tagged as Windows inadvertently.
    62  		if runtime.GOOS == "windows" {
    63  			if strings.Contains(hdr.Name, ":") {
    64  				logrus.Warnf("Windows: Ignoring %s (is this a Linux image?)", hdr.Name)
    65  				continue
    66  			}
    67  		}
    68  
    69  		// Note as these operations are platform specific, so must the slash be.
    70  		if !strings.HasSuffix(hdr.Name, string(os.PathSeparator)) {
    71  			// Not the root directory, ensure that the parent directory exists.
    72  			// This happened in some tests where an image had a tarfile without any
    73  			// parent directories.
    74  			parent := filepath.Dir(hdr.Name)
    75  			parentPath := filepath.Join(dest, parent)
    76  
    77  			if _, err := os.Lstat(parentPath); err != nil && os.IsNotExist(err) {
    78  				err = system.MkdirAll(parentPath, 0600)
    79  				if err != nil {
    80  					return 0, err
    81  				}
    82  			}
    83  		}
    84  
    85  		// Skip AUFS metadata dirs
    86  		if strings.HasPrefix(hdr.Name, ".wh..wh.") {
    87  			// Regular files inside /.wh..wh.plnk can be used as hardlink targets
    88  			// We don't want this directory, but we need the files in them so that
    89  			// such hardlinks can be resolved.
    90  			if strings.HasPrefix(hdr.Name, ".wh..wh.plnk") && hdr.Typeflag == tar.TypeReg {
    91  				basename := filepath.Base(hdr.Name)
    92  				aufsHardlinks[basename] = hdr
    93  				if aufsTempdir == "" {
    94  					if aufsTempdir, err = ioutil.TempDir("", "dockerplnk"); err != nil {
    95  						return 0, err
    96  					}
    97  					defer os.RemoveAll(aufsTempdir)
    98  				}
    99  				if err := createTarFile(filepath.Join(aufsTempdir, basename), dest, hdr, tr, true, nil); err != nil {
   100  					return 0, err
   101  				}
   102  			}
   103  			continue
   104  		}
   105  		path := filepath.Join(dest, hdr.Name)
   106  		rel, err := filepath.Rel(dest, path)
   107  		if err != nil {
   108  			return 0, err
   109  		}
   110  
   111  		// Note as these operations are platform specific, so must the slash be.
   112  		if strings.HasPrefix(rel, ".."+string(os.PathSeparator)) {
   113  			return 0, breakoutError(fmt.Errorf("%q is outside of %q", hdr.Name, dest))
   114  		}
   115  		base := filepath.Base(path)
   116  
   117  		if strings.HasPrefix(base, ".wh.") {
   118  			originalBase := base[len(".wh."):]
   119  			originalPath := filepath.Join(filepath.Dir(path), originalBase)
   120  			if err := os.RemoveAll(originalPath); err != nil {
   121  				return 0, err
   122  			}
   123  		} else {
   124  			// If path exits we almost always just want to remove and replace it.
   125  			// The only exception is when it is a directory *and* the file from
   126  			// the layer is also a directory. Then we want to merge them (i.e.
   127  			// just apply the metadata from the layer).
   128  			if fi, err := os.Lstat(path); err == nil {
   129  				if !(fi.IsDir() && hdr.Typeflag == tar.TypeDir) {
   130  					if err := os.RemoveAll(path); err != nil {
   131  						return 0, err
   132  					}
   133  				}
   134  			}
   135  
   136  			trBuf.Reset(tr)
   137  			srcData := io.Reader(trBuf)
   138  			srcHdr := hdr
   139  
   140  			// Hard links into /.wh..wh.plnk don't work, as we don't extract that directory, so
   141  			// we manually retarget these into the temporary files we extracted them into
   142  			if hdr.Typeflag == tar.TypeLink && strings.HasPrefix(filepath.Clean(hdr.Linkname), ".wh..wh.plnk") {
   143  				linkBasename := filepath.Base(hdr.Linkname)
   144  				srcHdr = aufsHardlinks[linkBasename]
   145  				if srcHdr == nil {
   146  					return 0, fmt.Errorf("Invalid aufs hardlink")
   147  				}
   148  				tmpFile, err := os.Open(filepath.Join(aufsTempdir, linkBasename))
   149  				if err != nil {
   150  					return 0, err
   151  				}
   152  				defer tmpFile.Close()
   153  				srcData = tmpFile
   154  			}
   155  
   156  			if err := createTarFile(path, dest, srcHdr, srcData, true, nil); err != nil {
   157  				return 0, err
   158  			}
   159  
   160  			// Directory mtimes must be handled at the end to avoid further
   161  			// file creation in them to modify the directory mtime
   162  			if hdr.Typeflag == tar.TypeDir {
   163  				dirs = append(dirs, hdr)
   164  			}
   165  		}
   166  	}
   167  
   168  	for _, hdr := range dirs {
   169  		path := filepath.Join(dest, hdr.Name)
   170  		ts := []syscall.Timespec{timeToTimespec(hdr.AccessTime), timeToTimespec(hdr.ModTime)}
   171  		if err := syscall.UtimesNano(path, ts); err != nil {
   172  			return 0, err
   173  		}
   174  	}
   175  
   176  	return size, nil
   177  }
   178  
   179  // ApplyLayer parses a diff in the standard layer format from `layer`,
   180  // and applies it to the directory `dest`. The stream `layer` can be
   181  // compressed or uncompressed.
   182  // Returns the size in bytes of the contents of the layer.
   183  func ApplyLayer(dest string, layer Reader) (int64, error) {
   184  	return applyLayerHandler(dest, layer, true)
   185  }
   186  
   187  // ApplyUncompressedLayer parses a diff in the standard layer format from
   188  // `layer`, and applies it to the directory `dest`. The stream `layer`
   189  // can only be uncompressed.
   190  // Returns the size in bytes of the contents of the layer.
   191  func ApplyUncompressedLayer(dest string, layer Reader) (int64, error) {
   192  	return applyLayerHandler(dest, layer, false)
   193  }
   194  
   195  // do the bulk load of ApplyLayer, but allow for not calling DecompressStream
   196  func applyLayerHandler(dest string, layer Reader, decompress bool) (int64, error) {
   197  	dest = filepath.Clean(dest)
   198  
   199  	// We need to be able to set any perms
   200  	oldmask, err := system.Umask(0)
   201  	if err != nil {
   202  		return 0, err
   203  	}
   204  	defer system.Umask(oldmask) // ignore err, ErrNotSupportedPlatform
   205  
   206  	if decompress {
   207  		layer, err = DecompressStream(layer)
   208  		if err != nil {
   209  			return 0, err
   210  		}
   211  	}
   212  	return UnpackLayer(dest, layer)
   213  }