forked from arqbackup/arq_restore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tree.m
316 lines (299 loc) · 11.4 KB
/
Tree.m
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
//
// Tree.m
// Backup
//
// Created by Stefan Reitshamer on 3/25/09.
// Copyright 2009 PhotoMinds LLC. All rights reserved.
//
#include <sys/stat.h>
#import "StringIO.h"
#import "IntegerIO.h"
#import "BooleanIO.h"
#import "Node.h"
#import "Tree.h"
#import "Blob.h"
#import "DataInputStream.h"
#import "RegexKitLite.h"
#import "BufferedInputStream.h"
#import "NSData-Gzip.h"
#import "GunzipInputStream.h"
#import "BlobKey.h"
#import "NSObject_extra.h"
#import "BlobKeyIO.h"
@interface Tree (internal)
- (BOOL)readHeader:(BufferedInputStream *)is error:(NSError **)error;
@end
@implementation Tree
@synthesize xattrsAreCompressed, xattrsBlobKey, xattrsSize, aclIsCompressed, aclBlobKey, uid, gid, mode, mtime_sec, mtime_nsec, flags, finderFlags, extendedFinderFlags, st_dev, treeVersion, st_rdev;
@synthesize ctime_sec, ctime_nsec, createTime_sec, createTime_nsec, st_nlink, st_ino, st_blocks, st_blksize;
@dynamic aggregateUncompressedDataSize;
+ (NSString *)errorDomain {
return @"TreeErrorDomain";
}
- (id)init {
if (self = [super init]) {
nodes = [[NSMutableDictionary alloc] init];
missingNodes = [[NSMutableDictionary alloc] init];
}
return self;
}
- (id)initWithBufferedInputStream:(BufferedInputStream *)is error:(NSError **)error {
if (self = [super init]) {
nodes = [[NSMutableDictionary alloc] init];
missingNodes = [[NSMutableDictionary alloc] init];
if (![self readHeader:is error:error]) {
[self release];
return nil;
}
if (treeVersion >= 12) {
if (![BooleanIO read:&xattrsAreCompressed from:is error:error]
|| ![BooleanIO read:&aclIsCompressed from:is error:error]) {
[self release];
return nil;
}
}
BOOL ret = [BlobKeyIO read:&xattrsBlobKey from:is treeVersion:treeVersion compressed:xattrsAreCompressed error:error]
&& [IntegerIO readUInt64:&xattrsSize from:is error:error]
&& [BlobKeyIO read:&aclBlobKey from:is treeVersion:treeVersion compressed:aclIsCompressed error:error]
&& [IntegerIO readInt32:&uid from:is error:error]
&& [IntegerIO readInt32:&gid from:is error:error]
&& [IntegerIO readInt32:&mode from:is error:error]
&& [IntegerIO readInt64:&mtime_sec from:is error:error]
&& [IntegerIO readInt64:&mtime_nsec from:is error:error]
&& [IntegerIO readInt64:&flags from:is error:error]
&& [IntegerIO readInt32:&finderFlags from:is error:error]
&& [IntegerIO readInt32:&extendedFinderFlags from:is error:error]
&& [IntegerIO readInt32:&st_dev from:is error:error]
&& [IntegerIO readInt32:&st_ino from:is error:error]
&& [IntegerIO readUInt32:&st_nlink from:is error:error]
&& [IntegerIO readInt32:&st_rdev from:is error:error]
&& [IntegerIO readInt64:&ctime_sec from:is error:error]
&& [IntegerIO readInt64:&ctime_nsec from:is error:error]
&& [IntegerIO readInt64:&st_blocks from:is error:error]
&& [IntegerIO readUInt32:&st_blksize from:is error:error];
[xattrsBlobKey retain];
[aclBlobKey retain];
if (!ret) {
goto initError;
}
if ([xattrsBlobKey sha1] == nil) {
[xattrsBlobKey release];
xattrsBlobKey = nil;
}
if ([aclBlobKey sha1] == nil) {
[aclBlobKey release];
aclBlobKey = nil;
}
if (treeVersion >= 11 && treeVersion <= 16) {
uint64_t unusedAggregateSizeOnDisk;
if (![IntegerIO readUInt64:&unusedAggregateSizeOnDisk from:is error:error]) {
goto initError;
}
}
if (treeVersion >= 15) {
if (![IntegerIO readInt64:&createTime_sec from:is error:error]
|| ![IntegerIO readInt64:&createTime_nsec from:is error:error]) {
goto initError;
}
}
if (treeVersion >= 18) {
uint32_t missingNodeCount;
if (![IntegerIO readUInt32:&missingNodeCount from:is error:error]) {
goto initError;
}
for (uint32_t i = 0; i < missingNodeCount; i++) {
NSString *missingNodeName = nil;
if (![StringIO read:&missingNodeName from:is error:error]) {
goto initError;
}
Node *node = [[[Node alloc] initWithInputStream:is treeVersion:treeVersion error:error] autorelease];
if (node == nil) {
goto initError;
}
[missingNodes setObject:node forKey:missingNodeName];
}
}
uint32_t nodeCount;
if (![IntegerIO readUInt32:&nodeCount from:is error:error]) {
goto initError;
}
for (uint32_t i = 0; i < nodeCount; i++) {
NSString *nodeName;
if (![StringIO read:&nodeName from:is error:error]) {
goto initError;
}
Node *node = [[Node alloc] initWithInputStream:is treeVersion:treeVersion error:error];
if (!node) {
goto initError;
}
[nodes setObject:node forKey:nodeName];
[node release];
}
goto initDone;
initError:
[self release];
self = nil;
}
initDone:
return self;
}
- (void)dealloc {
[xattrsBlobKey release];
[aclBlobKey release];
[nodes release];
[missingNodes release];
[super dealloc];
}
- (NSArray *)childNodeNames {
return [nodes allKeys];
}
- (Node *)childNodeWithName:(NSString *)name {
return [nodes objectForKey:name];
}
- (BOOL)containsNodeNamed:(NSString *)name {
return [nodes objectForKey:name] != nil;
}
- (BOOL)containsMissingItems {
if ([missingNodes count] > 0) {
return YES;
}
for (Node *node in [nodes allValues]) {
if ([node isTree] && [node treeContainsMissingItems]) {
return YES;
}
}
return NO;
}
- (NSArray *)missingChildNodeNames {
return [missingNodes allKeys];
}
- (Node *)missingChildNodeWithName:(NSString *)name {
return [missingNodes objectForKey:name];
}
- (NSDictionary *)nodes {
return nodes;
}
- (NSDictionary *)missingNodes {
return missingNodes;
}
- (NSData *)toData {
NSMutableData *data = [[[NSMutableData alloc] init] autorelease];
[BooleanIO write:xattrsAreCompressed to:data];
[BooleanIO write:aclIsCompressed to:data];
[BlobKeyIO write:xattrsBlobKey to:data];
[IntegerIO writeUInt64:xattrsSize to:data];
[BlobKeyIO write:aclBlobKey to:data];
[IntegerIO writeInt32:uid to:data];
[IntegerIO writeInt32:gid to:data];
[IntegerIO writeInt32:mode to:data];
[IntegerIO writeInt64:mtime_sec to:data];
[IntegerIO writeInt64:mtime_nsec to:data];
[IntegerIO writeInt64:flags to:data];
[IntegerIO writeInt32:finderFlags to:data];
[IntegerIO writeInt32:extendedFinderFlags to:data];
[IntegerIO writeInt32:st_dev to:data];
[IntegerIO writeInt32:st_ino to:data];
[IntegerIO writeUInt32:st_nlink to:data];
[IntegerIO writeInt32:st_rdev to:data];
[IntegerIO writeInt64:ctime_sec to:data];
[IntegerIO writeInt64:ctime_nsec to:data];
[IntegerIO writeInt64:st_blocks to:data];
[IntegerIO writeUInt32:st_blksize to:data];
[IntegerIO writeInt64:createTime_sec to:data];
[IntegerIO writeInt64:createTime_nsec to:data];
[IntegerIO writeUInt32:(uint32_t)[missingNodes count] to:data];
for (NSString *missingNodeName in [missingNodes allKeys]) {
[StringIO write:missingNodeName to:data];
[[missingNodes objectForKey:missingNodeName] writeToData:data];
}
[IntegerIO writeUInt32:(uint32_t)[nodes count] to:data];
NSMutableArray *nodeNames = [NSMutableArray arrayWithArray:[nodes allKeys]];
[nodeNames sortUsingSelector:@selector(compare:)];
for (NSString *nodeName in nodeNames) {
[StringIO write:nodeName to:data];
Node *node = [nodes objectForKey:nodeName];
[node writeToData:data];
}
char header[TREE_HEADER_LENGTH + 1];
sprintf(header, "TreeV%03d", CURRENT_TREE_VERSION);
NSMutableData *completeData = [[[NSMutableData alloc] init] autorelease];
[completeData appendBytes:header length:TREE_HEADER_LENGTH];
[completeData appendBytes:[data bytes] length:[data length]];
return completeData;
}
- (BOOL)ctimeMatchesStat:(struct stat *)st {
return st->st_ctimespec.tv_sec == ctime_sec && st->st_ctimespec.tv_nsec == ctime_nsec;
}
- (uint64_t)aggregateUncompressedDataSize {
//FIXME: This doesn't include the size of the ACL.
uint64_t ret = xattrsSize;
for (Node *node in [nodes allValues]) {
ret += [node uncompressedDataSize];
}
return ret;
}
#pragma mark NSObject
- (BOOL)isEqual:(id)object {
if (![object isKindOfClass:[Tree class]]) {
return NO;
}
Tree *other = (Tree *)object;
BOOL ret = (treeVersion == [other treeVersion]
&& xattrsAreCompressed == [other xattrsAreCompressed]
&& [NSObject equalObjects:xattrsBlobKey and:[other xattrsBlobKey]]
&& xattrsSize == [other xattrsSize]
&& aclIsCompressed == [other aclIsCompressed]
&& [NSObject equalObjects:aclBlobKey and:[other aclBlobKey]]
&& uid == [other uid]
&& gid == [other gid]
&& mode == [other mode]
&& mtime_sec == [other mtime_sec]
&& mtime_nsec == [other mtime_nsec]
&& flags == [other flags]
&& finderFlags == [other finderFlags]
&& extendedFinderFlags == [other extendedFinderFlags]
&& st_dev == [other st_dev]
&& st_ino == [other st_ino]
&& st_nlink == [other st_nlink]
&& st_rdev == [other st_rdev]
&& ctime_sec == [other ctime_sec]
&& ctime_nsec == [other ctime_nsec]
&& createTime_sec == [other createTime_sec]
&& createTime_nsec == [other createTime_nsec]
&& [missingNodes isEqual:[other missingNodes]]
&& st_blocks == [other st_blocks]
&& st_blksize == [other st_blksize]
&& [nodes isEqual:[other nodes]]);
return ret;
}
- (NSUInteger)hash {
return (NSUInteger)treeVersion + [nodes hash];
}
@end
@implementation Tree (internal)
- (BOOL)readHeader:(BufferedInputStream *)is error:(NSError **)error {
BOOL ret = NO;
unsigned char *buf = (unsigned char *)malloc(TREE_HEADER_LENGTH);
if (![is readExactly:TREE_HEADER_LENGTH into:buf error:error]) {
goto readHeader_error;
}
NSString *header = [[[NSString alloc] initWithBytes:buf length:TREE_HEADER_LENGTH encoding:NSASCIIStringEncoding] autorelease];
if (![header hasPrefix:@"TreeV"] || [header length] < 6) {
SETNSERROR([Tree errorDomain], ERROR_INVALID_OBJECT_VERSION, @"invalid Tree header: %@", header);
goto readHeader_error;
}
treeVersion = [[header substringFromIndex:5] intValue];
if (treeVersion < 10) {
SETNSERROR([Tree errorDomain], ERROR_INVALID_OBJECT_VERSION, @"invalid Tree header: %@", header);
goto readHeader_error;
}
if (treeVersion == 13) {
SETNSERROR([Tree errorDomain], ERROR_INVALID_OBJECT_VERSION, @"invalid Tree version 13");
goto readHeader_error;
}
ret = YES;
readHeader_error:
free(buf);
return ret;
}
@end