do it better

This commit is contained in:
brent s 2021-03-31 17:18:06 -04:00
parent 558cf384ea
commit dec40965a1
Signed by: bts
GPG Key ID: 8C004C2F93481F6B
2 changed files with 78 additions and 39 deletions

View File

@ -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
}

View File

@ -19,8 +19,10 @@
package paths package paths


import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"os/user" "os/user"
"path/filepath" "path/filepath"
@ -31,37 +33,75 @@ import (


var err error var err error


func ExpandHome(path *string) error { func ExpandHome(path *string) (err error) {
// Props to this guy. // Props to this guy.
// https://stackoverflow.com/a/43578461/733214 // https://stackoverflow.com/a/43578461/733214
if len(*path) == 0 { if len(*path) == 0 {
return errors.New("empty path") return errors.New("empty path")
} else if (*path)[0] != '~' { } else if (*path)[0] != '~' {
return nil return
} }
// E(ffective)UID (e.g. chown'd user for SUID) // E(ffective)UID (e.g. chown'd user for SUID)
/* /*
uid := strconv.Itoa(syscall.Geteuid()) uid := strconv.Itoa(syscall.Geteuid())
usr, err := user.LookupId(euid) usr, err := user.LookupId(euid)
*/ */
// R(real)UID (invoking user) // R(real)UID (invoking user)
usr, err := user.Current() usr, err := user.Current()
if err != nil { if err != nil {
return err return
} }
*path = filepath.Join(usr.HomeDir, (*path)[1:]) *path = filepath.Join(usr.HomeDir, (*path)[1:])
return nil return
} }


func GetPathEnv() ([]string, error) { func GetPathEnv() (s []string, err error) {
paths := []string{} s = make([]string, 0)
for _, p := range strings.Split(os.Getenv("PATH"), ":") { for _, p := range strings.Split(os.Getenv("PATH"), ":") {
if err = RealPath(&p); err != nil { 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 { func MakeDirIfNotExist(path *string) error {
@ -97,31 +137,38 @@ func RealPath(path *string) error {
return nil return nil
} }


func RealPathExists(path *string) (bool, error) { func RealPathExists(path *string) (exists bool, err error) {
// I know it's hacky, but we use the bool as a sort of proto-state-machine thing. if err = RealPath(path); err != nil {
// If err != nil and bool is true, the error occurred during path absolution. return
// 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 { 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) { func RealPathExistsStat(path *string) (exists bool, stat os.FileInfo, err error) {
// Same deal as RealPathExists. if err = RealPath(path); err != nil {
// If err != nil and bool is true, the error occurred during path absolution. return
// 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 stat, err = os.Stat(*path); err != nil {
if err != nil { if os.IsNotExist(err) {
return false, nil, err exists = false
err = nil
} else {
return
}
} else {
exists = true
} }
return true, stat, nil
return
} }