github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/internal/fileutils/fileutils_lin_mac.go (about)

     1  //go:build !windows
     2  // +build !windows
     3  
     4  package fileutils
     5  
     6  import (
     7  	"os"
     8  	"path/filepath"
     9  
    10  	"github.com/ActiveState/cli/internal/errs"
    11  	"golang.org/x/sys/unix"
    12  )
    13  
    14  const LineEnd = "\n"
    15  
    16  // IsExecutable determines if the file at the given path has any execute permissions.
    17  // This function does not care whether the current user can has enough privilege to
    18  // execute the file.
    19  func IsExecutable(path string) bool {
    20  	stat, err := os.Stat(path)
    21  	return err == nil && (stat.Mode()&(0111) > 0)
    22  }
    23  
    24  // IsWritable returns true if the given path is writable
    25  func IsWritable(path string) bool {
    26  	for !TargetExists(path) && path != "" {
    27  		path = filepath.Dir(path)
    28  	}
    29  	return unix.Access(path, unix.W_OK) == nil
    30  }
    31  
    32  // ResolveUniquePath gets the absolute location of the provided path
    33  // with the best effort attempt to produce the same result for all possible paths to the
    34  // given target.
    35  func ResolveUniquePath(path string) (string, error) {
    36  	// "un-clean" file paths seem to confuse the EvalSymlinks function on MacOS, so we have to clean the path here.
    37  	evalPath, err := ResolvePath(filepath.Clean(path))
    38  	if err != nil {
    39  		return "", errs.Wrap(err, "cannot resolve path")
    40  	}
    41  
    42  	return filepath.Clean(evalPath), nil
    43  }
    44  
    45  func HideFile(path string) error {
    46  	return nil
    47  }