Skip to content
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: fix and cleanup constants #20765

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 37 additions & 22 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,31 @@

'use strict';

const constants = process.binding('constants').fs;
const { S_IFIFO, S_IFLNK, S_IFMT, S_IFREG, S_IFSOCK } = constants;
const { fs: constants } = process.binding('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 pathModule = require('path');
const { isUint8Array } = require('internal/util/types');
const { createPromise, promiseResolve } = process.binding('util');

const binding = process.binding('fs');
const fs = exports;
const { Buffer } = require('buffer');
const { Buffer, kMaxLength } = require('buffer');
const errors = require('internal/errors');
const {
ERR_FS_FILE_TOO_LARGE,
Expand All @@ -57,6 +72,7 @@ const {
preprocessSymlinkDestination,
Stats,
getStatsFromBinding,
realpathCacheKey,
stringToFlags,
stringToSymlinkType,
toUnixTimestamp,
Expand Down Expand Up @@ -105,7 +121,6 @@ function lazyAssert() {
}

const kMinPoolSpace = 128;
const { kMaxLength } = require('buffer');

const isWindows = process.platform === 'win32';

Expand Down Expand Up @@ -181,16 +196,16 @@ function isFileType(stats, fileType) {

// Don't allow mode to accidentally be overwritten.
Object.defineProperties(fs, {
F_OK: { enumerable: true, value: constants.F_OK || 0 },
R_OK: { enumerable: true, value: constants.R_OK || 0 },
W_OK: { enumerable: true, value: constants.W_OK || 0 },
X_OK: { enumerable: true, value: constants.X_OK || 0 },
F_OK: { enumerable: true, value: F_OK || 0 },
R_OK: { enumerable: true, value: R_OK || 0 },
W_OK: { enumerable: true, value: W_OK || 0 },
X_OK: { enumerable: true, value: X_OK || 0 },
});

fs.access = function(path, mode, callback) {
if (typeof mode === 'function') {
callback = mode;
mode = fs.F_OK;
mode = F_OK;
}

path = getPathFromURL(path);
Expand All @@ -207,7 +222,7 @@ fs.accessSync = function(path, mode) {
validatePath(path);

if (mode === undefined)
mode = fs.F_OK;
mode = F_OK;
else
mode = mode | 0;

Expand All @@ -224,7 +239,7 @@ fs.exists = function(path, callback) {
}

try {
fs.access(path, fs.FS_OK, suppressedCallback);
fs.access(path, F_OK, suppressedCallback);
} catch (err) {
return callback(false);
}
Expand All @@ -246,7 +261,7 @@ Object.defineProperty(fs.exists, internalUtil.promisify.custom, {
// TODO(joyeecheung): deprecate the never-throw-on-invalid-arguments behavior
fs.existsSync = function(path) {
try {
fs.accessSync(path, fs.FS_OK);
fs.accessSync(path, F_OK);
return true;
} catch (e) {
return false;
Expand Down Expand Up @@ -1056,10 +1071,10 @@ fs.fchmodSync = function(fd, mode) {
handleErrorFromBinding(ctx);
};

if (constants.O_SYMLINK !== undefined) {
if (O_SYMLINK !== undefined) {
fs.lchmod = function(path, mode, 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) {
callback(err);
return;
Expand All @@ -1075,7 +1090,7 @@ if (constants.O_SYMLINK !== undefined) {
};

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,
// but still try to close, and report closing errors if they occur.
Expand Down Expand Up @@ -1112,10 +1127,10 @@ fs.chmodSync = function(path, mode) {
handleErrorFromBinding(ctx);
};

if (constants.O_SYMLINK !== undefined) {
if (O_SYMLINK !== undefined) {
fs.lchown = function(path, uid, gid, 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) {
callback(err);
return;
Expand All @@ -1131,7 +1146,7 @@ if (constants.O_SYMLINK !== undefined) {
};

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;
try {
ret = fs.fchownSync(fd, uid, gid);
Expand Down Expand Up @@ -1633,7 +1648,7 @@ fs.realpathSync = function realpathSync(p, options) {
validatePath(p);
p = pathModule.resolve(p);

const cache = options[internalFS.realpathCacheKey];
const cache = options[realpathCacheKey];
const maybeCachedResult = cache && cache.get(p);
if (maybeCachedResult) {
return maybeCachedResult;
Expand Down Expand Up @@ -1938,14 +1953,14 @@ fs.mkdtempSync = function(prefix, options) {

// Define copyFile() flags.
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: {
enumerable: true,
value: constants.UV_FS_COPYFILE_FICLONE
value: UV_FS_COPYFILE_FICLONE
},
COPYFILE_FICLONE_FORCE: {
enumerable: true,
value: constants.UV_FS_COPYFILE_FICLONE_FORCE
value: UV_FS_COPYFILE_FICLONE_FORCE
}
});

Expand Down