]> Cypherpunks repositories - gostls13.git/commitdiff
os, syscall: test Chtimes on directories, fix on Windows
authorRuss Cox <rsc@golang.org>
Mon, 6 Oct 2014 23:22:48 +0000 (19:22 -0400)
committerRuss Cox <rsc@golang.org>
Mon, 6 Oct 2014 23:22:48 +0000 (19:22 -0400)
Fixes #8090.

LGTM=alex.brainman
R=alex.brainman
CC=golang-codereviews
https://golang.org/cl/154020043

src/os/os_test.go
src/syscall/syscall_windows.go

index 973cc3a7bffce4f118db9d17a8ada67446818852..a30a2b0313ba35d17dddbc42c53dcf34069e3cab 100644 (file)
@@ -124,7 +124,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
 }
@@ -755,35 +770,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
 
index bda8214c3c44d624400fb17cbbad40665bfec070..e89fd096ace87ca77bd9c371cd069e4fa045cdd3 100644 (file)
@@ -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
        }
@@ -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
        }