9ae4bcaa |
package archive |
33f6a0aa |
import ( |
576985a1 |
"archive/tar" |
87ca750c |
"bytes" |
33f6a0aa |
"fmt" |
d54ce808 |
"io" |
45c45a2c |
"io/ioutil" |
33f6a0aa |
"os"
"path/filepath" |
32d6d495 |
"sort" |
33f6a0aa |
"strings" |
a5031d47 |
"syscall" |
10cd902f |
"time" |
359f8aca |
|
442b4562 |
"github.com/docker/docker/pkg/idtools" |
84d76e55 |
"github.com/docker/docker/pkg/pools" |
a02f67be |
"github.com/docker/docker/pkg/system" |
1009e6a4 |
"github.com/sirupsen/logrus" |
33f6a0aa |
)
|
ba332b7d |
// ChangeType represents the change type. |
33f6a0aa |
type ChangeType int
const ( |
ba332b7d |
// ChangeModify represents the modify operation. |
33f6a0aa |
ChangeModify = iota |
ba332b7d |
// ChangeAdd represents the add operation. |
33f6a0aa |
ChangeAdd |
ba332b7d |
// ChangeDelete represents the delete operation. |
33f6a0aa |
ChangeDelete
)
|
7aa28b6b |
func (c ChangeType) String() string {
switch c {
case ChangeModify:
return "C"
case ChangeAdd:
return "A"
case ChangeDelete:
return "D"
}
return ""
}
|
ba332b7d |
// Change represents a change, it wraps the change type and path.
// It describes changes of the files in the path respect to the
// parent layers. The change could be modify, add, delete.
// This is used for layer diff. |
33f6a0aa |
type Change struct {
Path string
Kind ChangeType
}
func (change *Change) String() string { |
7aa28b6b |
return fmt.Sprintf("%s %s", change.Kind, change.Path) |
33f6a0aa |
}
|
32d6d495 |
// for sort.Sort
type changesByPath []Change
func (c changesByPath) Less(i, j int) bool { return c[i].Path < c[j].Path }
func (c changesByPath) Len() int { return len(c) }
func (c changesByPath) Swap(i, j int) { c[j], c[i] = c[i], c[j] }
|
10cd902f |
// 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)
}
|
0fe56ce1 |
// Changes walks the path rw and determines changes for the files in the path,
// with respect to the parent layers |
33f6a0aa |
func Changes(layers []string, rw string) ([]Change, error) { |
8222c863 |
return changes(layers, rw, aufsDeletedFile, aufsMetadataSkip)
}
func aufsMetadataSkip(path string) (skip bool, err error) {
skip, err = filepath.Match(string(os.PathSeparator)+WhiteoutMetaPrefix+"*", path)
if err != nil {
skip = true
}
return
}
func aufsDeletedFile(root, path string, fi os.FileInfo) (string, error) {
f := filepath.Base(path)
// If there is a whiteout, then the file was removed
if strings.HasPrefix(f, WhiteoutPrefix) {
originalFile := f[len(WhiteoutPrefix):]
return filepath.Join(filepath.Dir(path), originalFile), nil
}
return "", nil
}
type skipChange func(string) (bool, error)
type deleteChange func(string, string, os.FileInfo) (string, error)
func changes(layers []string, rw string, dc deleteChange, sc skipChange) ([]Change, error) { |
e2c6a8be |
var (
changes []Change
changedDirs = make(map[string]struct{})
)
|
33f6a0aa |
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
} |
3c177dc8 |
// As this runs on the daemon side, file paths are OS specific.
path = filepath.Join(string(os.PathSeparator), path) |
33f6a0aa |
// Skip root |
3c177dc8 |
if path == string(os.PathSeparator) { |
33f6a0aa |
return nil
}
|
8222c863 |
if sc != nil {
if skip, err := sc(path); skip {
return err
} |
33f6a0aa |
}
change := Change{
Path: path,
}
|
8222c863 |
deletedFile, err := dc(rw, path, f)
if err != nil {
return err
}
|
33f6a0aa |
// Find out what kind of modification happened |
8222c863 |
if deletedFile != "" {
change.Path = deletedFile |
33f6a0aa |
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() { |
10cd902f |
if f.Size() == stat.Size() && f.Mode() == stat.Mode() && sameFsTime(f.ModTime(), stat.ModTime()) { |
33f6a0aa |
// Both directories are the same, don't record the change
return nil
}
}
change.Kind = ChangeModify
break
}
}
}
|
e2c6a8be |
// If /foo/bar/file.txt is modified, then /foo/bar must be part of the changed files.
// This block is here to ensure the change is recorded even if the |
927b334e |
// modify time, mode and size of the parent directory in the rw and ro layers are all equal. |
e2c6a8be |
// Check https://github.com/docker/docker/pull/13590 for details.
if f.IsDir() {
changedDirs[path] = struct{}{}
}
if change.Kind == ChangeAdd || change.Kind == ChangeDelete {
parent := filepath.Dir(path)
if _, ok := changedDirs[parent]; !ok && parent != "/" {
changes = append(changes, Change{Path: parent, Kind: ChangeModify})
changedDirs[parent] = struct{}{}
}
}
|
33f6a0aa |
// Record change
changes = append(changes, change)
return nil
}) |
46c9c5c8 |
if err != nil && !os.IsNotExist(err) { |
33f6a0aa |
return nil, err
}
return changes, nil
} |
a5031d47 |
|
ba332b7d |
// FileInfo describes the information of a file. |
b6ef4bc9 |
type FileInfo struct { |
87ca750c |
parent *FileInfo
name string |
7e420ad8 |
stat *system.StatT |
87ca750c |
children map[string]*FileInfo
capability []byte |
691bbf6a |
added bool |
b6ef4bc9 |
} |
a5031d47 |
|
ba332b7d |
// LookUp looks up the file information of a file.
func (info *FileInfo) LookUp(path string) *FileInfo { |
3c177dc8 |
// As this runs on the daemon side, file paths are OS specific. |
ba332b7d |
parent := info |
3c177dc8 |
if path == string(os.PathSeparator) { |
ba332b7d |
return info |
b6ef4bc9 |
} |
a5031d47 |
|
3c177dc8 |
pathElements := strings.Split(path, string(os.PathSeparator)) |
b6ef4bc9 |
for _, elem := range pathElements {
if elem != "" {
child := parent.children[elem]
if child == nil {
return nil
}
parent = child |
a5031d47 |
} |
b6ef4bc9 |
}
return parent
} |
a5031d47 |
|
b6ef4bc9 |
func (info *FileInfo) path() string {
if info.parent == nil { |
3c177dc8 |
// As this runs on the daemon side, file paths are OS specific.
return string(os.PathSeparator) |
b6ef4bc9 |
}
return filepath.Join(info.parent.path(), info.name)
} |
a5031d47 |
|
b6ef4bc9 |
func (info *FileInfo) addChanges(oldInfo *FileInfo, changes *[]Change) { |
691bbf6a |
sizeAtEntry := len(*changes)
|
b6ef4bc9 |
if oldInfo == nil {
// add |
a5031d47 |
change := Change{ |
b6ef4bc9 |
Path: info.path(),
Kind: ChangeAdd, |
a5031d47 |
} |
b6ef4bc9 |
*changes = append(*changes, change) |
691bbf6a |
info.added = true |
b6ef4bc9 |
} |
a5031d47 |
|
b6ef4bc9 |
// 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 |
a5031d47 |
} |
b6ef4bc9 |
} |
a5031d47 |
|
b6ef4bc9 |
for name, newChild := range info.children { |
087f7307 |
oldChild := oldChildren[name] |
b6ef4bc9 |
if oldChild != nil {
// change? |
2180aa4f |
oldStat := oldChild.stat
newStat := newChild.stat |
b6ef4bc9 |
// 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 |
8228ee4b |
if statDifferent(oldStat, newStat) || |
087f7307 |
!bytes.Equal(oldChild.capability, newChild.capability) { |
b6ef4bc9 |
change := Change{
Path: newChild.path(),
Kind: ChangeModify,
}
*changes = append(*changes, change) |
691bbf6a |
newChild.added = true |
a5031d47 |
} |
b6ef4bc9 |
// Remove from copy so we can detect deletions
delete(oldChildren, name) |
a5031d47 |
}
|
b6ef4bc9 |
newChild.addChanges(oldChild, changes) |
a5031d47 |
} |
b6ef4bc9 |
for _, oldChild := range oldChildren {
// delete
change := Change{
Path: oldChild.path(),
Kind: ChangeDelete,
}
*changes = append(*changes, change) |
a5031d47 |
} |
b6ef4bc9 |
|
691bbf6a |
// If there were changes inside this directory, we need to add it, even if the directory
// itself wasn't changed. This is needed to properly save and restore filesystem permissions. |
3c177dc8 |
// As this runs on the daemon side, file paths are OS specific.
if len(*changes) > sizeAtEntry && info.isDir() && !info.added && info.path() != string(os.PathSeparator) { |
691bbf6a |
change := Change{
Path: info.path(),
Kind: ChangeModify,
}
// Let's insert the directory entry before the recently added entries located inside this dir
*changes = append(*changes, change) // just to resize the slice, will be overwritten
copy((*changes)[sizeAtEntry+1:], (*changes)[sizeAtEntry:])
(*changes)[sizeAtEntry] = change
}
|
b6ef4bc9 |
}
|
ba332b7d |
// Changes add changes to file information. |
b6ef4bc9 |
func (info *FileInfo) Changes(oldInfo *FileInfo) []Change {
var changes []Change
info.addChanges(oldInfo, &changes)
return changes
}
func newRootFileInfo() *FileInfo { |
3c177dc8 |
// As this runs on the daemon side, file paths are OS specific. |
b6ef4bc9 |
root := &FileInfo{ |
3c177dc8 |
name: string(os.PathSeparator), |
b6ef4bc9 |
children: make(map[string]*FileInfo),
}
return root
}
|
dee6b481 |
// ChangesDirs compares two directories and generates an array of Change objects describing the changes.
// If oldDir is "", then all files in newDir will be Add-Changes. |
b6ef4bc9 |
func ChangesDirs(newDir, oldDir string) ([]Change, error) { |
359f8aca |
var (
oldRoot, newRoot *FileInfo
) |
45c45a2c |
if oldDir == "" {
emptyDir, err := ioutil.TempDir("", "empty")
if err != nil { |
359f8aca |
return nil, err
} |
45c45a2c |
defer os.Remove(emptyDir)
oldDir = emptyDir
}
oldRoot, newRoot, err := collectFileInfoForChanges(oldDir, newDir)
if err != nil {
return nil, err |
b6ef4bc9 |
}
return newRoot.Changes(oldRoot), nil |
a5031d47 |
} |
99210c9c |
|
0fe56ce1 |
// ChangesSize calculates the size in bytes of the provided changes, based on newDir. |
5d76681c |
func ChangesSize(newDir string, changes []Change) int64 { |
4102537c |
var (
size int64
sf = make(map[uint64]struct{})
) |
5d76681c |
for _, change := range changes {
if change.Kind == ChangeModify || change.Kind == ChangeAdd {
file := filepath.Join(newDir, change.Path) |
4102537c |
fileInfo, err := os.Lstat(file)
if err != nil {
logrus.Errorf("Can not stat %q: %s", file, err)
continue
}
|
5d76681c |
if fileInfo != nil && !fileInfo.IsDir() { |
4102537c |
if hasHardlinks(fileInfo) {
inode := getIno(fileInfo)
if _, ok := sf[inode]; !ok {
size += fileInfo.Size()
sf[inode] = struct{}{}
}
} else {
size += fileInfo.Size()
} |
5d76681c |
}
}
}
return size
}
|
0fe56ce1 |
// ExportChanges produces an Archive from the provided changes, relative to dir. |
aa2cc187 |
func ExportChanges(dir string, changes []Change, uidMaps, gidMaps []idtools.IDMap) (io.ReadCloser, error) { |
d54ce808 |
reader, writer := io.Pipe()
go func() { |
5672eeb5 |
ta := newTarAppender(idtools.NewIDMappingsFromMaps(uidMaps, gidMaps), writer)
|
f9f80443 |
// this buffer is needed for the duration of this piped stream
defer pools.BufioWriter32KPool.Put(ta.Buffer)
|
32d6d495 |
sort.Sort(changesByPath(changes))
|
d54ce808 |
// 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) |
2fb5d0c3 |
whiteOut := filepath.Join(whiteOutDir, WhiteoutPrefix+whiteOutBase) |
b64209f7 |
timestamp := time.Now() |
d54ce808 |
hdr := &tar.Header{
Name: whiteOut[1:],
Size: 0, |
b64209f7 |
ModTime: timestamp,
AccessTime: timestamp,
ChangeTime: timestamp, |
d54ce808 |
} |
f9f80443 |
if err := ta.TarWriter.WriteHeader(hdr); err != nil { |
6f4d8470 |
logrus.Debugf("Can't write whiteout header: %s", err) |
d54ce808 |
}
} else {
path := filepath.Join(dir, change.Path) |
f9f80443 |
if err := ta.addTarFile(path, change.Path[1:]); err != nil { |
6f4d8470 |
logrus.Debugf("Can't add file %s to tar: %s", path, err) |
d54ce808 |
}
} |
2c7f50a7 |
} |
5b77e51e |
|
d54ce808 |
// Make sure to check the error on Close. |
f9f80443 |
if err := ta.TarWriter.Close(); err != nil { |
6f4d8470 |
logrus.Debugf("Can't close layer: %s", err) |
2c7f50a7 |
} |
f14a9ed0 |
if err := writer.Close(); err != nil { |
6f4d8470 |
logrus.Debugf("failed close Changes writer: %s", err) |
f14a9ed0 |
} |
d54ce808 |
}()
return reader, nil |
99210c9c |
} |