Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix mtime precision on some filesystems
- Loading branch information
Showing
3 changed files
with
95 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
'use strict'; | ||
|
||
const cacheSymbol = Symbol(); | ||
|
||
function probe(file, fs, callback) { | ||
const cachedPrecision = fs[cacheSymbol]; | ||
|
||
if (cachedPrecision) { | ||
return fs.stat(file, (err, stat) => { | ||
if (err) { | ||
return callback(err); | ||
} | ||
|
||
callback(null, stat.mtime, cachedPrecision); | ||
}); | ||
} | ||
|
||
// Set mtime by ceiling Date.now() to seconds + 5ms so that it's "not on the second" | ||
const mtime = new Date((Math.ceil(Date.now() / 1000) * 1000) + 5); | ||
|
||
fs.utimes(file, mtime, mtime, (err) => { | ||
if (err) { | ||
return callback(err); | ||
} | ||
|
||
fs.stat(file, (err, stat) => { | ||
if (err) { | ||
return callback(err); | ||
} | ||
|
||
const precision = stat.mtime.getTime() % 1000 === 0 ? 's' : 'ms'; | ||
|
||
// Cache the precision in a non-enumerable way | ||
Object.defineProperty(fs, cacheSymbol, { value: precision }); | ||
|
||
callback(null, stat.mtime, precision); | ||
}); | ||
}); | ||
} | ||
|
||
function getMtime(precision) { | ||
let now = Date.now(); | ||
|
||
if (precision === 's') { | ||
now = Math.ceil(now / 1000) * 1000; | ||
} | ||
|
||
return new Date(now); | ||
} | ||
|
||
module.exports.probe = probe; | ||
module.exports.getMtime = getMtime; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters