Skip to content

Commit 3f60ec5

Browse files
author
Preetha
authored
Merge pull request #107 from hashicorp/dont-preserve-zero-access-times
Preserve access and modify time in tar only when valid
2 parents b345bfc + 9ac4135 commit 3f60ec5

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

decompress_tar.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"os"
88
"path/filepath"
9+
"time"
910
)
1011

1112
// untar is a shared helper for untarring an archive. The reader should provide
@@ -14,6 +15,7 @@ func untar(input io.Reader, dst, src string, dir bool) error {
1415
tarR := tar.NewReader(input)
1516
done := false
1617
dirHdrs := []*tar.Header{}
18+
now := time.Now()
1719
for {
1820
hdr, err := tarR.Next()
1921
if err == io.EOF {
@@ -95,8 +97,16 @@ func untar(input io.Reader, dst, src string, dir bool) error {
9597
return err
9698
}
9799

98-
// Set the access and modification time
99-
if err := os.Chtimes(path, hdr.AccessTime, hdr.ModTime); err != nil {
100+
// Set the access and modification time if valid, otherwise default to current time
101+
aTime := now
102+
mTime := now
103+
if hdr.AccessTime.Unix() > 0 {
104+
aTime = hdr.AccessTime
105+
}
106+
if hdr.ModTime.Unix() > 0 {
107+
mTime = hdr.ModTime
108+
}
109+
if err := os.Chtimes(path, aTime, mTime); err != nil {
100110
return err
101111
}
102112
}
@@ -109,7 +119,15 @@ func untar(input io.Reader, dst, src string, dir bool) error {
109119
return err
110120
}
111121
// Set the mtime/atime attributes since they would have been changed during extraction
112-
if err := os.Chtimes(path, dirHdr.AccessTime, dirHdr.ModTime); err != nil {
122+
aTime := now
123+
mTime := now
124+
if dirHdr.AccessTime.Unix() > 0 {
125+
aTime = dirHdr.AccessTime
126+
}
127+
if dirHdr.ModTime.Unix() > 0 {
128+
mTime = dirHdr.ModTime
129+
}
130+
if err := os.Chtimes(path, aTime, mTime); err != nil {
113131
return err
114132
}
115133
}

decompress_testing.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,13 @@ func TestDecompressor(t testing.T, d Decompressor, cases []TestDecompressCase) {
7272

7373
if tc.Mtime != nil {
7474
actual := fi.ModTime()
75-
expected := *tc.Mtime
76-
if actual != expected {
77-
t.Fatalf("err %s: expected mtime '%s' for %s, got '%s'", tc.Input, expected.String(), dst, actual.String())
75+
if tc.Mtime.Unix() > 0 {
76+
expected := *tc.Mtime
77+
if actual != expected {
78+
t.Fatalf("err %s: expected mtime '%s' for %s, got '%s'", tc.Input, expected.String(), dst, actual.String())
79+
}
80+
} else if actual.Unix() <= 0 {
81+
t.Fatalf("err %s: expected mtime to be > 0, got '%s'", actual.String())
7882
}
7983
}
8084

@@ -103,10 +107,15 @@ func TestDecompressor(t testing.T, d Decompressor, cases []TestDecompressCase) {
103107
t.Fatalf("err: %s", err)
104108
}
105109
actual := fi.ModTime()
106-
expected := *tc.Mtime
107-
if actual != expected {
108-
t.Fatalf("err %s: expected mtime '%s' for %s, got '%s'", tc.Input, expected.String(), path, actual.String())
110+
if tc.Mtime.Unix() > 0 {
111+
expected := *tc.Mtime
112+
if actual != expected {
113+
t.Fatalf("err %s: expected mtime '%s' for %s, got '%s'", tc.Input, expected.String(), path, actual.String())
114+
}
115+
} else if actual.Unix() < 0 {
116+
t.Fatalf("err %s: expected mtime to be > 0, got '%s'", actual.String())
109117
}
118+
110119
}
111120
}
112121
}()

0 commit comments

Comments
 (0)