memmap: opening empty path should be an error

This commit is contained in:
aviau 2018-06-07 15:23:05 -04:00
parent 787d034dfe
commit 3d76d59b73
No known key found for this signature in database
GPG Key ID: 8F2B113C6535C5A7
3 changed files with 20 additions and 5 deletions

View File

@ -73,7 +73,7 @@ func TestLstatIfPossible(t *testing.T) {
} }
testLstat := func(l Lstater, pathFile, pathSymlink string) { testLstat := func(l Lstater, pathFile, pathSymlink string) {
shouldLstat := pathSymlink != "" shouldLstat := pathSymlink != FilePathSeparator
statRegular := checkLstat(l, pathFile, shouldLstat) statRegular := checkLstat(l, pathFile, shouldLstat)
statSymlink := checkLstat(l, pathSymlink, shouldLstat) statSymlink := checkLstat(l, pathSymlink, shouldLstat)
if statRegular == nil || statSymlink == nil { if statRegular == nil || statSymlink == nil {
@ -81,7 +81,7 @@ func TestLstatIfPossible(t *testing.T) {
} }
symSym := statSymlink.Mode()&os.ModeSymlink == os.ModeSymlink symSym := statSymlink.Mode()&os.ModeSymlink == os.ModeSymlink
if symSym == (pathSymlink == "") { if symSym == (pathSymlink == FilePathSeparator) {
t.Fatal("expected the FileInfo to describe the symlink") t.Fatal("expected the FileInfo to describe the symlink")
} }
@ -95,8 +95,8 @@ func TestLstatIfPossible(t *testing.T) {
testLstat(overlayFs1, pathFile, pathSymlink) testLstat(overlayFs1, pathFile, pathSymlink)
testLstat(overlayFs2, pathFile, pathSymlink) testLstat(overlayFs2, pathFile, pathSymlink)
testLstat(basePathFs, "afero.txt", "symafero.txt") testLstat(basePathFs, "afero.txt", "symafero.txt")
testLstat(overlayFsMemOnly, pathFileMem, "") testLstat(overlayFsMemOnly, pathFileMem, FilePathSeparator)
testLstat(basePathFsMem, "aferom.txt", "") testLstat(basePathFsMem, "aferom.txt", FilePathSeparator)
testLstat(roFs, pathFile, pathSymlink) testLstat(roFs, pathFile, pathSymlink)
testLstat(roFsMem, pathFileMem, "") testLstat(roFsMem, pathFileMem, FilePathSeparator)
} }

View File

@ -159,6 +159,11 @@ func (m *MemMapFs) MkdirAll(path string, perm os.FileMode) error {
// Handle some relative paths // Handle some relative paths
func normalizePath(path string) string { func normalizePath(path string) string {
// Cleaning an empty path would result in "."
if path == "" {
return path
}
path = filepath.Clean(path) path = filepath.Clean(path)
switch path { switch path {

View File

@ -449,3 +449,13 @@ func TestMemFsUnexpectedEOF(t *testing.T) {
t.Fatal("Expected ErrUnexpectedEOF") t.Fatal("Expected ErrUnexpectedEOF")
} }
} }
func TestStatEmptyPath(t *testing.T) {
t.Parallel()
fs := NewMemMapFs()
if _, err := fs.Stat(""); !os.IsNotExist(err) {
t.Fatal(err)
}
}