开发者

Recursive chmod in ruby on mac OS X?

I need to delete an application (MyApp.app), which has read only permissions in all its enclosed folders. Before I delete it, I should change the permissions on all enclosed files/directories to 0644. How do I do this recursively?

I've tried

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

but FileUtils.chmod doesn't work recursively. I cannot use Unix commands - it has to be Ruby.

EDIT: I cannot use Unix commands in the current context. OK, this is a RubyCocoa application and the source you see is a part of ruby script, that is supposed to uninstall the application (please don't comment on that, since that's the code my customer has). Uninstalling includes removing all traces of the application, killing the process and in the end deleting the application itself. Normally it works, but not in the case when for some reason the MyApp.app folder gets read only permission. So i thought to run a chmod recursively on the folder and them remove it, but it's not straight forward in Ruby for some reason. That's why i'm asking for a help. There are plenty examples on how to do it from a command line, but how do you do it from the code?

Here some more from 开发者_StackOverflowthe code, just to show how's it implemented:

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

Thanks again.


FileUtils.chmod_R should do it

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

-- EDIT --

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

A quick test appears to work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜