/* 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 . */ package paths import ( "bytes" "errors" "fmt" "io/ioutil" "os" "os/user" "path/filepath" // "strconv" "strings" // "syscall" ) var err error func ExpandHome(path *string) (err 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 } // 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 } *path = filepath.Join(usr.HomeDir, (*path)[1:]) return } func GetPathEnv() (s []string, err error) { s = make([]string, 0) for _, p := range strings.Split(os.Getenv("PATH"), ":") { if err = RealPath(&p); err != nil { return } s = append(s, p) } return } func GetEnvPid(pid uint32) (env map[string]string, err error) { var envBytes []byte var envArr [][]byte var procPath string var exists bool env = make(map[string]string) procPath = fmt.Sprintf("/proc/%v/environ", pid) if exists, err = RealPathExists(&procPath); err != nil { return } if !exists { err = errors.New(fmt.Sprintf("information for pid %v does not exist", pid)) } if envBytes, err = ioutil.ReadFile(procPath); err != nil { return } envArr = bytes.Split(envBytes, []byte{0x0}) for _, b := range envArr { // s := strings.TrimSpace(string(b)) s := string(b) e := strings.SplitN(s, "=", 2) for _, i := range e { if len(i) != 2 { continue } env[string(i[0])] = string(i[1]) } } return } 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) (exists bool, err error) { if err = RealPath(path); err != nil { return } if _, err := os.Stat(*path); err != nil { if os.IsNotExist(err) { exists = false err = nil } else { return } } else { exists = true } return } func RealPathExistsStat(path *string) (exists bool, stat os.FileInfo, err error) { if err = RealPath(path); err != nil { return } if stat, err = os.Stat(*path); err != nil { if os.IsNotExist(err) { exists = false err = nil } else { return } } else { exists = true } return }