找回密码
 注册
搜索
热搜: 回贴
微赢网络技术论坛 门户 服务器 Linux/BSD 查看内容

文件属性中时间的一点探讨

2009-12-20 13:44| 发布者: admin| 查看: 73| 评论: 0|原作者: 慕容紫英

文件时间这里指的是struct stat中的st_atime, st_mtime, st_ctime。这几个数据其实来自于文件系统中的Inode结构,在《深入理解linux内核》书中提到:
struct timespec i_atime /*Time of last file access*/
struct timespec i_mtime /*Time of last file write*/
struct timespec i_ctime /*Time of last inode change*/
下面写一个小程序来说明(该程序只是为了说明问题,可能有很多不规范之处:)):




#include sys/types.h>
#include sys/stat.h>
#include unistd.h>
#include fcntl.h>
#include errno.h>
#include stdio.h>
int main(int argc, char **argv)
{
int fd;
char buf[4];
struct stat stbuf;
printf("\t\tatime\t\tmtime\t\tctime\n");
if (lstat(argv[1], &stbuf) == -1) {;
perror("lstat");
return -1;
}
printf("before open\t");
printf("%u\t%u\t%u\n", stbuf.st_atime, stbuf.st_mtime, stbuf.st_ctime);
if ((fd = open(argv[1], O_RDWR)) == -1) {
perror("open");
return -2;
}
if (fstat(fd, &stbuf) == -1) {
perror("fstat");
return -2;
}
printf("after open\t");
printf("%u\t%u\t%u\n", stbuf.st_atime, stbuf.st_mtime, stbuf.st_ctime);
read(fd, buf, sizeof(buf));
if (fstat(fd, &stbuf) == -1)
perror("fstat");
printf("after read\t");
printf("%u\t%u\t%u\n", stbuf.st_atime, stbuf.st_mtime, stbuf.st_ctime);

lseek(fd, 1, SEEK_SET);
if (fstat(fd, &stbuf) == -1)
perror("fstat");
printf("after lseek\t");
printf("%u\t%u\t%u\n", stbuf.st_atime, stbuf.st_mtime, stbuf.st_ctime);

sleep(1);
ftruncate(fd, 0);
if (fstat(fd, &stbuf) == -1) {
perror("fstat");
return -1;
}
printf("after truncate\t");
printf("%u\t%u\t%u\n", stbuf.st_atime, stbuf.st_mtime, stbuf.st_ctime);

sleep(1);
write(fd, "test", 4);
if (fstat(fd, &stbuf) == -1) {
perror("fstat");
return -1;
}
printf("after write\t");
printf("%u\t%u\t%u\n", stbuf.st_atime, stbuf.st_mtime, stbuf.st_ctime);
close(fd);
if (lstat(argv[1], &stbuf) == -1) {
perror("lstat");
return -1;
}
printf("after close\t");
printf("%u\t%u\t%u\n", stbuf.st_atime, stbuf.st_mtime, stbuf.st_ctime);
sleep(1);
if (chmod(argv[1], S_IRUSR | S_IWUSR | S_IXUSR) == -1) {
perror("chmod");
return -2;
}
if (lstat(argv[1], &stbuf) == -1) {
perror("lstat");
return -1;
}
printf("after chmod\t");
printf("%u\t%u\t%u\n", stbuf.st_atime, stbuf.st_mtime, stbuf.st_ctime);
return 0;
}
这里我随便创建个文件测试了下,下面是显示结果:$ ./file_time temp
atime mtime ctime
before open 1229504644 1229504646 1229504647
after open 1229504644 1229504646 1229504647
after read 1229504682 1229504646 1229504647
after lseek 1229504682 1229504646 1229504647
after truncate 1229504682 1229504683 1229504683
after write 1229504682 1229504684 1229504684
after close 1229504682 1229504684 1229504684
after chmod 1229504682 1229504684 1229504685
可以看出open和close以及lseek不会改变任何一个时间。
read操作会改变atime。
truncate和write会改变mtime和ctime。
chmod会改变ctime。
至于其它的操作,跟以上某一种操作是类似的,可以用稍微修改该程序得到。
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://www.cublog.cn/images/face/001.gif');}" onmousewheel="return imgzoom(this);" alt="" />





最新评论

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-30 07:28 , Processed in 0.115701 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

返回顶部