fix: handle path binaries properly on windows (closes #117, #118) (#120)

* fix: handle path binaries properly on windows (closes #117, #118)

* refactor: dont return exec name since it isnt needed

* fix: return correct error in find exec function and stat always

* fix: remove filepath import for exec file check on unix
pull/128/head
sammyette 2022-03-16 19:44:32 -04:00 committed by GitHub
parent 01d937afd8
commit 20a4cdb505
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 17 deletions

24
exec.go
View File

@ -2,6 +2,7 @@ package main
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
@ -17,6 +18,8 @@ import (
"mvdan.cc/sh/v3/syntax"
)
var errNotExec = errors.New("not executable")
func runInput(input, origInput string) {
running = true
cmdString := aliases.Resolve(input)
@ -137,7 +140,7 @@ func execCommand(cmd, old string) error {
}
err := lookpath(args[0])
if err == os.ErrPermission {
if err == errNotExec {
hooks.Em.Emit("command.no-perm", args[0])
return interp.NewExitStatus(126)
} else if err != nil {
@ -156,19 +159,17 @@ func execCommand(cmd, old string) error {
return err
}
// custom lookpath function so we know if a command is found *and* has execute permission
func lookpath(file string) error {
func lookpath(file string) error { // custom lookpath function so we know if a command is found *and* is executable
skip := []string{"./", "/", "../", "~/"}
for _, s := range skip {
if strings.HasPrefix(file, s) {
err := findExecutable(file)
return err
return findExecutable(file)
}
}
for _, dir := range filepath.SplitList(os.Getenv("PATH")) {
path := filepath.Join(dir, file)
err := findExecutable(path)
if err == os.ErrPermission {
if err == errNotExec {
return err
} else if err == nil {
return nil
@ -178,17 +179,6 @@ func lookpath(file string) error {
return os.ErrNotExist
}
func findExecutable(name string) error {
f, err := os.Stat(name)
if err != nil {
return err
}
if m := f.Mode(); !m.IsDir() && m & 0111 != 0 {
return nil
}
return os.ErrPermission
}
func splitInput(input string) ([]string, string) {
// end my suffering
// TODO: refactor this garbage

18
execfile_unix.go 100644
View File

@ -0,0 +1,18 @@
// +build linux darwin
package main
import (
"os"
)
func findExecutable(path string) error {
f, err := os.Stat(path)
if err != nil {
return err
}
if m := f.Mode(); !m.IsDir() && m & 0111 != 0 {
return nil
}
return errNotExec
}

View File

@ -0,0 +1,27 @@
// +build windows
package main
import (
"path/filepath"
"os"
)
func findExecutable(path string) error {
nameExt := filepath.Ext(path)
if nameExt == "" {
for _, ext := range filepath.SplitList(os.Getenv("PATHEXT")) {
_, err := os.Stat(path + ext)
if err == nil {
return nil
}
}
}
_, err := os.Stat(path)
if err == nil {
return errNotExec
}
return os.ErrNotExist
}