Skip to content

Commit

Permalink
os, syscall: test Chtimes on directories, fix on Windows
Browse files Browse the repository at this point in the history
Fixes golang#8090.

LGTM=alex.brainman
R=alex.brainman
CC=golang-codereviews
https://golang.org/cl/154020043
  • Loading branch information
rsc authored and wheatman committed Jun 25, 2018
1 parent 03a2ec9 commit d9f8e67
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
47 changes: 38 additions & 9 deletions src/os/os_test.go
Expand Up @@ -126,7 +126,22 @@ func newFile(testName string, t *testing.T) (f *File) {
}
f, err := ioutil.TempFile(dir, "_Go_"+testName)
if err != nil {
t.Fatalf("open %s: %s", testName, err)
t.Fatalf("TempFile %s: %s", testName, err)
}
return
}

func newDir(testName string, t *testing.T) (name string) {
// Use a local file system, not NFS.
// On Unix, override $TMPDIR in case the user
// has it set to an NFS-mounted directory.
dir := ""
if runtime.GOOS != "android" && runtime.GOOS != "windows" {
dir = "/tmp"
}
name, err := ioutil.TempDir(dir, "_Go_"+testName)
if err != nil {
t.Fatalf("TempDir %s: %s", testName, err)
}
return
}
Expand Down Expand Up @@ -761,35 +776,49 @@ func TestTruncate(t *testing.T) {
}
}

// Use TempDir() to make sure we're on a local file system,
// Use TempDir (via newFile) to make sure we're on a local file system,
// so that timings are not distorted by latency and caching.
// On NFS, timings can be off due to caching of meta-data on
// NFS servers (Issue 848).
func TestChtimes(t *testing.T) {
f := newFile("TestChtimes", t)
defer Remove(f.Name())
defer f.Close()

f.Write([]byte("hello, world\n"))
f.Close()

st, err := Stat(f.Name())
testChtimes(t, f.Name())
}

// Use TempDir (via newDir) to make sure we're on a local file system,
// so that timings are not distorted by latency and caching.
// On NFS, timings can be off due to caching of meta-data on
// NFS servers (Issue 848).
func TestChtimesDir(t *testing.T) {
name := newDir("TestChtimes", t)
defer RemoveAll(name)

testChtimes(t, name)
}

func testChtimes(t *testing.T, name string) {
st, err := Stat(name)
if err != nil {
t.Fatalf("Stat %s: %s", f.Name(), err)
t.Fatalf("Stat %s: %s", name, err)
}
preStat := st

// Move access and modification time back a second
at := Atime(preStat)
mt := preStat.ModTime()
err = Chtimes(f.Name(), at.Add(-time.Second), mt.Add(-time.Second))
err = Chtimes(name, at.Add(-time.Second), mt.Add(-time.Second))
if err != nil {
t.Fatalf("Chtimes %s: %s", f.Name(), err)
t.Fatalf("Chtimes %s: %s", name, err)
}

st, err = Stat(f.Name())
st, err = Stat(name)
if err != nil {
t.Fatalf("second Stat %s: %s", f.Name(), err)
t.Fatalf("second Stat %s: %s", name, err)
}
postStat := st

Expand Down
4 changes: 2 additions & 2 deletions src/syscall/syscall_windows.go
Expand Up @@ -468,7 +468,7 @@ func Utimes(path string, tv []Timeval) (err error) {
}
h, e := CreateFile(pathp,
FILE_WRITE_ATTRIBUTES, FILE_SHARE_WRITE, nil,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0)
if e != nil {
return e
}
Expand All @@ -488,7 +488,7 @@ func UtimesNano(path string, ts []Timespec) (err error) {
}
h, e := CreateFile(pathp,
FILE_WRITE_ATTRIBUTES, FILE_SHARE_WRITE, nil,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0)
if e != nil {
return e
}
Expand Down

0 comments on commit d9f8e67

Please sign in to comment.