Add whiteout handling by switching to a two-phase approach. (#139)

* Add whiteout handling by switching to a two-phase approach.

Also only handle hardlinks within one layer

* Simplify the run test.
This commit is contained in:
dlorenc
2018-04-23 12:50:21 -07:00
committed by GitHub
parent 7ceba77ef0
commit 844d9ef0d9
10 changed files with 107 additions and 60 deletions
+19 -5
View File
@@ -23,6 +23,7 @@ import (
"io"
"io/ioutil"
"os"
"path/filepath"
"syscall"
pkgutil "github.com/GoogleContainerTools/container-diff/pkg/util"
@@ -31,10 +32,8 @@ import (
"github.com/sirupsen/logrus"
)
var hardlinks = make(map[uint64]string)
// AddToTar adds the file i to tar w at path p
func AddToTar(p string, i os.FileInfo, w *tar.Writer) error {
func AddToTar(p string, i os.FileInfo, hardlinks map[uint64]string, w *tar.Writer) error {
linkDst := ""
if i.Mode()&os.ModeSymlink != 0 {
var err error
@@ -49,7 +48,7 @@ func AddToTar(p string, i os.FileInfo, w *tar.Writer) error {
}
hdr.Name = p
hardlink, linkDst := checkHardlink(p, i)
hardlink, linkDst := checkHardlink(p, hardlinks, i)
if hardlink {
hdr.Linkname = linkDst
hdr.Typeflag = tar.TypeLink
@@ -72,8 +71,23 @@ func AddToTar(p string, i os.FileInfo, w *tar.Writer) error {
return nil
}
func Whiteout(p string, w *tar.Writer) error {
dir := filepath.Dir(p)
name := ".wh." + filepath.Base(p)
th := &tar.Header{
Name: filepath.Join(dir, name),
Size: 0,
}
if err := w.WriteHeader(th); err != nil {
return err
}
return nil
}
// Returns true if path is hardlink, and the link destination
func checkHardlink(p string, i os.FileInfo) (bool, string) {
func checkHardlink(p string, hardlinks map[uint64]string, i os.FileInfo) (bool, string) {
hardlink := false
linkDst := ""
if sys := i.Sys(); sys != nil {
+1 -1
View File
@@ -101,7 +101,7 @@ func createTar(testdir string, writer io.Writer) error {
if err != nil {
return err
}
if err := AddToTar(filePath, fi, w); err != nil {
if err := AddToTar(filePath, fi, map[uint64]string{}, w); err != nil {
return err
}
}