/
btrfs_rb_tree.go
335 lines (306 loc) · 7.99 KB
/
btrfs_rb_tree.go
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
This file is part of btrsync.
Btrsync is free software: you can redistribute it and/or modify it under the terms of the
GNU Lesser General Public License as published by the Free Software Foundation, either
version 3 of the License, or (at your option) any later version.
Btrsync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with btrsync.
If not, see <https://www.gnu.org/licenses/>.
*/
package btrfs
import (
"errors"
"fmt"
"os"
"time"
"github.com/google/uuid"
)
// RBRoot represents the root of the Btrfs tree.
type RBRoot struct {
RBNode *RBNode
}
// RBNode represents a node in the Btrfs tree.
type RBNode struct {
RBLeft *RBNode
RBRight *RBNode
Info *RootInfo
}
// RootInfo represents the information about a Btrfs root.
// RBNode contains a reference to the node in the tree.
type RootInfo struct {
RBNode *RBNode
RootID ObjectID
RootOffset uint64
Flags uint64
RefTree ObjectID
DirID uint64
TopID uint64
Generation uint64
OriginalGeneration uint64
CreationTime time.Time
SendTime time.Time
ReceiveTime time.Time
UUID uuid.UUID
ParentUUID uuid.UUID
ReceivedUUID uuid.UUID
Path string
Name string
// Only populated by resolving the path while building a tree.
FullPath string
Deleted bool
// Only populated during SubvolumeSearch with SearchWithSnapshots
Snapshots []*RootInfo
// The underlying item and reference that built this info
Item *BtrfsRootItem
Ref *BtrfsRootRef
}
func newRBRoot() *RBRoot { return &RBRoot{} }
func (r *RBRoot) InsertRoot(info *RootInfo) {
if r.RBNode == nil {
r.RBNode = info.RBNode
return
}
r.insertRoot(r.RBNode, info)
}
func (r *RBRoot) insertRoot(node *RBNode, info *RootInfo) {
if info.RootID < node.Info.RootID {
if node.RBLeft == nil {
node.RBLeft = info.RBNode
return
}
r.insertRoot(node.RBLeft, info)
}
if node.RBRight == nil {
node.RBRight = info.RBNode
return
}
r.insertRoot(node.RBRight, info)
}
func (r *RBRoot) LookupRoot(rootID ObjectID) *RootInfo {
if r.RBNode == nil {
return nil
}
return r.lookupRoot(r.RBNode, rootID)
}
func (r *RBRoot) lookupRoot(node *RBNode, rootID ObjectID) *RootInfo {
if node.Info.RootID == rootID {
return node.Info
}
if rootID < node.Info.RootID {
if node.RBLeft == nil {
return nil
}
return r.lookupRoot(node.RBLeft, rootID)
}
if node.RBRight == nil {
return nil
}
return r.lookupRoot(node.RBRight, rootID)
}
func (r *RBRoot) UpdateRoot(info *RootInfo) bool {
toUpdate := r.LookupRoot(info.RootID)
if toUpdate == nil {
return false
}
if info.RootOffset != 0 {
toUpdate.RootOffset = info.RootOffset
}
if info.Flags != 0 {
toUpdate.Flags = info.Flags
}
if info.RefTree != 0 {
toUpdate.RefTree = info.RefTree
}
if info.DirID != 0 {
toUpdate.DirID = info.DirID
}
if info.TopID != 0 {
toUpdate.TopID = info.TopID
}
if info.Generation != 0 {
toUpdate.Generation = info.Generation
}
if info.OriginalGeneration != 0 {
toUpdate.OriginalGeneration = info.OriginalGeneration
}
if !info.CreationTime.IsZero() {
toUpdate.CreationTime = info.CreationTime
}
if !info.SendTime.IsZero() {
toUpdate.SendTime = info.SendTime
}
if !info.ReceiveTime.IsZero() {
toUpdate.ReceiveTime = info.ReceiveTime
}
if info.UUID != uuid.Nil {
toUpdate.UUID = info.UUID
}
if info.ParentUUID != uuid.Nil {
toUpdate.ParentUUID = info.ParentUUID
}
if info.ReceivedUUID != uuid.Nil {
toUpdate.ReceivedUUID = info.ReceivedUUID
}
if info.Path != "" {
toUpdate.Path = info.Path
}
if info.Name != "" {
toUpdate.Name = info.Name
}
if info.FullPath != "" {
toUpdate.FullPath = info.FullPath
}
if info.Deleted {
toUpdate.Deleted = info.Deleted
}
if info.Item != nil {
toUpdate.Item = info.Item
}
if info.Ref != nil {
toUpdate.Ref = info.Ref
}
return true
}
// RBTreeIterFunc is the function signature for the RBTreeIterFunc.
// Lasterr is the last error returned by the function. If the function returns
// an ErrStopTreeIteration error, the iteration will stop and the error will be
// returned by RBTree.Iterate.
type RBTreeIterFunc func(info *RootInfo, lastErr error) error
var ErrStopTreeIteration = fmt.Errorf("stop tree iteration")
func (r *RBRoot) PreOrderIterate(f RBTreeIterFunc) error {
if r.RBNode == nil {
return nil
}
return r.preOrderIterate(r.RBNode, f, nil)
}
func (r *RBRoot) preOrderIterate(node *RBNode, f RBTreeIterFunc, lastErr error) error {
lastErr = f(node.Info, lastErr)
if lastErr != nil && errors.Is(lastErr, ErrStopTreeIteration) {
return lastErr
}
if node.RBLeft != nil {
lastErr = r.preOrderIterate(node.RBLeft, f, lastErr)
if lastErr != nil && errors.Is(lastErr, ErrStopTreeIteration) {
return lastErr
}
}
if node.RBRight != nil {
lastErr = r.preOrderIterate(node.RBRight, f, lastErr)
if lastErr != nil && errors.Is(lastErr, ErrStopTreeIteration) {
return lastErr
}
}
return lastErr
}
func (r *RBRoot) PostOrderIterate(f RBTreeIterFunc) error {
if r.RBNode == nil {
return nil
}
return r.postOrderIterate(r.RBNode, f, nil)
}
func (r *RBRoot) postOrderIterate(node *RBNode, f RBTreeIterFunc, lastErr error) error {
if node.RBLeft != nil {
lastErr = r.postOrderIterate(node.RBLeft, f, lastErr)
if lastErr != nil && errors.Is(lastErr, ErrStopTreeIteration) {
return lastErr
}
}
if node.RBRight != nil {
lastErr = r.postOrderIterate(node.RBRight, f, lastErr)
if lastErr != nil && errors.Is(lastErr, ErrStopTreeIteration) {
return lastErr
}
}
return f(node.Info, lastErr)
}
func (r *RBRoot) InOrderIterate(f RBTreeIterFunc) error {
if r.RBNode == nil {
return nil
}
return r.inOrderIterate(r.RBNode, f, nil)
}
func (r *RBRoot) inOrderIterate(node *RBNode, f RBTreeIterFunc, lastErr error) error {
if node.RBLeft != nil {
lastErr = r.inOrderIterate(node.RBLeft, f, lastErr)
if lastErr != nil && errors.Is(lastErr, ErrStopTreeIteration) {
return lastErr
}
}
lastErr = f(node.Info, lastErr)
if lastErr != nil && errors.Is(lastErr, ErrStopTreeIteration) {
return lastErr
}
if node.RBRight != nil {
lastErr = r.inOrderIterate(node.RBRight, f, lastErr)
if lastErr != nil && errors.Is(lastErr, ErrStopTreeIteration) {
return lastErr
}
}
return lastErr
}
func (r *RBRoot) FilterFromRoot(rootID ObjectID) *RBRoot {
tree := newRBRoot()
r.InOrderIterate(func(info *RootInfo, lastErr error) error {
if info.RootID == rootID || info.RefTree == rootID {
inf := info
node := &RBNode{Info: inf}
inf.RBNode = node
tree.InsertRoot(inf)
return nil
}
parent := tree.LookupRoot(info.RefTree)
for parent != nil && parent.RefTree != 0 {
parent = tree.LookupRoot(parent.RefTree)
if parent == nil {
break
}
if parent.RootID == rootID || parent.RefTree == rootID {
inf := info
node := &RBNode{Info: inf}
inf.RBNode = node
tree.InsertRoot(inf)
return nil
}
}
return nil
})
return tree
}
func (r *RBRoot) resolveFullPaths(fromRoot string, topID uint64) error {
f, err := os.OpenFile(fromRoot, os.O_RDONLY, os.ModeDir)
if err != nil {
return err
}
defer f.Close()
return r.PreOrderIterate(func(info *RootInfo, lastErr error) error {
if lastErr != nil {
return lastErr
}
info.Deleted = info.RefTree == 0
if info.Path != "" || info.RefTree == 0 {
return nil
}
// Lookup path relative to the parent subvolume
var path string
path, err := lookupInoPath(f.Fd(), info)
if err != nil {
return err
}
info.Path = path
// Resolve full path up to root mount
fullpath := path
next := info.RefTree
for uint64(next) != topID && next != FSTreeObjectID {
found := r.LookupRoot(next)
if found == nil {
break
}
fullpath = found.Name + "/" + fullpath
next = found.RefTree
}
info.FullPath = fullpath
return nil
})
}