关于linux文件时间属性(atime mtime ctime)

>>转载请注明来源:飘零的代码 piao2010 ’s blog,谢谢!^_^
>>本文链接地址:关于linux文件时间属性(atime mtime ctime)

前阵子和两个朋友在吃饭时无意中提到了linux系统是否有文件创建时间,因为之前遇到过这个问题,查过一些资料,所以我说linux是没有文件创建时间的。关于文件的时间属性可以查一下stat系统调用:man 2 stat

  struct stat {
              dev_t     st_dev;     /* ID of device containing file */
              ino_t     st_ino;     /* inode number */
              mode_t    st_mode;    /* protection */
              nlink_t   st_nlink;   /* number of hard links */
              uid_t     st_uid;     /* user ID of owner */
              gid_t     st_gid;     /* group ID of owner */
              dev_t     st_rdev;    /* device ID (if special file) */
              off_t     st_size;    /* total size, in bytes */
              blksize_t st_blksize; /* blocksize for filesystem I/O */
              blkcnt_t  st_blocks;  /* number of blocks allocated */
              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 */
          };
//省略不相关部分
       Not all of the Linux filesystems implement all of the time fields.  Some file system types allow mounting in  such  a  way  that  file
       accesses do not cause an update of the st_atime field. (See 'noatime' in mount(8).)
     The  field st_atime is changed by file accesses, e.g. by execve(2), mknod(2), pipe(2), utime(2) and read(2) (of more than zero bytes).
       Other routines, like mmap(2), may or may not update st_atime.
 
       The field st_mtime is changed by file modifications, e.g. by mknod(2), truncate(2), utime(2) and write(2) (of more than  zero  bytes).
       Moreover,  st_mtime  of  a  directory  is  changed  by the creation or deletion of files in that directory.  The st_mtime field is not
       changed for changes in owner, group, hard link count, or mode.
 
       The field st_ctime is changed by writing or by setting inode information (i.e., owner, group, link count, mode, etc.).

从上述可以得到:
st_atime:文件访问时间,会改变这个时间的系统调用execve(2), mknod(2), pipe(2), utime(2) and read(2) (of more than zero bytes)。
st_mtime:文件内容更新时间,会改变这个时间的系统调用mknod(2), truncate(2), utime(2) and write(2) (of more than zero bytes),目录的mtime 随着文件的创建、删除而更新。
st_ctime:文件状态更新时间,包括文件内容更新和indoe信息的更新,会改变这个时间的命令chmod chown。

我们知道文件的基本信息是保存在inode里的,顺便看一下这个结构体:

struct inode {
	struct hlist_node	i_hash;
	struct list_head	i_list;
	struct list_head	i_sb_list;
	struct list_head	i_dentry;
	unsigned long		i_ino;
	atomic_t		i_count;
	umode_t			i_mode;
	unsigned int		i_nlink;
	uid_t			i_uid;
	gid_t			i_gid;
	dev_t			i_rdev;
	loff_t			i_size;
	struct timespec		i_atime;
	struct timespec		i_mtime;
	struct timespec		i_ctime;
//后面省略
 
};

linux 是没有文件创建时间的,这里的ctime并不是creation time,而是状态改变时间(change time).

无觅相关文章插件,快速提升流量

Leave a Reply