Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strip file times to make tarballs more reproducible #126

Merged
merged 2 commits into from
Jan 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
mxey marked this conversation as resolved.
Show resolved Hide resolved

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