go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/system/prober/probe_windows.go (about)

     1  // Copyright 2017 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package prober
    16  
    17  import (
    18  	"os"
    19  	"path/filepath"
    20  	"strings"
    21  
    22  	"go.chromium.org/luci/common/errors"
    23  	"go.chromium.org/luci/common/system/environ"
    24  )
    25  
    26  func chkStat(file string) error {
    27  	d, err := os.Stat(file)
    28  	if err != nil {
    29  		return err
    30  	}
    31  	if d.IsDir() {
    32  		return os.ErrPermission
    33  	}
    34  	return nil
    35  }
    36  
    37  func hasExt(file string) bool {
    38  	i := strings.LastIndex(file, ".")
    39  	if i < 0 {
    40  		return false
    41  	}
    42  	return strings.LastIndexAny(file, `:\/`) < i
    43  }
    44  
    45  func findExecutable(file string, exts []string) (string, error) {
    46  	if len(exts) == 0 {
    47  		return file, chkStat(file)
    48  	}
    49  	if hasExt(file) {
    50  		if chkStat(file) == nil {
    51  			return file, nil
    52  		}
    53  	}
    54  	for _, e := range exts {
    55  		if f := file + e; chkStat(f) == nil {
    56  			return f, nil
    57  		}
    58  	}
    59  	return "", os.ErrNotExist
    60  }
    61  
    62  // findInDir is a paraphrased and trimmed version of "exec.LookPath"
    63  // (for Windows),
    64  //
    65  // Copied from:
    66  // https://github.com/golang/go/blob/d234f9a75413fdae7643e4be9471b4aeccf02478/src/os/exec/lp_windows.go
    67  //
    68  // Modified to:
    69  //   - Use a supplied "dir" instead of scanning through PATH.
    70  //   - Not consider cases where "file" is an absolute path
    71  //   - Ignore the possibility that "file" may be in the CWD; only look in "dir".
    72  func findInDir(file, dir string, env environ.Env) (string, error) {
    73  	var exts []string
    74  	if x, ok := env.Lookup(`PATHEXT`); ok {
    75  		for _, e := range strings.Split(strings.ToLower(x), `;`) {
    76  			if e == "" {
    77  				continue
    78  			}
    79  			if e[0] != '.' {
    80  				e = "." + e
    81  			}
    82  			exts = append(exts, e)
    83  		}
    84  	} else {
    85  		exts = []string{".com", ".exe", ".bat", ".cmd"}
    86  	}
    87  
    88  	if f, err := findExecutable(filepath.Join(dir, file), exts); err == nil {
    89  		return f, nil
    90  	}
    91  	return "", errors.New("not found")
    92  }