afero
afero copied to clipboard
MemMapFs and Walk issues with leading /
If I create a directory called Hello and then do a Walk from "/" then the walk function is called with an error open /Hello: file does not exist.
Similarly if I create a directory called /Hello but do a Walk from "" then I get a similar error; open Hello: file does not exist
Clearly the Walk is finding the directory but then failing to get information about it!
The Mkdir and Walk starting points must either both be anchored at / or not.
example code:
package main
import (
"fmt"
"os"
"github.com/spf13/afero"
)
func find_walk(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Found", path)
}
return err
}
func main() {
MyFs := afero.NewMemMapFs()
MyFs.Mkdir("Hello", 0777)
MyFs.Mkdir("Hello/There", 0777)
MyFs.Create("Hello/There/Everyone")
err := afero.Walk(MyFs, "/", find_walk)
if err != nil {
fmt.Println("Find failed", err)
}
}
This returns the following:
% ./main
Found /
open /Hello: file does not exist
Find failed open /Hello: file does not exist
If I change the Walk to "" then it works:
Found
Found Hello
Found Hello/There
Found Hello/There/Everyone
Similarly if I do Mkdir with a leading / and if the Walk is from "" then I get the opposite error:
MyFs.Mkdir("/Hello", 0777)
MyFs.Mkdir("/Hello/There", 0777)
MyFs.Create("/Hello/There/Everyone")
...
Found
Hello open Hello: file does not exist
Find failed open Hello: file does not exist
Even more fun is if the Mkdir is inconsistent! Starting from "/" returns things twice!
MyFs.Mkdir("Hello", 0777)
MyFs.Mkdir("/Hello/There", 0777)
MyFs.Create("/Hello/There/Everyone")
err := afero.Walk(MyFs, "/", find_walk)
...
Found /
Found /Hello
Found /Hello/There
Found /Hello/There/Everyone
Found /Hello
Found /Hello/There
Found /Hello/There/Everyone
...