forked from arqbackup/arq_restore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommit.m
303 lines (282 loc) · 10.9 KB
/
Commit.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
//
// Commit.m
// Backup
//
// Created by Stefan Reitshamer on 3/21/09.
// Copyright 2009 PhotoMinds LLC. All rights reserved.
//
#import "IntegerIO.h"
#import "DateIO.h"
#import "StringIO.h"
#import "Commit.h"
#import "Blob.h"
#import "DataInputStream.h"
#import "RegexKitLite.h"
#import "StorageType.h"
#import "CommitFailedFile.h"
#import "BufferedInputStream.h"
#import "BooleanIO.h"
#import "DataIO.h"
#import "BlobKey.h"
#define HEADER_LENGTH (10)
@interface Commit (internal)
- (BOOL)readHeader:(BufferedInputStream *)is error:(NSError **)error;
@end
@implementation Commit
+ (NSString *)errorDomain {
return @"CommitErrorDomain";
}
@synthesize commitVersion,
author = _author,
comment = _comment,
treeBlobKey = _treeBlobKey,
parentCommitBlobKey = _parentCommitBlobKey,
location = _location,
computer = _computer,
creationDate = _creationDate,
commitFailedFiles = _commitFailedFiles,
hasMissingNodes = _hasMissingNodes,
isComplete = _isComplete,
bucketXMLData = _bucketXMLData;
- (id)initWithCommit:(Commit *)theCommit parentCommitBlobKey:(BlobKey *)theParentBlobKey {
if (self = [super init]) {
_author = [[theCommit author] copy];
_comment = [[theCommit comment] copy];
_parentCommitBlobKey = [theParentBlobKey retain];
_treeBlobKey = [[theCommit treeBlobKey] copy];
_location = [[theCommit location] copy];
_computer = [[theCommit computer] copy];
_creationDate = [[theCommit creationDate] copy];
_commitFailedFiles = [[theCommit commitFailedFiles] copy];
_hasMissingNodes = [theCommit hasMissingNodes];
_isComplete = [theCommit isComplete];
_bucketXMLData = [[theCommit bucketXMLData] copy];
}
return self;
}
- (id) initWithAuthor:(NSString *)theAuthor
comment:(NSString *)theComment
parentCommitBlobKey:(BlobKey *)theParentCommitBlobKey
treeBlobKey:(BlobKey *)theTreeBlobKey
location:(NSString *)theLocation
creationDate:(NSDate *)theCreationDate
commitFailedFiles:(NSArray *)theCommitFailedFiles
hasMissingNodes:(BOOL)theHasMissingNodes
isComplete:(BOOL)theIsComplete
bucketXMLData:(NSData *)theBucketXMLData {
if (self = [super init]) {
commitVersion = CURRENT_COMMIT_VERSION;
_author = [theAuthor copy];
_comment = [theComment copy];
_parentCommitBlobKey = [theParentCommitBlobKey retain];
_treeBlobKey = [theTreeBlobKey retain];
_location = [theLocation copy];
NSRange computerRange = [_location rangeOfRegex:@"^file://([^/]+)/" capture:1];
if (computerRange.location != NSNotFound) {
_computer = [[_location substringWithRange:computerRange] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
} else {
_computer = @"";
}
[_computer retain];
_creationDate = [theCreationDate retain];
_commitFailedFiles = [theCommitFailedFiles copy];
_hasMissingNodes = theHasMissingNodes;
_isComplete = theIsComplete;
_bucketXMLData = [theBucketXMLData copy];
}
return self;
}
- (id)initWithBufferedInputStream:(BufferedInputStream *)is error:(NSError **)error {
if (self = [super init]) {
if (![self readHeader:is error:error]) {
goto init_error;
}
if (![StringIO read:&_author from:is error:error]) {
goto init_error;
}
[_author retain];
if (![StringIO read:&_comment from:is error:error]) {
goto init_error;
}
[_comment retain];
uint64_t parentCommitKeyCount = 0;
if (![IntegerIO readUInt64:&parentCommitKeyCount from:is error:error]) {
goto init_error;
}
for (uint64_t i = 0; i < parentCommitKeyCount; i++) {
NSString *key;
BOOL cryptoKeyStretched = NO;
if (![StringIO read:&key from:is error:error]) {
goto init_error;
}
if (commitVersion >= 4) {
if (![BooleanIO read:&cryptoKeyStretched from:is error:error]) {
goto init_error;
}
}
if (_parentCommitBlobKey != nil) {
HSLogError(@"IGNORING EXTRA PARENT COMMIT BLOB KEY!");
} else {
_parentCommitBlobKey = [[BlobKey alloc] initWithSHA1:key storageType:StorageTypeS3 stretchEncryptionKey:cryptoKeyStretched compressed:NO];
}
}
NSString *treeSHA1 = nil;
BOOL treeStretchedKey = NO;
if (![StringIO read:&treeSHA1 from:is error:error]) {
goto init_error;
}
if (commitVersion >= 4) {
if (![BooleanIO read:&treeStretchedKey from:is error:error]) {
goto init_error;
}
}
BOOL treeIsCompressed = NO;
if (commitVersion >= 8) {
if (![BooleanIO read:&treeIsCompressed from:is error:error]) {
goto init_error;
}
}
_treeBlobKey = [[BlobKey alloc] initWithSHA1:treeSHA1 storageType:StorageTypeS3 stretchEncryptionKey:treeStretchedKey compressed:treeIsCompressed];
if (![StringIO read:&_location from:is error:error]) {
goto init_error;
}
[_location retain];
NSRange computerRange = [_location rangeOfRegex:@"^file://([^/]+)/" capture:1];
if (computerRange.location != NSNotFound) {
_computer = [[_location substringWithRange:computerRange] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
} else {
_computer = @"";
}
[_computer retain];
// Removed mergeCommonAncestorCommitBlobKey in Commit version 8. It was never used.
if (commitVersion < 8) {
NSString *mergeCommonAncestorCommitSHA1 = nil;
BOOL mergeCommonAncestorCommitStretchedKey = NO;
if (![StringIO read:&mergeCommonAncestorCommitSHA1 from:is error:error]) {
goto init_error;
}
if (commitVersion >= 4) {
if (![BooleanIO read:&mergeCommonAncestorCommitStretchedKey from:is error:error]) {
goto init_error;
}
}
// if (mergeCommonAncestorCommitSHA1 != nil) {
// _mergeCommonAncestorCommitBlobKey = [[BlobKey alloc] initWithSHA1:mergeCommonAncestorCommitSHA1 stretchEncryptionKey:mergeCommonAncestorCommitStretchedKey];
// }
}
if (![DateIO read:&_creationDate from:is error:error]) {
goto init_error;
}
[_creationDate retain];
if (commitVersion >= 3) {
uint64_t commitFailedFileCount = 0;
if (![IntegerIO readUInt64:&commitFailedFileCount from:is error:error]) {
goto init_error;
}
NSMutableArray *commitFailedFiles = [NSMutableArray array];
for (uint64_t index = 0; index < commitFailedFileCount; index++) {
CommitFailedFile *cff = [[CommitFailedFile alloc] initWithInputStream:is error:error];
if (cff == nil) {
goto init_error;
}
[commitFailedFiles addObject:cff];
[cff release];
}
_commitFailedFiles = [commitFailedFiles retain];
}
if (commitVersion >= 8) {
if (![BooleanIO read:&_hasMissingNodes from:is error:error]) {
goto init_error;
}
}
if (commitVersion >= 9) {
if (![BooleanIO read:&_isComplete from:is error:error]) {
goto init_error;
}
} else {
_isComplete = YES;
}
if (commitVersion >= 5) {
if (![DataIO read:&_bucketXMLData from:is error:error]) {
goto init_error;
}
[_bucketXMLData retain];
}
}
goto init_done;
init_error:
[self release];
self = nil;
init_done:
return self;
}
- (void)dealloc {
[_author release];
[_comment release];
[_parentCommitBlobKey release];
[_treeBlobKey release];
[_location release];
[_computer release];
[_creationDate release];
[_commitFailedFiles release];
[_bucketXMLData release];
[super dealloc];
}
- (NSData *)toData {
NSMutableData *data = [[[NSMutableData alloc] init] autorelease];
char header[HEADER_LENGTH + 1];
sprintf(header, "CommitV%03d", CURRENT_COMMIT_VERSION);
[data appendBytes:header length:HEADER_LENGTH];
[StringIO write:_author to:data];
[StringIO write:_comment to:data];
if (_parentCommitBlobKey == nil) {
[IntegerIO writeUInt64:0 to:data];
} else {
[IntegerIO writeUInt64:1 to:data];
[StringIO write:[_parentCommitBlobKey sha1] to:data];
[BooleanIO write:[_parentCommitBlobKey stretchEncryptionKey] to:data];
}
[StringIO write:[_treeBlobKey sha1] to:data];
[BooleanIO write:[_treeBlobKey stretchEncryptionKey] to:data];
[BooleanIO write:[_treeBlobKey compressed] to:data];
[StringIO write:_location to:data];
[DateIO write:_creationDate to:data];
uint64_t commitFailedFilesCount = (uint64_t)[_commitFailedFiles count];
[IntegerIO writeUInt64:commitFailedFilesCount to:data];
for (CommitFailedFile *cff in _commitFailedFiles) {
[cff writeTo:data];
}
[BooleanIO write:_hasMissingNodes to:data];
[BooleanIO write:_isComplete to:data];
[DataIO write:_bucketXMLData to:data];
return data;
}
#pragma mark NSObject
- (NSString *)description {
return [NSString stringWithFormat:@"<Commit: created=%@ tree=%@ parent=%@ complete=%@ missingnodes=%@>", _creationDate, _treeBlobKey, _parentCommitBlobKey, (_isComplete ? @"YES" : @"NO"), (_hasMissingNodes ? @"YES" : @"NO")];
}
@end
@implementation Commit (internal)
- (BOOL)readHeader:(BufferedInputStream *)is error:(NSError **)error {
BOOL ret = NO;
unsigned char *buf = (unsigned char *)malloc(HEADER_LENGTH);
if (![is readExactly:HEADER_LENGTH into:buf error:error]) {
goto readHeader_error;
}
NSString *header = [[[NSString alloc] initWithBytes:buf length:HEADER_LENGTH encoding:NSASCIIStringEncoding] autorelease];
if (![header hasPrefix:@"CommitV"] || [header length] < 8) {
HSLogDebug(@"current Commit version: %d", CURRENT_COMMIT_VERSION);
SETNSERROR([Commit errorDomain], ERROR_INVALID_COMMIT_HEADER, @"invalid header %@", header);
goto readHeader_error;
}
commitVersion = [[header substringFromIndex:7] intValue];
if (commitVersion > CURRENT_COMMIT_VERSION || commitVersion < 2) {
SETNSERROR([Commit errorDomain], ERROR_INVALID_OBJECT_VERSION, @"invalid Commit version %d", commitVersion);
goto readHeader_error;
}
ret = YES;
readHeader_error:
free(buf);
return ret;
}
@end