1
0
mirror of https://github.com/golang/go synced 2024-11-25 07:17:56 -07:00

os: return some invented data from Stat(DevNull) on windows

Fixes #3321.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/5831043
This commit is contained in:
Alex Brainman 2012-03-15 16:33:45 +11:00
parent 235863cb12
commit 4b872d61fe
2 changed files with 41 additions and 0 deletions

View File

@ -1047,3 +1047,22 @@ func TestSameFile(t *testing.T) {
t.Errorf("files should be different") t.Errorf("files should be different")
} }
} }
func TestDevNullFile(t *testing.T) {
f, err := Open(DevNull)
if err != nil {
t.Fatalf("Open(%s): %v", DevNull, err)
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
t.Fatalf("Stat(%s): %v", DevNull, err)
}
name := filepath.Base(DevNull)
if fi.Name() != name {
t.Fatalf("wrong file name have %v want %v", fi.Name(), name)
}
if fi.Size() != 0 {
t.Fatalf("wrong file size have %d want 0", fi.Size())
}
}

View File

@ -21,6 +21,9 @@ func (file *File) Stat() (fi FileInfo, err error) {
// I don't know any better way to do that for directory // I don't know any better way to do that for directory
return Stat(file.name) return Stat(file.name)
} }
if file.name == DevNull {
return statDevNull()
}
var d syscall.ByHandleFileInformation var d syscall.ByHandleFileInformation
e := syscall.GetFileInformationByHandle(syscall.Handle(file.fd), &d) e := syscall.GetFileInformationByHandle(syscall.Handle(file.fd), &d)
if e != nil { if e != nil {
@ -41,6 +44,9 @@ func Stat(name string) (fi FileInfo, err error) {
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)}
} }
if name == DevNull {
return statDevNull()
}
var d syscall.Win32FileAttributeData var d syscall.Win32FileAttributeData
e := syscall.GetFileAttributesEx(syscall.StringToUTF16Ptr(name), syscall.GetFileExInfoStandard, (*byte)(unsafe.Pointer(&d))) e := syscall.GetFileAttributesEx(syscall.StringToUTF16Ptr(name), syscall.GetFileExInfoStandard, (*byte)(unsafe.Pointer(&d)))
if e != nil { if e != nil {
@ -69,6 +75,22 @@ func Lstat(name string) (fi FileInfo, err error) {
return Stat(name) return Stat(name)
} }
// statDevNull return FileInfo structure describing DevNull file ("NUL").
// It creates invented data, since none of windows api will return
// that information.
func statDevNull() (fi FileInfo, err error) {
return &fileStat{
name: DevNull,
mode: ModeDevice | ModeCharDevice | 0666,
sys: &winSys{
// hopefully this will work for SameFile
vol: 0,
idxhi: 0,
idxlo: 0,
},
}, nil
}
// basename removes trailing slashes and the leading // basename removes trailing slashes and the leading
// directory name and drive letter from path name. // directory name and drive letter from path name.
func basename(name string) string { func basename(name string) string {