Skip to content

Commit ddae7fb

Browse files
ianlancetaylorbradfitz
authored andcommitted
os: don't use test logger for Getwd
Otherwise, on systems for which syscall does not implement Getwd, a lot of unnecessary files and directories get added to the testlog, right up the root directory. This was causing tests on such systems to fail to cache in practice. Updates #22593 Change-Id: Ic8cb3450ea62aa0ca8eeb15754349f151cd76f85 Reviewed-on: https://go-review.googlesource.com/83455 Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]> Reviewed-by: Russ Cox <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 3f5c1ad commit ddae7fb

9 files changed

+58
-66
lines changed

src/os/file.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,16 @@ func Create(name string) (*File, error) {
258258
return OpenFile(name, O_RDWR|O_CREATE|O_TRUNC, 0666)
259259
}
260260

261+
// OpenFile is the generalized open call; most users will use Open
262+
// or Create instead. It opens the named file with specified flag
263+
// (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,
264+
// methods on the returned File can be used for I/O.
265+
// If there is an error, it will be of type *PathError.
266+
func OpenFile(name string, flag int, perm FileMode) (*File, error) {
267+
testlog.Open(name)
268+
return openFileNolog(name, flag, perm)
269+
}
270+
261271
// lstat is overridden in tests.
262272
var lstat = Lstat
263273

src/os/file_plan9.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package os
66

77
import (
88
"internal/poll"
9-
"internal/testlog"
109
"io"
1110
"runtime"
1211
"syscall"
@@ -80,14 +79,8 @@ func syscallMode(i FileMode) (o uint32) {
8079
return
8180
}
8281

83-
// OpenFile is the generalized open call; most users will use Open
84-
// or Create instead. It opens the named file with specified flag
85-
// (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,
86-
// methods on the returned File can be used for I/O.
87-
// If there is an error, it will be of type *PathError.
88-
func OpenFile(name string, flag int, perm FileMode) (*File, error) {
89-
testlog.Open(name)
90-
82+
// openFileNolog is the Plan 9 implementation of OpenFile.
83+
func openFileNolog(name string, flag int, perm FileMode) (*File, error) {
9184
var (
9285
fd int
9386
e error

src/os/file_unix.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package os
88

99
import (
1010
"internal/poll"
11-
"internal/testlog"
1211
"runtime"
1312
"syscall"
1413
)
@@ -154,14 +153,8 @@ func epipecheck(file *File, e error) {
154153
// On Unix-like systems, it is "/dev/null"; on Windows, "NUL".
155154
const DevNull = "/dev/null"
156155

157-
// OpenFile is the generalized open call; most users will use Open
158-
// or Create instead. It opens the named file with specified flag
159-
// (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,
160-
// methods on the returned File can be used for I/O.
161-
// If there is an error, it will be of type *PathError.
162-
func OpenFile(name string, flag int, perm FileMode) (*File, error) {
163-
testlog.Open(name)
164-
156+
// openFileNolog is the Unix implementation of OpenFile.
157+
func openFileNolog(name string, flag int, perm FileMode) (*File, error) {
165158
chmod := false
166159
if !supportsCreateWithStickyBit && flag&O_CREATE != 0 && perm&ModeSticky != 0 {
167160
if _, err := Stat(name); IsNotExist(err) {

src/os/file_windows.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package os
77
import (
88
"internal/poll"
99
"internal/syscall/windows"
10-
"internal/testlog"
1110
"runtime"
1211
"syscall"
1312
"unicode/utf16"
@@ -149,14 +148,8 @@ func openDir(name string) (file *File, err error) {
149148
return f, nil
150149
}
151150

152-
// OpenFile is the generalized open call; most users will use Open
153-
// or Create instead. It opens the named file with specified flag
154-
// (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,
155-
// methods on the returned File can be used for I/O.
156-
// If there is an error, it will be of type *PathError.
157-
func OpenFile(name string, flag int, perm FileMode) (*File, error) {
158-
testlog.Open(name)
159-
151+
// openFileNolog is the Windows implementation of OpenFile.
152+
func openFileNolog(name string, flag int, perm FileMode) (*File, error) {
160153
if name == "" {
161154
return nil, &PathError{"open", name, syscall.ENOENT}
162155
}

src/os/getwd.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ func Getwd() (dir string, err error) {
3030

3131
// Clumsy but widespread kludge:
3232
// if $PWD is set and matches ".", use it.
33-
dot, err := Stat(".")
33+
dot, err := statNolog(".")
3434
if err != nil {
3535
return "", err
3636
}
3737
dir = Getenv("PWD")
3838
if len(dir) > 0 && dir[0] == '/' {
39-
d, err := Stat(dir)
39+
d, err := statNolog(dir)
4040
if err == nil && SameFile(dot, d) {
4141
return dir, nil
4242
}
@@ -56,15 +56,15 @@ func Getwd() (dir string, err error) {
5656
dir = getwdCache.dir
5757
getwdCache.Unlock()
5858
if len(dir) > 0 {
59-
d, err := Stat(dir)
59+
d, err := statNolog(dir)
6060
if err == nil && SameFile(dot, d) {
6161
return dir, nil
6262
}
6363
}
6464

6565
// Root is a special case because it has no parent
6666
// and ends in a slash.
67-
root, err := Stat("/")
67+
root, err := statNolog("/")
6868
if err != nil {
6969
// Can't stat root - no hope.
7070
return "", err
@@ -81,7 +81,7 @@ func Getwd() (dir string, err error) {
8181
if len(parent) >= 1024 { // Sanity check
8282
return "", syscall.ENAMETOOLONG
8383
}
84-
fd, err := Open(parent)
84+
fd, err := openFileNolog(parent, O_RDONLY, 0)
8585
if err != nil {
8686
return "", err
8787
}
@@ -93,7 +93,7 @@ func Getwd() (dir string, err error) {
9393
return "", err
9494
}
9595
for _, name := range names {
96-
d, _ := Lstat(parent + "/" + name)
96+
d, _ := lstatNolog(parent + "/" + name)
9797
if SameFile(d, dot) {
9898
dir = "/" + name + dir
9999
goto Found

src/os/stat.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package os
6+
7+
import "internal/testlog"
8+
9+
// Stat returns a FileInfo describing the named file.
10+
// If there is an error, it will be of type *PathError.
11+
func Stat(name string) (FileInfo, error) {
12+
testlog.Stat(name)
13+
return statNolog(name)
14+
}
15+
16+
// Lstat returns a FileInfo describing the named file.
17+
// If the file is a symbolic link, the returned FileInfo
18+
// describes the symbolic link. Lstat makes no attempt to follow the link.
19+
// If there is an error, it will be of type *PathError.
20+
func Lstat(name string) (FileInfo, error) {
21+
testlog.Stat(name)
22+
return lstatNolog(name)
23+
}

src/os/stat_plan9.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package os
66

77
import (
8-
"internal/testlog"
98
"syscall"
109
"time"
1110
)
@@ -87,23 +86,18 @@ func dirstat(arg interface{}) (*syscall.Dir, error) {
8786
return nil, &PathError{"stat", name, err}
8887
}
8988

90-
// Stat returns a FileInfo describing the named file.
91-
// If there is an error, it will be of type *PathError.
92-
func Stat(name string) (FileInfo, error) {
93-
testlog.Stat(name)
89+
// statNolog implements Stat for Plan 9.
90+
func statNolog(name string) (FileInfo, error) {
9491
d, err := dirstat(name)
9592
if err != nil {
9693
return nil, err
9794
}
9895
return fileInfoFromStat(d), nil
9996
}
10097

101-
// Lstat returns a FileInfo describing the named file.
102-
// If the file is a symbolic link, the returned FileInfo
103-
// describes the symbolic link. Lstat makes no attempt to follow the link.
104-
// If there is an error, it will be of type *PathError.
105-
func Lstat(name string) (FileInfo, error) {
106-
return Stat(name)
98+
// lstatNolog implements Lstat for Plan 9.
99+
func lstatNolog(name string) (FileInfo, error) {
100+
return statNolog(name)
107101
}
108102

109103
// For testing.

src/os/stat_unix.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
package os
88

99
import (
10-
"internal/testlog"
1110
"syscall"
1211
)
1312

@@ -26,10 +25,8 @@ func (f *File) Stat() (FileInfo, error) {
2625
return &fs, nil
2726
}
2827

29-
// Stat returns a FileInfo describing the named file.
30-
// If there is an error, it will be of type *PathError.
31-
func Stat(name string) (FileInfo, error) {
32-
testlog.Stat(name)
28+
// statNolog stats a file with no test logging.
29+
func statNolog(name string) (FileInfo, error) {
3330
var fs fileStat
3431
err := syscall.Stat(name, &fs.sys)
3532
if err != nil {
@@ -39,12 +36,8 @@ func Stat(name string) (FileInfo, error) {
3936
return &fs, nil
4037
}
4138

42-
// Lstat returns a FileInfo describing the named file.
43-
// If the file is a symbolic link, the returned FileInfo
44-
// describes the symbolic link. Lstat makes no attempt to follow the link.
45-
// If there is an error, it will be of type *PathError.
46-
func Lstat(name string) (FileInfo, error) {
47-
testlog.Stat(name)
39+
// lstatNolog lstats a file with no test logging.
40+
func lstatNolog(name string) (FileInfo, error) {
4841
var fs fileStat
4942
err := syscall.Lstat(name, &fs.sys)
5043
if err != nil {

src/os/stat_windows.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package os
66

77
import (
88
"internal/syscall/windows"
9-
"internal/testlog"
109
"syscall"
1110
"unsafe"
1211
)
@@ -57,10 +56,8 @@ func (file *File) Stat() (FileInfo, error) {
5756
}, nil
5857
}
5958

60-
// Stat returns a FileInfo structure describing the named file.
61-
// If there is an error, it will be of type *PathError.
62-
func Stat(name string) (FileInfo, error) {
63-
testlog.Stat(name)
59+
// statNolog implements Stat for Windows.
60+
func statNolog(name string) (FileInfo, error) {
6461
if len(name) == 0 {
6562
return nil, &PathError{"Stat", name, syscall.Errno(syscall.ERROR_PATH_NOT_FOUND)}
6663
}
@@ -158,12 +155,8 @@ func statWithFindFirstFile(name string, namep *uint16) (FileInfo, error) {
158155
}, nil
159156
}
160157

161-
// Lstat returns the FileInfo structure describing the named file.
162-
// If the file is a symbolic link, the returned FileInfo
163-
// describes the symbolic link. Lstat makes no attempt to follow the link.
164-
// If there is an error, it will be of type *PathError.
165-
func Lstat(name string) (FileInfo, error) {
166-
testlog.Stat(name)
158+
// lstatNolog implements Lstat for Windows.
159+
func lstatNolog(name string) (FileInfo, error) {
167160
if len(name) == 0 {
168161
return nil, &PathError{"Lstat", name, syscall.Errno(syscall.ERROR_PATH_NOT_FOUND)}
169162
}

0 commit comments

Comments
 (0)