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
Родитель 235863cb12
Коммит 4b872d61fe
2 изменённых файлов: 41 добавлений и 0 удалений

Просмотреть файл

@ -1047,3 +1047,22 @@ func TestSameFile(t *testing.T) {
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())
}
}

Просмотреть файл

@ -21,6 +21,9 @@ func (file *File) Stat() (fi FileInfo, err error) {
// I don't know any better way to do that for directory
return Stat(file.name)
}
if file.name == DevNull {
return statDevNull()
}
var d syscall.ByHandleFileInformation
e := syscall.GetFileInformationByHandle(syscall.Handle(file.fd), &d)
if e != nil {
@ -41,6 +44,9 @@ func Stat(name string) (fi FileInfo, err error) {
if len(name) == 0 {
return nil, &PathError{"Stat", name, syscall.Errno(syscall.ERROR_PATH_NOT_FOUND)}
}
if name == DevNull {
return statDevNull()
}
var d syscall.Win32FileAttributeData
e := syscall.GetFileAttributesEx(syscall.StringToUTF16Ptr(name), syscall.GetFileExInfoStandard, (*byte)(unsafe.Pointer(&d)))
if e != nil {
@ -69,6 +75,22 @@ func Lstat(name string) (fi FileInfo, err error) {
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
// directory name and drive letter from path name.
func basename(name string) string {