How do I find out what the mtime resolution of a system is from Node.js?
Why I'm asking
In Node.js, fs.watch
will sometimes emit duplicate change
events. In order to avoid taking redundant actions, it's common to use code like this (from CoffeeScript's coffee
utility):
if event is 'change'
fs.stat source, (err, stats) ->
throw err if err
return if stats.size is prevStats.size and
stats.mtime.getTime() is prevStats.mtime.getTime()
prevStats = stats
...
Here's the problem: Under OS X, because of the underlying HFS+ filesystem, mtime
has a resolution of only 1 second. That is, mtime.getTime()
values are of the form
1322068921000
So whenever two changes occur within 1 second of each other, there's a chance that the second change will not affect the file's mtime
. Thanks to the stats.size
check, this is only a problem if the second change had no effect on the file's size. Still, it's a problem.
A reliable solution would be to "debounce" the change
events by the mtime interval; i.e. under OS X, when a change
occurs, I would wait 1 second to act on it (thereby grouping all change events that may have the same mtime
together). But I'd like to delay the event by the minimum possible time under each file system, rather than adopting the greatest common denominator.