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,8 +6,9 @@ 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.
func skipStdinCopyError(err error) bool {
// Ignore hungup errors copying to stdin if the program // Ignore hungup errors copying to stdin if the program
// completed successfully otherwise. // completed successfully otherwise.
// See Issue 35753. // See Issue 35753.
@ -15,5 +16,4 @@ func init() {
return ok && return ok &&
pe.Op == "write" && pe.Path == "|1" && pe.Op == "write" && pe.Path == "|1" &&
pe.Err.Error() == "i/o on hungup channel" pe.Err.Error() == "i/o on hungup channel"
}
} }

View File

@ -11,8 +11,9 @@ 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.
func skipStdinCopyError(err error) bool {
// Ignore EPIPE errors copying to stdin if the program // Ignore EPIPE errors copying to stdin if the program
// completed successfully otherwise. // completed successfully otherwise.
// See Issue 9173. // See Issue 9173.
@ -20,5 +21,4 @@ func init() {
return ok && return ok &&
pe.Op == "write" && pe.Path == "|1" && pe.Op == "write" && pe.Path == "|1" &&
pe.Err == syscall.EPIPE pe.Err == syscall.EPIPE
}
} }

View File

@ -9,8 +9,9 @@ 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.
func skipStdinCopyError(err error) bool {
// Ignore ERROR_BROKEN_PIPE and ERROR_NO_DATA errors copying // Ignore ERROR_BROKEN_PIPE and ERROR_NO_DATA errors copying
// to stdin if the program completed successfully otherwise. // to stdin if the program completed successfully otherwise.
// See Issue 20445. // See Issue 20445.
@ -19,5 +20,4 @@ func init() {
return ok && return ok &&
pe.Op == "write" && pe.Path == "|1" && pe.Op == "write" && pe.Path == "|1" &&
(pe.Err == syscall.ERROR_BROKEN_PIPE || pe.Err == _ERROR_NO_DATA) (pe.Err == syscall.ERROR_BROKEN_PIPE || pe.Err == _ERROR_NO_DATA)
}
} }