Skip to content

Commit

Permalink
fixup! fs: support BigInt in fs.*stat and fs.watchFile
Browse files Browse the repository at this point in the history
  • Loading branch information
joyeecheung committed Jun 5, 2018
1 parent 17fda05 commit 3caab98
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
8 changes: 6 additions & 2 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ function Stats(
}

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
// eslint-disable-next-line no-undef
return (this.mode & BigInt(S_IFMT)) === BigInt(property);
Expand Down Expand Up @@ -193,9 +197,9 @@ Stats.prototype.isSocket = function() {
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] < 0 ? undefined : stats[6 + offset],
isWindows ? undefined : stats[6 + offset], // blksize
stats[7 + offset], stats[8 + offset],
stats[9 + offset] < 0 ? undefined : stats[9 + offset],
isWindows ? undefined : stats[9 + offset], // blocks
stats[10 + offset], stats[11 + offset],
stats[12 + offset], stats[13 + offset]);
}
Expand Down
4 changes: 2 additions & 2 deletions src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,14 @@ v8::Local<v8::Value> FillStatsArray(AliasedBuffer<NativeT, V8T>* fields_ptr,
#if defined(__POSIX__)
fields[offset + 6] = s->st_blksize;
#else
fields[offset + 6] = -1;
fields[offset + 6] = 0;
#endif
fields[offset + 7] = s->st_ino;
fields[offset + 8] = s->st_size;
#if defined(__POSIX__)
fields[offset + 9] = s->st_blocks;
#else
fields[offset + 9] = -1;
fields[offset + 9] = 0;
#endif
// Dates.
// NO-LINT because the fields are 'long' and we just want to cast to `unsigned`
Expand Down
26 changes: 17 additions & 9 deletions test/parallel/test-fs-stat-bigint.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ tmpdir.refresh();
const fn = path.join(tmpdir.path, 'test-file');
fs.writeFileSync(fn, 'test');

const link = path.join(tmpdir.path, 'symbolic-link');
fs.symlinkSync(fn, link);
let link;
if (!common.isWindows) {
link = path.join(tmpdir.path, 'symbolic-link');
fs.symlinkSync(fn, link);
}

function verifyStats(bigintStats, numStats) {
for (const key of Object.keys(numStats)) {
Expand Down Expand Up @@ -59,6 +62,9 @@ function verifyStats(bigintStats, numStats) {
bigintStats.isSymbolicLink(),
numStats.isSymbolicLink()
);
} else if (common.isWindows && (key === 'blksize' || key === 'blocks')) {
assert.strictEqual(bigintStats[key], undefined);
assert.strictEqual(numStats[key], undefined);
} else if (Number.isSafeInteger(val)) {
// eslint-disable-next-line no-undef
assert.strictEqual(bigintStats[key], BigInt(val));
Expand All @@ -77,7 +83,7 @@ function verifyStats(bigintStats, numStats) {
verifyStats(bigintStats, numStats);
}

{
if (!common.isWindows) {
const bigintStats = fs.lstatSync(link, { bigint: true });
const numStats = fs.lstatSync(link);
verifyStats(bigintStats, numStats);
Expand All @@ -99,7 +105,7 @@ function verifyStats(bigintStats, numStats) {
});
}

{
if (!common.isWindows) {
fs.lstat(link, { bigint: true }, (err, bigintStats) => {
fs.lstat(link, (err, numStats) => {
verifyStats(bigintStats, numStats);
Expand All @@ -123,11 +129,13 @@ function verifyStats(bigintStats, numStats) {
verifyStats(bigintStats, numStats);
})();

(async function() {
const bigintStats = await promiseFs.lstat(link, { bigint: true });
const numStats = await promiseFs.lstat(link);
verifyStats(bigintStats, numStats);
})();
if (!common.isWindows) {
(async function() {
const bigintStats = await promiseFs.lstat(link, { bigint: true });
const numStats = await promiseFs.lstat(link);
verifyStats(bigintStats, numStats);
})();
}

(async function() {
const handle = await promiseFs.open(fn, 'r');
Expand Down

0 comments on commit 3caab98

Please sign in to comment.