2017-04-14 37 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 } 
相关问题