为什么stat的st_size字段在64位OSX上偏移96并且可以计算出来?
Why is stat's st_size field offset 96 on 64bit OSX and can it be calculated?
使用来自 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 字节。造成这种差异的原因是什么?如何从原始源代码中看出这一点?
在OS X 上,struct timespec
的两个字段都是long
,这在通常的LP64 约定中是64 位的。因此,sizeof(struct timespec) == 16
(您可以自己检查),它在 64 位边界上对齐,为 st_size
.
提供 96 的偏移量
使用来自 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 字节。造成这种差异的原因是什么?如何从原始源代码中看出这一点?
在OS X 上,struct timespec
的两个字段都是long
,这在通常的LP64 约定中是64 位的。因此,sizeof(struct timespec) == 16
(您可以自己检查),它在 64 位边界上对齐,为 st_size
.