From 4b872d61fe49ecd2ccca4dd8b285e4777d660932 Mon Sep 17 00:00:00 2001 From: Alex Brainman Date: Thu, 15 Mar 2012 16:33:45 +1100 Subject: [PATCH] os: return some invented data from Stat(DevNull) on windows Fixes #3321. R=golang-dev, bradfitz CC=golang-dev https://golang.org/cl/5831043 --- src/pkg/os/os_test.go | 19 +++++++++++++++++++ src/pkg/os/stat_windows.go | 22 ++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/pkg/os/os_test.go b/src/pkg/os/os_test.go index d1e241f006b..dec80cc0915 100644 --- a/src/pkg/os/os_test.go +++ b/src/pkg/os/os_test.go @@ -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()) + } +} diff --git a/src/pkg/os/stat_windows.go b/src/pkg/os/stat_windows.go index 68417483450..75351c805a2 100644 --- a/src/pkg/os/stat_windows.go +++ b/src/pkg/os/stat_windows.go @@ -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 {