2013-11-07 19:35:26 -05:00
|
|
|
package archive
|
2013-03-18 03:15:35 -04:00
|
|
|
|
|
|
|
import (
|
2013-12-16 07:47:09 -05:00
|
|
|
"archive/tar"
|
2013-03-18 03:15:35 -04:00
|
|
|
"fmt"
|
2013-12-16 07:47:09 -05:00
|
|
|
"github.com/dotcloud/docker/utils"
|
|
|
|
"io"
|
2013-03-18 03:15:35 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2013-11-01 00:52:39 -04:00
|
|
|
"syscall"
|
2013-12-13 09:46:41 -05:00
|
|
|
"time"
|
2013-03-18 03:15:35 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type ChangeType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ChangeModify = iota
|
|
|
|
ChangeAdd
|
|
|
|
ChangeDelete
|
|
|
|
)
|
|
|
|
|
|
|
|
type Change struct {
|
|
|
|
Path string
|
|
|
|
Kind ChangeType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (change *Change) String() string {
|
|
|
|
var kind string
|
|
|
|
switch change.Kind {
|
|
|
|
case ChangeModify:
|
|
|
|
kind = "C"
|
|
|
|
case ChangeAdd:
|
|
|
|
kind = "A"
|
|
|
|
case ChangeDelete:
|
|
|
|
kind = "D"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s %s", kind, change.Path)
|
|
|
|
}
|
|
|
|
|
2013-12-13 09:46:41 -05:00
|
|
|
// Gnu tar and the go tar writer don't have sub-second mtime
|
|
|
|
// precision, which is problematic when we apply changes via tar
|
|
|
|
// files, we handle this by comparing for exact times, *or* same
|
|
|
|
// second count and either a or b having exactly 0 nanoseconds
|
|
|
|
func sameFsTime(a, b time.Time) bool {
|
|
|
|
return a == b ||
|
|
|
|
(a.Unix() == b.Unix() &&
|
|
|
|
(a.Nanosecond() == 0 || b.Nanosecond() == 0))
|
|
|
|
}
|
|
|
|
|
|
|
|
func sameFsTimeSpec(a, b syscall.Timespec) bool {
|
|
|
|
return a.Sec == b.Sec &&
|
|
|
|
(a.Nsec == b.Nsec || a.Nsec == 0 || b.Nsec == 0)
|
|
|
|
}
|
|
|
|
|
2013-03-18 03:15:35 -04:00
|
|
|
func Changes(layers []string, rw string) ([]Change, error) {
|
|
|
|
var changes []Change
|
|
|
|
err := filepath.Walk(rw, func(path string, f os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rebase path
|
|
|
|
path, err = filepath.Rel(rw, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
path = filepath.Join("/", path)
|
|
|
|
|
|
|
|
// Skip root
|
|
|
|
if path == "/" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip AUFS metadata
|
|
|
|
if matched, err := filepath.Match("/.wh..wh.*", path); err != nil || matched {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
change := Change{
|
|
|
|
Path: path,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find out what kind of modification happened
|
|
|
|
file := filepath.Base(path)
|
|
|
|
// If there is a whiteout, then the file was removed
|
|
|
|
if strings.HasPrefix(file, ".wh.") {
|
2013-06-03 13:25:51 -04:00
|
|
|
originalFile := file[len(".wh."):]
|
2013-03-18 03:15:35 -04:00
|
|
|
change.Path = filepath.Join(filepath.Dir(path), originalFile)
|
|
|
|
change.Kind = ChangeDelete
|
|
|
|
} else {
|
|
|
|
// Otherwise, the file was added
|
|
|
|
change.Kind = ChangeAdd
|
|
|
|
|
|
|
|
// ...Unless it already existed in a top layer, in which case, it's a modification
|
|
|
|
for _, layer := range layers {
|
|
|
|
stat, err := os.Stat(filepath.Join(layer, path))
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
// The file existed in the top layer, so that's a modification
|
|
|
|
|
|
|
|
// However, if it's a directory, maybe it wasn't actually modified.
|
|
|
|
// If you modify /foo/bar/baz, then /foo will be part of the changed files only because it's the parent of bar
|
|
|
|
if stat.IsDir() && f.IsDir() {
|
2013-12-13 09:46:41 -05:00
|
|
|
if f.Size() == stat.Size() && f.Mode() == stat.Mode() && sameFsTime(f.ModTime(), stat.ModTime()) {
|
2013-03-18 03:15:35 -04:00
|
|
|
// Both directories are the same, don't record the change
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
change.Kind = ChangeModify
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record change
|
|
|
|
changes = append(changes, change)
|
|
|
|
return nil
|
|
|
|
})
|
2013-08-30 15:45:01 -04:00
|
|
|
if err != nil && !os.IsNotExist(err) {
|
2013-03-18 03:15:35 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return changes, nil
|
|
|
|
}
|
2013-11-01 00:52:39 -04:00
|
|
|
|
2013-11-11 08:35:29 -05:00
|
|
|
type FileInfo struct {
|
|
|
|
parent *FileInfo
|
|
|
|
name string
|
|
|
|
stat syscall.Stat_t
|
|
|
|
children map[string]*FileInfo
|
|
|
|
}
|
2013-11-01 00:52:39 -04:00
|
|
|
|
2013-11-11 08:35:29 -05:00
|
|
|
func (root *FileInfo) LookUp(path string) *FileInfo {
|
|
|
|
parent := root
|
|
|
|
if path == "/" {
|
|
|
|
return root
|
|
|
|
}
|
2013-11-01 00:52:39 -04:00
|
|
|
|
2013-11-11 08:35:29 -05:00
|
|
|
pathElements := strings.Split(path, "/")
|
|
|
|
for _, elem := range pathElements {
|
|
|
|
if elem != "" {
|
|
|
|
child := parent.children[elem]
|
|
|
|
if child == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
parent = child
|
2013-11-01 00:52:39 -04:00
|
|
|
}
|
2013-11-11 08:35:29 -05:00
|
|
|
}
|
|
|
|
return parent
|
|
|
|
}
|
2013-11-01 00:52:39 -04:00
|
|
|
|
2013-11-11 08:35:29 -05:00
|
|
|
func (info *FileInfo) path() string {
|
|
|
|
if info.parent == nil {
|
|
|
|
return "/"
|
|
|
|
}
|
|
|
|
return filepath.Join(info.parent.path(), info.name)
|
|
|
|
}
|
2013-11-01 00:52:39 -04:00
|
|
|
|
2013-11-11 08:35:29 -05:00
|
|
|
func (info *FileInfo) isDir() bool {
|
|
|
|
return info.parent == nil || info.stat.Mode&syscall.S_IFDIR == syscall.S_IFDIR
|
|
|
|
}
|
|
|
|
|
|
|
|
func (info *FileInfo) addChanges(oldInfo *FileInfo, changes *[]Change) {
|
|
|
|
if oldInfo == nil {
|
|
|
|
// add
|
2013-11-01 00:52:39 -04:00
|
|
|
change := Change{
|
2013-11-11 08:35:29 -05:00
|
|
|
Path: info.path(),
|
|
|
|
Kind: ChangeAdd,
|
2013-11-01 00:52:39 -04:00
|
|
|
}
|
2013-11-11 08:35:29 -05:00
|
|
|
*changes = append(*changes, change)
|
|
|
|
}
|
2013-11-01 00:52:39 -04:00
|
|
|
|
2013-11-11 08:35:29 -05:00
|
|
|
// We make a copy so we can modify it to detect additions
|
|
|
|
// also, we only recurse on the old dir if the new info is a directory
|
|
|
|
// otherwise any previous delete/change is considered recursive
|
|
|
|
oldChildren := make(map[string]*FileInfo)
|
|
|
|
if oldInfo != nil && info.isDir() {
|
|
|
|
for k, v := range oldInfo.children {
|
|
|
|
oldChildren[k] = v
|
2013-11-01 00:52:39 -04:00
|
|
|
}
|
2013-11-11 08:35:29 -05:00
|
|
|
}
|
2013-11-01 00:52:39 -04:00
|
|
|
|
2013-11-11 08:35:29 -05:00
|
|
|
for name, newChild := range info.children {
|
|
|
|
oldChild, _ := oldChildren[name]
|
|
|
|
if oldChild != nil {
|
|
|
|
// change?
|
|
|
|
oldStat := &oldChild.stat
|
|
|
|
newStat := &newChild.stat
|
|
|
|
// Note: We can't compare inode or ctime or blocksize here, because these change
|
|
|
|
// when copying a file into a container. However, that is not generally a problem
|
|
|
|
// because any content change will change mtime, and any status change should
|
|
|
|
// be visible when actually comparing the stat fields. The only time this
|
|
|
|
// breaks down is if some code intentionally hides a change by setting
|
|
|
|
// back mtime
|
|
|
|
if oldStat.Mode != newStat.Mode ||
|
2013-11-01 00:52:39 -04:00
|
|
|
oldStat.Uid != newStat.Uid ||
|
|
|
|
oldStat.Gid != newStat.Gid ||
|
|
|
|
oldStat.Rdev != newStat.Rdev ||
|
2013-11-11 08:35:29 -05:00
|
|
|
// Don't look at size for dirs, its not a good measure of change
|
|
|
|
(oldStat.Size != newStat.Size && oldStat.Mode&syscall.S_IFDIR != syscall.S_IFDIR) ||
|
2013-12-13 09:46:41 -05:00
|
|
|
!sameFsTimeSpec(getLastModification(oldStat), getLastModification(newStat)) {
|
2013-11-11 08:35:29 -05:00
|
|
|
change := Change{
|
|
|
|
Path: newChild.path(),
|
|
|
|
Kind: ChangeModify,
|
|
|
|
}
|
|
|
|
*changes = append(*changes, change)
|
2013-11-01 00:52:39 -04:00
|
|
|
}
|
2013-11-11 08:35:29 -05:00
|
|
|
|
|
|
|
// Remove from copy so we can detect deletions
|
|
|
|
delete(oldChildren, name)
|
2013-11-01 00:52:39 -04:00
|
|
|
}
|
|
|
|
|
2013-11-11 08:35:29 -05:00
|
|
|
newChild.addChanges(oldChild, changes)
|
2013-11-01 00:52:39 -04:00
|
|
|
}
|
2013-11-11 08:35:29 -05:00
|
|
|
for _, oldChild := range oldChildren {
|
|
|
|
// delete
|
|
|
|
change := Change{
|
|
|
|
Path: oldChild.path(),
|
|
|
|
Kind: ChangeDelete,
|
|
|
|
}
|
|
|
|
*changes = append(*changes, change)
|
2013-11-01 00:52:39 -04:00
|
|
|
}
|
2013-11-11 08:35:29 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (info *FileInfo) Changes(oldInfo *FileInfo) []Change {
|
|
|
|
var changes []Change
|
|
|
|
|
|
|
|
info.addChanges(oldInfo, &changes)
|
|
|
|
|
|
|
|
return changes
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRootFileInfo() *FileInfo {
|
|
|
|
root := &FileInfo{
|
|
|
|
name: "/",
|
|
|
|
children: make(map[string]*FileInfo),
|
|
|
|
}
|
|
|
|
return root
|
|
|
|
}
|
|
|
|
|
|
|
|
func collectFileInfo(sourceDir string) (*FileInfo, error) {
|
|
|
|
root := newRootFileInfo()
|
|
|
|
|
|
|
|
err := filepath.Walk(sourceDir, func(path string, f os.FileInfo, err error) error {
|
2013-11-01 00:52:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rebase path
|
2013-11-11 08:35:29 -05:00
|
|
|
relPath, err := filepath.Rel(sourceDir, path)
|
2013-11-01 00:52:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
relPath = filepath.Join("/", relPath)
|
|
|
|
|
|
|
|
if relPath == "/" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-11 08:35:29 -05:00
|
|
|
parent := root.LookUp(filepath.Dir(relPath))
|
|
|
|
if parent == nil {
|
|
|
|
return fmt.Errorf("collectFileInfo: Unexpectedly no parent for %s", relPath)
|
2013-11-01 00:52:39 -04:00
|
|
|
}
|
|
|
|
|
2013-11-11 08:35:29 -05:00
|
|
|
info := &FileInfo{
|
|
|
|
name: filepath.Base(relPath),
|
|
|
|
children: make(map[string]*FileInfo),
|
|
|
|
parent: parent,
|
|
|
|
}
|
2013-11-01 00:52:39 -04:00
|
|
|
|
2013-11-11 08:35:29 -05:00
|
|
|
if err := syscall.Lstat(path, &info.stat); err != nil {
|
|
|
|
return err
|
2013-11-01 00:52:39 -04:00
|
|
|
}
|
|
|
|
|
2013-11-11 08:35:29 -05:00
|
|
|
parent.children[info.name] = info
|
|
|
|
|
2013-11-01 00:52:39 -04:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-11-11 08:35:29 -05:00
|
|
|
return root, nil
|
2013-11-01 00:52:39 -04:00
|
|
|
}
|
2013-11-07 21:48:52 -05:00
|
|
|
|
2013-11-11 08:35:29 -05:00
|
|
|
// Compare two directories and generate an array of Change objects describing the changes
|
|
|
|
func ChangesDirs(newDir, oldDir string) ([]Change, error) {
|
|
|
|
oldRoot, err := collectFileInfo(oldDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
newRoot, err := collectFileInfo(newDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return newRoot.Changes(oldRoot), nil
|
2013-11-01 00:52:39 -04:00
|
|
|
}
|
2013-11-07 21:48:52 -05:00
|
|
|
|
2013-11-19 04:36:54 -05:00
|
|
|
func ChangesSize(newDir string, changes []Change) int64 {
|
|
|
|
var size int64
|
|
|
|
for _, change := range changes {
|
|
|
|
if change.Kind == ChangeModify || change.Kind == ChangeAdd {
|
|
|
|
file := filepath.Join(newDir, change.Path)
|
|
|
|
fileInfo, _ := os.Lstat(file)
|
|
|
|
if fileInfo != nil && !fileInfo.IsDir() {
|
|
|
|
size += fileInfo.Size()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return size
|
|
|
|
}
|
|
|
|
|
2013-12-16 07:47:09 -05:00
|
|
|
func major(device uint64) uint64 {
|
|
|
|
return (device >> 8) & 0xfff
|
|
|
|
}
|
|
|
|
|
|
|
|
func minor(device uint64) uint64 {
|
|
|
|
return (device & 0xff) | ((device >> 12) & 0xfff00)
|
|
|
|
}
|
|
|
|
|
2013-11-14 16:57:33 -05:00
|
|
|
func ExportChanges(dir string, changes []Change) (Archive, error) {
|
2013-12-16 07:47:09 -05:00
|
|
|
reader, writer := io.Pipe()
|
|
|
|
tw := tar.NewWriter(writer)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
// In general we log errors here but ignore them because
|
|
|
|
// during e.g. a diff operation the container can continue
|
|
|
|
// mutating the filesystem and we can see transient errors
|
|
|
|
// from this
|
|
|
|
for _, change := range changes {
|
|
|
|
if change.Kind == ChangeDelete {
|
|
|
|
whiteOutDir := filepath.Dir(change.Path)
|
|
|
|
whiteOutBase := filepath.Base(change.Path)
|
|
|
|
whiteOut := filepath.Join(whiteOutDir, ".wh."+whiteOutBase)
|
|
|
|
hdr := &tar.Header{
|
|
|
|
Name: whiteOut[1:],
|
|
|
|
Size: 0,
|
|
|
|
ModTime: time.Now(),
|
|
|
|
AccessTime: time.Now(),
|
|
|
|
ChangeTime: time.Now(),
|
|
|
|
}
|
|
|
|
if err := tw.WriteHeader(hdr); err != nil {
|
|
|
|
utils.Debugf("Can't write whiteout header: %s\n", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
path := filepath.Join(dir, change.Path)
|
|
|
|
|
|
|
|
var stat syscall.Stat_t
|
|
|
|
if err := syscall.Lstat(path, &stat); err != nil {
|
|
|
|
utils.Debugf("Can't stat source file: %s\n", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
mtim := getLastModification(&stat)
|
|
|
|
atim := getLastAccess(&stat)
|
|
|
|
hdr := &tar.Header{
|
|
|
|
Name: change.Path[1:],
|
|
|
|
Mode: int64(stat.Mode & 07777),
|
|
|
|
Uid: int(stat.Uid),
|
|
|
|
Gid: int(stat.Gid),
|
2014-01-15 04:07:47 -05:00
|
|
|
ModTime: time.Unix(int64(mtim.Sec), int64(mtim.Nsec)),
|
|
|
|
AccessTime: time.Unix(int64(atim.Sec), int64(atim.Nsec)),
|
2013-12-16 07:47:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if stat.Mode&syscall.S_IFDIR == syscall.S_IFDIR {
|
|
|
|
hdr.Typeflag = tar.TypeDir
|
|
|
|
} else if stat.Mode&syscall.S_IFLNK == syscall.S_IFLNK {
|
|
|
|
hdr.Typeflag = tar.TypeSymlink
|
|
|
|
if link, err := os.Readlink(path); err != nil {
|
|
|
|
utils.Debugf("Can't readlink source file: %s\n", err)
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
hdr.Linkname = link
|
|
|
|
}
|
|
|
|
} else if stat.Mode&syscall.S_IFBLK == syscall.S_IFBLK ||
|
|
|
|
stat.Mode&syscall.S_IFCHR == syscall.S_IFCHR {
|
|
|
|
if stat.Mode&syscall.S_IFBLK == syscall.S_IFBLK {
|
|
|
|
hdr.Typeflag = tar.TypeBlock
|
|
|
|
} else {
|
|
|
|
hdr.Typeflag = tar.TypeChar
|
|
|
|
}
|
2014-01-15 04:07:47 -05:00
|
|
|
hdr.Devmajor = int64(major(uint64(stat.Rdev)))
|
|
|
|
hdr.Devminor = int64(minor(uint64(stat.Rdev)))
|
2013-12-16 07:47:09 -05:00
|
|
|
} else if stat.Mode&syscall.S_IFIFO == syscall.S_IFIFO ||
|
|
|
|
stat.Mode&syscall.S_IFSOCK == syscall.S_IFSOCK {
|
|
|
|
hdr.Typeflag = tar.TypeFifo
|
|
|
|
} else if stat.Mode&syscall.S_IFREG == syscall.S_IFREG {
|
|
|
|
hdr.Typeflag = tar.TypeReg
|
|
|
|
hdr.Size = stat.Size
|
|
|
|
} else {
|
|
|
|
utils.Debugf("Unknown file type: %s\n", path)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tw.WriteHeader(hdr); err != nil {
|
|
|
|
utils.Debugf("Can't write tar header: %s\n", err)
|
|
|
|
}
|
|
|
|
if hdr.Typeflag == tar.TypeReg {
|
|
|
|
if file, err := os.Open(path); err != nil {
|
|
|
|
utils.Debugf("Can't open file: %s\n", err)
|
|
|
|
} else {
|
|
|
|
_, err := io.Copy(tw, file)
|
|
|
|
if err != nil {
|
|
|
|
utils.Debugf("Can't copy file: %s\n", err)
|
|
|
|
}
|
|
|
|
file.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-11 17:30:38 -05:00
|
|
|
}
|
2013-12-16 07:47:09 -05:00
|
|
|
// Make sure to check the error on Close.
|
|
|
|
if err := tw.Close(); err != nil {
|
|
|
|
utils.Debugf("Can't close layer: %s\n", err)
|
2013-11-11 17:30:38 -05:00
|
|
|
}
|
2013-12-16 07:47:09 -05:00
|
|
|
writer.Close()
|
|
|
|
}()
|
|
|
|
return reader, nil
|
2013-11-07 21:48:52 -05:00
|
|
|
}
|