Commit 20995f62 authored by Jannis Andrija Schnitzer's avatar Jannis Andrija Schnitzer Committed by Daniel Martí

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: default avatarJoe Tsai <thebrokentoaster@gmail.com>
parent 9017b614
......@@ -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.
......
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment