Skip to content

Commit

Permalink
add option to strip file times to make tarballs more reproducible (or…
Browse files Browse the repository at this point in the history
  • Loading branch information
mxey authored and shizhMSFT committed Jan 22, 2020
1 parent ea09e1e commit 93c4cc2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 4 additions & 1 deletion pkg/content/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ type FileStore struct {
DisableOverwrite bool
AllowPathTraversalOnWrite bool

// Reproducible enables stripping times from added files
Reproducible bool

root string
descriptor *sync.Map // map[digest.Digest]ocispec.Descriptor
pathMap *sync.Map
Expand Down Expand Up @@ -109,7 +112,7 @@ func (s *FileStore) descFromDir(name, mediaType, root string) (ocispec.Descripto
zw := gzip.NewWriter(io.MultiWriter(file, digester.Hash()))
defer zw.Close()
tarDigester := digest.Canonical.Digester()
if err := tarDirectory(root, name, io.MultiWriter(zw, tarDigester.Hash())); err != nil {
if err := tarDirectory(root, name, io.MultiWriter(zw, tarDigester.Hash()), s.Reproducible); err != nil {
return ocispec.Descriptor{}, err
}

Expand Down
9 changes: 8 additions & 1 deletion pkg/content/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"strings"
"time"

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

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

if stripTimes {
header.ModTime = time.Time{}
header.AccessTime = time.Time{}
header.ChangeTime = time.Time{}
}

// Write file
if err := tw.WriteHeader(header); err != nil {
return errors.Wrap(err, "tar")
Expand Down

0 comments on commit 93c4cc2

Please sign in to comment.