vfs: add debug dump function to dump the state of the VFS cache

This commit is contained in:
Nick Craig-Wood 2021-03-16 11:19:18 +00:00
parent 9deab5a563
commit c72d2c67ed

View File

@ -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<nil *Dir>\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