Skip to content

Commit 93c4cc2

Browse files
mxeyshizhMSFT
authored andcommitted
add option to strip file times to make tarballs more reproducible (#126)
1 parent ea09e1e commit 93c4cc2

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

pkg/content/file.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ type FileStore struct {
2828
DisableOverwrite bool
2929
AllowPathTraversalOnWrite bool
3030

31+
// Reproducible enables stripping times from added files
32+
Reproducible bool
33+
3134
root string
3235
descriptor *sync.Map // map[digest.Digest]ocispec.Descriptor
3336
pathMap *sync.Map
@@ -109,7 +112,7 @@ func (s *FileStore) descFromDir(name, mediaType, root string) (ocispec.Descripto
109112
zw := gzip.NewWriter(io.MultiWriter(file, digester.Hash()))
110113
defer zw.Close()
111114
tarDigester := digest.Canonical.Digester()
112-
if err := tarDirectory(root, name, io.MultiWriter(zw, tarDigester.Hash())); err != nil {
115+
if err := tarDirectory(root, name, io.MultiWriter(zw, tarDigester.Hash()), s.Reproducible); err != nil {
113116
return ocispec.Descriptor{}, err
114117
}
115118

pkg/content/utils.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99
"path/filepath"
1010
"strings"
11+
"time"
1112

1213
digest "github.com/opencontainers/go-digest"
1314
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
@@ -22,7 +23,7 @@ func ResolveName(desc ocispec.Descriptor) (string, bool) {
2223

2324
// tarDirectory walks the directory specified by path, and tar those files with a new
2425
// path prefix.
25-
func tarDirectory(root, prefix string, w io.Writer) error {
26+
func tarDirectory(root, prefix string, w io.Writer, stripTimes bool) error {
2627
tw := tar.NewWriter(w)
2728
defer tw.Close()
2829
if err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
@@ -56,6 +57,12 @@ func tarDirectory(root, prefix string, w io.Writer) error {
5657
header.Uname = ""
5758
header.Gname = ""
5859

60+
if stripTimes {
61+
header.ModTime = time.Time{}
62+
header.AccessTime = time.Time{}
63+
header.ChangeTime = time.Time{}
64+
}
65+
5966
// Write file
6067
if err := tw.WriteHeader(header); err != nil {
6168
return errors.Wrap(err, "tar")

0 commit comments

Comments
 (0)