Skip to content
This repository was archived by the owner on Jun 25, 2022. It is now read-only.
This repository was archived by the owner on Jun 25, 2022. It is now read-only.

Implementation of http.File and http.FileSystem doesn't handle directories correctly #211

@stoewer

Description

@stoewer

When opening a directory from a packr.Box using Open the resulting file object returns wrong information about this directory:

  1. IsDir() returns false
  2. ReadDir(-1) does not return os.FileInfo about files in this directory.

Assuming a folder structure like this:

resources
 |- testdata
     |- test_a.txt
     |- test_b.txt

The following small program illustrates this behavior:

func main() {
	httpDir := http.Dir("./resources")
	fmt.Println(listFiles(httpDir, "./testdata"))

	packrDir := packr.New("box", "./resources")
	fmt.Println(listFiles(packrDir, "./testdata"))
}

func listFiles(fs http.FileSystem, name string) []string {
	file, err := fs.Open(name)
	if err != nil {
		panic(err)
	}
	info, err := file.Stat()
	if err != nil {
		panic(err)
	}
	if !info.IsDir() {
		return []string{info.Name()}
	}
	childInfo, err := file.Readdir(-1)
	if err != nil {
		panic(err)
	}
	var names []string
	for _, info := range childInfo {
		if !info.IsDir() {
			names = append(names, info.Name())
		}
	}
	return names
}

The program prints out:

[test_a.txt test_b.txt]
[./testdata]

But of course the second line is expected to be identical to the first one.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions