afero icon indicating copy to clipboard operation
afero copied to clipboard

MemMapFs.OpenFile allows opening, reading from, and writing to directories as regular files without error.

Open andremarianiello opened this issue 8 years ago • 1 comments

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.

andremarianiello avatar Oct 11 '16 20:10 andremarianiello

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")
}

XenoPhex avatar Jan 25 '21 00:01 XenoPhex