| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package prober |
| 6 |
| 7 import ( |
| 8 "os" |
| 9 "path/filepath" |
| 10 "strings" |
| 11 |
| 12 "github.com/luci/luci-go/common/errors" |
| 13 "github.com/luci/luci-go/common/system/environ" |
| 14 ) |
| 15 |
| 16 func chkStat(file string) error { |
| 17 d, err := os.Stat(file) |
| 18 if err != nil { |
| 19 return err |
| 20 } |
| 21 if d.IsDir() { |
| 22 return os.ErrPermission |
| 23 } |
| 24 return nil |
| 25 } |
| 26 |
| 27 func hasExt(file string) bool { |
| 28 i := strings.LastIndex(file, ".") |
| 29 if i < 0 { |
| 30 return false |
| 31 } |
| 32 return strings.LastIndexAny(file, `:\/`) < i |
| 33 } |
| 34 |
| 35 func findExecutable(file string, exts []string) (string, error) { |
| 36 if len(exts) == 0 { |
| 37 return file, chkStat(file) |
| 38 } |
| 39 if hasExt(file) { |
| 40 if chkStat(file) == nil { |
| 41 return file, nil |
| 42 } |
| 43 } |
| 44 for _, e := range exts { |
| 45 if f := file + e; chkStat(f) == nil { |
| 46 return f, nil |
| 47 } |
| 48 } |
| 49 return "", os.ErrNotExist |
| 50 } |
| 51 |
| 52 // findInDir is a paraphrased and trimmed version of "exec.LookPath" |
| 53 // (for Windows), |
| 54 // |
| 55 // Copied from: |
| 56 // https://github.com/golang/go/blob/d234f9a75413fdae7643e4be9471b4aeccf02478/sr
c/os/exec/lp_windows.go |
| 57 // |
| 58 // Modified to: |
| 59 // - Use a supplied "dir" instead of scanning through PATH. |
| 60 // - Not consider cases where "file" is an absolute path |
| 61 // - Ignore the possibility that "file" may be in the CWD; only look in "di
r". |
| 62 func findInDir(file, dir string, env environ.Env) (string, error) { |
| 63 var exts []string |
| 64 if x, ok := env.Get(`PATHEXT`); ok { |
| 65 for _, e := range strings.Split(strings.ToLower(x), `;`) { |
| 66 if e == "" { |
| 67 continue |
| 68 } |
| 69 if e[0] != '.' { |
| 70 e = "." + e |
| 71 } |
| 72 exts = append(exts, e) |
| 73 } |
| 74 } else { |
| 75 exts = []string{".com", ".exe", ".bat", ".cmd"} |
| 76 } |
| 77 |
| 78 if f, err := findExecutable(filepath.Join(dir, file), exts); err == nil
{ |
| 79 return f, nil |
| 80 } |
| 81 return "", errors.New("not found") |
| 82 } |
| OLD | NEW |