如何在Linux中获取文件创builddate?

我正在处理一批文件,这些文件包含有关同一个对象在不同时间的信息,唯一的sorting方法是创builddate。 我正在使用这个:

//char* buffer has the name of file struct stat buf; FILE *tf; tf = fopen(buffer,"r"); //check handle fstat(tf, &buf); fclose(tf); pMyObj->lastchanged=buf.st_mtime; 

但是这似乎并不奏效。 我究竟做错了什么? 还有其他更可靠/简单的方法来获取Linux下的文件创builddate吗?

fstat在文件描述符而不是FILE结构上工作。 最简单的版本:

 #include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #ifdef HAVE_ST_BIRTHTIME #define birthtime(x) x.st_birthtime #else #define birthtime(x) x.st_ctime #endif int main(int argc, char *argv[]) { struct stat st; size_t i; for( i=1; i<argc; i++ ) { if( stat(argv[i], &st) != 0 ) perror(argv[i]); printf("%i\n", birthtime(st)); } return 0; } 

你将需要通过检查sys / stat.h或者使用某种autoconf结构来判断你的系统是否在它的stat结构中有st_birthtime。

“创建日期”的最接近的近似值是struct statst_ctime成员,但实际上记录了上次更改inode的时间。 如果您创建了该文件,并且从不修改其大小或权限,那么这就是创建时间。 否则,至少在标准的Unix系统中,没有创建文件的时间记录。

出于您的目的,按st_mtime排序…或获取名称中带有时间戳的文件。


请注意,如果您使用的是Darwin(Mac OS X),则创建时间可用。 从stat(2)的手册页stat(2)

但是,当定义宏_DARWIN_FEATURE_64_BIT_INODE时,stat结构现在将被定义为:

  struct stat { /* when _DARWIN_FEATURE_64_BIT_INODE is defined */ dev_t st_dev; /* ID of device containing file */ mode_t st_mode; /* Mode of file (see below) */ nlink_t st_nlink; /* Number of hard links */ ino_t st_ino; /* File serial number */ uid_t st_uid; /* User ID of the file */ gid_t st_gid; /* Group ID of the file */ dev_t st_rdev; /* Device ID */ struct timespec st_atimespec; /* time of last access */ struct timespec st_mtimespec; /* time of last data modification */ struct timespec st_ctimespec; /* time of last status change */ struct timespec st_birthtimespec; /* time of file creation(birth) */ off_t st_size; /* file size, in bytes */ blkcnt_t st_blocks; /* blocks allocated for file */ blksize_t st_blksize; /* optimal blocksize for I/O */ uint32_t st_flags; /* user defined flags for file */ uint32_t st_gen; /* file generation number */ int32_t st_lspare; /* RESERVED: DO NOT USE! */ int64_t st_qspare[2]; /* RESERVED: DO NOT USE! */ }; 

请注意st_birthtimespec字段。 请注意,所有时间都在struct timespec值中,所以存在亚秒时间( tv_nsec给出纳秒分辨率)。 POSIX 2008 <sys/stat.h>要求struct timespec在标准时间上保持时间; 达尔文遵循这一点。

要在linux中获取文件创建日期,我使用下面的方法

 root@sathishkumar# cat << _eof > test.txt > Hello > This is my test file > _eof root@sathishkumar# cat test.txt Hello This is my test file root@sathishkumar# ls -i test.txt 2097517 test.txt root@sathishkumar# debugfs -R 'stat <2097517>' /dev/sda5 Inode: 2097517 Type: regular Mode: 0664 Flags: 0x80000 Generation: 4245143992 Version: 0x00000000:00000001 User: 1000 Group: 1000 Size: 27 File ACL: 0 Directory ACL: 0 Links: 1 Blockcount: 8 Fragment: Address: 0 Number: 0 Size: 0 ctime: 0x50ea6d84:4826cc94 -- Mon Jan 7 12:09:00 2013 atime: 0x50ea6d8e:75ed8a04 -- Mon Jan 7 12:09:10 2013 mtime: 0x50ea6d84:4826cc94 -- Mon Jan 7 12:09:00 2013 crtime: 0x5056d493:bbabf49c -- Mon Sep 17 13:13:15 2012 Size of extra inode fields: 28 EXTENTS: (0):8421789 

atime:上次打开或执行文件的时间

ctime:更新inode信息的时间。 当文件被修改时,ctime也会被更新

mtime:上次修改时间

crtime:文件创建时间

文件创建时间不存储在任何地方,只能检索以下内容之一:

 time_t st_atime; /* time of last access */ time_t st_mtime; /* time of last modification */ time_t st_ctime; /* time of last status change */ 

但是,你的代码应该给你最后的修改时间。 注意:您可以使用stat()而不是fstat()而不打开文件( stat()将文件名作为参数)。