afero icon indicating copy to clipboard operation
afero copied to clipboard

HttpFs.Dir doesn't work properly when given root path

Open nekr0z opened this issue 5 years ago • 1 comments

If you try

httpFs := afero.NewHttpFs(<ExistingFS>)
fileserver := http.FileServer(httpFs.Dir("/")))
http.Handle("/", fileserver)

you'll find that your fileserver successfully lists the directory, but gives 404 on any file. However, if you do

httpFs := afero.NewHttpFs(<ExistingFS>)
fileserver := http.FileServer(httpFs.Dir("/something/")))
http.Handle("/", fileserver)

everything works as expected.

nekr0z avatar Oct 19 '19 08:10 nekr0z

Similar bug for memory fs

package main

import "github.com/spf13/afero"
import "fmt"

func main() {

	fass := afero.NewMemMapFs()
	fass.Mkdir("/", 0755)
	slash, _ := afero.DirExists(fass, "/")
	fmt.Printf("%t\n", slash)

	st, err := fass.Stat("/")
	fmt.Printf("«%s»\n", st.Name())
        fmt.Printf("err: %T\n", err)

}

os.Stat("/").Name() returns "/", but the memory file system returns "", which is very wrong. Compare with the unix tool basename / - which also returns / for slash and not "".

tox2ik avatar Dec 26 '20 00:12 tox2ik