github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/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  
    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"
    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 io.Reader, options *TarOptions) (size int64, err error) {
    23      //fmt.Println("pkg/archive/diff.go  UnpackLayer()")
    24  	tr := tar.NewReader(layer)
    25  	trBuf := pools.BufioReader32KPool.Get(tr)
    26  	defer pools.BufioReader32KPool.Put(trBuf)
    27  
    28  	var dirs []*tar.Header
    29  	unpackedPaths := make(map[string]struct{})
    30  
    31  	if options == nil {
    32  		options = &TarOptions{}
    33  	}
    34  	if options.ExcludePatterns == nil {
    35  		options.ExcludePatterns = []string{}
    36  	}
    37  	remappedRootUID, remappedRootGID, err := idtools.GetRootUIDGID(options.UIDMaps, options.GIDMaps)
    38  	if err != nil {
    39  		return 0, err
    40  	}
    41  
    42      //fmt.Println("pkg/archive/diff.go  UnpackLayer() after getrootuidgid")
    43  	aufsTempdir := ""
    44  	aufsHardlinks := make(map[string]*tar.Header)
    45  
    46  	if options == nil {
    47  		options = &TarOptions{}
    48  	}
    49  	// Iterate through the files in the archive.
    50  	for {
    51  		hdr, err := tr.Next()
    52  		if err == io.EOF {
    53  			// end of tar archive
    54  			break
    55  		}
    56  		if err != nil {
    57  			return 0, err
    58  		}
    59  
    60  		size += hdr.Size
    61  
    62  		// Normalize name, for safety and for a simple is-root check
    63  		hdr.Name = filepath.Clean(hdr.Name)
    64  
    65  		// Windows does not support filenames with colons in them. Ignore
    66  		// these files. This is not a problem though (although it might
    67  		// appear that it is). Let's suppose a client is running docker pull.
    68  		// The daemon it points to is Windows. Would it make sense for the
    69  		// client to be doing a docker pull Ubuntu for example (which has files
    70  		// with colons in the name under /usr/share/man/man3)? No, absolutely
    71  		// not as it would really only make sense that they were pulling a
    72  		// Windows image. However, for development, it is necessary to be able
    73  		// to pull Linux images which are in the repository.
    74  		//
    75  		// TODO Windows. Once the registry is aware of what images are Windows-
    76  		// specific or Linux-specific, this warning should be changed to an error
    77  		// to cater for the situation where someone does manage to upload a Linux
    78  		// image but have it tagged as Windows inadvertently.
    79  		if runtime.GOOS == "windows" {
    80  			if strings.Contains(hdr.Name, ":") {
    81  				logrus.Warnf("Windows: Ignoring %s (is this a Linux image?)", hdr.Name)
    82  				continue
    83  			}
    84  		}
    85  
    86  		// Note as these operations are platform specific, so must the slash be.
    87  		if !strings.HasSuffix(hdr.Name, string(os.PathSeparator)) {
    88  			// Not the root directory, ensure that the parent directory exists.
    89  			// This happened in some tests where an image had a tarfile without any
    90  			// parent directories.
    91  			parent := filepath.Dir(hdr.Name)
    92  			parentPath := filepath.Join(dest, parent)
    93  
    94  			if _, err := os.Lstat(parentPath); err != nil && os.IsNotExist(err) {
    95  				err = system.MkdirAll(parentPath, 0600)
    96  				if err != nil {
    97  					return 0, err
    98  				}
    99  			}
   100  		}
   101  
   102      //fmt.Println("pkg/archive/diff.go  UnpackLayer() after if{}")
   103  		// Skip AUFS metadata dirs
   104  		if strings.HasPrefix(hdr.Name, WhiteoutMetaPrefix) {
   105  			// Regular files inside /.wh..wh.plnk can be used as hardlink targets
   106  			// We don't want this directory, but we need the files in them so that
   107  			// such hardlinks can be resolved.
   108  			if strings.HasPrefix(hdr.Name, WhiteoutLinkDir) && hdr.Typeflag == tar.TypeReg {
   109  				basename := filepath.Base(hdr.Name)
   110  				aufsHardlinks[basename] = hdr
   111  				if aufsTempdir == "" {
   112  					if aufsTempdir, err = ioutil.TempDir("", "dockerplnk"); err != nil {
   113  						return 0, err
   114  					}
   115  					defer os.RemoveAll(aufsTempdir)
   116  				}
   117  				if err := createTarFile(filepath.Join(aufsTempdir, basename), dest, hdr, tr, true, nil, options.InUserNS); err != nil {
   118  					return 0, err
   119  				}
   120  			}
   121  
   122  			if hdr.Name != WhiteoutOpaqueDir {
   123  				continue
   124  			}
   125  		}
   126  		path := filepath.Join(dest, hdr.Name)
   127  		rel, err := filepath.Rel(dest, path)
   128  		if err != nil {
   129  			return 0, err
   130  		}
   131  
   132      //fmt.Println("pkg/archive/diff.go  UnpackLayer() after hasprefix()")
   133  		// Note as these operations are platform specific, so must the slash be.
   134  		if strings.HasPrefix(rel, ".."+string(os.PathSeparator)) {
   135  			return 0, breakoutError(fmt.Errorf("%q is outside of %q", hdr.Name, dest))
   136  		}
   137  		base := filepath.Base(path)
   138  
   139  		if strings.HasPrefix(base, WhiteoutPrefix) {
   140  			dir := filepath.Dir(path)
   141  			if base == WhiteoutOpaqueDir {
   142  				_, err := os.Lstat(dir)
   143  				if err != nil {
   144  					return 0, err
   145  				}
   146  				err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
   147  					if err != nil {
   148  						if os.IsNotExist(err) {
   149  							err = nil // parent was deleted
   150  						}
   151  						return err
   152  					}
   153  					if path == dir {
   154  						return nil
   155  					}
   156  					if _, exists := unpackedPaths[path]; !exists {
   157  						err := os.RemoveAll(path)
   158  						return err
   159  					}
   160  					return nil
   161  				})
   162  				if err != nil {
   163  					return 0, err
   164  				}
   165  			} else {
   166  				originalBase := base[len(WhiteoutPrefix):]
   167  				originalPath := filepath.Join(dir, originalBase)
   168  				if err := os.RemoveAll(originalPath); err != nil {
   169  					return 0, err
   170  				}
   171  			}
   172  		} else {
   173      //fmt.Println("pkg/archive/diff.go  UnpackLayer() before elselastat")
   174  			// If path exits we almost always just want to remove and replace it.
   175  			// The only exception is when it is a directory *and* the file from
   176  			// the layer is also a directory. Then we want to merge them (i.e.
   177  			// just apply the metadata from the layer).
   178  			if fi, err := os.Lstat(path); err == nil {
   179  				if !(fi.IsDir() && hdr.Typeflag == tar.TypeDir) {
   180  					if err := os.RemoveAll(path); err != nil {
   181  						return 0, err
   182  					}
   183  				}
   184  			}
   185  
   186  			trBuf.Reset(tr)
   187  			srcData := io.Reader(trBuf)
   188  			srcHdr := hdr
   189  
   190  			// Hard links into /.wh..wh.plnk don't work, as we don't extract that directory, so
   191  			// we manually retarget these into the temporary files we extracted them into
   192  			if hdr.Typeflag == tar.TypeLink && strings.HasPrefix(filepath.Clean(hdr.Linkname), WhiteoutLinkDir) {
   193  				linkBasename := filepath.Base(hdr.Linkname)
   194  				srcHdr = aufsHardlinks[linkBasename]
   195  				if srcHdr == nil {
   196  					return 0, fmt.Errorf("Invalid aufs hardlink")
   197  				}
   198  				tmpFile, err := os.Open(filepath.Join(aufsTempdir, linkBasename))
   199  				if err != nil {
   200  					return 0, err
   201  				}
   202  				defer tmpFile.Close()
   203  				srcData = tmpFile
   204  			}
   205  
   206  			// if the options contain a uid & gid maps, convert header uid/gid
   207  			// entries using the maps such that lchown sets the proper mapped
   208  			// uid/gid after writing the file. We only perform this mapping if
   209  			// the file isn't already owned by the remapped root UID or GID, as
   210  			// that specific uid/gid has no mapping from container -> host, and
   211  			// those files already have the proper ownership for inside the
   212  			// container.
   213  			if srcHdr.Uid != remappedRootUID {
   214  				xUID, err := idtools.ToHost(srcHdr.Uid, options.UIDMaps)
   215  				if err != nil {
   216  					return 0, err
   217  				}
   218  				srcHdr.Uid = xUID
   219  			}
   220  			if srcHdr.Gid != remappedRootGID {
   221  				xGID, err := idtools.ToHost(srcHdr.Gid, options.GIDMaps)
   222  				if err != nil {
   223  					return 0, err
   224  				}
   225  				srcHdr.Gid = xGID
   226  			}
   227  			if err := createTarFile(path, dest, srcHdr, srcData, true, nil, options.InUserNS); err != nil {
   228  				return 0, err
   229  			}
   230  
   231  			// Directory mtimes must be handled at the end to avoid further
   232  			// file creation in them to modify the directory mtime
   233  			if hdr.Typeflag == tar.TypeDir {
   234  				dirs = append(dirs, hdr)
   235  			}
   236  			unpackedPaths[path] = struct{}{}
   237  		}
   238  	}
   239  
   240      //fmt.Println("pkg/archive/diff.go  UnpackLayer() before hadrfor")
   241  	for _, hdr := range dirs {
   242  		path := filepath.Join(dest, hdr.Name)
   243  		if err := system.Chtimes(path, hdr.AccessTime, hdr.ModTime); err != nil {
   244  			return 0, err
   245  		}
   246  	}
   247  
   248  	return size, nil
   249  }
   250  
   251  // ApplyLayer parses a diff in the standard layer format from `layer`,
   252  // and applies it to the directory `dest`. The stream `layer` can be
   253  // compressed or uncompressed.
   254  // Returns the size in bytes of the contents of the layer.
   255  func ApplyLayer(dest string, layer io.Reader) (int64, error) {
   256  	return applyLayerHandler(dest, layer, &TarOptions{}, true)
   257  }
   258  
   259  // ApplyUncompressedLayer parses a diff in the standard layer format from
   260  // `layer`, and applies it to the directory `dest`. The stream `layer`
   261  // can only be uncompressed.
   262  // Returns the size in bytes of the contents of the layer.
   263  func ApplyUncompressedLayer(dest string, layer io.Reader, options *TarOptions) (int64, error) {
   264  	return applyLayerHandler(dest, layer, options, false)
   265  }
   266  
   267  // do the bulk load of ApplyLayer, but allow for not calling DecompressStream
   268  func applyLayerHandler(dest string, layer io.Reader, options *TarOptions, decompress bool) (int64, error) {
   269      fmt.Println("pkg/archive/diff.go  applyLayerHandler()")
   270  	dest = filepath.Clean(dest)
   271  
   272  	// We need to be able to set any perms
   273  	oldmask, err := system.Umask(0)
   274  	if err != nil {
   275  		return 0, err
   276  	}
   277  	defer system.Umask(oldmask) // ignore err, ErrNotSupportedPlatform
   278  
   279      fmt.Println("pkg/archive/diff.go  applyLayerHandler() before decompress")
   280  	if decompress {
   281  		layer, err = DecompressStream(layer)
   282      fmt.Println("pkg/archive/diff.go  applyLayerHandler() decompressing")
   283  		if err != nil {
   284  			return 0, err
   285  		}
   286  	}
   287      fmt.Println("pkg/archive/diff.go  applyLayerHandler() decompressed")
   288  	return UnpackLayer(dest, layer, options)
   289  }