github.com/rentongzhang/docker@v1.8.2-rc1/pkg/archive/changes.go (about)

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