Skip to content

Commit f60bcba

Browse files
Replaced queryFileType with IsPipe
This is the only functionality we actually use. Mocking a boolean is easier as we don't leak Windows' internals.
1 parent ff3b96f commit f60bcba

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

exec.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,8 @@ func (c *Cmd) readerDescriptor(r io.Reader) (f *os.File, err error) {
367367
}
368368

369369
if f, ok := r.(*os.File); ok {
370-
ft, err := queryFileType(f)
371-
if err == nil && ft == fileTypePipe {
370+
isPipe, err := IsPipe(f)
371+
if err == nil && isPipe {
372372
// It's a pipe: no need to create our own pipe.
373373
return f, nil
374374
}
@@ -410,8 +410,8 @@ func (c *Cmd) writerDescriptor(w io.Writer) (f *os.File, err error) {
410410
}
411411

412412
if f, ok := w.(*os.File); ok {
413-
ft, err := queryFileType(f)
414-
if err == nil && ft == fileTypePipe {
413+
isPipe, err := IsPipe(f)
414+
if err == nil && isPipe {
415415
// It's a pipe: no need to create our own pipe.
416416
return f, nil
417417
}

win32_linux.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import (
1010
"github.com/ubuntu/decorate"
1111
)
1212

13-
func queryFileType(f *os.File) (fileType, error) {
14-
return 0, errors.New("not implemented")
13+
// IsPipe checks if a file's descriptor is a pipe vs. any other type of object.
14+
// This implementation will always fail on Linux.
15+
func IsPipe(f *os.File) (bool, error) {
16+
return false, errors.New("not implemented")
1517
}
1618

1719
func wslConfigureDistribution(distributionName string, defaultUID uint32, wslDistributionFlags wslFlags) (err error) {

win32_windows.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,23 @@ var (
2828
)
2929

3030
// Windows' typedefs.
31-
type wBOOL = int // Windows' BOOL
32-
type char = byte // Windows' CHAR (which is the same as C's char)
31+
type wBOOL = int // Windows' BOOL
32+
type char = byte // Windows' CHAR (which is the same as C's char)
33+
const fileTypePipe = 0x0003 // Windows' FILE_TYPE_PIPE
3334

34-
func queryFileType(f *os.File) (fileType, error) {
35+
// IsPipe checks if a file's descriptor is a pipe vs. any other type of object.
36+
func IsPipe(f *os.File) (bool, error) {
3537
n, err := windows.GetFileType(windows.Handle(f.Fd()))
3638
if err != nil {
37-
return fileTypeUnknown, err
39+
return false, err
3840
}
39-
return fileType(n), nil
41+
42+
var isPipe bool
43+
if n == fileTypePipe {
44+
isPipe = true
45+
}
46+
47+
return isPipe, nil
4048
}
4149

4250
// wslLaunch replaces os.StartProcess with WSL commands.

0 commit comments

Comments
 (0)