-
Notifications
You must be signed in to change notification settings - Fork 7
/
api.go
407 lines (321 loc) · 9.67 KB
/
api.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package filesystem
import (
"fmt"
"io"
"github.com/Cloud-Foundations/Dominator/lib/filter"
"github.com/Cloud-Foundations/Dominator/lib/format"
"github.com/Cloud-Foundations/Dominator/lib/hash"
)
type NumLinksTable map[uint64]int
type ListSelector uint8
const (
ListSelectSkipMode = 1 << iota
ListSelectSkipNumLinks
ListSelectSkipUid
ListSelectSkipGid
ListSelectSkipSizeDevnum
ListSelectSkipMtime
ListSelectSkipName
ListSelectSkipData
ListSelectAll = 0
)
type GenericInode interface {
GetGid() uint32
GetUid() uint32
List(w io.Writer, name string, numLinksTable NumLinksTable,
numLinks int, listSelector ListSelector, filter *filter.Filter) error
SetGid(gid uint32)
SetUid(uid uint32)
WriteMetadata(name string) error
}
type InodeTable map[uint64]GenericInode
type InodeToFilenamesTable map[uint64][]string
type FilenameToInodeTable map[string]uint64
type HashToInodesTable map[hash.Hash][]uint64
type FileSystem struct {
InodeTable InodeTable
inodeToFilenamesTable InodeToFilenamesTable
filenameToInodeTable FilenameToInodeTable
hashToInodesTable HashToInodesTable
NumRegularInodes uint64
TotalDataBytes uint64
numComputedRegularInodes *uint64
DirectoryCount uint64
DirectoryInode
}
func Decode(reader io.Reader) (*FileSystem, error) {
return decode(reader)
}
func (fs *FileSystem) BuildNumLinksTable() NumLinksTable {
return buildNumLinksTable(fs)
}
func (fs *FileSystem) ComputeTotalDataBytes() {
fs.computeTotalDataBytes()
}
func (fs *FileSystem) Encode(writer io.Writer) error {
return fs.encode(writer)
}
func (fs *FileSystem) EstimateUsage(blockSize uint64) uint64 {
return fs.estimateUsage(blockSize)
}
func (fs *FileSystem) FilenameToInodeTable() FilenameToInodeTable {
return fs.buildFilenameToInodeTable()
}
func (fs *FileSystem) Filter(filter *filter.Filter) *FileSystem {
return fs.filter(filter)
}
func (fs *FileSystem) ForEachFile(
fn func(name string, inodeNumber uint64, inode GenericInode) error) error {
return fs.forEachFile(fn)
}
func (fs *FileSystem) GetObjects() map[hash.Hash]uint64 {
return fs.getObjects()
}
func (fs *FileSystem) HashToInodesTable() HashToInodesTable {
return fs.buildHashToInodesTable()
}
func (fs *FileSystem) InodeToFilenamesTable() InodeToFilenamesTable {
return fs.buildInodeToFilenamesTable()
}
func (fs *FileSystem) List(w io.Writer) error {
return fs.list(w, ListSelectAll, nil)
}
func (fs *FileSystem) Listf(w io.Writer, listSelector ListSelector,
filter *filter.Filter) error {
return fs.list(w, listSelector, filter)
}
func (fs *FileSystem) NumComputedRegularInodes() uint64 {
return fs.computeNumComputedRegularInodes()
}
func (fs *FileSystem) RebuildInodePointers() error {
return fs.rebuildInodePointers()
}
func (fs *FileSystem) String() string {
return fmt.Sprintf("Tree: %d inodes, total file size: %s, number of regular inodes: %d",
len(fs.InodeTable),
format.FormatBytes(fs.TotalDataBytes),
fs.NumRegularInodes)
}
type DirectoryInode struct {
EntryList []*DirectoryEntry
EntriesByName map[string]*DirectoryEntry
Mode FileMode
Uid uint32
Gid uint32
}
func (directory *DirectoryInode) BuildEntryMap() {
directory.buildEntryMap()
}
func (inode *DirectoryInode) GetGid() uint32 {
return inode.Gid
}
func (inode *DirectoryInode) GetUid() uint32 {
return inode.Uid
}
func (inode *DirectoryInode) List(w io.Writer, name string,
numLinksTable NumLinksTable, numLinks int,
listSelector ListSelector, filter *filter.Filter) error {
return inode.list(w, name, numLinksTable, numLinks, listSelector, filter)
}
func (inode *DirectoryInode) ReplaceStrings(replaceFunc func(string) string) {
inode.replaceStrings(replaceFunc)
}
func (inode *DirectoryInode) SetGid(gid uint32) {
inode.Gid = gid
}
func (inode *DirectoryInode) SetUid(uid uint32) {
inode.Uid = uid
}
func (inode *DirectoryInode) Write(name string) error {
return inode.write(name)
}
func (inode *DirectoryInode) WriteMetadata(name string) error {
return inode.writeMetadata(name)
}
type DirectoryEntry struct {
Name string
InodeNumber uint64
inode GenericInode // Keep private to avoid encoding/transmission.
}
func (dirent *DirectoryEntry) Inode() GenericInode {
return dirent.inode
}
func (dirent *DirectoryEntry) SetInode(inode GenericInode) {
dirent.inode = inode
}
func (dirent *DirectoryEntry) String() string {
return dirent.Name
}
type RegularInode struct {
Mode FileMode
Uid uint32
Gid uint32
MtimeNanoSeconds int32
MtimeSeconds int64
Size uint64
Hash hash.Hash
}
func (inode *RegularInode) GetGid() uint32 {
return inode.Gid
}
func (inode *RegularInode) GetUid() uint32 {
return inode.Uid
}
func (inode *RegularInode) List(w io.Writer, name string,
numLinksTable NumLinksTable, numLinks int,
listSelector ListSelector, filter *filter.Filter) error {
return inode.list(w, name, numLinksTable, numLinks, listSelector)
}
func (inode *RegularInode) SetGid(gid uint32) {
inode.Gid = gid
}
func (inode *RegularInode) SetUid(uid uint32) {
inode.Uid = uid
}
func (inode *RegularInode) WriteMetadata(name string) error {
return inode.writeMetadata(name)
}
type ComputedRegularInode struct {
Mode FileMode
Uid uint32
Gid uint32
Source string
}
func (inode *ComputedRegularInode) GetGid() uint32 {
return inode.Gid
}
func (inode *ComputedRegularInode) GetUid() uint32 {
return inode.Uid
}
func (inode *ComputedRegularInode) List(w io.Writer, name string,
numLinksTable NumLinksTable, numLinks int,
listSelector ListSelector, filter *filter.Filter) error {
return inode.list(w, name, numLinksTable, numLinks, listSelector)
}
func (inode *ComputedRegularInode) SetGid(gid uint32) {
inode.Gid = gid
}
func (inode *ComputedRegularInode) SetUid(uid uint32) {
inode.Uid = uid
}
func (inode *ComputedRegularInode) WriteMetadata(name string) error {
panic("cannot write metadata for computed file: " + name)
}
type SymlinkInode struct {
Uid uint32
Gid uint32
Symlink string
}
func (inode *SymlinkInode) GetGid() uint32 {
return inode.Gid
}
func (inode *SymlinkInode) GetUid() uint32 {
return inode.Uid
}
func (inode *SymlinkInode) List(w io.Writer, name string,
numLinksTable NumLinksTable, numLinks int,
listSelector ListSelector, filter *filter.Filter) error {
return inode.list(w, name, numLinksTable, numLinks, listSelector)
}
func (inode *SymlinkInode) SetGid(gid uint32) {
inode.Gid = gid
}
func (inode *SymlinkInode) SetUid(uid uint32) {
inode.Uid = uid
}
func (inode *SymlinkInode) Write(name string) error {
return inode.write(name)
}
func (inode *SymlinkInode) WriteMetadata(name string) error {
return inode.writeMetadata(name)
}
type SpecialInode struct {
Mode FileMode
Uid uint32
Gid uint32
MtimeNanoSeconds int32
MtimeSeconds int64
Rdev uint64
}
func (inode *SpecialInode) GetGid() uint32 {
return inode.Gid
}
func (inode *SpecialInode) GetUid() uint32 {
return inode.Uid
}
func (inode *SpecialInode) List(w io.Writer, name string,
numLinksTable NumLinksTable, numLinks int,
listSelector ListSelector, filter *filter.Filter) error {
return inode.list(w, name, numLinksTable, numLinks, listSelector)
}
func (inode *SpecialInode) SetGid(gid uint32) {
inode.Gid = gid
}
func (inode *SpecialInode) SetUid(uid uint32) {
inode.Uid = uid
}
func (inode *SpecialInode) Write(name string) error {
return inode.write(name)
}
func (inode *SpecialInode) WriteMetadata(name string) error {
return inode.writeMetadata(name)
}
type FileMode uint32
func (mode FileMode) String() string {
return mode.string()
}
func CompareFileSystems(left, right *FileSystem, logWriter io.Writer) bool {
return compareFileSystems(left, right, logWriter)
}
func CompareDirectoryInodes(left, right *DirectoryInode,
logWriter io.Writer) bool {
return compareDirectoryInodes(left, right, logWriter)
}
func CompareDirectoriesMetadata(left, right *DirectoryInode,
logWriter io.Writer) bool {
return compareDirectoriesMetadata(left, right, logWriter)
}
func CompareDirectoryEntries(left, right *DirectoryEntry,
logWriter io.Writer) bool {
return compareDirectoryEntries(left, right, logWriter)
}
func CompareInodes(left, right GenericInode, logWriter io.Writer) (
sameType, sameMetadata, sameData bool) {
return compareInodes(left, right, logWriter)
}
func CompareRegularInodes(left, right *RegularInode, logWriter io.Writer) bool {
return compareRegularInodes(left, right, logWriter)
}
func CompareRegularInodesMetadata(left, right *RegularInode,
logWriter io.Writer) bool {
return compareRegularInodesMetadata(left, right, logWriter)
}
func CompareRegularInodesData(left, right *RegularInode,
logWriter io.Writer) bool {
return compareRegularInodesData(left, right, logWriter)
}
func CompareSymlinkInodes(left, right *SymlinkInode, logWriter io.Writer) bool {
return compareSymlinkInodes(left, right, logWriter)
}
func CompareSymlinkInodesMetadata(left, right *SymlinkInode,
logWriter io.Writer) bool {
return compareSymlinkInodesMetadata(left, right, logWriter)
}
func CompareSymlinkInodesData(left, right *SymlinkInode,
logWriter io.Writer) bool {
return compareSymlinkInodesData(left, right, logWriter)
}
func CompareSpecialInodes(left, right *SpecialInode, logWriter io.Writer) bool {
return compareSpecialInodes(left, right, logWriter)
}
func CompareSpecialInodesMetadata(left, right *SpecialInode,
logWriter io.Writer) bool {
return compareSpecialInodesMetadata(left, right, logWriter)
}
func CompareSpecialInodesData(left, right *SpecialInode,
logWriter io.Writer) bool {
return compareSpecialInodesData(left, right, logWriter)
}
func ForceWriteMetadata(inode GenericInode, name string) error {
return forceWriteMetadata(inode, name)
}