1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
package main
import (
"fmt"
"os"
"lukeshu.com/btrfs-tools/pkg/btrfs"
)
func main() {
if err := Main(os.Args[1]); err != nil {
fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err)
os.Exit(1)
}
}
const version = "5.17"
func Main(imgfilename string) (err error) {
maybeSetErr := func(_err error) {
if _err != nil && err == nil {
err = _err
}
}
fh, err := os.Open(imgfilename)
if err != nil {
return err
}
defer func() {
maybeSetErr(fh.Close())
}()
fs := &btrfs.FS{
Devices: []*btrfs.Device{
{
File: fh,
},
},
}
superblock, err := fs.Superblock()
if err != nil {
return err
}
fmt.Printf("btrfs-progs v%s \n", version)
if superblock.Data.RootTree != 0 && false { // XXX
fmt.Printf("root tree\n")
printTree(fs, superblock.Data.RootTree)
}
if superblock.Data.ChunkTree != 0 {
fmt.Printf("chunk tree\n")
printTree(fs, superblock.Data.ChunkTree)
}
if superblock.Data.LogTree != 0 {
fmt.Printf("log root tree\n")
printTree(fs, superblock.Data.LogTree)
}
if superblock.Data.BlockGroupRoot != 0 {
fmt.Printf("block group tree\n")
printTree(fs, superblock.Data.BlockGroupRoot)
}
return nil
}
// printTree mimics btrfs-progs kernel-shared/print-tree.c:btrfs_print_tree()
func printTree(fs *btrfs.FS, root btrfs.LogicalAddr) {
node, err := fs.ReadNode(root)
if err != nil {
fmt.Printf("error: %v\n", err)
return
}
printHeaderInfo(node)
// TODO
}
// printHeaderInfo mimics btrfs-progs kernel-shared/print-tree.c:print_header_info()
func printHeaderInfo(node btrfs.Node) {
var typename string
switch node := node.(type) {
case *btrfs.InternalNode:
typename = "node"
fmt.Printf("node %d level %d items %d free space %d",
node.Header.Addr,
node.Header.Data.Level,
node.Header.Data.NumItems,
node.Header.Data.MaxItems-node.Header.Data.NumItems)
case *btrfs.LeafNode:
typename = "leaf"
fmt.Printf("leaf %d items %d free space %d",
node.Header.Addr,
node.Header.Data.NumItems,
node.FreeSpace())
}
fmt.Printf(" generation %d owner %v\n",
node.GetNodeHeader().Data.Generation,
node.GetNodeHeader().Data.Owner)
fmt.Printf("%s %d flags %s backref revision %d\n",
typename,
node.GetNodeHeader().Addr,
node.GetNodeHeader().Data.Flags,
node.GetNodeHeader().Data.BackrefRev)
}
|