diff --git a/src/compress/gzip/gunzip.go b/src/compress/gzip/gunzip.go index bc303898b3..8bd750bd8b 100644 --- a/src/compress/gzip/gunzip.go +++ b/src/compress/gzip/gunzip.go @@ -186,7 +186,11 @@ func (z *Reader) readHeader() (hdr Header, err error) { return hdr, ErrHeader } flg := z.buf[3] - hdr.ModTime = time.Unix(int64(le.Uint32(z.buf[4:8])), 0) + if t := int64(le.Uint32(z.buf[4:8])); t > 0 { + // Section 2.3.1, the zero value for MTIME means that the + // modified time is not set. + hdr.ModTime = time.Unix(t, 0) + } // z.buf[8] is XFL and is currently ignored. hdr.OS = z.buf[9] z.digest = crc32.ChecksumIEEE(z.buf[:10]) diff --git a/src/compress/gzip/gzip.go b/src/compress/gzip/gzip.go index c70321970b..aafb442a66 100644 --- a/src/compress/gzip/gzip.go +++ b/src/compress/gzip/gzip.go @@ -10,6 +10,7 @@ import ( "fmt" "hash/crc32" "io" + "time" ) // These constants are copied from the flate package, so that code that imports @@ -143,10 +144,7 @@ func (z *Writer) Write(p []byte) (int, error) { // Write the GZIP header lazily. if !z.wroteHeader { z.wroteHeader = true - z.buf[0] = gzipID1 - z.buf[1] = gzipID2 - z.buf[2] = gzipDeflate - z.buf[3] = 0 + z.buf = [10]byte{0: gzipID1, 1: gzipID2, 2: gzipDeflate} if z.Extra != nil { z.buf[3] |= 0x04 } @@ -156,13 +154,15 @@ func (z *Writer) Write(p []byte) (int, error) { if z.Comment != "" { z.buf[3] |= 0x10 } - le.PutUint32(z.buf[4:8], uint32(z.ModTime.Unix())) + if z.ModTime.After(time.Unix(0, 0)) { + // Section 2.3.1, the zero value for MTIME means that the + // modified time is not set. + le.PutUint32(z.buf[4:8], uint32(z.ModTime.Unix())) + } if z.level == BestCompression { z.buf[8] = 2 } else if z.level == BestSpeed { z.buf[8] = 4 - } else { - z.buf[8] = 0 } z.buf[9] = z.OS n, z.err = z.w.Write(z.buf[:10]) diff --git a/src/compress/gzip/gzip_test.go b/src/compress/gzip/gzip_test.go index 09271b24e9..865c529f55 100644 --- a/src/compress/gzip/gzip_test.go +++ b/src/compress/gzip/gzip_test.go @@ -8,6 +8,7 @@ import ( "bufio" "bytes" "io/ioutil" + "reflect" "testing" "time" ) @@ -24,6 +25,9 @@ func TestEmpty(t *testing.T) { if err != nil { t.Fatalf("NewReader: %v", err) } + if want := (Header{OS: 255}); !reflect.DeepEqual(r.Header, want) { + t.Errorf("Header mismatch:\ngot %#v\nwant %#v", r.Header, want) + } b, err := ioutil.ReadAll(r) if err != nil { t.Fatalf("ReadAll: %v", err) diff --git a/src/compress/gzip/issue14937_test.go b/src/compress/gzip/issue14937_test.go index 432ad16b1c..e76d47cc4e 100644 --- a/src/compress/gzip/issue14937_test.go +++ b/src/compress/gzip/issue14937_test.go @@ -7,7 +7,6 @@ import ( "runtime" "strings" "testing" - "time" ) // Per golang.org/issue/14937, check that every .gz file @@ -16,8 +15,12 @@ func TestGZIPFilesHaveZeroMTimes(t *testing.T) { if testing.Short() && testenv.Builder() == "" { t.Skip("skipping in short mode") } + goroot, err := filepath.EvalSymlinks(runtime.GOROOT()) + if err != nil { + t.Fatal("error evaluating GOROOT: ", err) + } var files []string - err := filepath.Walk(runtime.GOROOT(), func(path string, info os.FileInfo, err error) error { + err = filepath.Walk(goroot, func(path string, info os.FileInfo, err error) error { if err != nil { return err } @@ -53,7 +56,7 @@ func checkZeroMTime(t *testing.T, path string) { return } defer gz.Close() - if !gz.ModTime.Equal(time.Unix(0, 0)) { + if !gz.ModTime.IsZero() { t.Errorf("gzip file %s has non-zero mtime (%s)", path, gz.ModTime) } }