Linux和MacOS中的fs.stat Birthtime/Birthtimems [英] fs.stat birthtime/birthtimeMs in Linux and MacOS
本文介绍了Linux和MacOS中的fs.stat Birthtime/Birthtimems的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
调用fs.stat
时返回的fs.Stats
对象的属性之一是birthtime
和birthtimeMs
,我假设这是创建文件时的属性。
Stats {
dev: 2114,
ino: 48064969,
mode: 33188,
nlink: 1,
uid: 85,
gid: 100,
rdev: 0,
size: 527,
blksize: 4096,
blocks: 8,
atimeMs: 1318289051000.1,
mtimeMs: 1318289051000.1,
ctimeMs: 1318289051000.1,
birthtimeMs: 1318289051000.1, // this value
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 } // and this value all the way down here
我想知道UNIX或Linux和MacOS环境中是否使用并可靠地返回了birthtime
和birthtimeMs
?
推荐答案
我对这个问题也很好奇,所以我在MacOS和LINUX上做了一个小实验。以下是我的观察结果。
首先,创建文件:
touch foo
检查时间,出生时间与其他时间相同,这是预期的。
> const fs = require("fs")
undefined
> fs.statSync("foo")
Stats {
...
atimeMs: 1643199913195.383,
mtimeMs: 1643199913195.383,
ctimeMs: 1643199913195.383,
birthtimeMs: 1643199913195.383,
atime: 2022-01-26T12:25:13.195Z,
mtime: 2022-01-26T12:25:13.195Z,
ctime: 2022-01-26T12:25:13.195Z,
birthtime: 2022-01-26T12:25:13.195Z
}
更新文件方式:echo
:
echo bar > foo
再次检查,mtime和ctime已更新,出生时间保持不变,目前为止一切正常。
> fs.statSync("foo")
Stats {
...
atimeMs: 1643199913195.383,
mtimeMs: 1643199961947.3816,
ctimeMs: 1643199961947.3816,
birthtimeMs: 1643199913195.383,
atime: 2022-01-26T12:25:13.195Z,
mtime: 2022-01-26T12:26:01.947Z,
ctime: 2022-01-26T12:26:01.947Z,
birthtime: 2022-01-26T12:25:13.195Z
}
使用vi
编辑文件,然后再次检查,出现意外情况:
> fs.statSync("foo")
Stats {
...
atimeMs: 1643199990663.3806,
mtimeMs: 1643199990663.3806,
ctimeMs: 1643199990667.3806,
birthtimeMs: 1643199990663.3806,
atime: 2022-01-26T12:26:30.663Z,
mtime: 2022-01-26T12:26:30.663Z,
ctime: 2022-01-26T12:26:30.667Z,
birthtime: 2022-01-26T12:26:30.663Z
}
出生时间和所有其他时间都更新了,似乎创建了一个新文件!经过一些挖掘,我发现vim实际上在默认情况下总是copy backup files back and forth,所以btime被篡改了。按照说明设置set backupcopy=yse
后,btime不再更新。
> fs.statSync("foo")
Stats {
...
atimeMs: 1643201126739.3442,
mtimeMs: 1643201129323.3442,
ctimeMs: 1643201129327.3442,
birthtimeMs: 1643200860799.3528,
atime: 2022-01-26T12:45:26.739Z,
mtime: 2022-01-26T12:45:29.323Z,
ctime: 2022-01-26T12:45:29.327Z,
birthtime: 2022-01-26T12:41:00.799Z // not the same as other times
}
似乎birthtime
在4.15内核的MacOS蒙特雷和Ubuntu上是稳定的。但是像VIM这样使用备份文件的程序将破坏该功能,请注意这一点。
根据答案here,Linux 4.11中添加了btime。
这篇关于Linux和MacOS中的fs.stat Birthtime/Birthtimems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文