2011-05-08 52 views
7

我正在處理批量的文件,這些文件包含有關同一對象在其生命不同時間的信息,唯一的排序方式是創建日期。 我正在使用這個:如何在Linux中獲取文件創建日期?

//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下的文件創建日期嗎?

+3

'fstat'不會因爲許多文件系統不要取「文件創建」時間戳值」跟蹤這些數據。你在使用什麼文件系統? – 2011-05-08 18:36:57

+0

一個是最新的Ubuntu桌面的標準,我想 - 我正在虛擬機上運行我的代碼(準確地說,是vmware播放器),並將所有的細節保存到Ubuntu安裝程序中。 – Srv19 2011-05-08 18:41:41

+0

嘗試'stat(buffer,&buf)'而不是這裏沒有用的'fopen' – mpez0 2011-05-08 19:51:48

回答

7

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。

+0

那麼我如何獲取文件描述符? – Srv19 2011-05-08 18:58:53

+1

@ srv19您可以使用open(2)或fileno(tf)來獲取文件描述符。然而,我給你的是一種檢查它的屬性而不必打開文件的方法。如果您仍然需要打開該文件並想要使用stdio功能,那麼fileno就是您的朋友。 – Mel 2011-05-08 19:05:00

+0

您的解決方案擺脫了我的問題。非常感謝。我懷疑我是使用fstat的使命。 – Srv19 2011-05-08 19:57:29

5

文件創建時間不存儲任何地方,你只能檢索以下之一:但是

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()將文件名稱作爲參數)。

+0

Bah,我忽略了你將FILE指針傳遞給fstat,而不是描述符:) – 2011-05-08 19:24:08

10

最近的近似「創建日期」是在struct statst_ctime成員,但實際上記錄最後一次inode的改變。如果您創建該文件並且從不修改其大小或權限,那麼該文件將用作創建時間。否則,至少在標準的Unix系統中,沒有創建文件的時間記錄。

出於您的目的,按st_mtime排序...或獲取名稱中帶有時間戳的文件。


請注意,如果您是在達爾文(Mac OS X上),創建時間是可用的。從手冊頁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時間保持在標準時間;達爾文遵循這一點。

+0

不幸的是,這些文件是由另一個應用程序創建的。可悲的是,我沒有權限改變任何事情。 – Srv19 2011-05-08 18:48:27

6

要獲得在Linux中的文件創建日期,我用下面的方法

[email protected]# cat <<_eof> test.txt 
> Hello 
> This is my test file 
> _eof 
[email protected]# cat test.txt 
Hello 
This is my test file 
[email protected]# ls -i test.txt 
2097517 test.txt 
[email protected]# 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:文件創建時間

+2

謝謝你的回答,但我必須注意到我的問題是關於使用linux核心c函數,而不是如何在shell中獲得創建時間。 – Srv19 2013-01-08 14:08:17

相關問題