Почему поле st_size stat смещено 96 на 64-битной OSX и его можно рассчитать?
Используя последние источники из репозитория Apple с открытым исходным кодом, я получил следующую структуру для структуры "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, показанных выше. В чем причина этого несоответствия и как это можно увидеть из исходного исходного кода?
1 ответ
Решение
На OS X оба поля struct timespec
являются long
, который является 64-битным в обычном соглашении LP64. Следовательно, sizeof(struct timespec) == 16
(вы можете проверить это самостоятельно), и он выровнен по 64-битной границе, что дает вам смещение 96 для st_size
,