nodejs - how to change creation time of file
Asked Answered
P

3

15

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?

Paulita answered 25/8, 2014 at 10:46 Comment(0)
C
12

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.

Creamcolored answered 29/5, 2017 at 10:24 Comment(7)
It's Linux file server but w.e it works and it's actually better than what I used to use bc allows undefined to keep field unchanged.Paulita
On Linux, setting the btime is a no-op, but mtime and atime will be set on Linux if they are provided.Creamcolored
Huh? Why? I have btrfs RAID array and I want to backup and be able to restore low level file metadata. That includes btime. node stat on btrfs filesystem returns 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 timePaulita
Oh... it seems linux actually explicitly forbids such operation and it requires changing system clock to old date, then creating hardlink to file and removing it, then setting system clock back to proper date... It's... pretty inconvenient...Paulita
As far as I understand Linux does not support the concept of birthtime. 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.Creamcolored
well ext4 and btrfs do support it but it's not exactly birthtime of file because like I said it's date of last inode change so it changes when for example hardlink to file is created. However as long as there's no inode changes it'll remain date of file creation. And I checked node support for it and yes, ctime returned by fs.statSync is correct and it does change after hardlink creation.Paulita
@ronomon/utimes is not longer maintained and doesn't work on Catalina and latest Node.js Check utimes instead -> github.com/baileyherbert/utimesBeaujolais
S
2

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 the ctime or 1970-01-01T00:00Z (ie, unix epoch timestamp 0)…

Prior to Node v0.12, the ctime held the birthtime on Windows systems. Note that as of v0.12, ctime is 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)

Sanctum answered 15/1, 2015 at 20:42 Comment(2)
ctime is not creation timeDescribe
@Describe thanks for mentioning, I've clarified my answer – at least on Windows, ctime has been used as creation time at the time of writing.Sanctum
S
0

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
Strohl answered 11/8, 2017 at 19:44 Comment(1)
At least up to node v10.0, fs.utimes only updates atime and mtime, and not the field the OP wanted, which is birthtime.Guillen

© 2022 - 2024 — McMap. All rights reserved.