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

os: make Stdin.Stat() return ModeCharDevice if Stdin is console

CL 20845 changed Stdin.Stat() so it returns ModeNamedPipe.
But introduced TestStatStdin does not test what Stdin.Stat()
returns when Stdin is console.

This CL adjusts both TestStatStdin and Stdin.Stat
implementations to handle console. Return ModeCharDevice
from Stdin.Stat() when Stdin is console on windows,
just like it does on unix.

Fixes #14853.

Change-Id: I54d73caee2aea45a99618d11600d8e82fe20d0c0
Reviewed-on: https://go-review.googlesource.com/34090
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Alex Brainman 2016-12-07 10:49:45 +11:00
parent 3f7a35d91c
commit 3b84a3c9ac
3 changed files with 25 additions and 10 deletions

View File

@ -1669,6 +1669,17 @@ func TestStatStdin(t *testing.T) {
Exit(0)
}
fi, err := Stdin.Stat()
if err != nil {
t.Fatal(err)
}
switch mode := fi.Mode(); {
case mode&ModeCharDevice != 0:
case mode&ModeNamedPipe != 0:
default:
t.Fatalf("unexpected Stdin mode (%v), want ModeCharDevice or ModeNamedPipe", mode)
}
var cmd *osexec.Cmd
if runtime.GOOS == "windows" {
cmd = osexec.Command("cmd", "/c", "echo output | "+Args[0]+" -test.run=TestStatStdin")

View File

@ -31,8 +31,9 @@ func (file *File) Stat() (FileInfo, error) {
if err != nil {
return nil, &PathError{"GetFileType", file.name, err}
}
if ft == syscall.FILE_TYPE_PIPE {
return &fileStat{name: basename(file.name), pipe: true}, nil
switch ft {
case syscall.FILE_TYPE_PIPE, syscall.FILE_TYPE_CHAR:
return &fileStat{name: basename(file.name), filetype: ft}, nil
}
var d syscall.ByHandleFileInformation
@ -50,10 +51,10 @@ func (file *File) Stat() (FileInfo, error) {
FileSizeHigh: d.FileSizeHigh,
FileSizeLow: d.FileSizeLow,
},
vol: d.VolumeSerialNumber,
idxhi: d.FileIndexHigh,
idxlo: d.FileIndexLow,
pipe: false,
filetype: ft,
vol: d.VolumeSerialNumber,
idxhi: d.FileIndexHigh,
idxlo: d.FileIndexLow,
}, nil
}

View File

@ -12,9 +12,9 @@ import (
// A fileStat is the implementation of FileInfo returned by Stat and Lstat.
type fileStat struct {
name string
sys syscall.Win32FileAttributeData
pipe bool
name string
sys syscall.Win32FileAttributeData
filetype uint32 // what syscall.GetFileType returns
// used to implement SameFile
sync.Mutex
@ -43,8 +43,11 @@ func (fs *fileStat) Mode() (m FileMode) {
if fs.sys.FileAttributes&syscall.FILE_ATTRIBUTE_REPARSE_POINT != 0 {
m |= ModeSymlink
}
if fs.pipe {
switch fs.filetype {
case syscall.FILE_TYPE_PIPE:
m |= ModeNamedPipe
case syscall.FILE_TYPE_CHAR:
m |= ModeCharDevice
}
return m
}