RSYNC Directories vs Files
Asked Answered
P

1

1

I'm trying to put together a little RSync program for the hell of it, and I managed to get it to work with the code below. The only problem is that it only Rsyncs single files and not directories. If you run rsync through the terminal command it can copy entire directories into other directories. Anyone know a fix?

 NSString *source = self.source.stringValue;
NSString *destination = self.destination.stringValue;

NSLog(@"The source is %@. The destination is %@.", source, destination);

NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath:@"/usr/bin/rsync"];

NSArray *arguments;
arguments = [NSArray arrayWithObjects: source, destination, nil];
[task setArguments: arguments];

NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];

NSFileHandle *file;
file = [pipe fileHandleForReading];

[task launch];

NSData *data;
data = [file readDataToEndOfFile];

I have two text fields for the source and destination which I take the string value and set those equal to the source and destination.

Papacy answered 27/5, 2012 at 15:2 Comment(0)
C
3

If you were calling rsync on the commandline, there's a switch that you would use to get the effect you're describing: the "-a" option: it's a shorthand for several other options, the most relevant one here being an instruction for rsync to recurse down from the source directory.

This would recursively copy the directory "foo" and all its contents into the directory "bar." If "bar" didn't exist, rsync would create it and then copy "foo" into it.

rsync -a foo bar

... would result in bar/foo/everything

The other teeny (but important!) detail to be aware of is whether or not you put a trailing slash on your source directory. If you instead said:

rsync -a foo/ bar

... you'd wind up with /bar/everything, but no directory called "foo" on the receiving side.

You'd be saying "copy the contents of the directory foo into the directory bar... but not the enclosing directory, foo."

Sorry this isn't more objective-C specific, but hopefully that'll get you going with rsync.

Cynic answered 28/5, 2012 at 0:27 Comment(1)
Perfect, this really helped actually. I don't know that much about the command line. I ended up having to just put in the "-a" as one of the arguments rather than in the string I sending the command line for some reason.Papacy

© 2022 - 2024 — McMap. All rights reserved.