vfs: fix incorrect detection of root in parent directory utility function

When using filepath.Dir, a difference to path.Dir is that it returns os PathSeparator
instead of slash when the path consists entirely of separators.

Also fixed casing of the function name, use OS in all caps instead of Os
as recommended here: https://github.com/golang/go/wiki/CodeReviewComments#initialisms
This commit is contained in:
albertony 2022-01-13 13:33:39 +01:00
parent a12c94caff
commit 2437eb3cce
2 changed files with 4 additions and 4 deletions

View File

@ -376,7 +376,7 @@ func rename(osOldPath, osNewPath string) error {
if !os.IsNotExist(err) {
return fmt.Errorf("Failed to stat destination: %s: %w", osNewPath, err)
}
parent := vfscommon.OsFindParent(osNewPath)
parent := vfscommon.OSFindParent(osNewPath)
err = createDir(parent)
if err != nil {
return fmt.Errorf("Failed to create parent dir: %s: %w", parent, err)

View File

@ -5,11 +5,11 @@ import (
"path/filepath"
)
// OsFindParent returns the parent directory of name, or "" for the
// OSFindParent returns the parent directory of name, or "" for the
// root for OS native paths.
func OsFindParent(name string) string {
func OSFindParent(name string) string {
parent := filepath.Dir(name)
if parent == "." || parent == "/" {
if parent == "." || (len(parent) == 1 && parent[0] == filepath.Separator) {
parent = ""
}
return parent