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"
"lukeshu.com/btrfs-tools/pkg/btrfs/btrfsitem"
"lukeshu.com/btrfs-tools/pkg/btrfsmisc"
)
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)
}
}
const version = "5.18.1"
func Main(imgfilename string) (err error) {
maybeSetErr := func(_err error) {
if _err != nil && err == nil {
err = _err
}
}
fs, err := btrfsmisc.Open(os.O_RDONLY, imgfilename)
if err != nil {
return err
}
defer func() {
maybeSetErr(fs.Close())
}()
superblock, err := fs.Superblock()
if err != nil {
return err
}
fmt.Printf("btrfs-progs v%v\n", version)
if superblock.Data.RootTree != 0 {
fmt.Printf("root tree\n")
if err := btrfsmisc.PrintTree(fs, superblock.Data.RootTree); err != nil {
return err
}
}
if superblock.Data.ChunkTree != 0 {
fmt.Printf("chunk tree\n")
if err := btrfsmisc.PrintTree(fs, superblock.Data.ChunkTree); err != nil {
return err
}
}
if superblock.Data.LogTree != 0 {
fmt.Printf("log root tree\n")
if err := btrfsmisc.PrintTree(fs, superblock.Data.LogTree); err != nil {
return err
}
}
if superblock.Data.BlockGroupRoot != 0 {
fmt.Printf("block group tree\n")
if err := btrfsmisc.PrintTree(fs, superblock.Data.BlockGroupRoot); err != nil {
return err
}
}
if err := fs.TreeWalk(superblock.Data.RootTree, btrfs.TreeWalkHandler{
Item: func(_ btrfs.TreePath, item btrfs.Item) error {
if item.Head.Key.ItemType != btrfsitem.ROOT_ITEM_KEY {
return nil
}
treeName, ok := map[btrfs.ObjID]string{
btrfs.ROOT_TREE_OBJECTID: "root",
btrfs.EXTENT_TREE_OBJECTID: "extent",
btrfs.CHUNK_TREE_OBJECTID: "chunk",
btrfs.DEV_TREE_OBJECTID: "device",
btrfs.FS_TREE_OBJECTID: "fs",
btrfs.ROOT_TREE_DIR_OBJECTID: "directory",
btrfs.CSUM_TREE_OBJECTID: "checksum",
btrfs.ORPHAN_OBJECTID: "orphan",
btrfs.TREE_LOG_OBJECTID: "log",
btrfs.TREE_LOG_FIXUP_OBJECTID: "log fixup",
btrfs.TREE_RELOC_OBJECTID: "reloc",
btrfs.DATA_RELOC_TREE_OBJECTID: "data reloc",
btrfs.EXTENT_CSUM_OBJECTID: "extent checksum",
btrfs.QUOTA_TREE_OBJECTID: "quota",
btrfs.UUID_TREE_OBJECTID: "uuid",
btrfs.FREE_SPACE_TREE_OBJECTID: "free space",
btrfs.MULTIPLE_OBJECTIDS: "multiple",
btrfs.BLOCK_GROUP_TREE_OBJECTID: "block group",
}[item.Head.Key.ObjectID]
if !ok {
treeName = "file"
}
fmt.Printf("%v tree %v \n", treeName, btrfsmisc.FmtKey(item.Head.Key))
return btrfsmisc.PrintTree(fs, item.Body.(btrfsitem.Root).ByteNr)
},
}); err != nil {
return err
}
fmt.Printf("total bytes %v\n", superblock.Data.TotalBytes)
fmt.Printf("bytes used %v\n", superblock.Data.BytesUsed)
fmt.Printf("uuid %v\n", superblock.Data.FSUUID)
return nil
}
|