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

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
}