Skip to content

Commit c68e60d

Browse files
committed
archive: add Windows creation time test
Add a regression test verifying that updating file timestamps also updates the file creation time on Windows. The current implementation preserves this behavior, which is relied on by archive extraction and should remain unchanged during future refactoring. Commit df55fdf replaced the use of our local chtimes implementation for os.Chtimes, which may be less advanced and not set the creation time. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent a11565d commit c68e60d

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

archive_windows_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ package archive
55
import (
66
"os"
77
"path/filepath"
8+
"syscall"
89
"testing"
10+
"time"
11+
12+
"gotest.tools/v3/assert"
913
)
1014

1115
func TestCopyFileWithInvalidDest(t *testing.T) {
@@ -67,3 +71,35 @@ func TestChmodTarEntry(t *testing.T) {
6771
}
6872
}
6973
}
74+
75+
// TestChtimesSetsCreationTime verifies that updating file timestamps also
76+
// sets the Windows creation time from the modification time.
77+
func TestChtimesSetsCreationTime(t *testing.T) {
78+
tmpDir := t.TempDir()
79+
file := filepath.Join(tmpDir, "file")
80+
assert.NilError(t, os.WriteFile(file, []byte("hello toto"), 0o644))
81+
82+
root, err := os.OpenRoot(tmpDir)
83+
assert.NilError(t, err)
84+
t.Cleanup(func() { _ = root.Close() })
85+
86+
aTime := time.Date(2000, time.January, 2, 3, 4, 5, 0, time.UTC)
87+
mTime := time.Date(2001, time.February, 3, 4, 5, 6, 0, time.UTC)
88+
assert.NilError(t, root.Chtimes("file", aTime, mTime))
89+
90+
fi, err := root.Stat("file")
91+
assert.NilError(t, err)
92+
93+
data, ok := fi.Sys().(*syscall.Win32FileAttributeData)
94+
assert.Assert(t, ok)
95+
96+
var (
97+
creationTime = time.Unix(0, data.CreationTime.Nanoseconds()).UTC()
98+
accessTime = time.Unix(0, data.LastAccessTime.Nanoseconds()).UTC()
99+
modificationTime = time.Unix(0, data.LastWriteTime.Nanoseconds()).UTC()
100+
)
101+
102+
assert.Equal(t, creationTime, mTime)
103+
assert.Equal(t, accessTime, aTime)
104+
assert.Equal(t, modificationTime, mTime)
105+
}

0 commit comments

Comments
 (0)