在 rsync NSTask 方法中包装排除选项

发布于 2024-10-12 08:43:55 字数 672 浏览 4 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝眼睛不忧郁 2024-10-19 08:43:55

对于任何遇到此问题寻找如何排除多个文件的人,事实证明您需要为每个要排除的文件/目录使用单独的 --exclude 。在命令行上, --exclude={file1,dir1/dir2,file2,dir1/dir\ with\ space} 模式可以工作,但该格式与 NSTask 配合不佳。对于 NSTask (swift),这将是:

task.arguments = ["-FLAGS", "--exclude", "file1", "--exclude", "dir1/dir2", "--exclude", "file2", "--exclude", "dir1/dir with spaces", "SRC", "DST"]

另请注意,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:

task.arguments = ["-FLAGS", "--exclude", "file1", "--exclude", "dir1/dir2", "--exclude", "file2", "--exclude", "dir1/dir with spaces", "SRC", "DST"]

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

无畏 2024-10-19 08:43:55

尝试仅使用 --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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文