summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2022-06-01 09:00:16 -0600
committerLuke Shumaker <lukeshu@lukeshu.com>2022-06-01 09:10:38 -0600
commit539787e5f70776db02e37f74048f9acec2171b0e (patch)
treee7246ba8a2a32111979c225d404164ef95133620
parentcf314d3f1f96e2c07b68a7171e27f58c899bcdbb (diff)
more
-rw-r--r--cmd/btrfs-dump-tree/main.go51
-rw-r--r--pkg/btrfs/types_btree.go2
2 files changed, 38 insertions, 15 deletions
diff --git a/cmd/btrfs-dump-tree/main.go b/cmd/btrfs-dump-tree/main.go
index 8fc09e9..46fd741 100644
--- a/cmd/btrfs-dump-tree/main.go
+++ b/cmd/btrfs-dump-tree/main.go
@@ -54,30 +54,53 @@ func Main(imgfilename string) (err error) {
fmt.Printf("btrfs-progs v%s \n", version)
if superblock.Data.RootTree != 0 {
fmt.Printf("root tree\n")
- printTree(fs, superblock.Data.RootTree)
+ if err := printTree(fs, superblock.Data.RootTree); err != nil {
+ return err
+ }
}
if superblock.Data.ChunkTree != 0 {
fmt.Printf("chunk tree\n")
- printTree(fs, superblock.Data.ChunkTree)
+ if err := printTree(fs, superblock.Data.ChunkTree); err != nil {
+ return err
+ }
}
if superblock.Data.LogTree != 0 {
fmt.Printf("log root tree\n")
- printTree(fs, superblock.Data.LogTree)
+ if err := printTree(fs, superblock.Data.LogTree); err != nil {
+ return err
+ }
}
if superblock.Data.BlockGroupRoot != 0 {
fmt.Printf("block group tree\n")
- printTree(fs, superblock.Data.BlockGroupRoot)
+ if err := printTree(fs, superblock.Data.BlockGroupRoot); err != nil {
+ return err
+ }
+ }
+ if err := fs.WalkTree(superblock.Data.RootTree, func(key btrfs.Key, dat []byte) error {
+ if key.ItemType != btrfsitem.ROOT_ITEM_KEY {
+ return nil
+ }
+ var obj btrfsitem.Root
+ n, err := binstruct.Unmarshal(dat, &obj)
+ if err != nil {
+ return err
+ }
+ if n != len(dat) {
+ return fmt.Errorf("left over data: got %d bytes but only consumed %d", len(dat), n)
+ }
+ return printTree(fs, obj.ByteNr)
+ }); err != nil {
+ return err
}
return nil
}
// printTree mimics btrfs-progs kernel-shared/print-tree.c:btrfs_print_tree() and kernel-shared/print-tree.c:btrfs_print_leaf()
-func printTree(fs *btrfs.FS, root btrfs.LogicalAddr) {
+func printTree(fs *btrfs.FS, root btrfs.LogicalAddr) error {
nodeRef, err := fs.ReadNode(root)
if err != nil {
- fmt.Printf("error: %v\n", err)
- return
+ return err
}
node := nodeRef.Data
printHeaderInfo(node)
@@ -89,7 +112,9 @@ func printTree(fs *btrfs.FS, root btrfs.LogicalAddr) {
item.Generation)
}
for _, item := range node.BodyInternal {
- printTree(fs, item.BlockPtr)
+ if err := printTree(fs, item.BlockPtr); err != nil {
+ return err
+ }
}
} else { // leaf
for i, item := range node.BodyLeaf {
@@ -109,8 +134,7 @@ func printTree(fs *btrfs.FS, root btrfs.LogicalAddr) {
var ref btrfsitem.InodeRef
n, err := binstruct.Unmarshal(dat, &ref)
if err != nil {
- fmt.Printf("error: %v\n", err)
- return
+ return err
}
fmt.Printf("\t\tindex %d namelen %d name: %s\n",
ref.Index, ref.NameLen, ref.Name)
@@ -128,12 +152,10 @@ func printTree(fs *btrfs.FS, root btrfs.LogicalAddr) {
var obj btrfsitem.Root
n, err := binstruct.Unmarshal(item.Body, &obj)
if err != nil {
- fmt.Printf("error: %v\n", err)
- return
+ return err
}
if n != len(item.Body) {
- fmt.Printf("error: left over data: got %d bytes but only consumed %d\n", len(item.Body), n)
- return
+ return fmt.Errorf("left over data: got %d bytes but only consumed %d", len(item.Body), n)
}
fmt.Printf("\t\tgeneration %d root_dirid %d bytenr %d byte_limit %d bytes_used %d\n",
@@ -210,6 +232,7 @@ func printTree(fs *btrfs.FS, root btrfs.LogicalAddr) {
}
}
}
+ return nil
}
// printHeaderInfo mimics btrfs-progs kernel-shared/print-tree.c:print_header_info()
diff --git a/pkg/btrfs/types_btree.go b/pkg/btrfs/types_btree.go
index 8e25eb4..9b06e5e 100644
--- a/pkg/btrfs/types_btree.go
+++ b/pkg/btrfs/types_btree.go
@@ -91,7 +91,7 @@ type Item struct {
}
// MaxItems returns the maximum possible valid value of
-// .Haad.NumItems.
+// .Head.NumItems.
func (node Node) MaxItems() uint32 {
bodyBytes := node.Size - uint32(binstruct.StaticSize(NodeHeader{}))
if node.Head.Level > 0 {