Merge pull request #96 from Snapstromegon/master

isDirectory resolve ~ to User HomeDir
This commit is contained in:
Daniel J Walsh 2020-03-23 18:20:19 -04:00 committed by GitHub
commit ac6db2fcba
1 changed files with 26 additions and 0 deletions

View File

@ -806,9 +806,35 @@ func IsValidDeviceMode(mode string) bool {
return true return true
} }
// resolveHomeDir converts a path referencing the home directory via "~"
// to an absolute path
func resolveHomeDir(path string) (string, error) {
// check if the path references the home dir to avoid work
// don't use strings.HasPrefix(path, "~") as this doesn't match "~" alone
// use strings.HasPrefix(...) to not match "something/~/something"
if !(path == "~" || strings.HasPrefix(path, "~/")) {
// path does not reference home dir -> Nothing to do
return path, nil
}
// only get HomeDir when necessary
home, err := unshare.HomeDir()
if err != nil {
return "", err
}
// replace the first "~" (start of path) with the HomeDir to resolve "~"
return strings.Replace(path, "~", home, 1), nil
}
// isDirectory tests whether the given path exists and is a directory. It // isDirectory tests whether the given path exists and is a directory. It
// follows symlinks. // follows symlinks.
func isDirectory(path string) error { func isDirectory(path string) error {
path, err := resolveHomeDir(path)
if err != nil {
return err
}
info, err := os.Stat(path) info, err := os.Stat(path)
if err != nil { if err != nil {
return err return err