diff options
author | Matt Layher <mdlayher@gmail.com> | 2015-08-27 14:52:06 -0400 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2015-11-13 02:02:32 +0000 |
commit | 3a3049897c0c62907da398819f61d77199df52ad (patch) | |
tree | e4f792ca8eb283609c84f114fb35d53d0cc4fedd /src/archive/tar/writer.go | |
parent | 7bb38f6e470995d54a8bac3a67f997efc1f60c69 (diff) | |
download | go-git-3a3049897c0c62907da398819f61d77199df52ad.tar.gz |
archive/tar: make output deterministic
Replaces PID in PaxHeaders with 0. Sorts PAX header keys before writing
them to the archive.
Fixes #12358
Change-Id: If239f89c85f1c9d9895a253fb06a47ad44960124
Reviewed-on: https://go-review.googlesource.com/13975
Reviewed-by: Russ Cox <rsc@golang.org>
Reviewed-by: Joe Tsai <joetsai@digital-static.net>
Diffstat (limited to 'src/archive/tar/writer.go')
-rw-r--r-- | src/archive/tar/writer.go | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/src/archive/tar/writer.go b/src/archive/tar/writer.go index 3547c1760a..0165b2259c 100644 --- a/src/archive/tar/writer.go +++ b/src/archive/tar/writer.go @@ -12,8 +12,8 @@ import ( "errors" "fmt" "io" - "os" "path" + "sort" "strconv" "strings" "time" @@ -288,11 +288,11 @@ func (tw *Writer) writePAXHeader(hdr *Header, paxHeaders map[string]string) erro // succeed, and seems harmless enough. ext.ModTime = hdr.ModTime // The spec asks that we namespace our pseudo files - // with the current pid. - pid := os.Getpid() + // with the current pid. However, this results in differing outputs + // for identical inputs. As such, the constant 0 is now used instead. + // golang.org/issue/12358 dir, file := path.Split(hdr.Name) - fullName := path.Join(dir, - fmt.Sprintf("PaxHeaders.%d", pid), file) + fullName := path.Join(dir, "PaxHeaders.0", file) ascii := toASCII(fullName) if len(ascii) > 100 { @@ -302,8 +302,15 @@ func (tw *Writer) writePAXHeader(hdr *Header, paxHeaders map[string]string) erro // Construct the body var buf bytes.Buffer - for k, v := range paxHeaders { - fmt.Fprint(&buf, paxHeader(k+"="+v)) + // Keys are sorted before writing to body to allow deterministic output. + var keys []string + for k := range paxHeaders { + keys = append(keys, k) + } + sort.Strings(keys) + + for _, k := range keys { + fmt.Fprint(&buf, paxHeader(k+"="+paxHeaders[k])) } ext.Size = int64(len(buf.Bytes())) |