From c72d2c67edfcd25839bf392f7fd14be94a46fea8 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Tue, 16 Mar 2021 11:19:18 +0000 Subject: [PATCH] vfs: add debug dump function to dump the state of the VFS cache --- vfs/dir.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/vfs/dir.go b/vfs/dir.go index 849793a00..97b812287 100644 --- a/vfs/dir.go +++ b/vfs/dir.go @@ -2,6 +2,7 @@ package vfs import ( "context" + "fmt" "os" "path" "sort" @@ -74,6 +75,47 @@ func (d *Dir) String() string { return d.path + "/" } +// Dumps the directory tree to the string builder with the given indent +func (d *Dir) dumpIndent(out *strings.Builder, indent string) { + if d == nil { + fmt.Fprintf(out, "%s\n", indent) + return + } + d.mu.RLock() + defer d.mu.RUnlock() + fmt.Fprintf(out, "%sPath: %s\n", indent, d.path) + fmt.Fprintf(out, "%sEntry: %v\n", indent, d.entry) + fmt.Fprintf(out, "%sRead: %v\n", indent, d.read) + fmt.Fprintf(out, "%s- items %d\n", indent, len(d.items)) + // Sort? + for leaf, node := range d.items { + switch x := node.(type) { + case *Dir: + fmt.Fprintf(out, "%s %s/ - %v\n", indent, leaf, x) + // check the parent is correct + if x.parent != d { + fmt.Fprintf(out, "%s PARENT POINTER WRONG\n", indent) + } + x.dumpIndent(out, indent+"\t") + case *File: + fmt.Fprintf(out, "%s %s - %v\n", indent, leaf, x) + default: + panic("bad dir entry") + } + } + fmt.Fprintf(out, "%s- virtual %d\n", indent, len(d.virtual)) + for leaf, state := range d.virtual { + fmt.Fprintf(out, "%s %s - %v\n", indent, leaf, state) + } +} + +// Dumps a nicely formatted directory tree to a string +func (d *Dir) dump() string { + var out strings.Builder + d.dumpIndent(&out, "") + return out.String() +} + // IsFile returns false for Dir - satisfies Node interface func (d *Dir) IsFile() bool { return false