afero
afero copied to clipboard
MemMapFs.OpenFile allows opening, reading from, and writing to directories as regular files without error.
This behavior does not match the behavior of most operating systems, and I believe is likely a bug. Treating a directory like a regular files in situations where is does not make sense should produce errors like OsFs.
I was about to file a new issue for this problem as I've recently encountered this issue while trying to write some tests.
I've created a test case if that could help fix the issue:
package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/spf13/afero"
)
func main() {
memoryFS := afero.NewMemMapFs()
err := memoryFS.Mkdir("/test-dir", 0777)
if err != nil {
fmt.Fprintf(os.Stderr, "Unexpected error creating in-memory directory: %s\n", err)
os.Exit(1)
}
what, err := afero.ReadFile(memoryFS, "/test-dir")
if err != nil { // The following if does not occur as expected
fmt.Fprintf(os.Stderr, "Expected error opening in-memory directory: %s\n", err)
os.Exit(1)
}
fmt.Printf("Directory: %#v\n", what)
dir, err := ioutil.TempDir("", "on-disk-test-")
if err != nil {
fmt.Fprintf(os.Stderr, "Unexpected error creating on-disk directory: %s\n", err)
os.Exit(1)
}
defer os.RemoveAll(dir)
diskFS := afero.NewOsFs()
_, err = afero.ReadFile(diskFS, dir)
if err != nil {
fmt.Fprintf(os.Stderr, "Expected error opening on-disk directory: %s\n", err)
os.Exit(2)
}
panic("Not expected to get this far")
}