summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJoyee Cheung <joyeec9h3@gmail.com>2018-04-07 17:01:06 +0800
committerJoyee Cheung <joyeec9h3@gmail.com>2018-06-07 22:05:23 +0800
commit1e7645c39ae5213a44267cff3d599264c2211f1a (patch)
treea000944a7267c0ad8b0dfe325d93f34f564844ed /lib
parentaf2a1045631028dfad0dd5d3eb4c4866fdf55730 (diff)
downloadandroid-node-v8-1e7645c39ae5213a44267cff3d599264c2211f1a.tar.gz
android-node-v8-1e7645c39ae5213a44267cff3d599264c2211f1a.tar.bz2
android-node-v8-1e7645c39ae5213a44267cff3d599264c2211f1a.zip
fs: support BigInt in fs.*stat and fs.watchFile
Add the `bigint: true` option to all the `fs.*stat` methods and `fs.watchFile`. PR-URL: https://github.com/nodejs/node/pull/20220 Fixes: https://github.com/nodejs/node/issues/12115 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Diffstat (limited to 'lib')
-rw-r--r--lib/fs.js56
-rw-r--r--lib/internal/fs/promises.js19
-rw-r--r--lib/internal/fs/utils.js16
-rw-r--r--lib/internal/fs/watchers.js4
4 files changed, 57 insertions, 38 deletions
diff --git a/lib/fs.js b/lib/fs.js
index 0dd2d0d90c..f80bb6ac33 100644
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -248,7 +248,7 @@ function readFileAfterOpen(err, fd) {
const req = new FSReqWrap();
req.oncomplete = readFileAfterStat;
req.context = context;
- binding.fstat(fd, req);
+ binding.fstat(fd, false, req);
}
function readFileAfterStat(err, stats) {
@@ -307,7 +307,7 @@ function readFile(path, options, callback) {
function tryStatSync(fd, isUserFd) {
const ctx = {};
- const stats = binding.fstat(fd, undefined, ctx);
+ const stats = binding.fstat(fd, false, undefined, ctx);
if (ctx.errno !== undefined && !isUserFd) {
fs.closeSync(fd);
throw errors.uvException(ctx);
@@ -760,55 +760,67 @@ function readdirSync(path, options) {
return result;
}
-function fstat(fd, callback) {
+function fstat(fd, options, callback) {
+ if (arguments.length < 3) {
+ callback = options;
+ options = {};
+ }
validateUint32(fd, 'fd');
- const req = new FSReqWrap();
+ const req = new FSReqWrap(options.bigint);
req.oncomplete = makeStatsCallback(callback);
- binding.fstat(fd, req);
+ binding.fstat(fd, options.bigint, req);
}
-function lstat(path, callback) {
+function lstat(path, options, callback) {
+ if (arguments.length < 3) {
+ callback = options;
+ options = {};
+ }
callback = makeStatsCallback(callback);
path = getPathFromURL(path);
validatePath(path);
- const req = new FSReqWrap();
+ const req = new FSReqWrap(options.bigint);
req.oncomplete = callback;
- binding.lstat(pathModule.toNamespacedPath(path), req);
+ binding.lstat(pathModule.toNamespacedPath(path), options.bigint, req);
}
-function stat(path, callback) {
+function stat(path, options, callback) {
+ if (arguments.length < 3) {
+ callback = options;
+ options = {};
+ }
callback = makeStatsCallback(callback);
path = getPathFromURL(path);
validatePath(path);
- const req = new FSReqWrap();
+ const req = new FSReqWrap(options.bigint);
req.oncomplete = callback;
- binding.stat(pathModule.toNamespacedPath(path), req);
+ binding.stat(pathModule.toNamespacedPath(path), options.bigint, req);
}
-function fstatSync(fd) {
+function fstatSync(fd, options = {}) {
validateUint32(fd, 'fd');
const ctx = { fd };
- const stats = binding.fstat(fd, undefined, ctx);
+ const stats = binding.fstat(fd, options.bigint, undefined, ctx);
handleErrorFromBinding(ctx);
return getStatsFromBinding(stats);
}
-function lstatSync(path) {
+function lstatSync(path, options = {}) {
path = getPathFromURL(path);
validatePath(path);
const ctx = { path };
const stats = binding.lstat(pathModule.toNamespacedPath(path),
- undefined, ctx);
+ options.bigint, undefined, ctx);
handleErrorFromBinding(ctx);
return getStatsFromBinding(stats);
}
-function statSync(path) {
+function statSync(path, options = {}) {
path = getPathFromURL(path);
validatePath(path);
const ctx = { path };
const stats = binding.stat(pathModule.toNamespacedPath(path),
- undefined, ctx);
+ options.bigint, undefined, ctx);
handleErrorFromBinding(ctx);
return getStatsFromBinding(stats);
}
@@ -1264,7 +1276,7 @@ function watchFile(filename, options, listener) {
if (stat === undefined) {
if (!watchers)
watchers = require('internal/fs/watchers');
- stat = new watchers.StatWatcher();
+ stat = new watchers.StatWatcher(options.bigint);
stat.start(filename, options.persistent, options.interval);
statWatchers.set(filename, stat);
}
@@ -1379,7 +1391,7 @@ function realpathSync(p, options) {
// On windows, check that the root exists. On unix there is no need.
if (isWindows && !knownHard[base]) {
const ctx = { path: base };
- binding.lstat(pathModule.toNamespacedPath(base), undefined, ctx);
+ binding.lstat(pathModule.toNamespacedPath(base), false, undefined, ctx);
handleErrorFromBinding(ctx);
knownHard[base] = true;
}
@@ -1421,7 +1433,7 @@ function realpathSync(p, options) {
const baseLong = pathModule.toNamespacedPath(base);
const ctx = { path: base };
- const stats = binding.lstat(baseLong, undefined, ctx);
+ const stats = binding.lstat(baseLong, false, undefined, ctx);
handleErrorFromBinding(ctx);
if (!isFileType(stats, S_IFLNK)) {
@@ -1444,7 +1456,7 @@ function realpathSync(p, options) {
}
if (linkTarget === null) {
const ctx = { path: base };
- binding.stat(baseLong, undefined, ctx);
+ binding.stat(baseLong, false, undefined, ctx);
handleErrorFromBinding(ctx);
linkTarget = binding.readlink(baseLong, undefined, undefined, ctx);
handleErrorFromBinding(ctx);
@@ -1465,7 +1477,7 @@ function realpathSync(p, options) {
// On windows, check that the root exists. On unix there is no need.
if (isWindows && !knownHard[base]) {
const ctx = { path: base };
- binding.lstat(pathModule.toNamespacedPath(base), undefined, ctx);
+ binding.lstat(pathModule.toNamespacedPath(base), false, undefined, ctx);
handleErrorFromBinding(ctx);
knownHard[base] = true;
}
diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js
index 54a69aa026..750d0d7579 100644
--- a/lib/internal/fs/promises.js
+++ b/lib/internal/fs/promises.js
@@ -81,8 +81,8 @@ class FileHandle {
return readFile(this, options);
}
- stat() {
- return fstat(this);
+ stat(options) {
+ return fstat(this, options);
}
truncate(len = 0) {
@@ -106,7 +106,6 @@ class FileHandle {
}
}
-
function validateFileHandle(handle) {
if (!(handle instanceof FileHandle))
throw new ERR_INVALID_ARG_TYPE('filehandle', 'FileHandle', handle);
@@ -127,7 +126,7 @@ async function writeFileHandle(filehandle, data, options) {
}
async function readFileHandle(filehandle, options) {
- const statFields = await binding.fstat(filehandle.fd, kUsePromises);
+ const statFields = await binding.fstat(filehandle.fd, false, kUsePromises);
let size;
if ((statFields[1/* mode */] & S_IFMT) === S_IFREG) {
@@ -318,25 +317,25 @@ async function symlink(target, path, type_) {
kUsePromises);
}
-async function fstat(handle) {
+async function fstat(handle, options = { bigint: false }) {
validateFileHandle(handle);
- const result = await binding.fstat(handle.fd, kUsePromises);
+ const result = await binding.fstat(handle.fd, options.bigint, kUsePromises);
return getStatsFromBinding(result);
}
-async function lstat(path) {
+async function lstat(path, options = { bigint: false }) {
path = getPathFromURL(path);
validatePath(path);
const result = await binding.lstat(pathModule.toNamespacedPath(path),
- kUsePromises);
+ options.bigint, kUsePromises);
return getStatsFromBinding(result);
}
-async function stat(path) {
+async function stat(path, options = { bigint: false }) {
path = getPathFromURL(path);
validatePath(path);
const result = await binding.stat(pathModule.toNamespacedPath(path),
- kUsePromises);
+ options.bigint, kUsePromises);
return getStatsFromBinding(result);
}
diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js
index a8c64e2b04..7092bbcb63 100644
--- a/lib/internal/fs/utils.js
+++ b/lib/internal/fs/utils.js
@@ -114,7 +114,7 @@ function preprocessSymlinkDestination(path, type, linkPath) {
}
function dateFromNumeric(num) {
- return new Date(num + 0.5);
+ return new Date(Number(num) + 0.5);
}
// Constructor for file stats.
@@ -155,7 +155,15 @@ function Stats(
}
Stats.prototype._checkModeProperty = function(property) {
- return ((this.mode & S_IFMT) === 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);
+ }
+ return (this.mode & S_IFMT) === property;
};
Stats.prototype.isDirectory = function() {
@@ -189,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]);
}
diff --git a/lib/internal/fs/watchers.js b/lib/internal/fs/watchers.js
index 1007a5a136..5fd6948f28 100644
--- a/lib/internal/fs/watchers.js
+++ b/lib/internal/fs/watchers.js
@@ -21,10 +21,10 @@ function emitStop(self) {
self.emit('stop');
}
-function StatWatcher() {
+function StatWatcher(bigint) {
EventEmitter.call(this);
- this._handle = new _StatWatcher();
+ this._handle = new _StatWatcher(bigint);
// uv_fs_poll is a little more powerful than ev_stat but we curb it for
// the sake of backwards compatibility