diff --git a/mfs/dir.go b/mfs/dir.go index 08b703fb26d..0cc6e6568c8 100644 --- a/mfs/dir.go +++ b/mfs/dir.go @@ -404,8 +404,15 @@ func (d *Directory) Path() string { cur := d var out string for cur != nil { - out = path.Join(cur.name, out) - cur = cur.parent.(*Directory) + switch parent := cur.parent.(type) { + case *Directory: + out = path.Join(cur.name, out) + cur = parent + case *Root: + return "/" + out + default: + panic("directory parent neither a directory nor a root") + } } return out } diff --git a/mfs/mfs_test.go b/mfs/mfs_test.go index d7124bdf108..25e61854b88 100644 --- a/mfs/mfs_test.go +++ b/mfs/mfs_test.go @@ -324,6 +324,11 @@ func TestDirectoryLoadFromDag(t *testing.T) { topd := topi.(*Directory) + path := topd.Path() + if path != "/foo" { + t.Fatalf("Expected path '/foo', got '%s'", path) + } + // mkdir over existing but unloaded child file should fail _, err = topd.Mkdir("a") if err == nil {