OSX上`stat`输出的每个字段的含义是什么?

时间:2019-01-19 03:27:52

标签: macos file unix stat man

$ stat Cargo.toml
16777220 9094681422 -rw-r--r-- 1 tonytonyjan staff 0 109 "Jan 19 10:05:13 2019" "Dec 31 17:52:29 2018" "Dec 31 17:52:29 2018" "Dec 14 16:32:26 2018" 4096 8 0 Cargo.toml

man stat没有解释,但是提到输出是通过lstat获得的:

  

显示的信息是通过使用给定参数调用lstat(2)并评估返回的结构获得的。

man lstat之后,它给出了一个C结构,看起来像我想要的:

The buf argument is a pointer to a stat structure as defined by <sys/stat.h> and into which information is placed concerning the file.  When the macro
     _DARWIN_FEATURE_64_BIT_INODE is not defined (see below for more information about this macro), the stat structure is defined as:

     struct stat { /* when _DARWIN_FEATURE_64_BIT_INODE is NOT defined */
         dev_t    st_dev;    /* device inode resides on */
         ino_t    st_ino;    /* inode's number */
         mode_t   st_mode;   /* inode protection mode */
         nlink_t  st_nlink;  /* number of hard links to the file */
         uid_t    st_uid;    /* user-id of owner */
         gid_t    st_gid;    /* group-id of owner */
         dev_t    st_rdev;   /* device type, for special file inode */
         struct timespec st_atimespec;  /* time of last access */
         struct timespec st_mtimespec;  /* time of last data modification */
         struct timespec st_ctimespec;  /* time of last file status change */
         off_t    st_size;   /* file size, in bytes */
         quad_t   st_blocks; /* blocks allocated for file */
         u_long   st_blksize;/* optimal file sys I/O ops blocksize */
         u_long   st_flags;  /* user defined flags for file */
         u_long   st_gen;    /* file generation number */
     };

不幸的是,我仍然无法将每个字段映射到stat的输出,例如:

$ stat Cargo.toml
16777220 9094681422 -rw-r--r-- 1 tonytonyjan staff 0 109 "Jan 19 10:05:13 2019" "Dec 31 17:52:29 2018" "Dec 31 17:52:29 2018" "Dec 14 16:32:26 2018" 4096 8 0 Cargo.toml
  • 16777220-设备inode驻留在
  • 9094681422-索引节点
  • -rw-r--r---保护模式
  • 1-硬链接数
  • tonytonyjan-用户
  • 人员-组
  • 0-不确定。是设备类型吗?
  • 109-大小
  • “ 2019年1月19日10:05:13”-最后访问权限
  • “ 2018年12月31日17:52:29”-最后修改
  • “ 2018年12月31日17:52:29”-上次文件状态更改
  • “ 2018年12月14日16:32:26”-应该只有3个时间戳,这是什么?
  • 4096-文件大小(以字节为单位)
  • 8-为文件分配的块
  • 0-最佳文件系统I / O操作块大小?用户定义的标志?或文件代号?
  • Cargo.toml-文件名

我的问题:

  1. 第一个0代表st_rdev吗?
  2. st_devst_rdev有什么区别?
  3. 0秒代表什么?
  4. 很多人我找不到正确的man页(man statman lstat都没有)。是否有任何官方文档详细说明每个stat字段?我在哪里可以找到它?

1 个答案:

答案 0 :(得分:0)

使用stat -s。它将以相同的顺序打印字段,但带有标签(并省略文件名):

:; stat -s /etc/man.conf | fmt
st_dev=16777220 st_ino=641593 st_mode=0100644 st_nlink=1 st_uid=0
st_gid=0 st_rdev=0 st_size=4574 st_atime=1547885737 st_mtime=1500152545
st_ctime=1512806119 st_birthtime=1500152545 st_blksize=4194304
st_blocks=0 st_flags=32

您的第一个神秘字段是st_rdev,即“特殊文件inode的设备类型”。由于我们未指定设备文件,因此该值为零。

您的第二个神秘字段是st_birthtimespec,即“文件创建(出生)的时间”(请参见stat(2)手册页)。这是Darwin 64位扩展。

您的4096不是文件大小(以字节为单位)。它是st_blksize,“ I / O的最佳块大小”。在我的示例中,它是4194304。也许您的文件在HFS +文件系统上。我的是在APFS文件系统上。

您的第三个神秘字段是st_flags,“用户定义的文件标志”。您的为零,因此未设置任何标志。我的示例(/etc/man.conf)设置了UF_COMPRESSED

  

st_dev和st_rdev有什么区别?

st_dev字段是指包含文件的设备(硬盘驱动器/分区/所有)。设备设备文件的st_rdev字段告诉内核文件本身代表什么设备。尝试在stat中的某些设备文件上运行/dev,例如/dev/null/dev/rdisk0,以查看非零的st_rdev值。

  

许多人我没有找到正确的手册页(既没有man stat也没有man lstat)。是否有任何官方文件详细说明每个统计信息字段?在哪里可以找到它?

使用man 1 stat来了解命令行stat程序的标志,就像我使用的-s标志一样。然后使用man 2 stat和您最喜欢的搜索引擎来了解这些字段的含义。