mac OS X上的ruby中的递归chmod?

时间:2009-10-14 19:00:13

标签: ruby recursion macos chmod

我需要删除一个应用程序(MyApp.app),它在所有附带的文件夹中只具有只读权限。在删除它之前,我应该将所有封闭文件/目录的权限更改为0644.如何递归执行此操作?

我试过

begin; FileUtils.chmod(0644, '#{config.appPath}'); rescue; end
begin; FileUtils.rm_r('#{config.appPath}'); rescue; end

但是FileUtils.chmod递归不起作用。我不能使用Unix命令 - 它必须是Ruby。

编辑:我不能在当前上下文中使用Unix命令。好的,这是一个RubyCocoa应用程序,您看到的源代码是ruby脚本的一部分,应该卸载应用程序(请不要对此进行评论,因为这是我的客户所拥有的代码)。卸载包括删除应用程序的所有跟踪,终止进程并最终删除应用程序本身。通常它可以工作,但在某些情况下MyApp.app文件夹获得只读权限的情况下不会。所以我想在文件夹上递归运行一个chmod并删除它,但由于某些原因,它在Ruby中并不是直截了当的。 这就是我要求帮助的原因。有很多关于如何从命令行执行此操作的示例,但是如何从代码中执行此操作?

这里有一些代码,只是为了展示它是如何实现的:

code =<<FOO
require 'fileutils'
# kill the app before deleting files in case it writes files on exit
%x{/bin/kill -9 #{NSProcessInfo.processInfo.processIdentifier}}
begin; FileUtils.chmod(0644, '#{TBConfig.appPath}'); rescue; end
begin; FileUtils.rm_r('#{TBConfig.appPath}'); rescue; end
FOO
    ff = Tempfile.new('timebridge')
    ff.write(code)
    ff.close
    %x{/usr/bin/ruby #{ff.path}}

再次感谢。

1 个答案:

答案 0 :(得分:6)

FileUtils.chmod_R应该这样做

http://www.ruby-doc.org/core/classes/FileUtils.html#M004351

- 编辑 -

seth@oxygen ~ $ mkdir -p foo/bar/seth

seth@oxygen ~ $ ls -ld foo
drwxr-xr-x  3 seth  staff  102 Oct 15 19:24 foo

seth@oxygen ~ $ ls -ld foo/bar
drwxr-xr-x  3 seth  staff  102 Oct 15 19:24 foo/bar

seth@oxygen ~ $ ls -ld foo/bar/seth
drwxr-xr-x  2 seth  staff  68 Oct 15 19:24 foo/bar/seth

seth@oxygen ~ $ cat test.rb
require 'fileutils'
begin; FileUtils.chmod_R(0777, 'foo'); rescue; end

seth@oxygen ~ $ ruby test.rb

seth@oxygen ~ $ ls -ld foo
drwxrwxrwx  3 seth  staff  102 Oct 15 19:24 foo

seth@oxygen ~ $ ls -ld foo/bar
drwxrwxrwx  3 seth  staff  102 Oct 15 19:24 foo/bar

seth@oxygen ~ $ ls -ld foo/bar/seth
drwxrwxrwx  2 seth  staff  68 Oct 15 19:24 foo/bar/seth

快速测试似乎有效。