forked from arqbackup/arq_restore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArqRestoreCommand.m
850 lines (753 loc) · 35.5 KB
/
ArqRestoreCommand.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
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
/*
Copyright (c) 2009-2017, Haystack Software LLC https://www.arqbackup.com
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the names of PhotoMinds LLC or Haystack Software, nor the names of
their contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <termios.h>
#import "ArqRestoreCommand.h"
#import "Target.h"
#import "AWSRegion.h"
#import "BackupSet.h"
#import "S3Service.h"
#import "UserAndComputer.h"
#import "Bucket.h"
#import "Repo.h"
#import "StandardRestorerParamSet.h"
#import "Tree.h"
#import "Commit.h"
#import "Node.h"
#import "BlobKey.h"
#import "StandardRestorer.h"
#import "S3GlacierRestorerParamSet.h"
#import "S3GlacierRestorer.h"
#import "GlacierRestorerParamSet.h"
#import "GlacierRestorer.h"
#import "S3AuthorizationProvider.h"
#import "S3AuthorizationProviderFactory.h"
#import "NSString_extra.h"
#import "TargetFactory.h"
#import "RegexKitLite.h"
#import "BackupSet.h"
#import "ExePath.h"
#import "AWSRegion.h"
#define BUFSIZE (65536)
@implementation ArqRestoreCommand
- (NSString *)errorDomain {
return @"ArqRestoreCommandErrorDomain";
}
- (BOOL)executeWithArgc:(int)argc argv:(const char **)argv error:(NSError **)error {
NSMutableArray *args = [NSMutableArray array];
for (int i = 0; i < argc; i++) {
[args addObject:[[[NSString alloc] initWithBytes:argv[i] length:strlen(argv[i]) encoding:NSUTF8StringEncoding] autorelease]];
}
if ([args count] < 2) {
SETNSERROR([self errorDomain], ERROR_USAGE, @"missing arguments");
return NO;
}
if ([args count] > 3 && [[args objectAtIndex:1] isEqualToString:@"-l"]) {
[[HSLog sharedHSLog] setHSLogLevel:[HSLog hsLogLevelForName:[args objectAtIndex:2]]];
args = [NSMutableArray arrayWithArray:[args subarrayWithRange:NSMakeRange(2, [args count] - 2)]];
}
NSString *cmd = [args objectAtIndex:1];
if ([cmd isEqualToString:@"listtargets"]) {
return [self listTargets:error];
} else if ([cmd isEqualToString:@"addtarget"]) {
return [self addTarget:args error:error];
} else if ([cmd isEqualToString:@"deletetarget"]) {
return [self deleteTarget:args error:error];
} else if ([cmd isEqualToString:@"listcomputers"]) {
return [self listComputers:args error:error];
} else if ([cmd isEqualToString:@"listfolders"]) {
return [self listFolders:args error:error];
} else if ([cmd isEqualToString:@"printplist"]) {
return [self printPlist:args error:error];
} else if ([cmd isEqualToString:@"listtree"]) {
return [self listTree:args error:error];
} else if ([cmd isEqualToString:@"restore"]) {
return [self restore:args error:error];
} else if ([cmd isEqualToString:@"clearcache"]) {
return [self clearCache:args error:error];
} else {
SETNSERROR([self errorDomain], ERROR_USAGE, @"unknown command: %@", cmd);
return NO;
}
return YES;
}
#pragma mark internal
- (BOOL)listTargets:(NSError **)error {
printf("%-20s %s\n", "nickname:", "url:");
for (Target *target in [[TargetFactory sharedTargetFactory] sortedTargets]) {
printf("%-20s %s\n", [[target nickname] UTF8String], [[[target endpoint] description] UTF8String]);
}
return YES;
}
- (BOOL)addTarget:(NSArray *)args error:(NSError **)error {
if ([args count] < 5) {
SETNSERROR([self errorDomain], ERROR_USAGE, @"missing arguments");
return NO;
}
NSString *targetUUID = [NSString stringWithRandomUUID];
NSString *targetNickname = [args objectAtIndex:2];
NSString *targetType = [args objectAtIndex:3];
NSURL *endpoint = nil;
NSString *secret = nil;
NSString *passphrase = nil;
NSString *oAuth2ClientId = nil;
NSString *oAuth2ClientSecret = nil;
NSString *oAuth2RedirectURI = nil;
if ([targetType isEqualToString:@"aws"]) {
if ([args count] != 5) {
SETNSERROR([self errorDomain], ERROR_USAGE, @"invalid arguments");
return NO;
}
NSString *accessKeyId = [args objectAtIndex:4];
AWSRegion *usEast1 = [AWSRegion usEast1];
NSString *urlString = [NSString stringWithFormat:@"https://%@@%@/any_bucket", accessKeyId, [[usEast1 s3EndpointWithSSL:NO] host]];
endpoint = [NSURL URLWithString:urlString];
secret = [self readPasswordWithPrompt:@"enter AWS secret key:" error:error];
if (secret == nil) {
return NO;
}
} else if ([targetType isEqualToString:@"local"]) {
if ([args count] != 5) {
SETNSERROR([self errorDomain], ERROR_USAGE, @"invalid arguments");
return NO;
}
endpoint = [NSURL fileURLWithPath:[args objectAtIndex:4]];
secret = @"unused";
} else {
SETNSERROR([self errorDomain], -1, @"unknown target type: %@", targetType);
return NO;
}
Target *target = [[[Target alloc] initWithUUID:targetUUID nickname:targetNickname endpoint:endpoint awsRequestSignatureVersion:4] autorelease];
[target setOAuth2ClientId:oAuth2ClientId];
[target setOAuth2RedirectURI:oAuth2RedirectURI];
if (![[TargetFactory sharedTargetFactory] saveTarget:target error:error]) {
return NO;
}
if (![target setSecret:secret trustedAppPaths:[NSArray arrayWithObject:[ExePath exePath]] error:error]) {
return NO;
}
if (passphrase != nil) {
if (![target setPassphrase:passphrase trustedAppPaths:[NSArray arrayWithObject:[ExePath exePath]] error:error]) {
return NO;
}
}
if (oAuth2ClientSecret != nil) {
if (![target setOAuth2ClientSecret:oAuth2ClientSecret trustedAppPaths:[NSArray arrayWithObject:[ExePath exePath]] error:error]) {
return NO;
}
}
return YES;
}
- (BOOL)deleteTarget:(NSArray *)args error:(NSError **)error {
if ([args count] != 3) {
SETNSERROR([self errorDomain], ERROR_USAGE, @"invalid arguments");
return NO;
}
Target *target = [[TargetFactory sharedTargetFactory] targetWithNickname:[args objectAtIndex:2]];
if (target == nil) {
SETNSERROR([self errorDomain], ERROR_NOT_FOUND, @"target not found");
return NO;
}
TargetConnection *conn = [[target newConnection:error] autorelease];
if (conn == nil) {
return NO;
}
if (![conn clearAllCachedData:error]) {
return NO;
}
return [[TargetFactory sharedTargetFactory] deleteTarget:target error:error];
}
- (BOOL)listComputers:(NSArray *)args error:(NSError **)error {
if ([args count] != 3) {
SETNSERROR([self errorDomain], ERROR_USAGE, @"invalid arguments");
return NO;
}
Target *target = [[TargetFactory sharedTargetFactory] targetWithNickname:[args objectAtIndex:2]];
if (target == nil) {
SETNSERROR([self errorDomain], ERROR_NOT_FOUND, @"target not found");
return NO;
}
NSArray *expandedTargetList = [self expandedTargetListForTarget:target error:error];
if (expandedTargetList == nil) {
return NO;
}
for (Target *theTarget in expandedTargetList) {
NSError *myError = nil;
HSLogDebug(@"getting backup sets for %@", theTarget);
NSArray *backupSets = [BackupSet allBackupSetsForTarget:theTarget targetConnectionDelegate:nil activityListener:nil error:&myError];
if (backupSets == nil) {
if ([myError isErrorWithDomain:[S3Service errorDomain] code:S3SERVICE_ERROR_AMAZON_ERROR] && [[[myError userInfo] objectForKey:@"HTTPStatusCode"] intValue] == 403) {
HSLogError(@"access denied getting backup sets for %@", theTarget);
} else {
HSLogError(@"error getting backup sets for %@: %@", theTarget, myError);
SETERRORFROMMYERROR;
return NO;
}
} else {
printf("target: %s\n", [[theTarget endpointDisplayName] UTF8String]);
for (BackupSet *backupSet in backupSets) {
printf("\tcomputer %s\n", [[backupSet computerUUID] UTF8String]);
printf("\t\t%s (%s)\n", [[[backupSet userAndComputer] computerName] UTF8String], [[[backupSet userAndComputer] userName] UTF8String]);
}
}
}
return YES;
}
- (BOOL)listFolders:(NSArray *)args error:(NSError **)error {
if ([args count] != 4) {
SETNSERROR([self errorDomain], ERROR_USAGE, @"invalid arguments");
return NO;
}
Target *target = [[TargetFactory sharedTargetFactory] targetWithNickname:[args objectAtIndex:2]];
if (target == nil) {
SETNSERROR([self errorDomain], ERROR_NOT_FOUND, @"target not found");
return NO;
}
NSString *theComputerUUID = [args objectAtIndex:3];
NSString *theEncryptionPassword = [self readPasswordWithPrompt:@"enter encryption password:" error:error];
if (theEncryptionPassword == nil) {
return NO;
}
BackupSet *backupSet = [self backupSetForTarget:target computerUUID:theComputerUUID error:error];
if (backupSet == nil) {
return NO;
}
// Reset Target:
target = [backupSet target];
NSArray *buckets = [Bucket bucketsWithTarget:target computerUUID:theComputerUUID encryptionPassword:theEncryptionPassword targetConnectionDelegate:nil error:error];
if (buckets == nil) {
return NO;
}
printf("target %s\n", [[target endpointDisplayName] UTF8String]);
printf("computer %s\n", [theComputerUUID UTF8String]);
for (Bucket *bucket in buckets) {
printf("\tfolder %s\n", [[bucket localPath] UTF8String]);
printf("\t\tuuid %s\n", [[bucket bucketUUID] UTF8String]);
}
return YES;
}
- (BOOL)printPlist:(NSArray *)args error:(NSError **)error {
if ([args count] != 5) {
SETNSERROR([self errorDomain], ERROR_USAGE, @"invalid arguments");
return NO;
}
Target *target = [[TargetFactory sharedTargetFactory] targetWithNickname:[args objectAtIndex:2]];
if (target == nil) {
SETNSERROR([self errorDomain], ERROR_NOT_FOUND, @"target not found");
return NO;
}
NSString *theComputerUUID = [args objectAtIndex:3];
NSString *theBucketUUID = [args objectAtIndex:4];
NSString *theEncryptionPassword = [self readPasswordWithPrompt:@"enter encryption password:" error:error];
if (theEncryptionPassword == nil) {
return NO;
}
BackupSet *backupSet = [self backupSetForTarget:target computerUUID:theComputerUUID error:error];
if (backupSet == nil) {
return NO;
}
// Reset Target:
target = [backupSet target];
NSArray *buckets = [Bucket bucketsWithTarget:target computerUUID:theComputerUUID encryptionPassword:theEncryptionPassword targetConnectionDelegate:nil error:error];
if (buckets == nil) {
return NO;
}
Bucket *matchingBucket = nil;
for (Bucket *bucket in buckets) {
if ([[bucket bucketUUID] isEqualToString:theBucketUUID]) {
matchingBucket = bucket;
break;
}
}
if (matchingBucket == nil) {
SETNSERROR([self errorDomain], ERROR_NOT_FOUND, @"folder %@ not found", theBucketUUID);
return NO;
}
printf("target %s\n", [[target endpointDisplayName] UTF8String]);
printf("computer %s\n", [theComputerUUID UTF8String]);
printf("folder %s\n", [theBucketUUID UTF8String]);
NSData *xmlData = [matchingBucket toXMLData];
NSString *xmlString = [[[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding] autorelease];
printf("%s\n", [xmlString UTF8String]);
return YES;
}
- (BOOL)listTree:(NSArray *)args error:(NSError **)error {
if ([args count] != 5) {
SETNSERROR([self errorDomain], ERROR_USAGE, @"invalid arguments");
return NO;
}
Target *target = [[TargetFactory sharedTargetFactory] targetWithNickname:[args objectAtIndex:2]];
if (target == nil) {
SETNSERROR([self errorDomain], ERROR_NOT_FOUND, @"target not found");
return NO;
}
NSString *theComputerUUID = [args objectAtIndex:3];
NSString *theBucketUUID = [args objectAtIndex:4];
NSString *theEncryptionPassword = [self readPasswordWithPrompt:@"enter encryption password:" error:error];
if (theEncryptionPassword == nil) {
return NO;
}
BackupSet *backupSet = [self backupSetForTarget:target computerUUID:theComputerUUID error:error];
if (backupSet == nil) {
return NO;
}
// Reset Target:
target = [backupSet target];
NSArray *buckets = [Bucket bucketsWithTarget:target computerUUID:theComputerUUID encryptionPassword:theEncryptionPassword targetConnectionDelegate:nil error:error];
if (buckets == nil) {
return NO;
}
Bucket *matchingBucket = nil;
for (Bucket *bucket in buckets) {
if ([[bucket bucketUUID] isEqualToString:theBucketUUID]) {
matchingBucket = bucket;
break;
}
}
if (matchingBucket == nil) {
SETNSERROR([self errorDomain], ERROR_NOT_FOUND, @"folder %@ not found", theBucketUUID);
return NO;
}
printf("target %s\n", [[target endpointDisplayName] UTF8String]);
printf("computer %s\n", [theComputerUUID UTF8String]);
printf("folder %s\n", [theBucketUUID UTF8String]);
Repo *repo = [[[Repo alloc] initWithBucket:matchingBucket encryptionPassword:theEncryptionPassword targetConnectionDelegate:nil repoDelegate:nil activityListener:nil error:error] autorelease];
if (repo == nil) {
return NO;
}
BlobKey *headBlobKey = [repo headBlobKey:error];
if (headBlobKey == nil) {
return NO;
}
Commit *head = [repo commitForBlobKey:headBlobKey error:error];
if (head == nil) {
return NO;
}
Tree *rootTree = [repo treeForBlobKey:[head treeBlobKey] error:error];
if (rootTree == nil) {
return NO;
}
return [self printTree:rootTree repo:repo relativePath:@"" error:error];
}
- (BOOL)printTree:(Tree *)theTree repo:(Repo *)theRepo relativePath:(NSString *)theRelativePath error:(NSError **)error {
for (NSString *childName in [theTree childNodeNames]) {
NSString *childRelativePath = [theRelativePath stringByAppendingFormat:@"/%@", childName];
Node *childNode = [theTree childNodeWithName:childName];
if ([childNode isTree]) {
printf("%s:\n", [childRelativePath UTF8String]);
Tree *childTree = [theRepo treeForBlobKey:[childNode treeBlobKey] error:error];
if (childTree == nil) {
return NO;
}
if (![self printTree:childTree
repo:theRepo
relativePath:childRelativePath
error:error]) {
return NO;
}
} else {
printf("%s\n", [childRelativePath UTF8String]);
}
}
return YES;
}
- (BOOL)restore:(NSArray *)args error:(NSError **)error {
if ([args count] != 5 && [args count] != 6) {
SETNSERROR([self errorDomain], ERROR_USAGE, @"invalid arguments");
return NO;
}
Target *target = [[TargetFactory sharedTargetFactory] targetWithNickname:[args objectAtIndex:2]];
if (target == nil) {
SETNSERROR([self errorDomain], ERROR_NOT_FOUND, @"target not found");
return NO;
}
NSString *theComputerUUID = [args objectAtIndex:3];
NSString *theBucketUUID = [args objectAtIndex:4];
NSString *theEncryptionPassword = [self readPasswordWithPrompt:@"enter encryption password:" error:error];
if (theEncryptionPassword == nil) {
return NO;
}
BackupSet *backupSet = [self backupSetForTarget:target computerUUID:theComputerUUID error:error];
if (backupSet == nil) {
return NO;
}
// Reset Target:
target = [backupSet target];
NSArray *buckets = [Bucket bucketsWithTarget:target computerUUID:theComputerUUID encryptionPassword:theEncryptionPassword targetConnectionDelegate:nil error:error];
if (buckets == nil) {
return NO;
}
Bucket *matchingBucket = nil;
for (Bucket *bucket in buckets) {
if ([[bucket bucketUUID] isEqualToString:theBucketUUID]) {
matchingBucket = bucket;
break;
}
}
if (matchingBucket == nil) {
SETNSERROR([self errorDomain], ERROR_NOT_FOUND, @"folder %@ not found", theBucketUUID);
return NO;
}
printf("target %s\n", [[target endpointDisplayName] UTF8String]);
printf("computer %s\n", [theComputerUUID UTF8String]);
printf("folder %s\n", [theBucketUUID UTF8String]);
Repo *repo = [[[Repo alloc] initWithBucket:matchingBucket encryptionPassword:theEncryptionPassword targetConnectionDelegate:nil repoDelegate:nil activityListener:nil error:error] autorelease];
if (repo == nil) {
return NO;
}
BlobKey *commitBlobKey = [repo headBlobKey:error];
if (commitBlobKey == nil) {
return NO;
}
Commit *commit = [repo commitForBlobKey:commitBlobKey error:error];
if (commit == nil) {
return NO;
}
BlobKey *treeBlobKey = [commit treeBlobKey];
NSString *nodeName = nil;
if ([args count] == 6) {
NSString *path = [args objectAtIndex:5];
if ([path hasPrefix:@"/"]) {
path = [path substringFromIndex:1];
}
NSArray *pathComponents = [path pathComponents];
for (NSUInteger index = 0; index < [pathComponents count]; index++) {
NSString *component = [pathComponents objectAtIndex:index];
Tree *childTree = [repo treeForBlobKey:treeBlobKey error:error];
if (childTree == nil) {
return NO;
}
Node *childNode = [childTree childNodeWithName:component];
if (childNode == nil) {
SETNSERROR([self errorDomain], ERROR_NOT_FOUND, @"path component '%@' not found", component);
return NO;
}
if (![childNode isTree] && index < ([pathComponents count] - 1)) {
// If it's a directory and we're not at the end of the path, fail.
SETNSERROR([self errorDomain], -1, @"'%@' is not a directory", component);
return NO;
}
if ([childNode isTree]) {
treeBlobKey = [childNode treeBlobKey];
} else {
nodeName = component;
}
}
} else {
Tree *rootTree = [repo treeForBlobKey:[commit treeBlobKey] error:error];
if (rootTree == nil) {
return NO;
}
if ([[rootTree childNodeNames] isEqualToArray:[NSArray arrayWithObject:@"."]]) {
// Single-file case.
nodeName = [[commit location] lastPathComponent];
}
}
NSString *restoreFileName = [args count] == 6 ? [[args objectAtIndex:5] lastPathComponent] : [[matchingBucket localPath] lastPathComponent];
NSString *destinationPath = [[[NSFileManager defaultManager] currentDirectoryPath] stringByAppendingPathComponent:restoreFileName];
if ([[NSFileManager defaultManager] fileExistsAtPath:destinationPath]) {
SETNSERROR([self errorDomain], -1, @"%@ already exists", destinationPath);
return NO;
}
printf("\nrestoring folder %s to %s\n\n", [[matchingBucket localPath] UTF8String], [destinationPath UTF8String]);
int bytesPerSecond = 500000;
AWSRegion *region = [AWSRegion regionWithS3Endpoint:[target endpoint]];
BOOL isGlacierDestination = [region supportsGlacier];
if ([matchingBucket storageType] == StorageTypeGlacier && isGlacierDestination) {
GlacierRestorerParamSet *paramSet = [[[GlacierRestorerParamSet alloc] initWithBucket:matchingBucket
encryptionPassword:theEncryptionPassword
downloadBytesPerSecond:bytesPerSecond
glacierRetrievalTier:GLACIER_RETRIEVAL_TIER_EXPEDITED
commitBlobKey:commitBlobKey
rootItemName:[[matchingBucket localPath] lastPathComponent]
treeVersion:CURRENT_TREE_VERSION
treeBlobKey:treeBlobKey
nodeName:nodeName
targetUID:getuid()
targetGID:getgid()
useTargetUIDAndGID:YES
destinationPath:destinationPath
logLevel:[[HSLog sharedHSLog] hsLogLevel]] autorelease];
[[[GlacierRestorer alloc] initWithGlacierRestorerParamSet:paramSet delegate:self] autorelease];
} else if ([matchingBucket storageType] == StorageTypeS3Glacier && isGlacierDestination) {
S3GlacierRestorerParamSet *paramSet = [[[S3GlacierRestorerParamSet alloc] initWithBucket:matchingBucket
encryptionPassword:theEncryptionPassword
downloadBytesPerSecond:bytesPerSecond
glacierRetrievalTier:GLACIER_RETRIEVAL_TIER_EXPEDITED
commitBlobKey:commitBlobKey
rootItemName:[[matchingBucket localPath] lastPathComponent]
treeVersion:CURRENT_TREE_VERSION
treeBlobKey:treeBlobKey
nodeName:nodeName
targetUID:getuid()
targetGID:getgid()
useTargetUIDAndGID:YES
destinationPath:destinationPath
logLevel:[[HSLog sharedHSLog] hsLogLevel]] autorelease];
S3GlacierRestorer *restorer = [[[S3GlacierRestorer alloc] initWithS3GlacierRestorerParamSet:paramSet delegate:self] autorelease];
[restorer run];
} else {
StandardRestorerParamSet *paramSet = [[[StandardRestorerParamSet alloc] initWithBucket:matchingBucket
encryptionPassword:theEncryptionPassword
commitBlobKey:commitBlobKey
rootItemName:[[matchingBucket localPath] lastPathComponent]
treeVersion:CURRENT_TREE_VERSION
treeBlobKey:treeBlobKey
nodeName:nodeName
targetUID:getuid()
targetGID:getgid()
useTargetUIDAndGID:YES
destinationPath:destinationPath
logLevel:[[HSLog sharedHSLog] hsLogLevel]] autorelease];
[[[StandardRestorer alloc] initWithParamSet:paramSet delegate:self] autorelease];
}
return YES;
}
- (BOOL)clearCache:(NSArray *)args error:(NSError **)error {
if ([args count] != 3) {
SETNSERROR([self errorDomain], ERROR_USAGE, @"invalid arguments");
return NO;
}
Target *target = [[TargetFactory sharedTargetFactory] targetWithNickname:[args objectAtIndex:2]];
if (target == nil) {
SETNSERROR([self errorDomain], ERROR_NOT_FOUND, @"target not found");
return NO;
}
TargetConnection *conn = [[target newConnection:error] autorelease];
if (conn == nil) {
return NO;
}
return [conn clearAllCachedData:error];
}
- (BackupSet *)backupSetForTarget:(Target *)theInitialTarget computerUUID:(NSString *)theComputerUUID error:(NSError **)error {
NSArray *expandedTargetList = [self expandedTargetListForTarget:theInitialTarget error:error];
if (expandedTargetList == nil) {
return nil;
}
for (Target *theTarget in expandedTargetList) {
NSError *myError = nil;
NSArray *backupSets = [BackupSet allBackupSetsForTarget:theTarget targetConnectionDelegate:nil activityListener:nil error:&myError];
if (backupSets == nil) {
if ([myError isErrorWithDomain:[S3Service errorDomain] code:S3SERVICE_ERROR_AMAZON_ERROR] && [[[myError userInfo] objectForKey:@"HTTPStatusCode"] intValue] == 403) {
HSLogError(@"access denied getting backup sets for %@", theTarget);
} else {
HSLogError(@"error getting backup sets for %@: %@", theTarget, myError);
SETERRORFROMMYERROR;
return nil;
}
} else {
for (BackupSet *backupSet in backupSets) {
if ([[backupSet computerUUID] isEqualToString:theComputerUUID]) {
return backupSet;
}
}
}
}
SETNSERROR([self errorDomain], ERROR_NOT_FOUND, @"backup set %@ not found at target\n", theComputerUUID);
return nil;
}
- (NSArray *)expandedTargetListForTarget:(Target *)theTarget error:(NSError **)error {
NSArray *targets = nil;
if ([theTarget targetType] == kTargetAWS) {
targets = [self expandedTargetsForS3Target:theTarget error:error];
} else {
targets = [NSArray arrayWithObject:theTarget];
}
return targets;
}
- (NSArray *)expandedTargetsForS3Target:(Target *)theTarget error:(NSError **)error {
NSString *theSecretKey = [theTarget secret:error];
if (theSecretKey == nil) {
return nil;
}
S3Service *s3 = nil;
if ([AWSRegion regionWithS3Endpoint:[theTarget endpoint]] != nil) {
// It's S3. Get bucket name list from us-east-1 region.
NSURL *usEast1Endpoint = [[AWSRegion usEast1] s3EndpointWithSSL:YES];
id <S3AuthorizationProvider> sap = [[S3AuthorizationProviderFactory sharedS3AuthorizationProviderFactory] providerForEndpoint:usEast1Endpoint
accessKey:[[theTarget endpoint] user]
secretKey:theSecretKey
signatureVersion:4
awsRegion:[AWSRegion usEast1]];
s3 = [[[S3Service alloc] initWithS3AuthorizationProvider:sap endpoint:usEast1Endpoint] autorelease];
} else {
s3 = [theTarget s3:error];
if (s3 == nil) {
return nil;
}
}
NSArray *s3BucketNames = [s3 s3BucketNamesWithTargetConnectionDelegate:nil error:error];
if (s3BucketNames == nil) {
return nil;
}
HSLogDebug(@"s3BucketNames for %@: %@", theTarget, s3BucketNames);
NSURL *originalEndpoint = [theTarget endpoint];
NSMutableArray *ret = [NSMutableArray array];
// WARNING: This is a hack! We're creating this Target using the same UUID so that the keychain lookups work!
NSString *targetUUID = [theTarget targetUUID];
for (NSString *s3BucketName in s3BucketNames) {
NSURL *endpoint = nil;
if ([theTarget targetType] == kTargetAWS) {
NSError *myError = nil;
NSString *location = [s3 locationOfS3Bucket:s3BucketName targetConnectionDelegate:nil error:&myError];
if (location == nil) {
HSLogError(@"failed to get location of %@: %@", s3BucketName, myError);
} else {
AWSRegion *awsRegion = [AWSRegion regionWithLocation:location];
HSLogDebug(@"awsRegion for s3BucketName %@: %@", s3BucketName, location);
NSURL *s3Endpoint = [awsRegion s3EndpointWithSSL:YES];
HSLogDebug(@"s3Endpoint: %@", s3Endpoint);
endpoint = [NSURL URLWithString:[NSString stringWithFormat:@"https://%@@%@/%@", [originalEndpoint user], [s3Endpoint host], s3BucketName]];
}
} else {
NSNumber *originalPort = [originalEndpoint port];
NSString *portString = (originalPort == nil) ? @"" : [NSString stringWithFormat:@":%d", [originalPort intValue]];
endpoint = [NSURL URLWithString:[NSString stringWithFormat:@"%@://%@@%@%@/%@", [originalEndpoint scheme], [originalEndpoint user], [originalEndpoint host], portString, s3BucketName]];
}
if (endpoint != nil) {
HSLogDebug(@"endpoint: %@", endpoint);
Target *target = [[[Target alloc] initWithUUID:targetUUID
nickname:s3BucketName
endpoint:endpoint
awsRequestSignatureVersion:[theTarget awsRequestSignatureVersion]] autorelease];
[ret addObject:target];
}
}
return ret;
}
#pragma mark StandardRestorerDelegate
// Methods return YES if cancel is requested.
- (BOOL)standardRestorerMessageDidChange:(NSString *)message {
printf("status: %s\n", [message UTF8String]);
return NO;
}
- (BOOL)standardRestorerFileBytesRestoredDidChange:(NSNumber *)theTransferred {
return NO;
}
- (BOOL)standardRestorerTotalFileBytesToRestoreDidChange:(NSNumber *)theTotal {
return NO;
}
- (BOOL)standardRestorerErrorMessage:(NSString *)theErrorMessage didOccurForPath:(NSString *)thePath {
printf("%s error: %s\n", [thePath UTF8String], [theErrorMessage UTF8String]);
return NO;
}
- (BOOL)standardRestorerDidSucceed {
return NO;
}
- (BOOL)standardRestorerDidFail:(NSError *)error {
printf("failed: %s\n", [[error localizedDescription] UTF8String]);
return NO;
}
#pragma mark S3GlacierRestorerDelegate
- (BOOL)s3GlacierRestorerMessageDidChange:(NSString *)message {
printf("status: %s\n", [message UTF8String]);
return NO;
}
- (BOOL)s3GlacierRestorerBytesRequestedDidChange:(NSNumber *)theRequested {
printf("requested %qu of %qu\n", [theRequested unsignedLongLongValue], maxRequested);
return NO;
}
- (BOOL)s3GlacierRestorerTotalBytesToRequestDidChange:(NSNumber *)theMaxRequested {
maxRequested = [theMaxRequested unsignedLongLongValue];
return NO;
}
- (BOOL)s3GlacierRestorerDidFinishRequesting {
return NO;
}
- (BOOL)s3GlacierRestorerBytesTransferredDidChange:(NSNumber *)theTransferred {
printf("restored %qu of %qu\n", [theTransferred unsignedLongLongValue], maxTransfer);
return NO;
}
- (BOOL)s3GlacierRestorerTotalBytesToTransferDidChange:(NSNumber *)theTotal {
maxTransfer = [theTotal unsignedLongLongValue];
return NO;
}
- (BOOL)s3GlacierRestorerErrorMessage:(NSString *)theErrorMessage didOccurForPath:(NSString *)thePath {
printf("%s error: %s\n", [thePath UTF8String], [theErrorMessage UTF8String]);
return NO;
}
- (void)s3GlacierRestorerDidSucceed {
printf("restore finished.\n");
}
- (void)s3GlacierRestorerDidFail:(NSError *)error {
printf("failed: %s\n", [[error localizedDescription] UTF8String]);
}
#pragma mark GlacierRestorerDelegate
- (BOOL)glacierRestorerMessageDidChange:(NSString *)message {
printf("status: %s\n", [message UTF8String]);
return NO;
}
- (BOOL)glacierRestorerBytesRequestedDidChange:(NSNumber *)theRequested {
return NO;
}
- (BOOL)glacierRestorerTotalBytesToRequestDidChange:(NSNumber *)theMaxRequested {
return NO;
}
- (BOOL)glacierRestorerDidFinishRequesting {
return NO;
}
- (BOOL)glacierRestorerBytesTransferredDidChange:(NSNumber *)theTransferred {
return NO;
}
- (BOOL)glacierRestorerTotalBytesToTransferDidChange:(NSNumber *)theTotal {
return NO;
}
- (BOOL)glacierRestorerErrorMessage:(NSString *)theErrorMessage didOccurForPath:(NSString *)thePath {
printf("%s error: %s\n", [thePath UTF8String], [theErrorMessage UTF8String]);
return NO;
}
- (BOOL)glacierRestorerDidSucceed {
return NO;
}
- (BOOL)glacierRestorerDidFail:(NSError *)error {
printf("failed: %s\n", [[error localizedDescription] UTF8String]);
return NO;
}
#pragma mark internal
- (NSString *)readPasswordWithPrompt:(NSString *)thePrompt error:(NSError **)error {
fprintf(stderr, "%s ", [thePrompt UTF8String]);
fflush(stderr);
struct termios oldTermios;
struct termios newTermios;
if (tcgetattr(STDIN_FILENO, &oldTermios) != 0) {
int errnum = errno;
HSLogError(@"tcgetattr error %d: %s", errnum, strerror(errnum));
SETNSERROR(@"UnixErrorDomain", errnum, @"%s", strerror(errnum));
return nil;
}
newTermios = oldTermios;
newTermios.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newTermios);
size_t bufsize = BUFSIZE;
char *buf = malloc(bufsize);
ssize_t len = getline(&buf, &bufsize, stdin);
free(buf);
tcsetattr(STDIN_FILENO, TCSANOW, &oldTermios);
if (len > 0 && buf[len - 1] == '\n') {
--len;
}
return [[[NSString alloc] initWithBytes:buf length:len encoding:NSUTF8StringEncoding] autorelease];
}
@end