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.
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.
来源:https://stackoverflow.com/questions/10774924/rsync-directories-vs-files