-
Notifications
You must be signed in to change notification settings - Fork 30.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fs: add *timeNs properties to BigInt Stats objects #21387
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
'use strict'; | ||
|
||
const { Reflect } = primordials; | ||
const { Object, Reflect } = primordials; | ||
|
||
const { Buffer, kMaxLength } = require('buffer'); | ||
const { | ||
|
@@ -16,7 +16,8 @@ const { | |
} = require('internal/errors'); | ||
const { | ||
isUint8Array, | ||
isDate | ||
isDate, | ||
isBigUint64Array | ||
} = require('internal/util/types'); | ||
const { once } = require('internal/util'); | ||
const { toPathIfFileURL } = require('internal/url'); | ||
|
@@ -230,27 +231,9 @@ function preprocessSymlinkDestination(path, type, linkPath) { | |
} | ||
} | ||
|
||
function dateFromNumeric(num) { | ||
return new Date(Number(num) + 0.5); | ||
} | ||
|
||
// Constructor for file stats. | ||
function Stats( | ||
dev, | ||
mode, | ||
nlink, | ||
uid, | ||
gid, | ||
rdev, | ||
blksize, | ||
ino, | ||
size, | ||
blocks, | ||
atim_msec, | ||
mtim_msec, | ||
ctim_msec, | ||
birthtim_msec | ||
) { | ||
function StatsBase(dev, mode, nlink, uid, gid, rdev, blksize, | ||
ino, size, blocks) { | ||
this.dev = dev; | ||
this.mode = mode; | ||
this.nlink = nlink; | ||
|
@@ -261,63 +244,132 @@ function Stats( | |
this.ino = ino; | ||
this.size = size; | ||
this.blocks = blocks; | ||
this.atimeMs = atim_msec; | ||
this.mtimeMs = mtim_msec; | ||
this.ctimeMs = ctim_msec; | ||
this.birthtimeMs = birthtim_msec; | ||
this.atime = dateFromNumeric(atim_msec); | ||
this.mtime = dateFromNumeric(mtim_msec); | ||
this.ctime = dateFromNumeric(ctim_msec); | ||
this.birthtime = dateFromNumeric(birthtim_msec); | ||
} | ||
|
||
Stats.prototype._checkModeProperty = function(property) { | ||
if (isWindows && (property === S_IFIFO || property === S_IFBLK || | ||
property === S_IFSOCK)) { | ||
return false; // Some types are not available on Windows | ||
} | ||
if (typeof this.mode === 'bigint') { // eslint-disable-line valid-typeof | ||
return (this.mode & BigInt(S_IFMT)) === BigInt(property); | ||
} | ||
return (this.mode & S_IFMT) === property; | ||
}; | ||
|
||
Stats.prototype.isDirectory = function() { | ||
StatsBase.prototype.isDirectory = function() { | ||
return this._checkModeProperty(S_IFDIR); | ||
}; | ||
|
||
Stats.prototype.isFile = function() { | ||
StatsBase.prototype.isFile = function() { | ||
return this._checkModeProperty(S_IFREG); | ||
}; | ||
|
||
Stats.prototype.isBlockDevice = function() { | ||
StatsBase.prototype.isBlockDevice = function() { | ||
return this._checkModeProperty(S_IFBLK); | ||
}; | ||
|
||
Stats.prototype.isCharacterDevice = function() { | ||
StatsBase.prototype.isCharacterDevice = function() { | ||
return this._checkModeProperty(S_IFCHR); | ||
}; | ||
|
||
Stats.prototype.isSymbolicLink = function() { | ||
StatsBase.prototype.isSymbolicLink = function() { | ||
return this._checkModeProperty(S_IFLNK); | ||
}; | ||
|
||
Stats.prototype.isFIFO = function() { | ||
StatsBase.prototype.isFIFO = function() { | ||
return this._checkModeProperty(S_IFIFO); | ||
}; | ||
|
||
Stats.prototype.isSocket = function() { | ||
StatsBase.prototype.isSocket = function() { | ||
return this._checkModeProperty(S_IFSOCK); | ||
}; | ||
|
||
const kNsPerMsBigInt = 10n ** 6n; | ||
const kNsPerSecBigInt = 10n ** 9n; | ||
const kMsPerSec = 10 ** 3; | ||
const kNsPerMs = 10 ** 6; | ||
function msFromTimeSpec(sec, nsec) { | ||
return sec * kMsPerSec + nsec / kNsPerMs; | ||
} | ||
|
||
function nsFromTimeSpecBigInt(sec, nsec) { | ||
return sec * kNsPerSecBigInt + nsec; | ||
} | ||
|
||
function dateFromMs(ms) { | ||
return new Date(Number(ms) + 0.5); | ||
} | ||
|
||
function BigIntStats(dev, mode, nlink, uid, gid, rdev, blksize, | ||
ino, size, blocks, | ||
atimeNs, mtimeNs, ctimeNs, birthtimeNs) { | ||
StatsBase.call(this, dev, mode, nlink, uid, gid, rdev, blksize, | ||
ino, size, blocks); | ||
|
||
this.atimeMs = atimeNs / kNsPerMsBigInt; | ||
this.mtimeMs = mtimeNs / kNsPerMsBigInt; | ||
this.ctimeMs = ctimeNs / kNsPerMsBigInt; | ||
this.birthtimeMs = birthtimeNs / kNsPerMsBigInt; | ||
this.atimeNs = atimeNs; | ||
this.mtimeNs = mtimeNs; | ||
this.ctimeNs = ctimeNs; | ||
this.birthtimeNs = birthtimeNs; | ||
this.atime = dateFromMs(this.atimeMs); | ||
this.mtime = dateFromMs(this.mtimeMs); | ||
this.ctime = dateFromMs(this.ctimeMs); | ||
this.birthtime = dateFromMs(this.birthtimeMs); | ||
} | ||
|
||
Object.setPrototypeOf(BigIntStats.prototype, StatsBase.prototype); | ||
Object.setPrototypeOf(BigIntStats, StatsBase); | ||
|
||
BigIntStats.prototype._checkModeProperty = function(property) { | ||
if (isWindows && (property === S_IFIFO || property === S_IFBLK || | ||
property === S_IFSOCK)) { | ||
return false; // Some types are not available on Windows | ||
} | ||
return (this.mode & BigInt(S_IFMT)) === BigInt(property); | ||
}; | ||
|
||
function Stats(dev, mode, nlink, uid, gid, rdev, blksize, | ||
ino, size, blocks, | ||
atimeMs, mtimeMs, ctimeMs, birthtimeMs) { | ||
StatsBase.call(this, dev, mode, nlink, uid, gid, rdev, blksize, | ||
ino, size, blocks); | ||
this.atimeMs = atimeMs; | ||
this.mtimeMs = mtimeMs; | ||
this.ctimeMs = ctimeMs; | ||
this.birthtimeMs = birthtimeMs; | ||
this.atime = dateFromMs(atimeMs); | ||
this.mtime = dateFromMs(mtimeMs); | ||
this.ctime = dateFromMs(ctimeMs); | ||
this.birthtime = dateFromMs(birthtimeMs); | ||
} | ||
|
||
Object.setPrototypeOf(Stats.prototype, StatsBase.prototype); | ||
Object.setPrototypeOf(Stats, StatsBase); | ||
|
||
Stats.prototype._checkModeProperty = function(property) { | ||
if (isWindows && (property === S_IFIFO || property === S_IFBLK || | ||
property === S_IFSOCK)) { | ||
return false; // Some types are not available on Windows | ||
} | ||
return (this.mode & S_IFMT) === property; | ||
}; | ||
|
||
function getStatsFromBinding(stats, offset = 0) { | ||
return new Stats(stats[0 + offset], stats[1 + offset], stats[2 + offset], | ||
stats[3 + offset], stats[4 + offset], stats[5 + offset], | ||
stats[6 + offset], // blksize | ||
stats[7 + offset], stats[8 + offset], | ||
stats[9 + offset], // blocks | ||
stats[10 + offset], stats[11 + offset], | ||
stats[12 + offset], stats[13 + offset]); | ||
if (isBigUint64Array(stats)) { | ||
return new BigIntStats( | ||
stats[0 + offset], stats[1 + offset], stats[2 + offset], | ||
stats[3 + offset], stats[4 + offset], stats[5 + offset], | ||
stats[6 + offset], stats[7 + offset], stats[8 + offset], | ||
stats[9 + offset], | ||
nsFromTimeSpecBigInt(stats[10 + offset], stats[11 + offset]), | ||
nsFromTimeSpecBigInt(stats[12 + offset], stats[13 + offset]), | ||
nsFromTimeSpecBigInt(stats[14 + offset], stats[15 + offset]), | ||
nsFromTimeSpecBigInt(stats[16 + offset], stats[17 + offset]) | ||
); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Fishrock123 I think we prefer leaving out the else block for early returns? There used to a PR adding a lint rule for that although it didn't land due to the amount of churn. |
||
return new Stats( | ||
stats[0 + offset], stats[1 + offset], stats[2 + offset], | ||
stats[3 + offset], stats[4 + offset], stats[5 + offset], | ||
stats[6 + offset], stats[7 + offset], stats[8 + offset], | ||
stats[9 + offset], | ||
msFromTimeSpec(stats[10 + offset], stats[11 + offset]), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can't pass a time spec into the |
||
msFromTimeSpec(stats[12 + offset], stats[13 + offset]), | ||
msFromTimeSpec(stats[14 + offset], stats[15 + offset]), | ||
msFromTimeSpec(stats[16 + offset], stats[17 + offset]) | ||
); | ||
} | ||
|
||
function stringToFlags(flags) { | ||
|
@@ -453,6 +505,7 @@ function warnOnNonPortableTemplate(template) { | |
|
||
module.exports = { | ||
assertEncoding, | ||
BigIntStats, // for testing | ||
copyObject, | ||
Dirent, | ||
getDirents, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be changed, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That'll only be available when this is released.