据我所知,stat/fstat系统调用只告诉我们3次有关文件的信息

  • 上次访问时间(st_atim字段)
  • 上次修改时间(st_mtim字段)
  • 上次状态更改时间(st_ctim)

当我在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().

Linux相关问答推荐

C++17/Linux:信号未解锁单独线程中被阻止的网络套接字调用

如何使 awk 输出更具可读性?

无法在嵌入式 Linux 中使用 libjpeg 以 RGB888 在 LCD(黑色)上显示 JPEG

为什么当凭证助手设置为存储 SSH 远程存储库时 git pull 不使用 .git-credentials

如何在linux中将2个不同大小的图像(边框,实际图像)合并为1个

使用 awk 从文件中检索一组特定的字符串

判断指定名称的画面是否存在

如何在 Linux 上捕获原始 HID 输入?

为什么 perf 不报告缓存未命中?

如何在非阻塞套接字上处理 OpenSSL SSL_ERROR_WANT_READ / WANT_WRITE

在 C 中检测 64 位编译

将 bash 脚本添加到路径

NGINX:connect() 到 unix:/var/run/php7.0-fpm.sock 失败(2:没有这样的文件或目录)

'&&' 与 '&' 与 Bash 中的 'test' 命令

在 linux 上查看文件大小

Supervisord - 将进程标准输出重定向到控制台

什么是适用于 Linux 的好的 Prolog IDE?

如何在 Linux 中使用单行命令获取 Java 版本

我可以使用 awk 将所有小写字母转换为大写吗?

如何拖尾除第一行以外的所有行