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
|
package main
import (
"fmt"
"lukeshu.com/btrfs-tools/pkg/btrfs"
"lukeshu.com/btrfs-tools/pkg/btrfs/btrfsitem"
"lukeshu.com/btrfs-tools/pkg/util"
)
func walkFS(fs *btrfs.FS, cbs btrfs.WalkTreeHandler, errCb func(error)) {
origItem := cbs.Item
cbs.Item = func(key btrfs.Key, body btrfsitem.Item) error {
if key.ItemType == btrfsitem.ROOT_ITEM_KEY {
root, ok := body.(btrfsitem.Root)
if !ok {
errCb(fmt.Errorf("ROOT_ITEM_KEY is a %T, not a btrfsitem.Root", body))
} else if err := fs.WalkTree(root.ByteNr, cbs); err != nil {
errCb(fmt.Errorf("tree %v: %w", key.ObjectID.Format(0), err))
}
}
if origItem != nil {
return origItem(key, body)
}
return nil
}
superblock, err := fs.Superblock()
if err != nil {
errCb(fmt.Errorf("superblock: %w", err))
return
}
if err := fs.WalkTree(superblock.Data.RootTree, cbs); err != nil {
errCb(fmt.Errorf("root tree: %w", err))
}
if err := fs.WalkTree(superblock.Data.ChunkTree, cbs); err != nil {
errCb(fmt.Errorf("chunk tree: %w", err))
}
if err := fs.WalkTree(superblock.Data.LogTree, cbs); err != nil {
errCb(fmt.Errorf("log tree: %w", err))
}
if err := fs.WalkTree(superblock.Data.BlockGroupRoot, cbs); err != nil {
errCb(fmt.Errorf("block group tree: %w", err))
}
}
func pass2(fs *btrfs.FS, foundNodes map[btrfs.LogicalAddr]struct{}) {
fmt.Printf("\nPass 2: orphaned nodes\n")
visitedNodes := make(map[btrfs.LogicalAddr]struct{})
walkFS(fs, btrfs.WalkTreeHandler{
Node: func(node *util.Ref[btrfs.LogicalAddr, btrfs.Node], err error) error {
if err != nil {
fmt.Printf("Pass 2: node error: %v\n", err)
}
if node != nil {
visitedNodes[node.Addr] = struct{}{}
}
return nil
},
}, func(err error) {
fmt.Printf("Pass 2: walk error: %v\n", err)
})
orphanedNodes := make(map[btrfs.LogicalAddr]struct{})
for foundNode := range foundNodes {
if _, visited := visitedNodes[foundNode]; !visited {
orphanedNodes[foundNode] = struct{}{}
}
}
//fmt.Printf("Pass 2: orphanedNodes=%#v\n", orphanedNodes)
}
|