在 C 中获取文件系统创建日期

Get filesystem creation date in C

我需要用 C 知道磁盘上文件系统的创建日期时间(在 Linux 机器上)。我想避免使用 shell 命令,例如

tune2fs -l /dev/sdb2 | grep 'Filesystem created:' 

并制作一个解析器。

谢谢

从用 C(或任何能够调用 C 例程的语言)编写的程序中,您将使用 stat(2) system call (or, with recent kernels and some file systems, the statx(2) one) to query the creation time of a given file (or directory). Of course, commands like ls(1) or stat(1) are using internally that stat(2) 系统调用。

没有标准和文件系统中立的方法来获取给定 file system. That information is not always kept. I guess that FAT filesystems, or distributed file systems such as NFS 的创建时间,不要保留它。

您可以在该文件系统的挂载点上使用 stat(2)

statfs(2) 系统调用检索一些文件系统信息,但不提供任何时间戳。

对于ext4文件系统,见ext4(5) and use proc(5). You might parse /proc/mounts and some /proc/fs/ext4/*/ directory. The pseudofiles in /proc/ can be parsed quickly,通常不涉及物理磁盘IO。

您也可以在 ext2/3/4 disk partition level, on an unmounted file ext[234] system, with a library like (or programs from) e2fsprogs 工作。如果挂载了某个文件系统,则不应访问(甚至只是读取)包含该文件系统的磁盘分区。

(你的问题应该给出一些动机和背景)