1
0
mirror of https://github.com/golang/go synced 2024-11-19 05:54:44 -07:00

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 <iant@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Ian Lance Taylor 2017-12-11 16:41:37 -08:00 committed by Brad Fitzpatrick
parent 3f5c1adeb8
commit ddae7fb1e8
9 changed files with 58 additions and 66 deletions

View File

@ -258,6 +258,16 @@ func Create(name string) (*File, error) {
return OpenFile(name, O_RDWR|O_CREATE|O_TRUNC, 0666) return OpenFile(name, O_RDWR|O_CREATE|O_TRUNC, 0666)
} }
// OpenFile is the generalized open call; most users will use Open
// or Create instead. It opens the named file with specified flag
// (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,
// methods on the returned File can be used for I/O.
// If there is an error, it will be of type *PathError.
func OpenFile(name string, flag int, perm FileMode) (*File, error) {
testlog.Open(name)
return openFileNolog(name, flag, perm)
}
// lstat is overridden in tests. // lstat is overridden in tests.
var lstat = Lstat var lstat = Lstat

View File

@ -6,7 +6,6 @@ package os
import ( import (
"internal/poll" "internal/poll"
"internal/testlog"
"io" "io"
"runtime" "runtime"
"syscall" "syscall"
@ -80,14 +79,8 @@ func syscallMode(i FileMode) (o uint32) {
return return
} }
// OpenFile is the generalized open call; most users will use Open // openFileNolog is the Plan 9 implementation of OpenFile.
// or Create instead. It opens the named file with specified flag func openFileNolog(name string, flag int, perm FileMode) (*File, error) {
// (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,
// methods on the returned File can be used for I/O.
// If there is an error, it will be of type *PathError.
func OpenFile(name string, flag int, perm FileMode) (*File, error) {
testlog.Open(name)
var ( var (
fd int fd int
e error e error

View File

@ -8,7 +8,6 @@ package os
import ( import (
"internal/poll" "internal/poll"
"internal/testlog"
"runtime" "runtime"
"syscall" "syscall"
) )
@ -154,14 +153,8 @@ func epipecheck(file *File, e error) {
// On Unix-like systems, it is "/dev/null"; on Windows, "NUL". // On Unix-like systems, it is "/dev/null"; on Windows, "NUL".
const DevNull = "/dev/null" const DevNull = "/dev/null"
// OpenFile is the generalized open call; most users will use Open // openFileNolog is the Unix implementation of OpenFile.
// or Create instead. It opens the named file with specified flag func openFileNolog(name string, flag int, perm FileMode) (*File, error) {
// (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,
// methods on the returned File can be used for I/O.
// If there is an error, it will be of type *PathError.
func OpenFile(name string, flag int, perm FileMode) (*File, error) {
testlog.Open(name)
chmod := false chmod := false
if !supportsCreateWithStickyBit && flag&O_CREATE != 0 && perm&ModeSticky != 0 { if !supportsCreateWithStickyBit && flag&O_CREATE != 0 && perm&ModeSticky != 0 {
if _, err := Stat(name); IsNotExist(err) { if _, err := Stat(name); IsNotExist(err) {

View File

@ -7,7 +7,6 @@ package os
import ( import (
"internal/poll" "internal/poll"
"internal/syscall/windows" "internal/syscall/windows"
"internal/testlog"
"runtime" "runtime"
"syscall" "syscall"
"unicode/utf16" "unicode/utf16"
@ -149,14 +148,8 @@ func openDir(name string) (file *File, err error) {
return f, nil return f, nil
} }
// OpenFile is the generalized open call; most users will use Open // openFileNolog is the Windows implementation of OpenFile.
// or Create instead. It opens the named file with specified flag func openFileNolog(name string, flag int, perm FileMode) (*File, error) {
// (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,
// methods on the returned File can be used for I/O.
// If there is an error, it will be of type *PathError.
func OpenFile(name string, flag int, perm FileMode) (*File, error) {
testlog.Open(name)
if name == "" { if name == "" {
return nil, &PathError{"open", name, syscall.ENOENT} return nil, &PathError{"open", name, syscall.ENOENT}
} }

View File

@ -30,13 +30,13 @@ func Getwd() (dir string, err error) {
// Clumsy but widespread kludge: // Clumsy but widespread kludge:
// if $PWD is set and matches ".", use it. // if $PWD is set and matches ".", use it.
dot, err := Stat(".") dot, err := statNolog(".")
if err != nil { if err != nil {
return "", err return "", err
} }
dir = Getenv("PWD") dir = Getenv("PWD")
if len(dir) > 0 && dir[0] == '/' { if len(dir) > 0 && dir[0] == '/' {
d, err := Stat(dir) d, err := statNolog(dir)
if err == nil && SameFile(dot, d) { if err == nil && SameFile(dot, d) {
return dir, nil return dir, nil
} }
@ -56,7 +56,7 @@ func Getwd() (dir string, err error) {
dir = getwdCache.dir dir = getwdCache.dir
getwdCache.Unlock() getwdCache.Unlock()
if len(dir) > 0 { if len(dir) > 0 {
d, err := Stat(dir) d, err := statNolog(dir)
if err == nil && SameFile(dot, d) { if err == nil && SameFile(dot, d) {
return dir, nil return dir, nil
} }
@ -64,7 +64,7 @@ func Getwd() (dir string, err error) {
// Root is a special case because it has no parent // Root is a special case because it has no parent
// and ends in a slash. // and ends in a slash.
root, err := Stat("/") root, err := statNolog("/")
if err != nil { if err != nil {
// Can't stat root - no hope. // Can't stat root - no hope.
return "", err return "", err
@ -81,7 +81,7 @@ func Getwd() (dir string, err error) {
if len(parent) >= 1024 { // Sanity check if len(parent) >= 1024 { // Sanity check
return "", syscall.ENAMETOOLONG return "", syscall.ENAMETOOLONG
} }
fd, err := Open(parent) fd, err := openFileNolog(parent, O_RDONLY, 0)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -93,7 +93,7 @@ func Getwd() (dir string, err error) {
return "", err return "", err
} }
for _, name := range names { for _, name := range names {
d, _ := Lstat(parent + "/" + name) d, _ := lstatNolog(parent + "/" + name)
if SameFile(d, dot) { if SameFile(d, dot) {
dir = "/" + name + dir dir = "/" + name + dir
goto Found goto Found

23
src/os/stat.go Normal file
View File

@ -0,0 +1,23 @@
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package os
import "internal/testlog"
// Stat returns a FileInfo describing the named file.
// If there is an error, it will be of type *PathError.
func Stat(name string) (FileInfo, error) {
testlog.Stat(name)
return statNolog(name)
}
// Lstat returns a FileInfo describing the named file.
// If the file is a symbolic link, the returned FileInfo
// describes the symbolic link. Lstat makes no attempt to follow the link.
// If there is an error, it will be of type *PathError.
func Lstat(name string) (FileInfo, error) {
testlog.Stat(name)
return lstatNolog(name)
}

View File

@ -5,7 +5,6 @@
package os package os
import ( import (
"internal/testlog"
"syscall" "syscall"
"time" "time"
) )
@ -87,10 +86,8 @@ func dirstat(arg interface{}) (*syscall.Dir, error) {
return nil, &PathError{"stat", name, err} return nil, &PathError{"stat", name, err}
} }
// Stat returns a FileInfo describing the named file. // statNolog implements Stat for Plan 9.
// If there is an error, it will be of type *PathError. func statNolog(name string) (FileInfo, error) {
func Stat(name string) (FileInfo, error) {
testlog.Stat(name)
d, err := dirstat(name) d, err := dirstat(name)
if err != nil { if err != nil {
return nil, err return nil, err
@ -98,12 +95,9 @@ func Stat(name string) (FileInfo, error) {
return fileInfoFromStat(d), nil return fileInfoFromStat(d), nil
} }
// Lstat returns a FileInfo describing the named file. // lstatNolog implements Lstat for Plan 9.
// If the file is a symbolic link, the returned FileInfo func lstatNolog(name string) (FileInfo, error) {
// describes the symbolic link. Lstat makes no attempt to follow the link. return statNolog(name)
// If there is an error, it will be of type *PathError.
func Lstat(name string) (FileInfo, error) {
return Stat(name)
} }
// For testing. // For testing.

View File

@ -7,7 +7,6 @@
package os package os
import ( import (
"internal/testlog"
"syscall" "syscall"
) )
@ -26,10 +25,8 @@ func (f *File) Stat() (FileInfo, error) {
return &fs, nil return &fs, nil
} }
// Stat returns a FileInfo describing the named file. // statNolog stats a file with no test logging.
// If there is an error, it will be of type *PathError. func statNolog(name string) (FileInfo, error) {
func Stat(name string) (FileInfo, error) {
testlog.Stat(name)
var fs fileStat var fs fileStat
err := syscall.Stat(name, &fs.sys) err := syscall.Stat(name, &fs.sys)
if err != nil { if err != nil {
@ -39,12 +36,8 @@ func Stat(name string) (FileInfo, error) {
return &fs, nil return &fs, nil
} }
// Lstat returns a FileInfo describing the named file. // lstatNolog lstats a file with no test logging.
// If the file is a symbolic link, the returned FileInfo func lstatNolog(name string) (FileInfo, error) {
// describes the symbolic link. Lstat makes no attempt to follow the link.
// If there is an error, it will be of type *PathError.
func Lstat(name string) (FileInfo, error) {
testlog.Stat(name)
var fs fileStat var fs fileStat
err := syscall.Lstat(name, &fs.sys) err := syscall.Lstat(name, &fs.sys)
if err != nil { if err != nil {

View File

@ -6,7 +6,6 @@ package os
import ( import (
"internal/syscall/windows" "internal/syscall/windows"
"internal/testlog"
"syscall" "syscall"
"unsafe" "unsafe"
) )
@ -57,10 +56,8 @@ func (file *File) Stat() (FileInfo, error) {
}, nil }, nil
} }
// Stat returns a FileInfo structure describing the named file. // statNolog implements Stat for Windows.
// If there is an error, it will be of type *PathError. func statNolog(name string) (FileInfo, error) {
func Stat(name string) (FileInfo, error) {
testlog.Stat(name)
if len(name) == 0 { if len(name) == 0 {
return nil, &PathError{"Stat", name, syscall.Errno(syscall.ERROR_PATH_NOT_FOUND)} return nil, &PathError{"Stat", name, syscall.Errno(syscall.ERROR_PATH_NOT_FOUND)}
} }
@ -158,12 +155,8 @@ func statWithFindFirstFile(name string, namep *uint16) (FileInfo, error) {
}, nil }, nil
} }
// Lstat returns the FileInfo structure describing the named file. // lstatNolog implements Lstat for Windows.
// If the file is a symbolic link, the returned FileInfo func lstatNolog(name string) (FileInfo, error) {
// describes the symbolic link. Lstat makes no attempt to follow the link.
// If there is an error, it will be of type *PathError.
func Lstat(name string) (FileInfo, error) {
testlog.Stat(name)
if len(name) == 0 { if len(name) == 0 {
return nil, &PathError{"Lstat", name, syscall.Errno(syscall.ERROR_PATH_NOT_FOUND)} return nil, &PathError{"Lstat", name, syscall.Errno(syscall.ERROR_PATH_NOT_FOUND)}
} }