github.com/titanous/docker@v1.4.1/pkg/archive/changes.go (about)

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