From dec40965a1f60705102f8b21d3fafdb72bb8b0c4 Mon Sep 17 00:00:00 2001 From: brent s Date: Wed, 31 Mar 2021 17:18:06 -0400 Subject: [PATCH] do it better --- paths/TODO | 8 ---- paths/func.go | 109 ++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 78 insertions(+), 39 deletions(-) delete mode 100644 paths/TODO diff --git a/paths/TODO b/paths/TODO deleted file mode 100644 index ae16fb1..0000000 --- a/paths/TODO +++ /dev/null @@ -1,8 +0,0 @@ -https://golangcode.com/check-if-a-file-exists/ - -E.G. -func fileExists(filename string) bool { - info, err := os.Stat(filename) - if os.IsNotExist(err) { - return false - } diff --git a/paths/func.go b/paths/func.go index 5bcde7c..ba22790 100644 --- a/paths/func.go +++ b/paths/func.go @@ -19,8 +19,10 @@ package paths import ( + "bytes" "errors" "fmt" + "io/ioutil" "os" "os/user" "path/filepath" @@ -31,37 +33,75 @@ import ( var err error -func ExpandHome(path *string) 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 nil + return } // E(ffective)UID (e.g. chown'd user for SUID) /* - uid := strconv.Itoa(syscall.Geteuid()) - usr, err := user.LookupId(euid) + uid := strconv.Itoa(syscall.Geteuid()) + usr, err := user.LookupId(euid) */ // R(real)UID (invoking user) usr, err := user.Current() if err != nil { - return err + return } *path = filepath.Join(usr.HomeDir, (*path)[1:]) - return nil + return } -func GetPathEnv() ([]string, error) { - paths := []string{} +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 nil, err + return } - paths = append(paths, p) + s = append(s, p) } - return paths, nil + + 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 { @@ -97,31 +137,38 @@ func RealPath(path *string) error { 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 +func RealPathExists(path *string) (exists bool, err error) { + if err = RealPath(path); err != nil { + return } if _, err := os.Stat(*path); err != nil { - return false, err + if os.IsNotExist(err) { + exists = false + err = nil + } else { + return + } + } else { + exists = true } - return true, nil + + return } -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 +func RealPathExistsStat(path *string) (exists bool, stat os.FileInfo, err error) { + if err = RealPath(path); err != nil { + return } - stat, err := os.Stat(*path) - if err != nil { - return false, nil, err + if stat, err = os.Stat(*path); err != nil { + if os.IsNotExist(err) { + exists = false + err = nil + } else { + return + } + } else { + exists = true } - return true, stat, nil + + return }