Commit 3b84a3c9 authored by Alex Brainman's avatar Alex Brainman

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/34090Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 3f7a35d9
......@@ -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")
......
......@@ -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,
},
filetype: ft,
vol: d.VolumeSerialNumber,
idxhi: d.FileIndexHigh,
idxlo: d.FileIndexLow,
pipe: false,
}, nil
}
......
......@@ -14,7 +14,7 @@ import (
type fileStat struct {
name string
sys syscall.Win32FileAttributeData
pipe bool
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
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment