lib: mask mode_t type of arguments with 0o777 · nodejs/node@2fe88d2
@@ -65,7 +65,6 @@ const internalUtil = require('internal/util');
6565const {
6666 copyObject,
6767 getOptions,
68- modeNum,
6968 nullCheck,
7069 preprocessSymlinkDestination,
7170 Stats,
@@ -85,6 +84,7 @@ const {
8584} = require('internal/constants');
8685const {
8786 isUint32,
87+ validateAndMaskMode,
8888 validateInteger,
8989 validateUint32
9090} = require('internal/validators');
@@ -549,32 +549,36 @@ fs.closeSync = function(fd) {
549549handleErrorFromBinding(ctx);
550550};
551551552-fs.open = function(path, flags, mode, callback_) {
553-var callback = makeCallback(arguments[arguments.length - 1]);
554-mode = modeNum(mode, 0o666);
555-552+fs.open = function(path, flags, mode, callback) {
556553path = getPathFromURL(path);
557554validatePath(path);
558-validateUint32(mode, 'mode');
555+const flagsNumber = stringToFlags(flags);
556+if (arguments.length < 4) {
557+callback = makeCallback(mode);
558+mode = 0o666;
559+} else {
560+mode = validateAndMaskMode(mode, 'mode', 0o666);
561+callback = makeCallback(callback);
562+}
559563560564const req = new FSReqWrap();
561565req.oncomplete = callback;
562566563567binding.open(pathModule.toNamespacedPath(path),
564-stringToFlags(flags),
568+flagsNumber,
565569mode,
566570req);
567571};
568572569573fs.openSync = function(path, flags, mode) {
570-mode = modeNum(mode, 0o666);
571574path = getPathFromURL(path);
572575validatePath(path);
573-validateUint32(mode, 'mode');
576+const flagsNumber = stringToFlags(flags);
577+mode = validateAndMaskMode(mode, 'mode', 0o666);
574578575579const ctx = { path };
576580const result = binding.open(pathModule.toNamespacedPath(path),
577-stringToFlags(flags), mode,
581+flagsNumber, mode,
578582undefined, ctx);
579583handleErrorFromBinding(ctx);
580584return result;
@@ -849,12 +853,16 @@ fs.fsyncSync = function(fd) {
849853};
850854851855fs.mkdir = function(path, mode, callback) {
852-if (typeof mode === 'function') callback = mode;
853-callback = makeCallback(callback);
854856path = getPathFromURL(path);
855857validatePath(path);
856-mode = modeNum(mode, 0o777);
857-validateUint32(mode, 'mode');
858+859+if (arguments.length < 3) {
860+callback = makeCallback(mode);
861+mode = 0o777;
862+} else {
863+callback = makeCallback(callback);
864+mode = validateAndMaskMode(mode, 'mode', 0o777);
865+}
858866859867const req = new FSReqWrap();
860868req.oncomplete = callback;
@@ -864,8 +872,7 @@ fs.mkdir = function(path, mode, callback) {
864872fs.mkdirSync = function(path, mode) {
865873path = getPathFromURL(path);
866874validatePath(path);
867-mode = modeNum(mode, 0o777);
868-validateUint32(mode, 'mode');
875+mode = validateAndMaskMode(mode, 'mode', 0o777);
869876const ctx = { path };
870877binding.mkdir(pathModule.toNamespacedPath(path), mode, undefined, ctx);
871878handleErrorFromBinding(ctx);
@@ -1047,25 +1054,18 @@ fs.unlinkSync = function(path) {
10471054};
1048105510491056fs.fchmod = function(fd, mode, callback) {
1050-mode = modeNum(mode);
10511057validateUint32(fd, 'fd');
1052-validateUint32(mode, 'mode');
1053-// Values for mode < 0 are already checked via the validateUint32 function
1054-if (mode > 0o777)
1055-throw new ERR_OUT_OF_RANGE('mode', undefined, mode);
1058+mode = validateAndMaskMode(mode, 'mode');
1059+callback = makeCallback(callback);
1056106010571061const req = new FSReqWrap();
1058-req.oncomplete = makeCallback(callback);
1062+req.oncomplete = callback;
10591063binding.fchmod(fd, mode, req);
10601064};
1061106510621066fs.fchmodSync = function(fd, mode) {
1063-mode = modeNum(mode);
10641067validateUint32(fd, 'fd');
1065-validateUint32(mode, 'mode');
1066-// Values for mode < 0 are already checked via the validateUint32 function
1067-if (mode > 0o777)
1068-throw new ERR_OUT_OF_RANGE('mode', undefined, mode);
1068+mode = validateAndMaskMode(mode, 'mode');
10691069const ctx = {};
10701070binding.fchmod(fd, mode, undefined, ctx);
10711071handleErrorFromBinding(ctx);
@@ -1106,11 +1106,10 @@ if (O_SYMLINK !== undefined) {
110611061107110711081108fs.chmod = function(path, mode, callback) {
1109-callback = makeCallback(callback);
11101109path = getPathFromURL(path);
11111110validatePath(path);
1112-mode = modeNum(mode);
1113-validateUint32(mode, 'mode');
1111+mode = validateAndMaskMode(mode, 'mode');
1112+callback = makeCallback(callback);
1114111311151114const req = new FSReqWrap();
11161115req.oncomplete = callback;
@@ -1120,8 +1119,8 @@ fs.chmod = function(path, mode, callback) {
11201119fs.chmodSync = function(path, mode) {
11211120path = getPathFromURL(path);
11221121validatePath(path);
1123-mode = modeNum(mode);
1124- validateUint32(mode, 'mode');
1122+mode = validateAndMaskMode(mode, 'mode');
1123+11251124const ctx = { path };
11261125binding.chmod(pathModule.toNamespacedPath(path), mode, undefined, ctx);
11271126handleErrorFromBinding(ctx);