ls -l命令和stat的结果不一致

jiangfeng999 2007-10-13 03:22:45
在linux下发现一个很奇怪的现象,我把几个7月的文件夹(以及文件夹下的文件和目录)tar到一个新的目录下。用ls -l命令看到的文件时间还是7月份的时间。然后我用以下代码去显示解压后的文件的时间,发现用程序显示出的时间却是当前解压缩的时间。
代码如下:
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <time.h>
#include <string.h>
int main()
{
char strFilename[200]="xxxxxxxxxxxxxxxxxxxx";
struct dirent* pdir;
char strFile[200];
DIR* dir;
time_t tDirCreate;
struct stat buf;
dir = opendir(strFilename);
while(( pdir = readdir(dir)) != NULL)
{
if ((strcmp(pdir->d_name,".") != 0) && (strcmp(pdir->d_name,"..") != 0))
{
memset(strFile,'\0',sizeof(strFile));
strcpy(strFile,strFilename);
strcat(strFile,"/");
strcat(strFile,pdir->d_name);
sprintf(strFile,"%s%s",strFile,"/");
memset(&buf,'\0',sizeof(buf));
stat(strFile,&buf);
tDirCreate=buf.st_ctime;
printf("\niDelFile:%s\n %s %s %s\n",strFile,ctime(&tDirCreate),
ctime(&buf.st_atime),ctime(&buf.st_mtime));
}
}
closedir(dir);
exit(1);
}


...全文
262 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
dai_weitao 2007-10-17
  • 打赏
  • 举报
回复
tar的问题. 保留/不保留原时间
acange 2007-10-17
  • 打赏
  • 举报
回复
你在解压得时候请加这个参数:
tar zvf --atime-preserve filename.tar
这样,他就会保持原来的时间
jiangfeng999 2007-10-16
  • 打赏
  • 举报
回复
嗯,我问题的关键在于要显示的这些文件是从其他地方tar过来的,而不是正常生成的文件.

ls -l的显示结果是
drwxr-xr-x 2 cjtest ffcscj 4096 Aug 27 17:11 20070801
drwxr-xr-x 2 cjtest ffcscj 4096 Aug 27 17:12 20070802
drwxr-xr-x 2 cjtest ffcscj 4096 Aug 27 17:12 20070803
drwxr-xr-x 2 cjtest ffcscj 4096 Aug 27 17:13 20070805

而用我这个程序显示出来的结果是:
20070801/
ctime:Wed Sep 26 22:03:00 2007
atime:Wed Sep 26 22:03:00 2007
mtime:Wed Sep 26 22:03:00 2007


20070802/
ctime:Wed Sep 26 22:03:00 2007
atime:Wed Sep 26 22:03:00 2007
mtime:Wed Sep 26 22:03:00 2007


20070805/
ctime:Wed Sep 26 22:03:00 2007
atime:Wed Sep 26 22:03:00 2007
mtime:Wed Sep 26 22:03:00 2007


20070803/
ctime:Wed Sep 26 22:03:00 2007
atime:Wed Sep 26 22:03:00 2007
mtime:Wed Sep 26 22:03:00 2007

都是当时解压缩的时间.
roadingyh 2007-10-15
  • 打赏
  • 举报
回复
time_t st_atime; /* time of lastaccess*/
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last change */

可能还是参数没有选对吧....
wangling21908 2007-10-14
  • 打赏
  • 举报
回复
不是很懂,顶
acange 2007-10-13
  • 打赏
  • 举报
回复
tDirCreate=buf.st_ctime;
它是 time of last status change ,
改成:
tDirCreate=buf.st_mtime

23,121

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧