2020-09-26 03:20:17 -04:00
|
|
|
/*
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2020-09-06 03:23:14 -04:00
|
|
|
package paths
|
|
|
|
|
|
|
|
import (
|
2021-03-31 17:18:06 -04:00
|
|
|
"bytes"
|
2020-09-06 03:23:14 -04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-03-31 17:18:06 -04:00
|
|
|
"io/ioutil"
|
2020-09-06 03:23:14 -04:00
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
"path/filepath"
|
2021-02-19 03:20:50 -05:00
|
|
|
// "strconv"
|
2020-09-25 16:54:33 -04:00
|
|
|
"strings"
|
2021-02-19 03:20:50 -05:00
|
|
|
// "syscall"
|
2020-09-06 03:23:14 -04:00
|
|
|
)
|
|
|
|
|
2020-09-25 16:54:33 -04:00
|
|
|
var err error
|
2020-09-12 01:25:27 -04:00
|
|
|
|
2021-03-31 17:18:06 -04:00
|
|
|
func ExpandHome(path *string) (err error) {
|
2020-09-06 03:23:14 -04:00
|
|
|
// Props to this guy.
|
|
|
|
// https://stackoverflow.com/a/43578461/733214
|
|
|
|
if len(*path) == 0 {
|
|
|
|
return errors.New("empty path")
|
|
|
|
} else if (*path)[0] != '~' {
|
2021-03-31 17:18:06 -04:00
|
|
|
return
|
2020-09-06 03:23:14 -04:00
|
|
|
}
|
2021-02-19 03:20:50 -05:00
|
|
|
// E(ffective)UID (e.g. chown'd user for SUID)
|
|
|
|
/*
|
2021-03-31 17:18:06 -04:00
|
|
|
uid := strconv.Itoa(syscall.Geteuid())
|
|
|
|
usr, err := user.LookupId(euid)
|
2021-02-19 03:20:50 -05:00
|
|
|
*/
|
|
|
|
// R(real)UID (invoking user)
|
2020-09-06 03:23:14 -04:00
|
|
|
usr, err := user.Current()
|
|
|
|
if err != nil {
|
2021-03-31 17:18:06 -04:00
|
|
|
return
|
2020-09-06 03:23:14 -04:00
|
|
|
}
|
|
|
|
*path = filepath.Join(usr.HomeDir, (*path)[1:])
|
2021-03-31 17:18:06 -04:00
|
|
|
return
|
2020-09-06 03:23:14 -04:00
|
|
|
}
|
|
|
|
|
2021-03-31 17:18:06 -04:00
|
|
|
func GetPathEnv() (s []string, err error) {
|
|
|
|
s = make([]string, 0)
|
2020-09-25 16:54:33 -04:00
|
|
|
for _, p := range strings.Split(os.Getenv("PATH"), ":") {
|
|
|
|
if err = RealPath(&p); err != nil {
|
2021-03-31 17:18:06 -04:00
|
|
|
return
|
2020-09-25 16:54:33 -04:00
|
|
|
}
|
2021-03-31 17:18:06 -04:00
|
|
|
s = append(s, p)
|
2020-09-25 16:54:33 -04:00
|
|
|
}
|
2021-03-31 17:18:06 -04:00
|
|
|
|
|
|
|
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
|
2020-09-25 16:54:33 -04:00
|
|
|
}
|
|
|
|
|
2020-09-06 03:23:14 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-03-31 17:18:06 -04:00
|
|
|
func RealPathExists(path *string) (exists bool, err error) {
|
|
|
|
if err = RealPath(path); err != nil {
|
|
|
|
return
|
2020-09-06 03:23:14 -04:00
|
|
|
}
|
2021-03-19 13:06:11 -04:00
|
|
|
if _, err := os.Stat(*path); err != nil {
|
2021-03-31 17:18:06 -04:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
exists = false
|
|
|
|
err = nil
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
exists = true
|
2020-09-06 03:23:14 -04:00
|
|
|
}
|
2021-03-31 17:18:06 -04:00
|
|
|
|
|
|
|
return
|
2020-09-06 03:23:14 -04:00
|
|
|
}
|
|
|
|
|
2021-03-31 17:18:06 -04:00
|
|
|
func RealPathExistsStat(path *string) (exists bool, stat os.FileInfo, err error) {
|
|
|
|
if err = RealPath(path); err != nil {
|
|
|
|
return
|
2020-09-06 03:23:14 -04:00
|
|
|
}
|
2021-03-31 17:18:06 -04:00
|
|
|
if stat, err = os.Stat(*path); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
exists = false
|
|
|
|
err = nil
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
exists = true
|
2020-09-06 03:23:14 -04:00
|
|
|
}
|
2021-03-31 17:18:06 -04:00
|
|
|
|
|
|
|
return
|
2020-09-06 03:23:14 -04:00
|
|
|
}
|