fs: consistent constants use and cleanup

* only require('buffer') once
* use constants directly
* fix incorrect constants

PR-URL: https://github.com/nodejs/node/pull/20765
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ron Korving <ron@ronkorving.nl>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
James M Snell 2018-05-14 16:36:37 -07:00
parent c8e7f8f77e
commit 1d3759a33c

View File

@ -24,8 +24,23 @@
'use strict'; 'use strict';
const constants = process.binding('constants').fs; const { fs: constants } = process.binding('constants');
const { S_IFIFO, S_IFLNK, S_IFMT, S_IFREG, S_IFSOCK } = constants; const {
S_IFIFO,
S_IFLNK,
S_IFMT,
S_IFREG,
S_IFSOCK,
F_OK,
R_OK,
W_OK,
X_OK,
O_WRONLY,
O_SYMLINK,
UV_FS_COPYFILE_EXCL,
UV_FS_COPYFILE_FICLONE,
UV_FS_COPYFILE_FICLONE_FORCE
} = constants;
const util = require('util'); const util = require('util');
const pathModule = require('path'); const pathModule = require('path');
const { isUint8Array } = require('internal/util/types'); const { isUint8Array } = require('internal/util/types');
@ -33,7 +48,7 @@ const { createPromise, promiseResolve } = process.binding('util');
const binding = process.binding('fs'); const binding = process.binding('fs');
const fs = exports; const fs = exports;
const { Buffer } = require('buffer'); const { Buffer, kMaxLength } = require('buffer');
const errors = require('internal/errors'); const errors = require('internal/errors');
const { const {
ERR_FS_FILE_TOO_LARGE, ERR_FS_FILE_TOO_LARGE,
@ -56,6 +71,7 @@ const {
preprocessSymlinkDestination, preprocessSymlinkDestination,
Stats, Stats,
getStatsFromBinding, getStatsFromBinding,
realpathCacheKey,
stringToFlags, stringToFlags,
stringToSymlinkType, stringToSymlinkType,
toUnixTimestamp, toUnixTimestamp,
@ -105,7 +121,6 @@ function lazyAssert() {
} }
const kMinPoolSpace = 128; const kMinPoolSpace = 128;
const { kMaxLength } = require('buffer');
const isWindows = process.platform === 'win32'; const isWindows = process.platform === 'win32';
@ -181,16 +196,16 @@ function isFileType(stats, fileType) {
// Don't allow mode to accidentally be overwritten. // Don't allow mode to accidentally be overwritten.
Object.defineProperties(fs, { Object.defineProperties(fs, {
F_OK: { enumerable: true, value: constants.F_OK || 0 }, F_OK: { enumerable: true, value: F_OK || 0 },
R_OK: { enumerable: true, value: constants.R_OK || 0 }, R_OK: { enumerable: true, value: R_OK || 0 },
W_OK: { enumerable: true, value: constants.W_OK || 0 }, W_OK: { enumerable: true, value: W_OK || 0 },
X_OK: { enumerable: true, value: constants.X_OK || 0 }, X_OK: { enumerable: true, value: X_OK || 0 },
}); });
fs.access = function(path, mode, callback) { fs.access = function(path, mode, callback) {
if (typeof mode === 'function') { if (typeof mode === 'function') {
callback = mode; callback = mode;
mode = fs.F_OK; mode = F_OK;
} }
path = getPathFromURL(path); path = getPathFromURL(path);
@ -207,7 +222,7 @@ fs.accessSync = function(path, mode) {
validatePath(path); validatePath(path);
if (mode === undefined) if (mode === undefined)
mode = fs.F_OK; mode = F_OK;
else else
mode = mode | 0; mode = mode | 0;
@ -224,7 +239,7 @@ fs.exists = function(path, callback) {
} }
try { try {
fs.access(path, fs.FS_OK, suppressedCallback); fs.access(path, F_OK, suppressedCallback);
} catch (err) { } catch (err) {
return callback(false); return callback(false);
} }
@ -246,7 +261,7 @@ Object.defineProperty(fs.exists, internalUtil.promisify.custom, {
// TODO(joyeecheung): deprecate the never-throw-on-invalid-arguments behavior // TODO(joyeecheung): deprecate the never-throw-on-invalid-arguments behavior
fs.existsSync = function(path) { fs.existsSync = function(path) {
try { try {
fs.accessSync(path, fs.FS_OK); fs.accessSync(path, F_OK);
return true; return true;
} catch (e) { } catch (e) {
return false; return false;
@ -1056,10 +1071,10 @@ fs.fchmodSync = function(fd, mode) {
handleErrorFromBinding(ctx); handleErrorFromBinding(ctx);
}; };
if (constants.O_SYMLINK !== undefined) { if (O_SYMLINK !== undefined) {
fs.lchmod = function(path, mode, callback) { fs.lchmod = function(path, mode, callback) {
callback = maybeCallback(callback); callback = maybeCallback(callback);
fs.open(path, constants.O_WRONLY | constants.O_SYMLINK, function(err, fd) { fs.open(path, O_WRONLY | O_SYMLINK, function(err, fd) {
if (err) { if (err) {
callback(err); callback(err);
return; return;
@ -1075,7 +1090,7 @@ if (constants.O_SYMLINK !== undefined) {
}; };
fs.lchmodSync = function(path, mode) { fs.lchmodSync = function(path, mode) {
const fd = fs.openSync(path, constants.O_WRONLY | constants.O_SYMLINK); const fd = fs.openSync(path, O_WRONLY | O_SYMLINK);
// Prefer to return the chmod error, if one occurs, // Prefer to return the chmod error, if one occurs,
// but still try to close, and report closing errors if they occur. // but still try to close, and report closing errors if they occur.
@ -1111,10 +1126,10 @@ fs.chmodSync = function(path, mode) {
handleErrorFromBinding(ctx); handleErrorFromBinding(ctx);
}; };
if (constants.O_SYMLINK !== undefined) { if (O_SYMLINK !== undefined) {
fs.lchown = function(path, uid, gid, callback) { fs.lchown = function(path, uid, gid, callback) {
callback = maybeCallback(callback); callback = maybeCallback(callback);
fs.open(path, constants.O_WRONLY | constants.O_SYMLINK, function(err, fd) { fs.open(path, O_WRONLY | O_SYMLINK, function(err, fd) {
if (err) { if (err) {
callback(err); callback(err);
return; return;
@ -1130,7 +1145,7 @@ if (constants.O_SYMLINK !== undefined) {
}; };
fs.lchownSync = function(path, uid, gid) { fs.lchownSync = function(path, uid, gid) {
const fd = fs.openSync(path, constants.O_WRONLY | constants.O_SYMLINK); const fd = fs.openSync(path, O_WRONLY | O_SYMLINK);
let ret; let ret;
try { try {
ret = fs.fchownSync(fd, uid, gid); ret = fs.fchownSync(fd, uid, gid);
@ -1632,7 +1647,7 @@ fs.realpathSync = function realpathSync(p, options) {
validatePath(p); validatePath(p);
p = pathModule.resolve(p); p = pathModule.resolve(p);
const cache = options[internalFS.realpathCacheKey]; const cache = options[realpathCacheKey];
const maybeCachedResult = cache && cache.get(p); const maybeCachedResult = cache && cache.get(p);
if (maybeCachedResult) { if (maybeCachedResult) {
return maybeCachedResult; return maybeCachedResult;
@ -1937,14 +1952,14 @@ fs.mkdtempSync = function(prefix, options) {
// Define copyFile() flags. // Define copyFile() flags.
Object.defineProperties(fs.constants, { Object.defineProperties(fs.constants, {
COPYFILE_EXCL: { enumerable: true, value: constants.UV_FS_COPYFILE_EXCL }, COPYFILE_EXCL: { enumerable: true, value: UV_FS_COPYFILE_EXCL },
COPYFILE_FICLONE: { COPYFILE_FICLONE: {
enumerable: true, enumerable: true,
value: constants.UV_FS_COPYFILE_FICLONE value: UV_FS_COPYFILE_FICLONE
}, },
COPYFILE_FICLONE_FORCE: { COPYFILE_FICLONE_FORCE: {
enumerable: true, enumerable: true,
value: constants.UV_FS_COPYFILE_FICLONE_FORCE value: UV_FS_COPYFILE_FICLONE_FORCE
} }
}); });