From 9b8038595d4065560ac6fd46e0e03a9b868b6a30 Mon Sep 17 00:00:00 2001 From: Matthew Suozzo Date: Tue, 29 Oct 2024 14:21:01 -0400 Subject: [PATCH] Change tar time stabilizer to use epoch (#136) --- docs/builds/ArtifactEquivalence@v0.1.md | 3 +-- pkg/archive/common.go | 2 +- pkg/archive/tar.go | 8 ++------ pkg/archive/tar_test.go | 10 ++++++---- 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/docs/builds/ArtifactEquivalence@v0.1.md b/docs/builds/ArtifactEquivalence@v0.1.md index 2f5f8111..5744afc0 100644 --- a/docs/builds/ArtifactEquivalence@v0.1.md +++ b/docs/builds/ArtifactEquivalence@v0.1.md @@ -146,8 +146,7 @@ process: 1. Create new tar entries with: - The same entry name - The same file contents - - ModTime and AccessTime as 1985 Oct 26 8:15am UTC (an arbitrary date - time) + - ModTime and AccessTime to the UNIX epoch - Uid and Gid of 0 - Empty Uname and Gname - Mode 0777 diff --git a/pkg/archive/common.go b/pkg/archive/common.go index 1776241f..1d4836b9 100644 --- a/pkg/archive/common.go +++ b/pkg/archive/common.go @@ -27,7 +27,7 @@ const ( RawFormat ) -// StabilizeOpts aggregates sanitizers to be used in stabilization. +// StabilizeOpts aggregates stabilizers to be used in stabilization. type StabilizeOpts struct { Stabilizers []any } diff --git a/pkg/archive/tar.go b/pkg/archive/tar.go index fdb6a334..031495cd 100644 --- a/pkg/archive/tar.go +++ b/pkg/archive/tar.go @@ -31,10 +31,6 @@ import ( "github.com/pkg/errors" ) -// Pick some arbitrary time to set all the time fields. -// Source: https://github.com/npm/pacote/blob/main/lib/util/tar-create-options.js#L28 -var arbitraryTime = time.Date(1985, time.October, 26, 8, 15, 0, 0, time.UTC) - // TarEntry represents an entry in a tar archive. type TarEntry struct { *tar.Header @@ -87,8 +83,8 @@ var StableTarFileOrder = TarArchiveStabilizer{ var StableTarTime = TarEntryStabilizer{ Name: "tar-time", Func: func(e *TarEntry) { - e.ModTime = arbitraryTime - e.AccessTime = arbitraryTime + e.ModTime = time.UnixMilli(0) + e.AccessTime = time.UnixMilli(0) e.ChangeTime = time.Time{} // NOTE: Without a PAX record, the tar library will disregard this value // and write the format as USTAR. Setting 'atime' ensures at least one diff --git a/pkg/archive/tar_test.go b/pkg/archive/tar_test.go index 1095af29..6aaa6b68 100644 --- a/pkg/archive/tar_test.go +++ b/pkg/archive/tar_test.go @@ -24,6 +24,8 @@ import ( "github.com/google/go-cmp/cmp" ) +var epoch = time.UnixMilli(0) + func TestStabilizeTar(t *testing.T) { testCases := []struct { test string @@ -39,7 +41,7 @@ func TestStabilizeTar(t *testing.T) { {&tar.Header{Name: "foo", Typeflag: tar.TypeReg, Size: 3, Mode: 0644, ModTime: time.Now(), AccessTime: time.Now()}, []byte("foo")}, }, expected: []*TarEntry{ - {&tar.Header{Name: "foo", Typeflag: tar.TypeReg, Size: 3, Mode: 0777, ModTime: arbitraryTime, AccessTime: arbitraryTime, PAXRecords: map[string]string{"atime": "499162500"}, Format: tar.FormatPAX}, []byte("foo")}, + {&tar.Header{Name: "foo", Typeflag: tar.TypeReg, Size: 3, Mode: 0777, ModTime: epoch, AccessTime: epoch, PAXRecords: map[string]string{"atime": "0"}, Format: tar.FormatPAX}, []byte("foo")}, }, }, { @@ -49,8 +51,8 @@ func TestStabilizeTar(t *testing.T) { {&tar.Header{Name: "bar", Typeflag: tar.TypeReg, Size: 3, Mode: 0644}, []byte("bar")}, }, expected: []*TarEntry{ - {&tar.Header{Name: "bar", Typeflag: tar.TypeReg, Size: 3, Mode: 0777, ModTime: arbitraryTime, AccessTime: arbitraryTime, PAXRecords: map[string]string{"atime": "499162500"}, Format: tar.FormatPAX}, []byte("bar")}, - {&tar.Header{Name: "foo", Typeflag: tar.TypeReg, Size: 3, Mode: 0777, ModTime: arbitraryTime, AccessTime: arbitraryTime, PAXRecords: map[string]string{"atime": "499162500"}, Format: tar.FormatPAX}, []byte("foo")}, + {&tar.Header{Name: "bar", Typeflag: tar.TypeReg, Size: 3, Mode: 0777, ModTime: epoch, AccessTime: epoch, PAXRecords: map[string]string{"atime": "0"}, Format: tar.FormatPAX}, []byte("bar")}, + {&tar.Header{Name: "foo", Typeflag: tar.TypeReg, Size: 3, Mode: 0777, ModTime: epoch, AccessTime: epoch, PAXRecords: map[string]string{"atime": "0"}, Format: tar.FormatPAX}, []byte("foo")}, }, }, { @@ -59,7 +61,7 @@ func TestStabilizeTar(t *testing.T) { {&tar.Header{Name: "foo", Typeflag: tar.TypeReg, Size: 3, Uid: 10, Uname: "user", Gid: 30, Gname: "group"}, []byte("foo")}, }, expected: []*TarEntry{ - {&tar.Header{Name: "foo", Typeflag: tar.TypeReg, Size: 3, Mode: 0777, ModTime: arbitraryTime, AccessTime: arbitraryTime, PAXRecords: map[string]string{"atime": "499162500"}, Format: tar.FormatPAX}, []byte("foo")}, + {&tar.Header{Name: "foo", Typeflag: tar.TypeReg, Size: 3, Mode: 0777, ModTime: epoch, AccessTime: epoch, PAXRecords: map[string]string{"atime": "0"}, Format: tar.FormatPAX}, []byte("foo")}, }, }, }