Skip to content

Commit 8b98096

Browse files
JacksonTiancjihrig
authored andcommitted
fs: make fs.access() flags read only
These flags were defined as constants, but could be overwritten when exported from fs. This commit exports the flags as read only properties of fs. PR-URL: #507 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 804e7aa commit 8b98096

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

lib/fs.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ const O_RDWR = constants.O_RDWR || 0;
2727
const O_SYNC = constants.O_SYNC || 0;
2828
const O_TRUNC = constants.O_TRUNC || 0;
2929
const O_WRONLY = constants.O_WRONLY || 0;
30-
const F_OK = constants.F_OK || 0;
31-
const R_OK = constants.R_OK || 0;
32-
const W_OK = constants.W_OK || 0;
33-
const X_OK = constants.X_OK || 0;
3430

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

@@ -164,18 +160,20 @@ fs.Stats.prototype.isSocket = function() {
164160
return this._checkModeProperty(constants.S_IFSOCK);
165161
};
166162

167-
fs.F_OK = F_OK;
168-
fs.R_OK = R_OK;
169-
fs.W_OK = W_OK;
170-
fs.X_OK = X_OK;
163+
// Don't allow mode to accidentally be overwritten.
164+
['F_OK', 'R_OK', 'W_OK', 'X_OK'].forEach(function(key) {
165+
Object.defineProperty(fs, key, {
166+
enumerable: true, value: constants[key] || 0, writable: false
167+
});
168+
});
171169

172170
fs.access = function(path, mode, callback) {
173171
if (!nullCheck(path, callback))
174172
return;
175173

176174
if (typeof mode === 'function') {
177175
callback = mode;
178-
mode = F_OK;
176+
mode = fs.F_OK;
179177
} else if (typeof callback !== 'function') {
180178
throw new TypeError('callback must be a function');
181179
}
@@ -190,7 +188,7 @@ fs.accessSync = function(path, mode) {
190188
nullCheck(path);
191189

192190
if (mode === undefined)
193-
mode = F_OK;
191+
mode = fs.F_OK;
194192
else
195193
mode = mode | 0;
196194

0 commit comments

Comments
 (0)