2018-02-05 16:05:59 -05:00
|
|
|
package archive // import "github.com/docker/docker/pkg/archive"
|
2013-03-18 03:15:35 -04:00
|
|
|
|
|
|
|
import (
|
2015-05-01 18:01:10 -04:00
|
|
|
"archive/tar"
|
2014-01-17 04:51:59 -05:00
|
|
|
"bytes"
|
2013-03-18 03:15:35 -04:00
|
|
|
"fmt"
|
2013-12-16 07:47:09 -05:00
|
|
|
"io"
|
2013-03-18 03:15:35 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-02-05 05:48:58 -05:00
|
|
|
"sort"
|
2013-03-18 03:15:35 -04:00
|
|
|
"strings"
|
2013-11-01 00:52:39 -04:00
|
|
|
"syscall"
|
2013-12-13 09:46:41 -05:00
|
|
|
"time"
|
2014-05-13 08:23:56 -04:00
|
|
|
|
2015-10-08 11:51:41 -04:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
2014-08-12 12:29:46 -04:00
|
|
|
"github.com/docker/docker/pkg/pools"
|
2014-07-24 16:37:44 -04:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2013-03-18 03:15:35 -04:00
|
|
|
)
|
|
|
|
|
2015-08-03 21:52:54 -04:00
|
|
|
// ChangeType represents the change type.
|
2013-03-18 03:15:35 -04:00
|
|
|
type ChangeType int
|
|
|
|
|
|
|
|
const (
|
2015-08-03 21:52:54 -04:00
|
|
|
// ChangeModify represents the modify operation.
|
2013-03-18 03:15:35 -04:00
|
|
|
ChangeModify = iota
|
2015-08-03 21:52:54 -04:00
|
|
|
// ChangeAdd represents the add operation.
|
2013-03-18 03:15:35 -04:00
|
|
|
ChangeAdd
|
2015-08-03 21:52:54 -04:00
|
|
|
// ChangeDelete represents the delete operation.
|
2013-03-18 03:15:35 -04:00
|
|
|
ChangeDelete
|
|
|
|
)
|
|
|
|
|
2015-11-02 11:28:34 -05:00
|
|
|
func (c ChangeType) String() string {
|
|
|
|
switch c {
|
|
|
|
case ChangeModify:
|
|
|
|
return "C"
|
|
|
|
case ChangeAdd:
|
|
|
|
return "A"
|
|
|
|
case ChangeDelete:
|
|
|
|
return "D"
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2015-08-03 21:52:54 -04:00
|
|
|
// 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.
|
2013-03-18 03:15:35 -04:00
|
|
|
type Change struct {
|
|
|
|
Path string
|
|
|
|
Kind ChangeType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (change *Change) String() string {
|
2015-11-02 11:28:34 -05:00
|
|
|
return fmt.Sprintf("%s %s", change.Kind, change.Path)
|
2013-03-18 03:15:35 -04:00
|
|
|
}
|
|
|
|
|
2015-02-05 05:48:58 -05:00
|
|
|
// 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] }
|
|
|
|
|
2018-10-05 18:46:59 -04:00
|
|
|
// Gnu tar doesn't have sub-second mtime precision. The go tar
|
|
|
|
// writer (1.10+) does when using PAX format, but we round times to seconds
|
|
|
|
// to ensure archives have the same hashes for backwards compatibility.
|
|
|
|
// See https://github.com/moby/moby/pull/35739/commits/fb170206ba12752214630b269a40ac7be6115ed4.
|
|
|
|
//
|
|
|
|
// Non-sub-second is problematic when we apply changes via tar
|
|
|
|
// files. We handle this by comparing for exact times, *or* same
|
2013-12-13 09:46:41 -05:00
|
|
|
// second count and either a or b having exactly 0 nanoseconds
|
|
|
|
func sameFsTime(a, b time.Time) bool {
|
2018-10-05 18:46:59 -04:00
|
|
|
return a.Equal(b) ||
|
2013-12-13 09:46:41 -05:00
|
|
|
(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)
|
|
|
|
}
|
|
|
|
|
2014-09-19 14:02:12 -04:00
|
|
|
// Changes walks the path rw and determines changes for the files in the path,
|
|
|
|
// with respect to the parent layers
|
2013-03-18 03:15:35 -04:00
|
|
|
func Changes(layers []string, rw string) ([]Change, error) {
|
2016-04-06 18:07:29 -04:00
|
|
|
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) {
|
2015-05-29 04:39:14 -04:00
|
|
|
var (
|
|
|
|
changes []Change
|
|
|
|
changedDirs = make(map[string]struct{})
|
|
|
|
)
|
|
|
|
|
2013-03-18 03:15:35 -04:00
|
|
|
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
|
|
|
|
}
|
2015-06-01 19:42:27 -04:00
|
|
|
|
|
|
|
// As this runs on the daemon side, file paths are OS specific.
|
|
|
|
path = filepath.Join(string(os.PathSeparator), path)
|
2013-03-18 03:15:35 -04:00
|
|
|
|
|
|
|
// Skip root
|
2015-06-01 19:42:27 -04:00
|
|
|
if path == string(os.PathSeparator) {
|
2013-03-18 03:15:35 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-06 18:07:29 -04:00
|
|
|
if sc != nil {
|
|
|
|
if skip, err := sc(path); skip {
|
|
|
|
return err
|
|
|
|
}
|
2013-03-18 03:15:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
change := Change{
|
|
|
|
Path: path,
|
|
|
|
}
|
|
|
|
|
2016-04-06 18:07:29 -04:00
|
|
|
deletedFile, err := dc(rw, path, f)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-03-18 03:15:35 -04:00
|
|
|
// Find out what kind of modification happened
|
2016-04-06 18:07:29 -04:00
|
|
|
if deletedFile != "" {
|
|
|
|
change.Path = deletedFile
|
2013-03-18 03:15:35 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-29 04:39:14 -04:00
|
|
|
// 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
|
2015-12-13 11:00:39 -05:00
|
|
|
// modify time, mode and size of the parent directory in the rw and ro layers are all equal.
|
2015-05-29 04:39:14 -04:00
|
|
|
// 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{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-18 03:15:35 -04:00
|
|
|
// 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
|
|
|
|
2015-08-03 21:52:54 -04:00
|
|
|
// FileInfo describes the information of a file.
|
2013-11-11 08:35:29 -05:00
|
|
|
type FileInfo struct {
|
2014-01-17 04:51:59 -05:00
|
|
|
parent *FileInfo
|
|
|
|
name string
|
2015-07-28 12:13:12 -04:00
|
|
|
stat *system.StatT
|
2014-01-17 04:51:59 -05:00
|
|
|
children map[string]*FileInfo
|
|
|
|
capability []byte
|
2014-07-12 18:28:04 -04:00
|
|
|
added bool
|
2013-11-11 08:35:29 -05:00
|
|
|
}
|
2013-11-01 00:52:39 -04:00
|
|
|
|
2015-08-03 21:52:54 -04:00
|
|
|
// LookUp looks up the file information of a file.
|
|
|
|
func (info *FileInfo) LookUp(path string) *FileInfo {
|
2015-06-01 19:42:27 -04:00
|
|
|
// As this runs on the daemon side, file paths are OS specific.
|
2015-08-03 21:52:54 -04:00
|
|
|
parent := info
|
2015-06-01 19:42:27 -04:00
|
|
|
if path == string(os.PathSeparator) {
|
2015-08-03 21:52:54 -04:00
|
|
|
return info
|
2013-11-11 08:35:29 -05:00
|
|
|
}
|
2013-11-01 00:52:39 -04:00
|
|
|
|
2015-06-01 19:42:27 -04:00
|
|
|
pathElements := strings.Split(path, string(os.PathSeparator))
|
2013-11-11 08:35:29 -05:00
|
|
|
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 {
|
2015-06-01 19:42:27 -04:00
|
|
|
// As this runs on the daemon side, file paths are OS specific.
|
|
|
|
return string(os.PathSeparator)
|
2013-11-11 08:35:29 -05:00
|
|
|
}
|
|
|
|
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) addChanges(oldInfo *FileInfo, changes *[]Change) {
|
2014-07-12 18:28:04 -04:00
|
|
|
|
|
|
|
sizeAtEntry := len(*changes)
|
|
|
|
|
2013-11-11 08:35:29 -05:00
|
|
|
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)
|
2014-07-12 18:28:04 -04:00
|
|
|
info.added = true
|
2013-11-11 08:35:29 -05:00
|
|
|
}
|
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 {
|
2017-03-30 05:04:42 -04:00
|
|
|
oldChild := oldChildren[name]
|
2013-11-11 08:35:29 -05:00
|
|
|
if oldChild != nil {
|
|
|
|
// change?
|
2014-11-13 15:36:05 -05:00
|
|
|
oldStat := oldChild.stat
|
|
|
|
newStat := newChild.stat
|
2013-11-11 08:35:29 -05:00
|
|
|
// 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
|
2015-05-07 18:14:11 -04:00
|
|
|
if statDifferent(oldStat, newStat) ||
|
2017-03-30 05:04:42 -04:00
|
|
|
!bytes.Equal(oldChild.capability, newChild.capability) {
|
2013-11-11 08:35:29 -05:00
|
|
|
change := Change{
|
|
|
|
Path: newChild.path(),
|
|
|
|
Kind: ChangeModify,
|
|
|
|
}
|
|
|
|
*changes = append(*changes, change)
|
2014-07-12 18:28:04 -04:00
|
|
|
newChild.added = true
|
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
|
|
|
|
2014-07-12 18:28:04 -04:00
|
|
|
// 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.
|
2015-06-01 19:42:27 -04:00
|
|
|
// 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) {
|
2014-07-12 18:28:04 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-11-11 08:35:29 -05:00
|
|
|
}
|
|
|
|
|
2015-08-03 21:52:54 -04:00
|
|
|
// Changes add changes to file information.
|
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 {
|
2015-06-01 19:42:27 -04:00
|
|
|
// As this runs on the daemon side, file paths are OS specific.
|
2013-11-11 08:35:29 -05:00
|
|
|
root := &FileInfo{
|
2015-06-01 19:42:27 -04:00
|
|
|
name: string(os.PathSeparator),
|
2013-11-11 08:35:29 -05:00
|
|
|
children: make(map[string]*FileInfo),
|
|
|
|
}
|
|
|
|
return root
|
|
|
|
}
|
|
|
|
|
2014-09-10 23:30:52 -04:00
|
|
|
// 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.
|
2013-11-11 08:35:29 -05:00
|
|
|
func ChangesDirs(newDir, oldDir string) ([]Change, error) {
|
2014-05-13 08:23:56 -04:00
|
|
|
var (
|
|
|
|
oldRoot, newRoot *FileInfo
|
|
|
|
)
|
2015-04-14 15:28:54 -04:00
|
|
|
if oldDir == "" {
|
2021-08-24 06:10:50 -04:00
|
|
|
emptyDir, err := os.MkdirTemp("", "empty")
|
2015-04-14 15:28:54 -04:00
|
|
|
if err != nil {
|
2014-05-13 08:23:56 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-14 15:28:54 -04:00
|
|
|
defer os.Remove(emptyDir)
|
|
|
|
oldDir = emptyDir
|
|
|
|
}
|
|
|
|
oldRoot, newRoot, err := collectFileInfoForChanges(oldDir, newDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2013-11-11 08:35:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return newRoot.Changes(oldRoot), nil
|
2013-11-01 00:52:39 -04:00
|
|
|
}
|
2013-11-07 21:48:52 -05:00
|
|
|
|
2014-09-19 14:02:12 -04:00
|
|
|
// ChangesSize calculates the size in bytes of the provided changes, based on newDir.
|
2013-11-19 04:36:54 -05:00
|
|
|
func ChangesSize(newDir string, changes []Change) int64 {
|
2015-10-12 15:11:22 -04:00
|
|
|
var (
|
|
|
|
size int64
|
|
|
|
sf = make(map[uint64]struct{})
|
|
|
|
)
|
2013-11-19 04:36:54 -05:00
|
|
|
for _, change := range changes {
|
|
|
|
if change.Kind == ChangeModify || change.Kind == ChangeAdd {
|
|
|
|
file := filepath.Join(newDir, change.Path)
|
2015-10-12 15:11:22 -04:00
|
|
|
fileInfo, err := os.Lstat(file)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Can not stat %q: %s", file, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2013-11-19 04:36:54 -05:00
|
|
|
if fileInfo != nil && !fileInfo.IsDir() {
|
2015-10-12 15:11:22 -04:00
|
|
|
if hasHardlinks(fileInfo) {
|
|
|
|
inode := getIno(fileInfo)
|
|
|
|
if _, ok := sf[inode]; !ok {
|
|
|
|
size += fileInfo.Size()
|
|
|
|
sf[inode] = struct{}{}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
size += fileInfo.Size()
|
|
|
|
}
|
2013-11-19 04:36:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return size
|
|
|
|
}
|
|
|
|
|
2014-09-19 14:02:12 -04:00
|
|
|
// ExportChanges produces an Archive from the provided changes, relative to dir.
|
2016-10-20 19:40:59 -04:00
|
|
|
func ExportChanges(dir string, changes []Change, uidMaps, gidMaps []idtools.IDMap) (io.ReadCloser, error) {
|
2013-12-16 07:47:09 -05:00
|
|
|
reader, writer := io.Pipe()
|
|
|
|
go func() {
|
2017-08-21 20:17:46 -04:00
|
|
|
ta := newTarAppender(idtools.NewIDMappingsFromMaps(uidMaps, gidMaps), writer, nil)
|
2017-05-24 14:10:15 -04:00
|
|
|
|
2014-09-15 14:45:53 -04:00
|
|
|
// this buffer is needed for the duration of this piped stream
|
|
|
|
defer pools.BufioWriter32KPool.Put(ta.Buffer)
|
|
|
|
|
2015-02-05 05:48:58 -05:00
|
|
|
sort.Sort(changesByPath(changes))
|
|
|
|
|
2013-12-16 07:47:09 -05:00
|
|
|
// 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)
|
2015-09-29 13:18:28 -04:00
|
|
|
whiteOut := filepath.Join(whiteOutDir, WhiteoutPrefix+whiteOutBase)
|
2014-05-30 20:03:51 -04:00
|
|
|
timestamp := time.Now()
|
2013-12-16 07:47:09 -05:00
|
|
|
hdr := &tar.Header{
|
|
|
|
Name: whiteOut[1:],
|
|
|
|
Size: 0,
|
2014-05-30 20:03:51 -04:00
|
|
|
ModTime: timestamp,
|
|
|
|
AccessTime: timestamp,
|
|
|
|
ChangeTime: timestamp,
|
2013-12-16 07:47:09 -05:00
|
|
|
}
|
2014-09-15 14:45:53 -04:00
|
|
|
if err := ta.TarWriter.WriteHeader(hdr); err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Can't write whiteout header: %s", err)
|
2013-12-16 07:47:09 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
path := filepath.Join(dir, change.Path)
|
2014-09-15 14:45:53 -04:00
|
|
|
if err := ta.addTarFile(path, change.Path[1:]); err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Can't add file %s to tar: %s", path, err)
|
2013-12-16 07:47:09 -05:00
|
|
|
}
|
|
|
|
}
|
2013-11-11 17:30:38 -05:00
|
|
|
}
|
2014-01-17 05:21:36 -05:00
|
|
|
|
2013-12-16 07:47:09 -05:00
|
|
|
// Make sure to check the error on Close.
|
2014-09-15 14:45:53 -04:00
|
|
|
if err := ta.TarWriter.Close(); err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Can't close layer: %s", err)
|
2013-11-11 17:30:38 -05:00
|
|
|
}
|
2014-10-28 17:01:10 -04:00
|
|
|
if err := writer.Close(); err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("failed close Changes writer: %s", err)
|
2014-10-28 17:01:10 -04:00
|
|
|
}
|
2013-12-16 07:47:09 -05:00
|
|
|
}()
|
|
|
|
return reader, nil
|
2013-11-07 21:48:52 -05:00
|
|
|
}
|