13 "github.com/Sirupsen/logrus"
14 "github.com/docker/docker/pkg/idtools"
15 "github.com/docker/docker/pkg/pools"
16 "github.com/docker/docker/pkg/system"
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 io.Reader, options *TarOptions) (size int64, err error) {
23 tr := tar.NewReader(layer)
24 trBuf := pools.BufioReader32KPool.Get(tr)
25 defer pools.BufioReader32KPool.Put(trBuf)
27 var dirs []*tar.Header
28 unpackedPaths := make(map[string]struct{})
31 options = &TarOptions{}
33 if options.ExcludePatterns == nil {
34 options.ExcludePatterns = []string{}
36 idMappings := idtools.NewIDMappingsFromMaps(options.UIDMaps, options.GIDMaps)
39 aufsHardlinks := make(map[string]*tar.Header)
41 // Iterate through the files in the archive.
54 // Normalize name, for safety and for a simple is-root check
55 hdr.Name = filepath.Clean(hdr.Name)
57 // Windows does not support filenames with colons in them. Ignore
58 // these files. This is not a problem though (although it might
59 // appear that it is). Let's suppose a client is running docker pull.
60 // The daemon it points to is Windows. Would it make sense for the
61 // client to be doing a docker pull Ubuntu for example (which has files
62 // with colons in the name under /usr/share/man/man3)? No, absolutely
63 // not as it would really only make sense that they were pulling a
64 // Windows image. However, for development, it is necessary to be able
65 // to pull Linux images which are in the repository.
67 // TODO Windows. Once the registry is aware of what images are Windows-
68 // specific or Linux-specific, this warning should be changed to an error
69 // to cater for the situation where someone does manage to upload a Linux
70 // image but have it tagged as Windows inadvertently.
71 if runtime.GOOS == "windows" {
72 if strings.Contains(hdr.Name, ":") {
73 logrus.Warnf("Windows: Ignoring %s (is this a Linux image?)", hdr.Name)
78 // Note as these operations are platform specific, so must the slash be.
79 if !strings.HasSuffix(hdr.Name, string(os.PathSeparator)) {
80 // Not the root directory, ensure that the parent directory exists.
81 // This happened in some tests where an image had a tarfile without any
82 // parent directories.
83 parent := filepath.Dir(hdr.Name)
84 parentPath := filepath.Join(dest, parent)
86 if _, err := os.Lstat(parentPath); err != nil && os.IsNotExist(err) {
87 err = system.MkdirAll(parentPath, 0600, "")
94 // Skip AUFS metadata dirs
95 if strings.HasPrefix(hdr.Name, WhiteoutMetaPrefix) {
96 // Regular files inside /.wh..wh.plnk can be used as hardlink targets
97 // We don't want this directory, but we need the files in them so that
98 // such hardlinks can be resolved.
99 if strings.HasPrefix(hdr.Name, WhiteoutLinkDir) && hdr.Typeflag == tar.TypeReg {
100 basename := filepath.Base(hdr.Name)
101 aufsHardlinks[basename] = hdr
102 if aufsTempdir == "" {
103 if aufsTempdir, err = ioutil.TempDir("", "dockerplnk"); err != nil {
106 defer os.RemoveAll(aufsTempdir)
108 if err := createTarFile(filepath.Join(aufsTempdir, basename), dest, hdr, tr, true, nil, options.InUserNS); err != nil {
113 if hdr.Name != WhiteoutOpaqueDir {
117 path := filepath.Join(dest, hdr.Name)
118 rel, err := filepath.Rel(dest, path)
123 // Note as these operations are platform specific, so must the slash be.
124 if strings.HasPrefix(rel, ".."+string(os.PathSeparator)) {
125 return 0, breakoutError(fmt.Errorf("%q is outside of %q", hdr.Name, dest))
127 base := filepath.Base(path)
129 if strings.HasPrefix(base, WhiteoutPrefix) {
130 dir := filepath.Dir(path)
131 if base == WhiteoutOpaqueDir {
132 _, err := os.Lstat(dir)
136 err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
138 if os.IsNotExist(err) {
139 err = nil // parent was deleted
146 if _, exists := unpackedPaths[path]; !exists {
147 err := os.RemoveAll(path)
156 originalBase := base[len(WhiteoutPrefix):]
157 originalPath := filepath.Join(dir, originalBase)
158 if err := os.RemoveAll(originalPath); err != nil {
163 // If path exits we almost always just want to remove and replace it.
164 // The only exception is when it is a directory *and* the file from
165 // the layer is also a directory. Then we want to merge them (i.e.
166 // just apply the metadata from the layer).
167 if fi, err := os.Lstat(path); err == nil {
168 if !(fi.IsDir() && hdr.Typeflag == tar.TypeDir) {
169 if err := os.RemoveAll(path); err != nil {
176 srcData := io.Reader(trBuf)
179 // Hard links into /.wh..wh.plnk don't work, as we don't extract that directory, so
180 // we manually retarget these into the temporary files we extracted them into
181 if hdr.Typeflag == tar.TypeLink && strings.HasPrefix(filepath.Clean(hdr.Linkname), WhiteoutLinkDir) {
182 linkBasename := filepath.Base(hdr.Linkname)
183 srcHdr = aufsHardlinks[linkBasename]
185 return 0, fmt.Errorf("Invalid aufs hardlink")
187 tmpFile, err := os.Open(filepath.Join(aufsTempdir, linkBasename))
191 defer tmpFile.Close()
195 if err := remapIDs(idMappings, srcHdr); err != nil {
199 if err := createTarFile(path, dest, srcHdr, srcData, true, nil, options.InUserNS); err != nil {
203 // Directory mtimes must be handled at the end to avoid further
204 // file creation in them to modify the directory mtime
205 if hdr.Typeflag == tar.TypeDir {
206 dirs = append(dirs, hdr)
208 unpackedPaths[path] = struct{}{}
212 for _, hdr := range dirs {
213 path := filepath.Join(dest, hdr.Name)
214 if err := system.Chtimes(path, hdr.AccessTime, hdr.ModTime); err != nil {
222 // ApplyLayer parses a diff in the standard layer format from `layer`,
223 // and applies it to the directory `dest`. The stream `layer` can be
224 // compressed or uncompressed.
225 // Returns the size in bytes of the contents of the layer.
226 func ApplyLayer(dest string, layer io.Reader) (int64, error) {
227 return applyLayerHandler(dest, layer, &TarOptions{}, true)
230 // ApplyUncompressedLayer parses a diff in the standard layer format from
231 // `layer`, and applies it to the directory `dest`. The stream `layer`
232 // can only be uncompressed.
233 // Returns the size in bytes of the contents of the layer.
234 func ApplyUncompressedLayer(dest string, layer io.Reader, options *TarOptions) (int64, error) {
235 return applyLayerHandler(dest, layer, options, false)
238 // do the bulk load of ApplyLayer, but allow for not calling DecompressStream
239 func applyLayerHandler(dest string, layer io.Reader, options *TarOptions, decompress bool) (int64, error) {
240 dest = filepath.Clean(dest)
242 // We need to be able to set any perms
243 oldmask, err := system.Umask(0)
247 defer system.Umask(oldmask) // ignore err, ErrNotSupportedPlatform
250 layer, err = DecompressStream(layer)
255 return UnpackLayer(dest, layer, options)