2017-04-14 36 views
0

我讀了如何獲得octal file permissions用perl another answer如何使用Ruby獲得八進制文件權限?

$ perl -e 'printf "%04o %s\n", (stat)[2] & 07777, $_ for @ARGV' *.txt 
0644 1.txt 
0644 2.txt 
0644 3.txt 
0644 4.txt 
0600 PerlOneLiner.txt 
0664 perl.txt 

到目前爲止,我已經使用了File::Stat類和#printf方法。不過,我在所有輸出中都領先一百。

$ ruby -e 'Dir["**/**"].each { |f| printf "%04o\t#{f}\n", File.stat(f).mode }' 
100711 cplink 
100644 hello_world.rb 
100755 lso 
100711 rename_images 
  • 什麼是領先的100意思是給我一個MacOS的機器上?
  • 爲什麼我的"%04o"無效?
  • 如何獲得與鏈接的perl腳本相同的輸出?
+0

這只是[File :: Stat#mode'](https://ruby-doc.org/core/File/Stat.html#method-i- mode)方法的返回值,*完全*如文檔中的示例所示。 –

+0

但是爲什麼100呢? – mbigras

+0

因爲這就是'File :: Stat#mode'返回的內容,完全按照文檔。正如文檔所說,「File :: Stat#mode」的返回值是依賴於平臺的,顯然,在您的平臺上,這就是它返回的結果。 –

回答

8

如果(從外殼man 2 stat)檢查您的libc手冊的第二節,你應該看到這樣的事情:

狀態信息字ST_MODE具有以下位:

#define S_IFMT 0170000 /* type of file */ 
#define S_IFIFO 0010000 /* named pipe (fifo) */ 
#define S_IFCHR 0020000 /* character special */ 
#define S_IFDIR 0040000 /* directory */ 
#define S_IFBLK 0060000 /* block special */ 
#define S_IFREG 0100000 /* regular */ 
#define S_IFLNK 0120000 /* symbolic link */ 
#define S_IFSOCK 0140000 /* socket */ 
#define S_IFWHT 0160000 /* whiteout */ 
#define S_ISUID 0004000 /* set user id on execution */ 
#define S_ISGID 0002000 /* set group id on execution */ 
#define S_ISVTX 0001000 /* save swapped text even after use */ 
#define S_IRUSR 0000400 /* read permission, owner */ 
#define S_IWUSR 0000200 /* write permission, owner */ 
#define S_IXUSR 0000100 /* execute/search permission, owner */ 

確切的內容不完全一樣,但任何Unixy系統上的八進制值應該是相同的。

你感興趣的將是部分的「這是一個普通文件」位:

#define S_IFREG 0100000 /* regular */ 

這就是你領先100從何而來。

如果在Perl版本回頭看看,你會看到,他們正在將一個位掩碼:

(stat)[2] & 07777 
      ^^^^^^^ 

搶剛權限位。如果你在Ruby中也這樣做:

printf "%04o\t#{f}\n", (File.stat(f).mode & 07777) 
# ----------------------------------------^^^^^^^ 

你會得到你所期望的那種輸出。


如果你沒有的libc手冊頁,那麼你可以看看OpenGroup's stat documentation這將指向你覆蓋的模式不同位struct stat documentation

┌─────────┬───────────┬───────────────────────────────────────────┐ 
│ Name │ Numeric │    Description     │    
│   │ Value │           │    
├─────────┼───────────┼───────────────────────────────────────────┤ 
│ S_IRWXU │ 0700  │ Read, write, execute/search by owner.  │ 
├─────────┼───────────┼───────────────────────────────────────────┤ 
│ S_IRUSR │ 0400  │ Read permission, owner.     │    
├─────────┼───────────┼───────────────────────────────────────────┤ 
│ S_IWUSR │ 0200  │ Write permission, owner.     │    
├─────────┼───────────┼───────────────────────────────────────────┤ 
│ S_IXUSR │ 0100  │ Execute/search permission, owner.   │ 
├─────────┼───────────┼───────────────────────────────────────────┤ 
│ S_IRWXG │ 070  │ Read, write, execute/search by group.  │ 
├─────────┼───────────┼───────────────────────────────────────────┤ 
│ S_IRGRP │ 040  │ Read permission, group.     │    
├─────────┼───────────┼───────────────────────────────────────────┤ 
│ S_IWGRP │ 020  │ Write permission, group.     │    
├─────────┼───────────┼───────────────────────────────────────────┤ 
│ S_IXGRP │ 010  │ Execute/search permission, group.   │ 
├─────────┼───────────┼───────────────────────────────────────────┤ 
│ S_IRWXO │ 07  │ Read, write, execute/search by others. │ 
├─────────┼───────────┼───────────────────────────────────────────┤ 
│ S_IROTH │ 04  │ Read permission, others.     │    
├─────────┼───────────┼───────────────────────────────────────────┤ 
│ S_IWOTH │ 02  │ Write permission, others.     │    
├─────────┼───────────┼───────────────────────────────────────────┤ 
│ S_IXOTH │ 01  │ Execute/search permission, others.  │ 
├─────────┼───────────┼───────────────────────────────────────────┤ 
│ S_ISUID │ 04000  │ Set-user-ID on execution.     │    
├─────────┼───────────┼───────────────────────────────────────────┤ 
│ S_ISGID │ 02000  │ Set-group-ID on execution.    │ 
├─────────┼───────────┼───────────────────────────────────────────┤ 
│ S_ISVTX │ 01000  │ On directories, restricted deletion flag. │ 
└─────────┴───────────┴───────────────────────────────────────────┘ 
2

是什麼前100名意味着我在macOS機器上?

File::Stat#mode方法的返回值是平臺相關的,很明顯,這就是它在您的平臺上返回的結果。

特別地,文檔說用於Unix機,使用來自stat(2)的定義,這on macOS如下:

狀態信息字ST_MODE具有以下位:

#define S_IFMT 0170000   /* type of file */ 
#define  S_IFIFO 0010000 /* named pipe (fifo) */ 
#define  S_IFCHR 0020000 /* character special */ 
#define  S_IFDIR 0040000 /* directory */ 
#define  S_IFBLK 0060000 /* block special */ 
#define  S_IFREG 0100000 /* regular */ 
#define  S_IFLNK 0120000 /* symbolic link */ 
#define  S_IFSOCK 0140000 /* socket */ 
#define  S_IFWHT 0160000 /* whiteout */ 
#define S_ISUID 0004000 /* set user id on execution */ 
#define S_ISGID 0002000 /* set group id on execution */ 
#define S_ISVTX 0001000 /* save swapped text even after use */ 
#define S_IRUSR 0000400 /* read permission, owner */ 
#define S_IWUSR 0000200 /* write permission, owner */ 
#define S_IXUSR 0000100 /* execute/search permission, owner */ 

這與Single Unix Specification中的說明相匹配,所以它或多或少都適用於全部 Unices,不僅僅是macOS。 (macOS有額外的「whiteout」文件類型,它與Time Machine,AFAIK有關,但沒關係,SUS允許附加的文件類型和權限位。)

因此,如果我正確解碼,那意味着hello_world.rb

  • 一個普通的文件,而不是一個FIFO,字符設備,目錄塊設備,符號連接,插座,或白化
  • 不SUID
  • 不SGID
  • 不粘膩
  • 可讀和可寫的,而不是由它的主人可執行
  • 可讀,但不可寫或組的可執行
  • 可讀,但不可寫或其他可執行

爲什麼我"%04o"無法正常工作?

%04o表示「格式爲八進制,最小長度爲4,如果長度小於4,則填充爲零」。而這正是它所做的。

如何獲得與鏈接的perl腳本相同的輸出?

如果你想獲得相同的輸出,你應該做同樣的事情:Perl腳本屏蔽了從模式的文件類型,如果你在Ruby中做同樣的,你應該得到相同的結果:

Dir["**/**"].each { |f| printf "%04o\t#{f}\n", File.stat(f).mode & 07777 } 
相關問題