【问题标题】:Why is stat's st_size field offset 96 on 64bit OSX and can it be calculated?为什么 stat 的 st_size 字段在 64 位 OSX 上的偏移量为 96,可以计算吗?
【发布时间】:2016-09-24 01:34:12
【问题描述】:

使用来自apple's open source repo 的最新资源,我为“stat”结构派生了以下结构(在 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 字节。造成这种差异的原因是什么?如何从原始源代码中看出这一点?

【问题讨论】:

    标签: macos stat bsd darwin


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2012-08-29
      • 1970-01-01
      • 2012-01-09
      • 1970-01-01
      • 2015-12-06
      • 1970-01-01
      • 2019-08-13
      • 2018-10-31
      • 2019-06-13
      相关资源
      最近更新 更多