RSYNC目录与文件

时间:2012-05-27 15:02:28

标签: objective-c rsync nstask

我正在尝试为它制作一个小的RSync程序,我设法让它使用下面的代码。唯一的问题是它只有Rsyncs单个文件而不是目录。如果通过terminal命令运行rsync,它可以将整个目录复制到其他目录中。有人知道修复吗?

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

我有源和目标的两个文本字段,我取字符串值并设置它们等于源和目标。

1 个答案:

答案 0 :(得分:3)

如果您在命令行上调用rsync,则会使用一个开关来获取您正在描述的效果:“ - a”选项:它是其他几个选项的简写,最相关的选项是rsync从源目录中递归的指令。

这将以递归方式将目录“foo”及其所有内容复制到目录“bar”中。如果“bar”不存在,rsync将创建它,然后将“foo”复制到其中。

rsync -a foo bar

...会导致bar / foo / everything

要注意的另一个很小的(但很重要的!)细节是你是否在源目录上放了一个斜杠。如果你改为说:

rsync -a foo/ bar

...你最终会使用/ bar / everything,但接收方没有名为“foo”的目录。

你会说“将目录foo的内容复制到目录栏中......但不包括封闭目录foo。”

对不起,这不是更具针对性的C,但希望能让你选择使用rsync。