In node.js how can I know whether fs.stat() will return usable crtime and/or birthtime fields for a given file/path/volume/fs?
Asked Answered
G

0

10

I recently learned that different OSes and even different filesystems under the same OS support different subsets of the timestamps returned by lstat.

The Stats object returned gives us four times, each in two different flavours.

js Date objects:

  • atime: the last time this file was accessed expressed in milliseconds since the POSIX Epoch
  • mtime: the last time this file was modified ...
  • ctime: the last time the file status was changed ...
  • birthtime: the creation time of this file

(atimeMs, mtimeMs, ctimeMs, and birthtimeMs are js Date object versions of each of the above)

"Modified" means the file's contents were changed by being written to etc. "Changed" means the file's metadata such as owners and permissions was changed.

Linux has traditionally never supported the concept of birth time, but as more newer filesystems did support it, it has recently had support added to hopefully all relevant layers of the Linux stack if I have read correctly.

But Windows and Mac both do support birth time as do their native filesystems.

Windows on the other hand did not traditionally support a concept of file change separate from file modification. But to comply to POSIX it added support at the API level and to NTFS. (It doesn't seem to be exposed anywhere in the GUI or commandline though). FAT fs does not support it.

When I call lstat on a file on Windows on an NTFS drive the results for ctime look good. When I call it on a file on a FAT drive, ctime contains junk. (In my case it's always 2076-11-29T08:54:34.955Z for every file.)

I don't know if this is a bug.

I don't know what birthtime returns on Linux on filesystems that don't support it. Hopefully null or undefined but perhaps also garbage. I also don't know what Linux or Mac return in ctime for files on FAT volumes.

So is there a way in Node to get info on which of these features are supported for a given file/path/fs?

Giffie answered 9/3, 2019 at 5:10 Comment(7)
have you figured out the answer to this? Also, on Windows I see that mtimeMs and birthtimeMs are equal if file is created and nothing more is written to it. Do you experience the same on Mac at least?Worms
My Mac gets temperamental so I only tried to figure it out on Windows so far and even then I gave up. I expect there is no way other than coming up with your own heuristics )-: If you find anything it would be great to hear though!Giffie
did you ever find out whether or not birthtime can be relied upon in Linux? i posted a question hereKienan
@BugWhisperer: No I didn't. I assume it's an overlooked edge case nobody cares about and until somebody does that it's undefined behaviour. But if somebody files a bug I'll definitely follow it.Giffie
Oh man :......(Kienan
I'm finding that birthtimeMS defaults to zero, and birthtime to the epoch, on usupported systems. NULL would have been preferable, as zero is an extremely unlikely but certainly valid birthtimeMS.Variegated
@RyanHanekamp: If you can check multiple different kinds of unsupported systems that would warrant a nodejs bug report IMHO.Giffie

© 2022 - 2024 — McMap. All rights reserved.