在 rsync NSTask 方法中包装排除选项
我正在尝试将 rsync 包装在 NSTask 中,并使用排除选项来不同步隐藏文件(点文件)。我知道这在命令行上有效:
rsync -az --exclude='.*' source destination
我的 NSTask 定义如下:
NSTask *rsyncTask;
rsyncTask = [[NSTask alloc] init];
[rsyncTask setLaunchPath: @"/usr/bin/rsync"];
NSArray *arguments;
arguments = [NSArray arrayWithObjects: @"-az", @"--exclude='.*'", source, destination, nil];
[rsyncTask setArguments: arguments];
没有排除参数,一切工作正常。事实上,上述定义可以正常工作,但隐藏文件仍然会被复制。
我尝试过:
- 使用反斜杠转义单刻度
- 使用转义双引号
- 使用两个反斜杠转义转义反斜杠
- 不使用 --exclude= 而只是 --exclude 并使用单独的数组元素 @"'.*'"
似乎什么也没有得到我想要的结果。
欢迎任何建议。
I am trying to wrap rsync in NSTask and use the exclude option to not sync hidden files (dot files). I know this works at the command line:
rsync -az --exclude='.*' source destination
My NSTask is defined as follows:
NSTask *rsyncTask;
rsyncTask = [[NSTask alloc] init];
[rsyncTask setLaunchPath: @"/usr/bin/rsync"];
NSArray *arguments;
arguments = [NSArray arrayWithObjects: @"-az", @"--exclude='.*'", source, destination, nil];
[rsyncTask setArguments: arguments];
Without the exclude argument things work fine. In fact things work fine with the above definition but hidden files are still copied.
I have tried:
- escaping the single ticks with a backslash
- using escaped double quotes
- using two backslashes to escape the escaping backslash
- not using --exclude= but just --exclude with a separate array element that is @"'.*'"
Nothing seems to get the results I want.
Any suggestions welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于任何遇到此问题寻找如何排除多个文件的人,事实证明您需要为每个要排除的文件/目录使用单独的
--exclude
。在命令行上,--exclude={file1,dir1/dir2,file2,dir1/dir\ with\ space}
模式可以工作,但该格式与 NSTask 配合不佳。对于 NSTask (swift),这将是:另请注意,NSTask 不需要转义空格。来自 docs, "NSTask 对象将路径和参数中的字符串转换为适当的 C 风格字符串(使用 fileSystemRepresentation),然后通过 argv[] 将它们传递给任务。参数中的字符串不会经过 shell 扩展,因此不需要进行特殊引用,并且 shell 变量(例如 $PWD)不会被解析。”
For anyone who comes across this looking for how to exclude multiple files, it turns out you need a separate
--exclude
for each file/directory you wish to exclude. On the command line the--exclude={file1,dir1/dir2,file2,dir1/dir\ with\ spaces}
pattern works, but that format does not play nice with NSTask. For NSTask (swift) this would be:Also note that NSTask does not require spaces to be escaped. From the docs, "The NSTask object converts both path and the strings in arguments to appropriate C-style strings (using fileSystemRepresentation) before passing them to the task via argv[]) . The strings in arguments do not undergo shell expansion, so you do not need to do special quoting, and shell variables, such as $PWD, are not resolved."
尝试仅使用
--exclude
和单独的参数@".*"
(不带单引号)。由于您直接将参数传递给任务,因此您不需要像在命令行中那样引用或转义内容。这是因为在命令行中,shell 正在解析您键入的内容,但在本例中没有 shell。
Try using just
--exclude
with a separate argument that is@".*"
(without single quotes).Since you are passing arguments directly to the task, you don't need to quote or escape things like you would at a command line. That's because at the command line, the shell is parsing what you type, but in this case there is no shell.