Chinaunix首页 | 论坛 | 博客
  • 博客访问: 544851
  • 博文数量: 83
  • 博客积分: 6010
  • 博客等级: 准将
  • 技术积分: 1169
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-29 22:34
文章分类

全部博文(83)

文章存档

2011年(3)

2010年(29)

2009年(30)

2008年(21)

我的朋友

分类: LINUX

2008-12-17 17:06:57

文件时间这里指的是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。

至于其它的操作,跟以上某一种操作是类似的,可以用稍微修改该程序得到。
阅读(1173) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~