fsStat class instance returns mtime, atime and ctime date objects, but there seems to be API only for changing mtime and atime (last modification and access i guess). How can i change creation time to create exact copy of file as it'd be also created the same time as original one?
3 Answers
It's not possible at present with Node itself, but you can use https://github.com/baileyherbert/utimes (a native add-on for Node) to change the creation time (aka btime) of a file on Windows and Mac.
7 Comments
undefined to keep field unchanged.btime is a no-op, but mtime and atime will be set on Linux if they are provided.atime, mtime, ctime and birthtime (though birthtime and ctime are the same). I want to change them. And yeah I'm aware in UNIX ctime is not actually file creation time but last inode update but whetever. As long as I won't use hardlinks it's creation timebirthtime. There is no Linux API to get or set a birthtime that I know of. Linux filesystems usually don't even have a field to store the birthtime of a file (even if Linux had the API to change it). Node introduces a dummy birthtime on fs.stats instances on Linux, which tracks ctime (which is meaningless). Windows and Mac are the systems which support birthtime.fs.statSync is correct and it does change after hardlink creation.tl;tr: That's not possible atm (Node.js <= v6).
Even though, fs.stat() returns the birthtime of files:
birthtime"Birth Time" - Time of file creation. Set once when the file is created. On filesystems where birthtime is not available, this field may instead hold either thectimeor1970-01-01T00:00Z(ie, unix epoch timestamp0)…Prior to Node v0.12, the
ctimeheld thebirthtimeon Windows systems. Note that as of v0.12,ctimeis not "creation time", and on Unix systems, it never was.
Updating is not possible. From https://github.com/joyent/node/issues/8252:
fs.utimes uses utime(2) (http://linux.die.net/man/2/utime), which doesn't allow you to change the ctime.
(Same holds for birthtime)
2 Comments
ctime has been used as creation time at the time of writing.The fs.utimes and fs.utimesSync methods are built into Node.js core. See https://nodejs.org/api/fs.html#fs_fs_utimes_path_atime_mtime_callback
Note:
The value should be a Unix timestamp in seconds. For example, Date.now() returns milliseconds, so it should be divided by 1000 before passing it in.
To convert a JS Date object to seconds:
new Date().getTime()/1000|0 1 Comment
fs.utimes only updates atime and mtime, and not the field the OP wanted, which is birthtime.