os/exec: make skipStdinCopyError a function instead of a variable

This makes clearer that skipStdinCopyError is always defined and never
overridden in tests.

Secondarily, it may also help reduce init-time work and allow the
linker and/or inliner to better optimize this package.

(Noticed while prototyping #50436.)

Change-Id: I4f3c1bc146384a98136a4039f82165ed106c14b8
Reviewed-on: https://go-review.googlesource.com/c/go/+/401897
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
This commit is contained in:
Bryan C. Mills 2022-04-22 22:00:16 -04:00 committed by Bryan Mills
parent 17d7983b29
commit ad5eaa8c4c
4 changed files with 32 additions and 36 deletions

View File

@ -230,10 +230,6 @@ func (c *Cmd) argv() []string {
return []string{c.Path} return []string{c.Path}
} }
// skipStdinCopyError optionally specifies a function which reports
// whether the provided stdin copy error should be ignored.
var skipStdinCopyError func(error) bool
func (c *Cmd) stdin() (f *os.File, err error) { func (c *Cmd) stdin() (f *os.File, err error) {
if c.Stdin == nil { if c.Stdin == nil {
f, err = os.Open(os.DevNull) f, err = os.Open(os.DevNull)
@ -257,7 +253,7 @@ func (c *Cmd) stdin() (f *os.File, err error) {
c.closeAfterWait = append(c.closeAfterWait, pw) c.closeAfterWait = append(c.closeAfterWait, pw)
c.goroutine = append(c.goroutine, func() error { c.goroutine = append(c.goroutine, func() error {
_, err := io.Copy(pw, c.Stdin) _, err := io.Copy(pw, c.Stdin)
if skip := skipStdinCopyError; skip != nil && skip(err) { if skipStdinCopyError(err) {
err = nil err = nil
} }
if err1 := pw.Close(); err == nil { if err1 := pw.Close(); err == nil {

View File

@ -6,14 +6,14 @@ package exec
import "io/fs" import "io/fs"
func init() { // skipStdinCopyError optionally specifies a function which reports
skipStdinCopyError = func(err error) bool { // whether the provided stdin copy error should be ignored.
// Ignore hungup errors copying to stdin if the program func skipStdinCopyError(err error) bool {
// completed successfully otherwise. // Ignore hungup errors copying to stdin if the program
// See Issue 35753. // completed successfully otherwise.
pe, ok := err.(*fs.PathError) // See Issue 35753.
return ok && pe, ok := err.(*fs.PathError)
pe.Op == "write" && pe.Path == "|1" && return ok &&
pe.Err.Error() == "i/o on hungup channel" pe.Op == "write" && pe.Path == "|1" &&
} pe.Err.Error() == "i/o on hungup channel"
} }

View File

@ -11,14 +11,14 @@ import (
"syscall" "syscall"
) )
func init() { // skipStdinCopyError optionally specifies a function which reports
skipStdinCopyError = func(err error) bool { // whether the provided stdin copy error should be ignored.
// Ignore EPIPE errors copying to stdin if the program func skipStdinCopyError(err error) bool {
// completed successfully otherwise. // Ignore EPIPE errors copying to stdin if the program
// See Issue 9173. // completed successfully otherwise.
pe, ok := err.(*fs.PathError) // See Issue 9173.
return ok && pe, ok := err.(*fs.PathError)
pe.Op == "write" && pe.Path == "|1" && return ok &&
pe.Err == syscall.EPIPE pe.Op == "write" && pe.Path == "|1" &&
} pe.Err == syscall.EPIPE
} }

View File

@ -9,15 +9,15 @@ import (
"syscall" "syscall"
) )
func init() { // skipStdinCopyError optionally specifies a function which reports
skipStdinCopyError = func(err error) bool { // whether the provided stdin copy error should be ignored.
// Ignore ERROR_BROKEN_PIPE and ERROR_NO_DATA errors copying func skipStdinCopyError(err error) bool {
// to stdin if the program completed successfully otherwise. // Ignore ERROR_BROKEN_PIPE and ERROR_NO_DATA errors copying
// See Issue 20445. // to stdin if the program completed successfully otherwise.
const _ERROR_NO_DATA = syscall.Errno(0xe8) // See Issue 20445.
pe, ok := err.(*fs.PathError) const _ERROR_NO_DATA = syscall.Errno(0xe8)
return ok && pe, ok := err.(*fs.PathError)
pe.Op == "write" && pe.Path == "|1" && return ok &&
(pe.Err == syscall.ERROR_BROKEN_PIPE || pe.Err == _ERROR_NO_DATA) pe.Op == "write" && pe.Path == "|1" &&
} (pe.Err == syscall.ERROR_BROKEN_PIPE || pe.Err == _ERROR_NO_DATA)
} }