2009-10-14 32 views
1

我需要刪除一個應用程序(MyApp.app),該應用程序在其所有包含的文件夾中都具有隻讀權限。在刪除它之前,我應該將所有包含的文件/目錄的權限更改爲0644.如何遞歸執行此操作?Mac OS X中的ruby遞歸chmod?

我已經試過

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

再次感謝。

+2

如果你只是刪除它,它有什麼用途來改變權限? – 2009-10-14 19:04:04

+0

當你說你*不能使用Unix命令*時,我感到困惑。你在OSX上。 Unix命令在那裏工作得非常好。這是功課嗎? (除此之外,Jeff是正確的:沒有理由更改您要刪除的內容的權限。) – Telemachus 2009-10-14 19:09:50

+0

如果MyApp.app對所有包含的文件夾擁有隻讀權限,我無法將其刪除。請參閱編輯以獲得更好的解釋。不,這不是一項功課... – 2009-10-14 21:00:04

回答

6

FileUtils.chmod_R應該這樣做

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

- 編輯 -

[email protected] ~ $ mkdir -p foo/bar/seth 

[email protected] ~ $ ls -ld foo 
drwxr-xr-x 3 seth staff 102 Oct 15 19:24 foo 

[email protected] ~ $ ls -ld foo/bar 
drwxr-xr-x 3 seth staff 102 Oct 15 19:24 foo/bar 

[email protected] ~ $ ls -ld foo/bar/seth 
drwxr-xr-x 2 seth staff 68 Oct 15 19:24 foo/bar/seth 

[email protected] ~ $ cat test.rb 
require 'fileutils' 
begin; FileUtils.chmod_R(0777, 'foo'); rescue; end 

[email protected] ~ $ ruby test.rb 

[email protected] ~ $ ls -ld foo 
drwxrwxrwx 3 seth staff 102 Oct 15 19:24 foo 

[email protected] ~ $ ls -ld foo/bar 
drwxrwxrwx 3 seth staff 102 Oct 15 19:24 foo/bar 

[email protected] ~ $ ls -ld foo/bar/seth 
drwxrwxrwx 2 seth staff 68 Oct 15 19:24 foo/bar/seth 

快速測試似乎工作。

+0

它不會遞歸出於某種原因。 – 2009-10-14 20:43:45

+0

我一定會在我身邊做同樣的測試。會讓你知道。 謝謝 – 2009-10-16 04:53:32

+0

@謝謝,非常感謝,它的工作!出於某種原因,當我嘗試它時,它沒有,但可能我沒有正確使用它。 – 2009-10-16 08:20:19