Skip to content

Commit

Permalink
added an option to specify a path within the folder to restore
Browse files Browse the repository at this point in the history
  • Loading branch information
sreitshamer committed Feb 4, 2017
1 parent 3bd7f24 commit cc41a65
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
52 changes: 43 additions & 9 deletions ArqRestoreCommand.m
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ - (BOOL)printTree:(Tree *)theTree repo:(Repo *)theRepo relativePath:(NSString *)
}

- (BOOL)restore:(NSArray *)args error:(NSError **)error {
if ([args count] != 5) {
if ([args count] != 5 && [args count] != 6) {
SETNSERROR([self errorDomain], ERROR_USAGE, @"invalid arguments");
return NO;
}
Expand Down Expand Up @@ -489,8 +489,41 @@ - (BOOL)restore:(NSArray *)args error:(NSError **)error {
if (commit == nil) {
return NO;
}

NSString *destinationPath = [[[NSFileManager defaultManager] currentDirectoryPath] stringByAppendingPathComponent:[[matchingBucket localPath] lastPathComponent]];

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;
}
}
}

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;
Expand All @@ -511,8 +544,9 @@ - (BOOL)restore:(NSArray *)args error:(NSError **)error {
commitBlobKey:commitBlobKey
rootItemName:[[matchingBucket localPath] lastPathComponent]
treeVersion:CURRENT_TREE_VERSION
treeBlobKey:[commit treeBlobKey]
nodeName:nil targetUID:getuid()
treeBlobKey:treeBlobKey
nodeName:nodeName
targetUID:getuid()
targetGID:getgid()
useTargetUIDAndGID:YES
destinationPath:destinationPath
Expand All @@ -527,8 +561,8 @@ - (BOOL)restore:(NSArray *)args error:(NSError **)error {
commitBlobKey:commitBlobKey
rootItemName:[[matchingBucket localPath] lastPathComponent]
treeVersion:CURRENT_TREE_VERSION
treeBlobKey:[commit treeBlobKey]
nodeName:nil
treeBlobKey:treeBlobKey
nodeName:nodeName
targetUID:getuid()
targetGID:getgid()
useTargetUIDAndGID:YES
Expand All @@ -542,8 +576,8 @@ - (BOOL)restore:(NSArray *)args error:(NSError **)error {
commitBlobKey:commitBlobKey
rootItemName:[[matchingBucket localPath] lastPathComponent]
treeVersion:CURRENT_TREE_VERSION
treeBlobKey:[commit treeBlobKey]
nodeName:nil
treeBlobKey:treeBlobKey
nodeName:nodeName
targetUID:getuid()
targetGID:getgid()
useTargetUIDAndGID:YES
Expand Down
2 changes: 1 addition & 1 deletion arq_restore.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static void printUsage(const char *exeName) {
fprintf(stderr, "\t%s [-l loglevel] listfolders <target_nickname> <computer_uuid>\n", exeName);
fprintf(stderr, "\t%s [-l loglevel] printplist <target_nickname> <computer_uuid> <folder_uuid>\n", exeName);
fprintf(stderr, "\t%s [-l loglevel] listtree <target_nickname> <computer_uuid> <folder_uuid>\n", exeName);
fprintf(stderr, "\t%s [-l loglevel] restore <target_nickname> <computer_uuid> <folder_uuid>\n", exeName);
fprintf(stderr, "\t%s [-l loglevel] restore <target_nickname> <computer_uuid> <folder_uuid> [relative_path]\n", exeName);
fprintf(stderr, "\t%s [-l loglevel] clearcache <target_nickname>\n", exeName);
fprintf(stderr, "\n");
fprintf(stderr, "log levels: none, error, warn, info, and debug\n");
Expand Down

0 comments on commit cc41a65

Please sign in to comment.