From 2437eb3ccefb64340d191e0fe5a4bdd5b67b3479 Mon Sep 17 00:00:00 2001 From: albertony <12441419+albertony@users.noreply.github.com> Date: Thu, 13 Jan 2022 13:33:39 +0100 Subject: [PATCH] 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 --- vfs/vfscache/cache.go | 2 +- vfs/vfscommon/path.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/vfs/vfscache/cache.go b/vfs/vfscache/cache.go index dc1b73f14..979652891 100644 --- a/vfs/vfscache/cache.go +++ b/vfs/vfscache/cache.go @@ -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) diff --git a/vfs/vfscommon/path.go b/vfs/vfscommon/path.go index 77225df0f..93b42a93e 100644 --- a/vfs/vfscommon/path.go +++ b/vfs/vfscommon/path.go @@ -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