|
|
|
|
@@ -20,19 +20,18 @@ package paths
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
`fmt`
|
|
|
|
|
`io/fs`
|
|
|
|
|
"fmt"
|
|
|
|
|
"io/fs"
|
|
|
|
|
"os"
|
|
|
|
|
"os/user"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
`strings`
|
|
|
|
|
|
|
|
|
|
"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.
|
|
|
|
|
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) {
|
|
|
|
|
|
|
|
|
|
@@ -84,9 +83,9 @@ func ExpandHome(path *string) (err error) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
MakeDirIfNotExist will create a directory at a given path if it doesn't exist.
|
|
|
|
|
MakeDirIfNotExist will create a directory at a given path if it doesn't exist.
|
|
|
|
|
|
|
|
|
|
See also the documentation for RealPath.
|
|
|
|
|
See also the documentation for RealPath.
|
|
|
|
|
*/
|
|
|
|
|
func MakeDirIfNotExist(path string) (err error) {
|
|
|
|
|
|
|
|
|
|
@@ -113,6 +112,8 @@ func MakeDirIfNotExist(path string) (err error) {
|
|
|
|
|
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.
|
|
|
|
|
@@ -121,12 +122,12 @@ func MakeDirIfNotExist(path string) (err error) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
RealPath will transform a given path into the very best guess for an absolute path in-place.
|
|
|
|
|
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:
|
|
|
|
|
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) {...}
|
|
|
|
|
if errors.Is(err, fs.ErrInvalid) {...}
|
|
|
|
|
*/
|
|
|
|
|
func RealPath(path *string) (err error) {
|
|
|
|
|
|
|
|
|
|
@@ -142,20 +143,20 @@ func RealPath(path *string) (err error) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
RealPathExists is like RealPath, but will also return a boolean as to whether the path
|
|
|
|
|
actually exists or not.
|
|
|
|
|
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:
|
|
|
|
|
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) {...}
|
|
|
|
|
if errors.Is(err, fs.ErrPermission) {...}
|
|
|
|
|
|
|
|
|
|
See also the documentation for RealPath.
|
|
|
|
|
See also the documentation for RealPath.
|
|
|
|
|
|
|
|
|
|
In those cases, it may be preferable to use RealPathExistsStat and checking stat for nil.
|
|
|
|
|
In those cases, it may be preferable to use RealPathExistsStat and checking stat for nil.
|
|
|
|
|
*/
|
|
|
|
|
func RealPathExists(path *string) (exists bool, err error) {
|
|
|
|
|
|
|
|
|
|
@@ -176,11 +177,11 @@ func RealPathExists(path *string) (exists bool, err error) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
RealPathExistsStat is like RealPathExists except it will also return the os.FileInfo
|
|
|
|
|
for the path (assuming it exists).
|
|
|
|
|
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.
|
|
|
|
|
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) {
|
|
|
|
|
|
|
|
|
|
|