github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/go/util/path.go (about)

     1  // Copyright (c) 2014, Kevin Walsh.  All rights reserved.
     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 util
    16  
    17  import (
    18  	"io/ioutil"
    19  	"os"
    20  	"path/filepath"
    21  	"strings"
    22  
    23  	"github.com/golang/glog"
    24  )
    25  
    26  // WritePath writes data to a file after creating any necessary directories.
    27  func WritePath(path string, data []byte, dirPerm, filePerm os.FileMode) error {
    28  	dir := filepath.Dir(path)
    29  	err := os.MkdirAll(dir, dirPerm)
    30  	if err != nil {
    31  		return err
    32  	}
    33  	return ioutil.WriteFile(path, data, filePerm)
    34  }
    35  
    36  // CreatePath creates a file after creating any necessary directories.
    37  func CreatePath(path string, dirPerm, filePerm os.FileMode) (*os.File, error) {
    38  	dir := filepath.Dir(path)
    39  	err := os.MkdirAll(dir, dirPerm)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	return os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, filePerm)
    44  }
    45  
    46  // FindExecutable searches for an executable in a path. If the name is
    47  // already an absolute  slash, the search path is ignored. If the search fails,
    48  // emptystring is returned.
    49  func FindExecutable(name string, dirs []string) string {
    50  	if filepath.IsAbs(name) {
    51  		// For absolute names, the file need not be executable
    52  		return name
    53  	}
    54  	for _, dir := range dirs {
    55  		path := filepath.Join(dir, name)
    56  		if IsExecutable(path) {
    57  			return path
    58  		}
    59  	}
    60  	return ""
    61  }
    62  
    63  // SystemPath returns the elements of $PATH
    64  func SystemPath() []string {
    65  	var dirs []string
    66  	if pathenv := os.Getenv("PATH"); pathenv != "" {
    67  		for _, dir := range strings.Split(pathenv, ":") {
    68  			if dir == "" {
    69  				dir = "." // Unix shell semantics: "" in $PATH means "."
    70  			}
    71  			dirs = append(dirs, dir)
    72  		}
    73  	}
    74  	return dirs
    75  }
    76  
    77  // GoBinPath returns dir/bin for each dir in $GOPATH
    78  func GoBinPath() []string {
    79  	var dirs []string
    80  	gopath := os.Getenv("GOPATH")
    81  	if gopath != "" {
    82  		for _, dir := range strings.Split(gopath, ":") {
    83  			dirs = append(dirs, dir+"/bin")
    84  		}
    85  	}
    86  	return dirs
    87  }
    88  
    89  // LocalPath returns the directory of the current executable
    90  func LocalPath() []string {
    91  	path, err := filepath.Abs(os.Args[0])
    92  	if err != nil {
    93  		glog.Errorf("%v: Can't get path of '%s'", err, os.Args[0])
    94  		return nil
    95  	} else {
    96  		return []string{filepath.Dir(path)}
    97  	}
    98  }
    99  
   100  // LiberalSearchPath returns LocalPath, GoBinPath, and SystemPath together, in
   101  // that order.
   102  func LiberalSearchPath() []string {
   103  	var dirs []string
   104  	dirs = append(dirs, LocalPath()...)
   105  	dirs = append(dirs, GoBinPath()...)
   106  	dirs = append(dirs, SystemPath()...)
   107  	return dirs
   108  }
   109  
   110  // IsExecutable checks whether the file has an executable bits set.
   111  func IsExecutable(file string) bool {
   112  	d, err := os.Stat(file)
   113  	return err == nil && !d.Mode().IsDir() && d.Mode()&0111 != 0
   114  }
   115  
   116  // IsDir checks whether the path is a directory.
   117  func IsDir(path string) bool {
   118  	info, err := os.Stat(path)
   119  	return err == nil && info.IsDir()
   120  }