github.com/codemac/docker@v1.2.1-0.20150518222241-6a18412d5b9c/pkg/archive/changes.go (about)

     1  package archive
     2  
     3  import (
     4  	"archive/tar"
     5  	"bytes"
     6  	"fmt"
     7  	"io"
     8  	"os"
     9  	"path/filepath"
    10  	"sort"
    11  	"strings"
    12  	"syscall"
    13  	"time"
    14  
    15  	"github.com/Sirupsen/logrus"
    16  	"github.com/docker/docker/pkg/pools"
    17  	"github.com/docker/docker/pkg/system"
    18  )
    19  
    20  type ChangeType int
    21  
    22  const (
    23  	ChangeModify = iota
    24  	ChangeAdd
    25  	ChangeDelete
    26  )
    27  
    28  type Change struct {
    29  	Path string
    30  	Kind ChangeType
    31  }
    32  
    33  func (change *Change) String() string {
    34  	var kind string
    35  	switch change.Kind {
    36  	case ChangeModify:
    37  		kind = "C"
    38  	case ChangeAdd:
    39  		kind = "A"
    40  	case ChangeDelete:
    41  		kind = "D"
    42  	}
    43  	return fmt.Sprintf("%s %s", kind, change.Path)
    44  }
    45  
    46  // for sort.Sort
    47  type changesByPath []Change
    48  
    49  func (c changesByPath) Less(i, j int) bool { return c[i].Path < c[j].Path }
    50  func (c changesByPath) Len() int           { return len(c) }
    51  func (c changesByPath) Swap(i, j int)      { c[j], c[i] = c[i], c[j] }
    52  
    53  // Gnu tar and the go tar writer don't have sub-second mtime
    54  // precision, which is problematic when we apply changes via tar
    55  // files, we handle this by comparing for exact times, *or* same
    56  // second count and either a or b having exactly 0 nanoseconds
    57  func sameFsTime(a, b time.Time) bool {
    58  	return a == b ||
    59  		(a.Unix() == b.Unix() &&
    60  			(a.Nanosecond() == 0 || b.Nanosecond() == 0))
    61  }
    62  
    63  func sameFsTimeSpec(a, b syscall.Timespec) bool {
    64  	return a.Sec == b.Sec &&
    65  		(a.Nsec == b.Nsec || a.Nsec == 0 || b.Nsec == 0)
    66  }
    67  
    68  // Changes walks the path rw and determines changes for the files in the path,
    69  // with respect to the parent layers
    70  func Changes(layers []string, rw string) ([]Change, error) {
    71  	var changes []Change
    72  	err := filepath.Walk(rw, func(path string, f os.FileInfo, err error) error {
    73  		if err != nil {
    74  			return err
    75  		}
    76  
    77  		// Rebase path
    78  		path, err = filepath.Rel(rw, path)
    79  		if err != nil {
    80  			return err
    81  		}
    82  		path = filepath.Join("/", path)
    83  
    84  		// Skip root
    85  		if path == "/" {
    86  			return nil
    87  		}
    88  
    89  		// Skip AUFS metadata
    90  		if matched, err := filepath.Match("/.wh..wh.*", path); err != nil || matched {
    91  			return err
    92  		}
    93  
    94  		change := Change{
    95  			Path: path,
    96  		}
    97  
    98  		// Find out what kind of modification happened
    99  		file := filepath.Base(path)
   100  		// If there is a whiteout, then the file was removed
   101  		if strings.HasPrefix(file, ".wh.") {
   102  			originalFile := file[len(".wh."):]
   103  			change.Path = filepath.Join(filepath.Dir(path), originalFile)
   104  			change.Kind = ChangeDelete
   105  		} else {
   106  			// Otherwise, the file was added
   107  			change.Kind = ChangeAdd
   108  
   109  			// ...Unless it already existed in a top layer, in which case, it's a modification
   110  			for _, layer := range layers {
   111  				stat, err := os.Stat(filepath.Join(layer, path))
   112  				if err != nil && !os.IsNotExist(err) {
   113  					return err
   114  				}
   115  				if err == nil {
   116  					// The file existed in the top layer, so that's a modification
   117  
   118  					// However, if it's a directory, maybe it wasn't actually modified.
   119  					// If you modify /foo/bar/baz, then /foo will be part of the changed files only because it's the parent of bar
   120  					if stat.IsDir() && f.IsDir() {
   121  						if f.Size() == stat.Size() && f.Mode() == stat.Mode() && sameFsTime(f.ModTime(), stat.ModTime()) {
   122  							// Both directories are the same, don't record the change
   123  							return nil
   124  						}
   125  					}
   126  					change.Kind = ChangeModify
   127  					break
   128  				}
   129  			}
   130  		}
   131  
   132  		// Record change
   133  		changes = append(changes, change)
   134  		return nil
   135  	})
   136  	if err != nil && !os.IsNotExist(err) {
   137  		return nil, err
   138  	}
   139  	return changes, nil
   140  }
   141  
   142  type FileInfo struct {
   143  	parent     *FileInfo
   144  	name       string
   145  	stat       *system.Stat_t
   146  	children   map[string]*FileInfo
   147  	capability []byte
   148  	added      bool
   149  }
   150  
   151  func (root *FileInfo) LookUp(path string) *FileInfo {
   152  	parent := root
   153  	if path == "/" {
   154  		return root
   155  	}
   156  
   157  	pathElements := strings.Split(path, "/")
   158  	for _, elem := range pathElements {
   159  		if elem != "" {
   160  			child := parent.children[elem]
   161  			if child == nil {
   162  				return nil
   163  			}
   164  			parent = child
   165  		}
   166  	}
   167  	return parent
   168  }
   169  
   170  func (info *FileInfo) path() string {
   171  	if info.parent == nil {
   172  		return "/"
   173  	}
   174  	return filepath.Join(info.parent.path(), info.name)
   175  }
   176  
   177  func (info *FileInfo) isDir() bool {
   178  	return info.parent == nil || info.stat.Mode()&syscall.S_IFDIR != 0
   179  }
   180  
   181  func (info *FileInfo) addChanges(oldInfo *FileInfo, changes *[]Change) {
   182  
   183  	sizeAtEntry := len(*changes)
   184  
   185  	if oldInfo == nil {
   186  		// add
   187  		change := Change{
   188  			Path: info.path(),
   189  			Kind: ChangeAdd,
   190  		}
   191  		*changes = append(*changes, change)
   192  		info.added = true
   193  	}
   194  
   195  	// We make a copy so we can modify it to detect additions
   196  	// also, we only recurse on the old dir if the new info is a directory
   197  	// otherwise any previous delete/change is considered recursive
   198  	oldChildren := make(map[string]*FileInfo)
   199  	if oldInfo != nil && info.isDir() {
   200  		for k, v := range oldInfo.children {
   201  			oldChildren[k] = v
   202  		}
   203  	}
   204  
   205  	for name, newChild := range info.children {
   206  		oldChild, _ := oldChildren[name]
   207  		if oldChild != nil {
   208  			// change?
   209  			oldStat := oldChild.stat
   210  			newStat := newChild.stat
   211  			// Note: We can't compare inode or ctime or blocksize here, because these change
   212  			// when copying a file into a container. However, that is not generally a problem
   213  			// because any content change will change mtime, and any status change should
   214  			// be visible when actually comparing the stat fields. The only time this
   215  			// breaks down is if some code intentionally hides a change by setting
   216  			// back mtime
   217  			if oldStat.Mode() != newStat.Mode() ||
   218  				oldStat.Uid() != newStat.Uid() ||
   219  				oldStat.Gid() != newStat.Gid() ||
   220  				oldStat.Rdev() != newStat.Rdev() ||
   221  				// Don't look at size for dirs, its not a good measure of change
   222  				(oldStat.Mode()&syscall.S_IFDIR != syscall.S_IFDIR &&
   223  					(!sameFsTimeSpec(oldStat.Mtim(), newStat.Mtim()) || (oldStat.Size() != newStat.Size()))) ||
   224  				bytes.Compare(oldChild.capability, newChild.capability) != 0 {
   225  				change := Change{
   226  					Path: newChild.path(),
   227  					Kind: ChangeModify,
   228  				}
   229  				*changes = append(*changes, change)
   230  				newChild.added = true
   231  			}
   232  
   233  			// Remove from copy so we can detect deletions
   234  			delete(oldChildren, name)
   235  		}
   236  
   237  		newChild.addChanges(oldChild, changes)
   238  	}
   239  	for _, oldChild := range oldChildren {
   240  		// delete
   241  		change := Change{
   242  			Path: oldChild.path(),
   243  			Kind: ChangeDelete,
   244  		}
   245  		*changes = append(*changes, change)
   246  	}
   247  
   248  	// If there were changes inside this directory, we need to add it, even if the directory
   249  	// itself wasn't changed. This is needed to properly save and restore filesystem permissions.
   250  	if len(*changes) > sizeAtEntry && info.isDir() && !info.added && info.path() != "/" {
   251  		change := Change{
   252  			Path: info.path(),
   253  			Kind: ChangeModify,
   254  		}
   255  		// Let's insert the directory entry before the recently added entries located inside this dir
   256  		*changes = append(*changes, change) // just to resize the slice, will be overwritten
   257  		copy((*changes)[sizeAtEntry+1:], (*changes)[sizeAtEntry:])
   258  		(*changes)[sizeAtEntry] = change
   259  	}
   260  
   261  }
   262  
   263  func (info *FileInfo) Changes(oldInfo *FileInfo) []Change {
   264  	var changes []Change
   265  
   266  	info.addChanges(oldInfo, &changes)
   267  
   268  	return changes
   269  }
   270  
   271  func newRootFileInfo() *FileInfo {
   272  	root := &FileInfo{
   273  		name:     "/",
   274  		children: make(map[string]*FileInfo),
   275  	}
   276  	return root
   277  }
   278  
   279  func collectFileInfo(sourceDir string) (*FileInfo, error) {
   280  	root := newRootFileInfo()
   281  
   282  	err := filepath.Walk(sourceDir, func(path string, f os.FileInfo, err error) error {
   283  		if err != nil {
   284  			return err
   285  		}
   286  
   287  		// Rebase path
   288  		relPath, err := filepath.Rel(sourceDir, path)
   289  		if err != nil {
   290  			return err
   291  		}
   292  		relPath = filepath.Join("/", relPath)
   293  
   294  		if relPath == "/" {
   295  			return nil
   296  		}
   297  
   298  		parent := root.LookUp(filepath.Dir(relPath))
   299  		if parent == nil {
   300  			return fmt.Errorf("collectFileInfo: Unexpectedly no parent for %s", relPath)
   301  		}
   302  
   303  		info := &FileInfo{
   304  			name:     filepath.Base(relPath),
   305  			children: make(map[string]*FileInfo),
   306  			parent:   parent,
   307  		}
   308  
   309  		s, err := system.Lstat(path)
   310  		if err != nil {
   311  			return err
   312  		}
   313  		info.stat = s
   314  
   315  		info.capability, _ = system.Lgetxattr(path, "security.capability")
   316  
   317  		parent.children[info.name] = info
   318  
   319  		return nil
   320  	})
   321  	if err != nil {
   322  		return nil, err
   323  	}
   324  	return root, nil
   325  }
   326  
   327  // ChangesDirs compares two directories and generates an array of Change objects describing the changes.
   328  // If oldDir is "", then all files in newDir will be Add-Changes.
   329  func ChangesDirs(newDir, oldDir string) ([]Change, error) {
   330  	var (
   331  		oldRoot, newRoot *FileInfo
   332  		err1, err2       error
   333  		errs             = make(chan error, 2)
   334  	)
   335  	go func() {
   336  		if oldDir != "" {
   337  			oldRoot, err1 = collectFileInfo(oldDir)
   338  		}
   339  		errs <- err1
   340  	}()
   341  	go func() {
   342  		newRoot, err2 = collectFileInfo(newDir)
   343  		errs <- err2
   344  	}()
   345  
   346  	// block until both routines have returned
   347  	for i := 0; i < 2; i++ {
   348  		if err := <-errs; err != nil {
   349  			return nil, err
   350  		}
   351  	}
   352  
   353  	return newRoot.Changes(oldRoot), nil
   354  }
   355  
   356  // ChangesSize calculates the size in bytes of the provided changes, based on newDir.
   357  func ChangesSize(newDir string, changes []Change) int64 {
   358  	var size int64
   359  	for _, change := range changes {
   360  		if change.Kind == ChangeModify || change.Kind == ChangeAdd {
   361  			file := filepath.Join(newDir, change.Path)
   362  			fileInfo, _ := os.Lstat(file)
   363  			if fileInfo != nil && !fileInfo.IsDir() {
   364  				size += fileInfo.Size()
   365  			}
   366  		}
   367  	}
   368  	return size
   369  }
   370  
   371  // ExportChanges produces an Archive from the provided changes, relative to dir.
   372  func ExportChanges(dir string, changes []Change) (Archive, error) {
   373  	reader, writer := io.Pipe()
   374  	go func() {
   375  		ta := &tarAppender{
   376  			TarWriter: tar.NewWriter(writer),
   377  			Buffer:    pools.BufioWriter32KPool.Get(nil),
   378  			SeenFiles: make(map[uint64]string),
   379  		}
   380  		// this buffer is needed for the duration of this piped stream
   381  		defer pools.BufioWriter32KPool.Put(ta.Buffer)
   382  
   383  		sort.Sort(changesByPath(changes))
   384  
   385  		// In general we log errors here but ignore them because
   386  		// during e.g. a diff operation the container can continue
   387  		// mutating the filesystem and we can see transient errors
   388  		// from this
   389  		for _, change := range changes {
   390  			if change.Kind == ChangeDelete {
   391  				whiteOutDir := filepath.Dir(change.Path)
   392  				whiteOutBase := filepath.Base(change.Path)
   393  				whiteOut := filepath.Join(whiteOutDir, ".wh."+whiteOutBase)
   394  				timestamp := time.Now()
   395  				hdr := &tar.Header{
   396  					Name:       whiteOut[1:],
   397  					Size:       0,
   398  					ModTime:    timestamp,
   399  					AccessTime: timestamp,
   400  					ChangeTime: timestamp,
   401  				}
   402  				if err := ta.TarWriter.WriteHeader(hdr); err != nil {
   403  					logrus.Debugf("Can't write whiteout header: %s", err)
   404  				}
   405  			} else {
   406  				path := filepath.Join(dir, change.Path)
   407  				if err := ta.addTarFile(path, change.Path[1:]); err != nil {
   408  					logrus.Debugf("Can't add file %s to tar: %s", path, err)
   409  				}
   410  			}
   411  		}
   412  
   413  		// Make sure to check the error on Close.
   414  		if err := ta.TarWriter.Close(); err != nil {
   415  			logrus.Debugf("Can't close layer: %s", err)
   416  		}
   417  		if err := writer.Close(); err != nil {
   418  			logrus.Debugf("failed close Changes writer: %s", err)
   419  		}
   420  	}()
   421  	return reader, nil
   422  }