diff options
| author | Alex Brainman <alex.brainman@gmail.com> | 2014-04-04 16:26:15 +1100 |
|---|---|---|
| committer | Alex Brainman <alex.brainman@gmail.com> | 2014-04-04 16:26:15 +1100 |
| commit | df8ec65b3abcdc8566176d6dae756273d8641706 (patch) | |
| tree | 4f2fbc527862b0e005f3617c64edb9b12d94dbdd /src/pkg/os/exec/exec.go | |
| parent | 72dbc4ccc85be730d12cada215ec7de9fdb872c1 (diff) | |
| download | go-df8ec65b3abcdc8566176d6dae756273d8641706.tar.xz | |
os/exec: always try appropriate command extensions during Cmd.Start on windows
Update #7362
Fixes #7377
Fixes #7570
LGTM=rsc
R=golang-codereviews, rsc
CC=golang-codereviews
https://golang.org/cl/83020043
Diffstat (limited to 'src/pkg/os/exec/exec.go')
| -rw-r--r-- | src/pkg/os/exec/exec.go | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/pkg/os/exec/exec.go b/src/pkg/os/exec/exec.go index d2cee03fcd..44b9cc08ce 100644 --- a/src/pkg/os/exec/exec.go +++ b/src/pkg/os/exec/exec.go @@ -13,7 +13,9 @@ import ( "io" "os" "path/filepath" + "runtime" "strconv" + "strings" "sync" "syscall" ) @@ -237,6 +239,32 @@ func (c *Cmd) Run() error { return c.Wait() } +// lookExtensions finds windows executable by its dir and path. +// It uses LookPath to try appropriate extensions. +// lookExtensions does not search PATH, instead it converts `prog` into `.\prog`. +func lookExtensions(path, dir string) (string, error) { + if filepath.Base(path) == path { + path = filepath.Join(".", path) + } + if dir == "" { + return LookPath(path) + } + if filepath.VolumeName(path) != "" { + return LookPath(path) + } + if len(path) > 1 && os.IsPathSeparator(path[0]) { + return LookPath(path) + } + dirandpath := filepath.Join(dir, path) + // We assume that LookPath will only add file extension. + lp, err := LookPath(dirandpath) + if err != nil { + return "", err + } + ext := strings.TrimPrefix(lp, dirandpath) + return path + ext, nil +} + // Start starts the specified command but does not wait for it to complete. // // The Wait method will return the exit code and release associated resources @@ -247,6 +275,15 @@ func (c *Cmd) Start() error { c.closeDescriptors(c.closeAfterWait) return c.lookPathErr } + if runtime.GOOS == "windows" { + lp, err := lookExtensions(c.Path, c.Dir) + if err != nil { + c.closeDescriptors(c.closeAfterStart) + c.closeDescriptors(c.closeAfterWait) + return err + } + c.Path = lp + } if c.Process != nil { return errors.New("exec: already started") } |
