diff options
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/btrfs-ls-trees/main.go | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/cmd/btrfs-ls-trees/main.go b/cmd/btrfs-ls-trees/main.go new file mode 100644 index 0000000..c3156f5 --- /dev/null +++ b/cmd/btrfs-ls-trees/main.go @@ -0,0 +1,73 @@ +package main + +import ( + "fmt" + "os" + "strconv" + "text/tabwriter" + + "lukeshu.com/btrfs-tools/pkg/btrfs" + "lukeshu.com/btrfs-tools/pkg/btrfs/btrfsitem" + "lukeshu.com/btrfs-tools/pkg/btrfsmisc" + "lukeshu.com/btrfs-tools/pkg/util" +) + +func main() { + if err := Main(os.Args[1:]...); err != nil { + fmt.Fprintf(os.Stderr, "%v: error: %v\n", os.Args[0], err) + os.Exit(1) + } +} + +func Main(imgfilenames ...string) (err error) { + maybeSetErr := func(_err error) { + if _err != nil && err == nil { + err = _err + } + } + + fs, err := btrfsmisc.Open(os.O_RDONLY, imgfilenames...) + if err != nil { + return err + } + defer func() { + maybeSetErr(fs.Close()) + }() + + var treeErrCnt int + var treeItemCnt map[btrfsitem.Type]int + btrfsmisc.WalkFS(fs, btrfsmisc.WalkFSHandler{ + PreTree: func(name string, laddr btrfs.LogicalAddr) { + treeErrCnt = 0 + treeItemCnt = make(map[btrfsitem.Type]int) + fmt.Printf("tree laddr=%v name=%q\n", laddr, name) + }, + Err: func(_ error) { + treeErrCnt++ + }, + WalkTreeHandler: btrfs.WalkTreeHandler{ + Item: func(_ btrfs.WalkTreePath, item btrfs.Item) error { + typ := item.Head.Key.ItemType + treeItemCnt[typ] = treeItemCnt[typ] + 1 + return nil + }, + }, + PostTree: func(name string, laddr btrfs.LogicalAddr) { + totalItems := 0 + for _, cnt := range treeItemCnt { + totalItems += cnt + } + numWidth := len(strconv.Itoa(util.Max(treeErrCnt, totalItems))) + + table := tabwriter.NewWriter(os.Stdout, 0, 8, 2, ' ', 0) + fmt.Fprintf(table, " errors\t% *s\n", numWidth, strconv.Itoa(treeErrCnt)) + for _, typ := range util.SortedMapKeys(treeItemCnt) { + fmt.Fprintf(table, " %v items\t% *s\n", typ, numWidth, strconv.Itoa(treeItemCnt[typ])) + } + fmt.Fprintf(table, " total items\t% *s\n", numWidth, strconv.Itoa(totalItems)) + table.Flush() + }, + }) + + return nil +} |