128 lines
3.2 KiB
Go
128 lines
3.2 KiB
Go
/*
|
|
SysUtils - a library to assist with various system-related functions
|
|
Copyright (C) 2020 Brent Saner
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
package paths
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"os/user"
|
|
"path/filepath"
|
|
// "strconv"
|
|
"strings"
|
|
// "syscall"
|
|
)
|
|
|
|
var err error
|
|
|
|
func ExpandHome(path *string) error {
|
|
// Props to this guy.
|
|
// https://stackoverflow.com/a/43578461/733214
|
|
if len(*path) == 0 {
|
|
return errors.New("empty path")
|
|
} else if (*path)[0] != '~' {
|
|
return nil
|
|
}
|
|
// E(ffective)UID (e.g. chown'd user for SUID)
|
|
/*
|
|
uid := strconv.Itoa(syscall.Geteuid())
|
|
usr, err := user.LookupId(euid)
|
|
*/
|
|
// R(real)UID (invoking user)
|
|
usr, err := user.Current()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
*path = filepath.Join(usr.HomeDir, (*path)[1:])
|
|
return nil
|
|
}
|
|
|
|
func GetPathEnv() ([]string, error) {
|
|
paths := []string{}
|
|
for _, p := range strings.Split(os.Getenv("PATH"), ":") {
|
|
if err = RealPath(&p); err != nil {
|
|
return nil, err
|
|
}
|
|
paths = append(paths, p)
|
|
}
|
|
return paths, nil
|
|
}
|
|
|
|
func MakeDirIfNotExist(path *string) error {
|
|
exists, stat, err := RealPathExistsStat(path)
|
|
if err != nil {
|
|
if !exists {
|
|
// This, at least as of golang 1.15, uses the user's umask.
|
|
// It does not actually create a dir with 0777.
|
|
// It's up to the caller to do an os.Chmod() on the path after, if desired.
|
|
os.MkdirAll(*path, 0777)
|
|
return nil
|
|
} else {
|
|
return err
|
|
}
|
|
}
|
|
// So it exists, but it probably isn't a dir.
|
|
if !stat.Mode().IsDir() {
|
|
return errors.New(fmt.Sprintf("path %v exists but is not a directory", *path))
|
|
}
|
|
// This should probably never happen. Probably.
|
|
return errors.New("undefined behaviour")
|
|
}
|
|
|
|
func RealPath(path *string) error {
|
|
err := ExpandHome(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
*path, err = filepath.Abs(*path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func RealPathExists(path *string) (bool, error) {
|
|
// I know it's hacky, but we use the bool as a sort of proto-state-machine thing.
|
|
// If err != nil and bool is true, the error occurred during path absolution.
|
|
// If err != nil and bool is false, the path does not exist.
|
|
err := RealPath(path)
|
|
if err != nil {
|
|
return true, err
|
|
}
|
|
if _, err := os.Stat(*path); err == nil {
|
|
return false, err
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func RealPathExistsStat(path *string) (bool, os.FileInfo, error) {
|
|
// Same deal as RealPathExists.
|
|
// If err != nil and bool is true, the error occurred during path absolution.
|
|
// If err != nil and bool is false, the path does not exist.
|
|
err := RealPath(path)
|
|
if err != nil {
|
|
return true, nil, err
|
|
}
|
|
stat, err := os.Stat(*path)
|
|
if err != nil {
|
|
return false, nil, err
|
|
}
|
|
return true, stat, nil
|
|
}
|