From bd13c53f8dc1504c9e681dfabef2afc1685ccec7 Mon Sep 17 00:00:00 2001 From: Derek McGowan Date: Thu, 23 Jun 2016 13:34:38 -0700 Subject: [PATCH] Fix overlay2 ignoring whiteout files Currently when overlay creates a whiteout file then the overlay2 layer is archived, the correct tar header will be created for the whiteout file, but the tar logic will then attempt to open the file causing a failure. When tar encounters such failures the file is skipped and excluded for the archive, causing the whiteout to be ignored. By skipping the copy of empty files, no open attempt will be made on whiteout files. Fixes #23863 Signed-off-by: Derek McGowan (github: dmcgowan) --- .../graphdriver/graphtest/graphtest_unix.go | 20 ++++++++++++++ daemon/graphdriver/graphtest/testutil.go | 26 +++++++++++++++++++ pkg/archive/archive.go | 2 +- pkg/archive/archive_linux.go | 1 + 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/daemon/graphdriver/graphtest/graphtest_unix.go b/daemon/graphdriver/graphtest/graphtest_unix.go index 4762c85556..b83fb13e3e 100644 --- a/daemon/graphdriver/graphtest/graphtest_unix.go +++ b/daemon/graphdriver/graphtest/graphtest_unix.go @@ -197,6 +197,8 @@ func DriverTestDiffApply(t testing.TB, fileCount int, drivername string, driverO defer PutDriver(t) base := stringid.GenerateRandomID() upper := stringid.GenerateRandomID() + deleteFile := "file-remove.txt" + deleteFileContent := []byte("This file should get removed in upper!") if err := driver.Create(base, "", "", nil); err != nil { t.Fatal(err) @@ -206,6 +208,10 @@ func DriverTestDiffApply(t testing.TB, fileCount int, drivername string, driverO t.Fatal(err) } + if err := addFile(driver, base, deleteFile, deleteFileContent); err != nil { + t.Fatal(err) + } + if err := driver.Create(upper, base, "", nil); err != nil { t.Fatal(err) } @@ -213,6 +219,11 @@ func DriverTestDiffApply(t testing.TB, fileCount int, drivername string, driverO if err := addManyFiles(driver, upper, fileCount, 6); err != nil { t.Fatal(err) } + + if err := removeFile(driver, upper, deleteFile); err != nil { + t.Fatal(err) + } + diffSize, err := driver.DiffSize(upper, "") if err != nil { t.Fatal(err) @@ -227,6 +238,10 @@ func DriverTestDiffApply(t testing.TB, fileCount int, drivername string, driverO t.Fatal(err) } + if err := checkFile(driver, diff, deleteFile, deleteFileContent); err != nil { + t.Fatal(err) + } + arch, err := driver.Diff(upper, base) if err != nil { t.Fatal(err) @@ -248,9 +263,14 @@ func DriverTestDiffApply(t testing.TB, fileCount int, drivername string, driverO if applyDiffSize != diffSize { t.Fatalf("Apply diff size different, got %d, expected %d", applyDiffSize, diffSize) } + if err := checkManyFiles(driver, diff, fileCount, 6); err != nil { t.Fatal(err) } + + if err := checkFileRemoved(driver, diff, deleteFile); err != nil { + t.Fatal(err) + } } // DriverTestChanges tests computed changes on a layer matches changes made diff --git a/daemon/graphdriver/graphtest/testutil.go b/daemon/graphdriver/graphtest/testutil.go index 5917d0dac4..dc3585751a 100644 --- a/daemon/graphdriver/graphtest/testutil.go +++ b/daemon/graphdriver/graphtest/testutil.go @@ -78,6 +78,32 @@ func addFile(drv graphdriver.Driver, layer, filename string, content []byte) err return ioutil.WriteFile(path.Join(root, filename), content, 0755) } +func removeFile(drv graphdriver.Driver, layer, filename string) error { + root, err := drv.Get(layer, "") + if err != nil { + return err + } + defer drv.Put(layer) + + return os.Remove(path.Join(root, filename)) +} + +func checkFileRemoved(drv graphdriver.Driver, layer, filename string) error { + root, err := drv.Get(layer, "") + if err != nil { + return err + } + defer drv.Put(layer) + + if _, err := os.Stat(path.Join(root, filename)); err == nil { + return fmt.Errorf("file still exists: %s", path.Join(root, filename)) + } else if !os.IsNotExist(err) { + return err + } + + return nil +} + func addManyFiles(drv graphdriver.Driver, layer string, count int, seed int64) error { root, err := drv.Get(layer, "") if err != nil { diff --git a/pkg/archive/archive.go b/pkg/archive/archive.go index 4f0b4ffefe..aec3b393de 100644 --- a/pkg/archive/archive.go +++ b/pkg/archive/archive.go @@ -359,7 +359,7 @@ func (ta *tarAppender) addTarFile(path, name string) error { return err } - if hdr.Typeflag == tar.TypeReg { + if hdr.Typeflag == tar.TypeReg && hdr.Size > 0 { file, err := os.Open(path) if err != nil { return err diff --git a/pkg/archive/archive_linux.go b/pkg/archive/archive_linux.go index 86d2c7fe34..277ff98885 100644 --- a/pkg/archive/archive_linux.go +++ b/pkg/archive/archive_linux.go @@ -26,6 +26,7 @@ func (overlayWhiteoutConverter) ConvertWrite(hdr *tar.Header, path string, fi os hdr.Name = WhiteoutPrefix + hdr.Name hdr.Mode = 0600 hdr.Typeflag = tar.TypeReg + hdr.Size = 0 } if fi.Mode()&os.ModeDir != 0 {