node.js中fs.stat里的atime,mtime,ctime,birthtime在debian下的更新规则与区别

栏目: 服务器 · 发布时间: 8年前

内容简介:node.js中fs.stat里的atime,mtime,ctime,birthtime在debian下的更新规则与区别

在node.js中可以通过fs.stats命令,可以查看文件的属性,状态,修改时间等:

var fs = require('fs');
fs.stat(filePath, function(err, stats) {
  console.log(states)
})

Stats {
  dev: 2114,
  ino: 48064969,
  mode: 33188,
  nlink: 1,
  uid: 85,
  gid: 100,
  rdev: 0,
  size: 527,
  blksize: 4096,
  blocks: 8,
  atime: Mon, 10 Oct 2011 23:24:11 GMT,
  mtime: Mon, 10 Oct 2011 23:24:11 GMT,
  ctime: Mon, 10 Oct 2011 23:24:11 GMT,
  birthtime: Mon, 10 Oct 2011 23:24:11 GMT }

其中:

atime是指access time,即文件被读取或者执行的时间,修改文件是不会改变access time的。网上很多资料都声称cat、more等读取文件的命令会改变atime,这其实与系统设置有关的,一般默认不会修改。

ctime即change time文件状态改变时间,指文件的i结点被修改的时间

mtime即modify time,指文件内容被修改的时间。

birthtime即文件创建时间,很多文件系统不支持。

在不同的操作系统和设置下,atime/ctime/mtime的更新规则是不一样的,比如我们在Linux Debian下实测的例子:

Debian不支持Birth文件创建时间

使用touch创建,并使用stat查看文件状态

root@VM-104-49-debian:/var/www# touch test.log
root@VM-104-49-debian:/var/www# stat test.log
  File: ‘test.log’
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fe01h/65025d    Inode: 405946      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2017-05-24 14:48:15.376000000 +0800
Modify: 2017-05-24 14:48:15.376000000 +0800
Change: 2017-05-24 14:48:15.376000000 +0800
 Birth: -

可以看到Birth是空的。因此在Debin等 Linux 下面通过 states.birthtime 来判断文件创建时间会有各样的BUG,

比如通过 fs.utimes修改atime和mtime时,在debian下birthtime也会更改。并且有时 birthtime会比mtime更新。

查看文件不会改变Access time

出于性能考虑使用cat查看文件内容,不会修改atime,在Windows下也是同时,不过可以通过系统配置启用atime访问时间的自动更新

root@VM-104-49-debian:/var/www# cat test.log
root@VM-104-49-debian:/var/www# stat test.log
  File: ‘test.log’
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fe01h/65025d    Inode: 405946      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2017-05-24 14:48:15.376000000 +0800
Modify: 2017-05-24 14:48:15.376000000 +0800
Change: 2017-05-24 14:48:15.376000000 +0800
 Birth: -

修改文件内容Modify、Change时间会一同修改

无论是使用重写还是添加,(mtime,ctime)会一同改变

root@VM-104-49-debian:/var/www# echo 'abc' > test.log
root@VM-104-49-debian:/var/www# stat test.log
  File: ‘test.log’
  Size: 4               Blocks: 8          IO Block: 4096   regular file
Device: fe01h/65025d    Inode: 405946      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2017-05-24 14:48:15.376000000 +0800
Modify: 2017-05-24 14:49:38.372000000 +0800
Change: 2017-05-24 14:49:38.372000000 +0800
 Birth: -
root@VM-104-49-debian:/var/www# echo 'abc' >> test.log
root@VM-104-49-debian:/var/www# stat test.log
  File: ‘test.log’
  Size: 8               Blocks: 8          IO Block: 4096   regular file
Device: fe01h/65025d    Inode: 405946      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2017-05-24 14:48:15.376000000 +0800
Modify: 2017-05-24 14:49:46.324000000 +0800
Change: 2017-05-24 14:49:46.324000000 +0800
 Birth: -

更改文件属性只会改变Change时间

通过chown更新文件所属用户,只有ctime发生改变。

root@VM-104-49-debian:/var/www# chown newghost test.log
root@VM-104-49-debian:/var/www# stat test.log
  File: ‘test.log’
  Size: 8               Blocks: 8          IO Block: 4096   regular file
Device: fe01h/65025d    Inode: 405946      Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/newghost)   Gid: (    0/    root)
Access: 2017-05-24 14:48:15.376000000 +0800
Modify: 2017-05-24 14:49:46.324000000 +0800
Change: 2017-05-24 14:51:14.792000000 +0800

因此可以看出states的atime是非常类似于创建时间的,而且一般也不会被更改。所以在服务器上可代替创建时间使用。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

阿里巴巴Java开发手册

阿里巴巴Java开发手册

杨冠宝 / 电子工业出版社 / 2018-1 / 35

《阿里巴巴Java开发手册》的愿景是码出高效,码出质量。它结合作者的开发经验和架构历程,提炼阿里巴巴集团技术团队的集体编程经验和软件设计智慧,浓缩成为立体的编程规范和最佳实践。众所周知,现代软件行业的高速发展对开发者的综合素质要求越来越高,因为不仅是编程相关的知识点,其他维度的知识点也会影响软件的最终交付质量,比如,数据库的表结构和索引设计缺陷可能带来软件的架构缺陷或性能风险;单元测试的失位导致集......一起来看看 《阿里巴巴Java开发手册》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具