Skip to content

Commit 2c0bb93

Browse files
Merge pull request #4844 from ipfs/fix/4800
fix(mfs): Directory.Path not working, add test
2 parents 036d3bd + 48f706b commit 2c0bb93

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

mfs/dir.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,15 @@ func (d *Directory) Path() string {
404404
cur := d
405405
var out string
406406
for cur != nil {
407-
out = path.Join(cur.name, out)
408-
cur = cur.parent.(*Directory)
407+
switch parent := cur.parent.(type) {
408+
case *Directory:
409+
out = path.Join(cur.name, out)
410+
cur = parent
411+
case *Root:
412+
return "/" + out
413+
default:
414+
panic("directory parent neither a directory nor a root")
415+
}
409416
}
410417
return out
411418
}

mfs/mfs_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,11 @@ func TestDirectoryLoadFromDag(t *testing.T) {
324324

325325
topd := topi.(*Directory)
326326

327+
path := topd.Path()
328+
if path != "/foo" {
329+
t.Fatalf("Expected path '/foo', got '%s'", path)
330+
}
331+
327332
// mkdir over existing but unloaded child file should fail
328333
_, err = topd.Mkdir("a")
329334
if err == nil {

0 commit comments

Comments
 (0)