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

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

内容简介: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是非常类似于创建时间的,而且一般也不会被更改。所以在服务器上可代替创建时间使用。


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

查看所有标签

猜你喜欢:

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

任正非传

任正非传

孙力科 / 浙江人民出版社 / 2017-5-2 / 39.80

编辑推荐: 超权威、超丰富、超真实的任正非传记 亲述任正非跌宕起伏、传奇精彩的一生 ◆知名财经作家孙力科,历时十年,数十次深入华为,采访华为和任正非历程中各个关键人物,几度增删,创作成此书 ◆全书展现了任正非从出生至今70多年的人生画卷,从入伍到退役进国企,从艰难创业到开拓海外市场,囊括其人生道路上各个关键点,时间跨度之长,内容之丰富,前所未有 ◆迄今为止,任正非一生......一起来看看 《任正非传》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

URL 编码/解码
URL 编码/解码

URL 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试