From 3d76d59b7339c7d3724bedcee205933578801a41 Mon Sep 17 00:00:00 2001 From: aviau Date: Thu, 7 Jun 2018 15:23:05 -0400 Subject: [PATCH] memmap: opening empty path should be an error --- lstater_test.go | 10 +++++----- memmap.go | 5 +++++ memmap_test.go | 10 ++++++++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/lstater_test.go b/lstater_test.go index 477924f..5723224 100644 --- a/lstater_test.go +++ b/lstater_test.go @@ -73,7 +73,7 @@ func TestLstatIfPossible(t *testing.T) { } testLstat := func(l Lstater, pathFile, pathSymlink string) { - shouldLstat := pathSymlink != "" + shouldLstat := pathSymlink != FilePathSeparator statRegular := checkLstat(l, pathFile, shouldLstat) statSymlink := checkLstat(l, pathSymlink, shouldLstat) if statRegular == nil || statSymlink == nil { @@ -81,7 +81,7 @@ func TestLstatIfPossible(t *testing.T) { } symSym := statSymlink.Mode()&os.ModeSymlink == os.ModeSymlink - if symSym == (pathSymlink == "") { + if symSym == (pathSymlink == FilePathSeparator) { t.Fatal("expected the FileInfo to describe the symlink") } @@ -95,8 +95,8 @@ func TestLstatIfPossible(t *testing.T) { testLstat(overlayFs1, pathFile, pathSymlink) testLstat(overlayFs2, pathFile, pathSymlink) testLstat(basePathFs, "afero.txt", "symafero.txt") - testLstat(overlayFsMemOnly, pathFileMem, "") - testLstat(basePathFsMem, "aferom.txt", "") + testLstat(overlayFsMemOnly, pathFileMem, FilePathSeparator) + testLstat(basePathFsMem, "aferom.txt", FilePathSeparator) testLstat(roFs, pathFile, pathSymlink) - testLstat(roFsMem, pathFileMem, "") + testLstat(roFsMem, pathFileMem, FilePathSeparator) } diff --git a/memmap.go b/memmap.go index 09498e7..8d163df 100644 --- a/memmap.go +++ b/memmap.go @@ -159,6 +159,11 @@ func (m *MemMapFs) MkdirAll(path string, perm os.FileMode) error { // Handle some relative paths func normalizePath(path string) string { + // Cleaning an empty path would result in "." + if path == "" { + return path + } + path = filepath.Clean(path) switch path { diff --git a/memmap_test.go b/memmap_test.go index 47414ab..5d9ac68 100644 --- a/memmap_test.go +++ b/memmap_test.go @@ -449,3 +449,13 @@ func TestMemFsUnexpectedEOF(t *testing.T) { t.Fatal("Expected ErrUnexpectedEOF") } } + +func TestStatEmptyPath(t *testing.T) { + t.Parallel() + + fs := NewMemMapFs() + + if _, err := fs.Stat(""); !os.IsNotExist(err) { + t.Fatal(err) + } +}