RSYNC 目录与文件 [英] RSYNC Directories vs Files

查看:58
本文介绍了RSYNC 目录与文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

如果您在命令行上调用 rsync,则可以使用一个开关来获得您所描述的效果:-a"选项:它是其他几个选项的简写,这里最相关的一个是 rsync 从源目录递归的指令.

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.

这将递归地将目录foo"及其所有内容复制到目录bar"中.如果bar"不存在,rsync 将创建它,然后将foo"复制到其中.

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

... 将导致 bar/foo/everything

... 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

...你会得到/bar/everything,但在接收端没有名为foo"的目录.

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

您会说将目录 foo 的内容复制到目录 bar...但不是封闭目录 foo."

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

抱歉,这不是更具体的 Objective-C,但希望这能让您使用 rsync.

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

这篇关于RSYNC 目录与文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆