Skip to content
This repository has been 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

Closed
stoewer opened this issue Jun 13, 2019 · 0 comments · Fixed by #241
Closed

Comments

@stoewer
Copy link

stoewer commented Jun 13, 2019

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.

Lesterpig added a commit to Lesterpig/packr that referenced this issue Sep 30, 2019
The disk resolver was the only one that returned directories as box
files. This modification allows http redirections of index.html to
work when working with the default disk resolver.

A test case has been added to verify http's behavior when working with
the disk resolver.

Fix gobuffalo#162
Fix gobuffalo#211
Fix gobuffalo#235
markbates pushed a commit that referenced this issue Sep 30, 2019
The disk resolver was the only one that returned directories as box
files. This modification allows http redirections of index.html to
work when working with the default disk resolver.

A test case has been added to verify http's behavior when working with
the disk resolver.

Fix #162
Fix #211
Fix #235
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant