为什么64位OSX上stat的st_size字段偏移96并且可以计算?

时间:2016-09-24 01:34:13

标签: macos stat bsd darwin

使用来自apple's open source repo的最新资源我已经为" stat"提供了以下结构。 struct(在go语法中):

type timespec struct {
    tv_sec      int32
    tv_nsec     uint32
}

type stat64 struct {
    st_dev      int32           /* [XSI] ID of device containing file */
    st_mode     uint16          /* [XSI] Mode of file (see below) */
    st_nlink    uint16          /* [XSI] Number of hard links */
    st_ino      uint64          /* [XSI] File serial number */
    st_uid      uint32          /* [XSI] User ID of the file */
    st_gid      uint32          /* [XSI] Group ID of the file */
    st_rdev     int32           /* [XSI] Device ID */

    st_atimespec        timespec    /* time of last access */
    st_mtimespec        timespec    /* time of last data modification */
    st_ctimespec        timespec    /* time of last status change */
    st_birthtimespec    timespec    /* time of file creation(birth) */

    st_size     int64           /* [XSI] file size, in bytes */
    st_blocks   int64           /* [XSI] blocks allocated for file */
    st_blksize  int32           /* [XSI] optimal blocksize for I/O */
    st_flags    uint32          /* user defined flags for file */
    st_gen      uint32          /* file generation number */
    st_lspare   int32           /* RESERVED: DO NOT USE! */
    st_qspare   [2]int64        /* RESERVED: DO NOT USE! */
}

但实际上,事实证明st_size的偏移量为96字节而不是上面显示的60字节。造成这种差异的原因是什么?如何从原始源代码中看到这一点?

1 个答案:

答案 0 :(得分:1)

在OS X上,struct timespec的两个字段都是long,在通常的LP64约定中是64位。因此,sizeof(struct timespec) == 16(您可以自己检查),并且它在64位边界上对齐,为st_size提供了96的偏移量。