/* 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 ( "errors" "fmt" "io/fs" "os" "os/user" "path/filepath" "strings" // "syscall" ) /* ExpandHome will take a tilde(~)-prefixed path and resolve it to the actual path in-place. "Nested" user paths (~someuser/somechroot/~someotheruser) are not supported as home directories are expected to be absolute paths. */ func ExpandHome(path *string) (err error) { var unameSplit []string var uname string var u *user.User // Props to this guy. // https://stackoverflow.com/a/43578461/733214 if len(*path) == 0 { err = errors.New("empty path") return } else if (*path)[0] != '~' { return } // E(ffective)UID (e.g. chown'd user for SUID) /* uid := strconv.Itoa(syscall.Geteuid()) u, err := user.LookupId(euid) */ // (Real)UID (invoking user) /* if u, err = user.Current(); err != nil { return } */ // K but do it smarter. unameSplit = strings.SplitN(*path, string(os.PathSeparator), 2) if len(unameSplit) != 2 { unameSplit = append(unameSplit, "") } uname = strings.TrimPrefix(unameSplit[0], "~") if uname == "" { if u, err = user.Current(); err != nil { return } } else { if u, err = user.Lookup(uname); err != nil { return } } *path = filepath.Join(u.HomeDir, unameSplit[1]) return } /* GetFirst is the file equivalent of envs.GetFirst. It iterates through paths, normalizing them along the way (so abstracted paths such as ~/foo/bar.txt and relative paths such as bar/baz.txt will still work), and returns the content of the first found existing file. If the first found path is a directory, content will be nil but isDir will be true (as will ok). If no path exists, ok will be false. As always, results are not guaranteed due to permissions, etc. potentially returning an inaccurate result. This is a thin wrapper around GetFirstWithRef. */ func GetFirst(paths []string) (content []byte, isDir, ok bool) { content, isDir, ok, _ = GetFirstWithRef(paths) return } /* GetFirstWithRef is the file equivalent of envs.GetFirstWithRef. It behaves exactly like GetFirst, but with an additional returned value, idx, which specifies the index in paths in which a path was found. As always, results are not guaranteed due to permissions, etc. potentially returning an inaccurate result. */ func GetFirstWithRef(paths []string) (content []byte, isDir, ok bool, idx int) { var locPaths []string var exists bool var stat os.FileInfo var err error idx = -1 // We have to be a little less cavalier about this. if paths == nil { return } locPaths = make([]string, len(paths)) locPaths = paths[:] // Create an explicit copy so we don't modify paths. for i, p := range locPaths { if exists, stat, err = RealPathExistsStat(&p); err != nil { err = nil continue } if !exists { continue } isDir = stat.IsDir() if !isDir { if content, err = os.ReadFile(p); err != nil { continue } } ok = true idx = i return } return } /* MakeDirIfNotExist will create a directory at a given path if it doesn't exist. See also the documentation for RealPath. This is a bit more sane option than os.MkdirAll as it will normalize paths a little better. */ func MakeDirIfNotExist(path string) (err error) { var stat os.FileInfo var exists bool var locPath string = path if exists, stat, err = RealPathExistsStat(&locPath); 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. if err = os.MkdirAll(locPath, 0777); err != nil { return } err = nil return } else { return } } // So it exists, but it probably isn't a dir. if !stat.Mode().IsDir() { err = errors.New(fmt.Sprintf("path %v exists but is not a directory", locPath)) return } else { return } // This should probably never happen. Probably. err = errors.New("undefined") return } /* RealPath will transform a given path into the very best guess for an absolute path in-place. It is recommended to check err (if not nil) for an invalid path error. If this is true, the path syntax/string itself is not supported on the runtime OS. This can be done via: if errors.Is(err, fs.ErrInvalid) {...} */ func RealPath(path *string) (err error) { if err = ExpandHome(path); err != nil { return } if *path, err = filepath.Abs(*path); err != nil { return } return } /* RealPathExists is like RealPath, but will also return a boolean as to whether the path actually exists or not. Note that err *may* be os.ErrPermission/fs.ErrPermission, in which case the exists value cannot be trusted as a permission error occurred when trying to stat the path - if the calling user/process does not have read permission on e.g. a parent directory, then exists may be false but the path may actually exist. This condition can be checked via via: if errors.Is(err, fs.ErrPermission) {...} See also the documentation for RealPath. In those cases, it may be preferable to use RealPathExistsStat and checking stat for nil. */ func RealPathExists(path *string) (exists bool, err error) { if err = RealPath(path); err != nil { return } if _, err = os.Stat(*path); err != nil { if errors.Is(err, fs.ErrNotExist) { err = nil } return } exists = true return } /* RealPathExistsStat is like RealPathExists except it will also return the os.FileInfo for the path (assuming it exists). If stat is nil, it is highly recommended to check err via the methods suggested in the documentation for RealPath and RealPathExists. */ func RealPathExistsStat(path *string) (exists bool, stat os.FileInfo, err error) { if exists, err = RealPathExists(path); err != nil { return } if stat, err = os.Stat(*path); err != nil { return } return }