据我所知,stat/fstat系统调用只告诉我们3次有关文件的信息
- 上次访问时间(st_atim字段)
- 上次修改时间(st_mtim字段)
- 上次状态更改时间(st_ctim)
当我在bash
中运行stat filename
命令时,我可以获得文件的创建时间为Birth
.我的问题是,如何通过一些系统调用获得gcc中的创建时间?
据我所知,stat/fstat系统调用只告诉我们3次有关文件的信息
当我在bash
中运行stat filename
命令时,我可以获得文件的创建时间为Birth
.我的问题是,如何通过一些系统调用获得gcc中的创建时间?
使用statx
.
从man 2 statx
:
概要 #包含sys/types.h> #包括sys/stat.h> #包含unistd.h> #包括fcntl.h> /* AT_* 常数的定义 */
int statx(int dirfd, const char *pathname, int flags, unsigned int mask, struct statx *statxbuf);
描述 此函数返回有关文件的信息,并将其存储在statxbuf指向的缓冲区中. 返回的缓冲区是一个 struct 关于以下- 降低类型:
struct statx { __u32 stx_mask; /* Mask of bits indicating filled fields */ __u32 stx_blksize; /* Block size for filesystem I/O */ __u64 stx_attributes; /* Extra file attribute indicators */ __u32 stx_nlink; /* Number of hard links */ __u32 stx_uid; /* User ID of owner */ __u32 stx_gid; /* Group ID of owner */ __u16 stx_mode; /* File type and mode */ __u64 stx_ino; /* Inode number */ __u64 stx_size; /* Total size in bytes */ __u64 stx_blocks; /* Number of 512B blocks allocated */ __u64 stx_attributes_mask; /* Mask to show what's supported in stx_attributes */ /* The following fields are file timestamps */ struct statx_timestamp stx_atime; /* Last access */ struct statx_timestamp stx_btime; /* Creation */ struct statx_timestamp stx_ctime; /* Last status change */ struct statx_timestamp stx_mtime; /* Last modification */
其中显示了创建文件戳的字段,命名为stx_btime
(我猜是状态延长出生时间).
请注意,对于Darwin(大概还有任何BDS),stat()
已经包含字段struct timespec st_birthtimespec
,并且没有statx()
.