Skip to content

Commit d6cfda7

Browse files
twavvgcalmettes-fbox
authored andcommitted
Use pax tar format (GoogleContainerTools#1809)
* Use PAX tar format * Add test case
1 parent f4da67c commit d6cfda7

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

pkg/util/tar_util.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ func (t *Tar) AddFileToTar(p string) error {
9797
// this makes this layer unnecessarily differ from a cached layer which does contain this information
9898
hdr.Uname = ""
9999
hdr.Gname = ""
100+
// use PAX format to preserve accurate mtime (match Docker behavior)
101+
hdr.Format = tar.FormatPAX
100102

101103
hardlink, linkDst := t.checkHardlink(p, i)
102104
if hardlink {

pkg/util/tar_util_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ limitations under the License.
1717
package util
1818

1919
import (
20+
"archive/tar"
21+
"bytes"
2022
"compress/gzip"
2123
"io"
2224
"io/ioutil"
2325
"os"
2426
"path/filepath"
2527
"testing"
28+
"time"
2629

2730
"github.com/GoogleContainerTools/kaniko/testutil"
2831
)
@@ -57,6 +60,39 @@ func Test_IsLocalTarArchive(t *testing.T) {
5760
}
5861
}
5962

63+
func Test_AddFileToTar(t *testing.T) {
64+
testDir, err := ioutil.TempDir("", "")
65+
if err != nil {
66+
t.Fatalf("err setting up temp dir: %v", err)
67+
}
68+
defer os.RemoveAll(testDir)
69+
70+
path := filepath.Join(testDir, regularFiles[0])
71+
if err := ioutil.WriteFile(path, []byte("hello"), os.ModePerm); err != nil {
72+
t.Fatal(err)
73+
}
74+
// use a pre-determined time with non-zero microseconds to avoid flakiness
75+
mtime := time.UnixMicro(1635533172891395)
76+
if err := os.Chtimes(path, mtime, mtime); err != nil {
77+
t.Fatal(err)
78+
}
79+
80+
buf := new(bytes.Buffer)
81+
tarw := NewTar(buf)
82+
if err := tarw.AddFileToTar(path); err != nil {
83+
t.Fatal(err)
84+
}
85+
tarw.Close()
86+
87+
// Check that the mtime is correct (#1808)
88+
tarReader := tar.NewReader(buf)
89+
hdr, err := tarReader.Next()
90+
if err != nil {
91+
t.Fatal(err)
92+
}
93+
testutil.CheckDeepEqual(t, mtime, hdr.ModTime)
94+
}
95+
6096
func setUpFilesAndTars(testDir string) error {
6197
regularFilesAndContents := map[string]string{
6298
regularFiles[0]: "",

0 commit comments

Comments
 (0)