Skip to content

Commit

Permalink
Change tar time stabilizer to use epoch (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
msuozzo authored Oct 29, 2024
1 parent 15e6dea commit 9b80385
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 13 deletions.
3 changes: 1 addition & 2 deletions docs/builds/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/archive/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 2 additions & 6 deletions pkg/archive/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions pkg/archive/tar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")},
},
},
{
Expand All @@ -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")},
},
},
{
Expand All @@ -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")},
},
},
}
Expand Down

0 comments on commit 9b80385

Please sign in to comment.