Skip to content

MemMapFs and Walk issues with leading / #450

@sweharris

Description

@sweharris

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
...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions