1

我正在尝试为它编写一个小 RSync 程序,并且我设法让它与下面的代码一起工作。唯一的问题是它只同步单个文件而不是目录。如果您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];

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

4

1 回答 1

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。”

抱歉,这不是更具体的 Objective-C,但希望这会让你继续使用 rsync。

于 2012-05-28T00:27:35.327 回答