mirror of
https://github.com/restic/restic.git
synced 2025-12-04 00:11:47 +00:00
Properly detect mintty output redirection
mintty on windows always uses pipes to connect stdout between processes and for the terminal output. The previous implementation always assumed that stdout connected to a pipe means that stdout is displayed on a mintty terminal. However, this detection breaks when using pipes to connect processes and for powershell which uses pipes when redirecting to a file. Now the pipe filename is queried and matched against the pattern used by msys / cygwin when connected to the terminal. In all other cases assume that a pipe is just a regular pipe.
This commit is contained in:
@@ -4,6 +4,7 @@ package termstatus
|
||||
|
||||
import (
|
||||
"io"
|
||||
"strings"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
|
||||
@@ -80,6 +81,22 @@ func isPipe(fd uintptr) bool {
|
||||
return err == nil && typ == windows.FILE_TYPE_PIPE
|
||||
}
|
||||
|
||||
func getFileNameByHandle(fd uintptr) (string, error) {
|
||||
type FILE_NAME_INFO struct {
|
||||
FileNameLength int32
|
||||
FileName [windows.MAX_LONG_PATH]uint16
|
||||
}
|
||||
|
||||
var fi FILE_NAME_INFO
|
||||
err := windows.GetFileInformationByHandleEx(windows.Handle(fd), windows.FileNameInfo, (*byte)(unsafe.Pointer(&fi)), uint32(unsafe.Sizeof(fi)))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
filename := syscall.UTF16ToString(fi.FileName[:])
|
||||
return filename, nil
|
||||
}
|
||||
|
||||
// CanUpdateStatus returns true if status lines can be printed, the process
|
||||
// output is not redirected to a file or pipe.
|
||||
func CanUpdateStatus(fd uintptr) bool {
|
||||
@@ -88,11 +105,23 @@ func CanUpdateStatus(fd uintptr) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// check that the output file type is a pipe (0x0003)
|
||||
// pipes require special handling
|
||||
if !isPipe(fd) {
|
||||
return false
|
||||
}
|
||||
|
||||
// assume we're running in mintty/cygwin
|
||||
return true
|
||||
fn, err := getFileNameByHandle(fd)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// inspired by https://github.com/RyanGlScott/mintty/blob/master/src/System/Console/MinTTY/Win32.hsc
|
||||
// terminal: \msys-dd50a72ab4668b33-pty0-to-master
|
||||
// pipe to cat: \msys-dd50a72ab4668b33-13244-pipe-0x16
|
||||
if (strings.HasPrefix(fn, "\\cygwin-") || strings.HasPrefix(fn, "\\msys-")) &&
|
||||
strings.Contains(fn, "-pty") && strings.HasSuffix(fn, "-master") {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user