aboutsummaryrefslogtreecommitdiff
path: root/src/archive
diff options
context:
space:
mode:
authorJannis Andrija Schnitzer <jannis@schnitzer.im>2019-04-08 07:31:59 +0000
committerDaniel Martí <mvdan@mvdan.cc>2019-04-08 07:55:52 +0000
commit20995f627422a3dce2039f06b5dfe1dc0ca174c6 (patch)
tree3892a70792ee5ecfffa47d923eeee05d8b9e9dbf /src/archive
parent9017b6149e0fcac8b60e5b755cddb98ea37daaf9 (diff)
downloadgo-20995f627422a3dce2039f06b5dfe1dc0ca174c6.tar.gz
go-20995f627422a3dce2039f06b5dfe1dc0ca174c6.zip
archive/zip: use Modified in FileHeader.FileInfo
The Modified field allows representation of extended timestamps, which provide more accuracy than the legacy MS-DOS timestamps. The FileInfo method provides an implementation of the os.FileInfo interface for files inside archives. With this change, we make FileInfo use the Modified field, if present, to return more detailed timestamps from its ModTime method. Fixes #28350 Change-Id: Ia31b5b871a3e61df38a3a1325787ae23ea0b8088 GitHub-Last-Rev: 13e94be3f8ba58717911354146670fc2bc594692 GitHub-Pull-Request: golang/go#28352 Reviewed-on: https://go-review.googlesource.com/c/go/+/144382 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
Diffstat (limited to 'src/archive')
-rw-r--r--src/archive/zip/struct.go13
-rw-r--r--src/archive/zip/zip_test.go41
2 files changed, 50 insertions, 4 deletions
diff --git a/src/archive/zip/struct.go b/src/archive/zip/struct.go
index bd637d185b..686e79781a 100644
--- a/src/archive/zip/struct.go
+++ b/src/archive/zip/struct.go
@@ -154,10 +154,15 @@ func (fi headerFileInfo) Size() int64 {
}
return int64(fi.fh.UncompressedSize)
}
-func (fi headerFileInfo) IsDir() bool { return fi.Mode().IsDir() }
-func (fi headerFileInfo) ModTime() time.Time { return fi.fh.ModTime() }
-func (fi headerFileInfo) Mode() os.FileMode { return fi.fh.Mode() }
-func (fi headerFileInfo) Sys() interface{} { return fi.fh }
+func (fi headerFileInfo) IsDir() bool { return fi.Mode().IsDir() }
+func (fi headerFileInfo) ModTime() time.Time {
+ if fi.fh.Modified.IsZero() {
+ return fi.fh.ModTime()
+ }
+ return fi.fh.Modified.UTC()
+}
+func (fi headerFileInfo) Mode() os.FileMode { return fi.fh.Mode() }
+func (fi headerFileInfo) Sys() interface{} { return fi.fh }
// FileInfoHeader creates a partially-populated FileHeader from an
// os.FileInfo.
diff --git a/src/archive/zip/zip_test.go b/src/archive/zip/zip_test.go
index 3d5c759851..efdb5bd044 100644
--- a/src/archive/zip/zip_test.go
+++ b/src/archive/zip/zip_test.go
@@ -114,6 +114,47 @@ func TestFileHeaderRoundTrip64(t *testing.T) {
testHeaderRoundTrip(fh, uint32max, fh.UncompressedSize64, t)
}
+func TestFileHeaderRoundTripModified(t *testing.T) {
+ fh := &FileHeader{
+ Name: "foo.txt",
+ UncompressedSize: 987654321,
+ Modified: time.Now().Local(),
+ ModifiedTime: 1234,
+ ModifiedDate: 5678,
+ }
+ fi := fh.FileInfo()
+ fh2, err := FileInfoHeader(fi)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if got, want := fh2.Modified, fh.Modified.UTC(); got != want {
+ t.Errorf("Modified: got %s, want %s\n", got, want)
+ }
+ if got, want := fi.ModTime(), fh.Modified.UTC(); got != want {
+ t.Errorf("Modified: got %s, want %s\n", got, want)
+ }
+}
+
+func TestFileHeaderRoundTripWithoutModified(t *testing.T) {
+ fh := &FileHeader{
+ Name: "foo.txt",
+ UncompressedSize: 987654321,
+ ModifiedTime: 1234,
+ ModifiedDate: 5678,
+ }
+ fi := fh.FileInfo()
+ fh2, err := FileInfoHeader(fi)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if got, want := fh2.ModTime(), fh.ModTime(); got != want {
+ t.Errorf("Modified: got %s, want %s\n", got, want)
+ }
+ if got, want := fi.ModTime(), fh.ModTime(); got != want {
+ t.Errorf("Modified: got %s, want %s\n", got, want)
+ }
+}
+
type repeatedByte struct {
off int64
b byte