Skip to content

Commit f8824d6

Browse files
committed
fs: use ES6 octal literals for mode
Update docs, comments and code to use ES6 octal literals instead of decimal + comment.
1 parent 0526d83 commit f8824d6

File tree

2 files changed

+23
-26
lines changed

2 files changed

+23
-26
lines changed

doc/api/fs.markdown

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ Synchronous rmdir(2).
270270
## fs.mkdir(path[, mode], callback)
271271

272272
Asynchronous mkdir(2). No arguments other than a possible exception are given
273-
to the completion callback. `mode` defaults to `0777`.
273+
to the completion callback. `mode` defaults to `0o777`.
274274

275275
## fs.mkdirSync(path[, mode])
276276

@@ -486,7 +486,7 @@ string. Otherwise it returns a buffer.
486486
* `data` {String | Buffer}
487487
* `options` {Object}
488488
* `encoding` {String | Null} default = `'utf8'`
489-
* `mode` {Number} default = `438` (aka `0666` in Octal)
489+
* `mode` {Number} default = `0o666`
490490
* `flag` {String} default = `'w'`
491491
* `callback` {Function}
492492

@@ -513,7 +513,7 @@ The synchronous version of `fs.writeFile`.
513513
* `data` {String | Buffer}
514514
* `options` {Object}
515515
* `encoding` {String | Null} default = `'utf8'`
516-
* `mode` {Number} default = `438` (aka `0666` in Octal)
516+
* `mode` {Number} default = `0o666`
517517
* `flag` {String} default = `'a'`
518518
* `callback` {Function}
519519

@@ -770,7 +770,7 @@ Returns a new ReadStream object (See `Readable Stream`).
770770
{ flags: 'r',
771771
encoding: null,
772772
fd: null,
773-
mode: 0666,
773+
mode: 0o666,
774774
autoClose: true
775775
}
776776

@@ -812,7 +812,7 @@ Returns a new WriteStream object (See `Writable Stream`).
812812
{ flags: 'w',
813813
encoding: null,
814814
fd: null,
815-
mode: 0666 }
815+
mode: 0o666 }
816816

817817
`options` may also include a `start` option to allow writing data at
818818
some position past the beginning of the file. Modifying a file rather

lib/fs.js

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22-
// Maintainers, keep in mind that octal literals are not allowed
23-
// in strict mode. Use the decimal value and add a comment with
24-
// the octal value. Example:
25-
//
26-
// var mode = 438; /* mode=0666 */
22+
// Maintainers, keep in mind that ES1-style octal literals (`0666`) are not
23+
// allowed in strict mode. Use ES6-style octal literals instead (`0o666`).
2724

2825
'use strict';
2926

@@ -262,7 +259,7 @@ fs.readFile = function(path, options, callback_) {
262259
var fd;
263260

264261
var flag = options.flag || 'r';
265-
fs.open(path, flag, 438 /*=0666*/, function(er, fd_) {
262+
fs.open(path, flag, 0o666, function(er, fd_) {
266263
if (er) return callback(er);
267264
fd = fd_;
268265

@@ -352,7 +349,7 @@ fs.readFileSync = function(path, options) {
352349
assertEncoding(encoding);
353350

354351
var flag = options.flag || 'r';
355-
var fd = fs.openSync(path, flag, 438 /*=0666*/);
352+
var fd = fs.openSync(path, flag, 0o666);
356353

357354
var size;
358355
var threw = true;
@@ -484,7 +481,7 @@ function modeNum(m, def) {
484481

485482
fs.open = function(path, flags, mode, callback) {
486483
callback = makeCallback(arguments[arguments.length - 1]);
487-
mode = modeNum(mode, 438 /*=0666*/);
484+
mode = modeNum(mode, 0o666);
488485

489486
if (!nullCheck(path, callback)) return;
490487

@@ -498,7 +495,7 @@ fs.open = function(path, flags, mode, callback) {
498495
};
499496

500497
fs.openSync = function(path, flags, mode) {
501-
mode = modeNum(mode, 438 /*=0666*/);
498+
mode = modeNum(mode, 0o666);
502499
nullCheck(path);
503500
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
504501
};
@@ -743,14 +740,14 @@ fs.mkdir = function(path, mode, callback) {
743740
var req = new FSReqWrap();
744741
req.oncomplete = callback;
745742
binding.mkdir(pathModule._makeLong(path),
746-
modeNum(mode, 511 /*=0777*/),
743+
modeNum(mode, 0o777),
747744
req);
748745
};
749746

750747
fs.mkdirSync = function(path, mode) {
751748
nullCheck(path);
752749
return binding.mkdir(pathModule._makeLong(path),
753-
modeNum(mode, 511 /*=0777*/));
750+
modeNum(mode, 0o777));
754751
};
755752

756753
fs.readdir = function(path, callback) {
@@ -1069,9 +1066,9 @@ fs.writeFile = function(path, data, options, callback) {
10691066
var callback = maybeCallback(arguments[arguments.length - 1]);
10701067

10711068
if (util.isFunction(options) || !options) {
1072-
options = { encoding: 'utf8', mode: 438 /*=0666*/, flag: 'w' };
1069+
options = { encoding: 'utf8', mode: 0o666, flag: 'w' };
10731070
} else if (util.isString(options)) {
1074-
options = { encoding: options, mode: 438, flag: 'w' };
1071+
options = { encoding: options, mode: 0o666, flag: 'w' };
10751072
} else if (!util.isObject(options)) {
10761073
throw new TypeError('Bad arguments');
10771074
}
@@ -1093,9 +1090,9 @@ fs.writeFile = function(path, data, options, callback) {
10931090

10941091
fs.writeFileSync = function(path, data, options) {
10951092
if (!options) {
1096-
options = { encoding: 'utf8', mode: 438 /*=0666*/, flag: 'w' };
1093+
options = { encoding: 'utf8', mode: 0o666, flag: 'w' };
10971094
} else if (util.isString(options)) {
1098-
options = { encoding: options, mode: 438, flag: 'w' };
1095+
options = { encoding: options, mode: 0o666, flag: 'w' };
10991096
} else if (!util.isObject(options)) {
11001097
throw new TypeError('Bad arguments');
11011098
}
@@ -1124,9 +1121,9 @@ fs.appendFile = function(path, data, options, callback_) {
11241121
var callback = maybeCallback(arguments[arguments.length - 1]);
11251122

11261123
if (util.isFunction(options) || !options) {
1127-
options = { encoding: 'utf8', mode: 438 /*=0666*/, flag: 'a' };
1124+
options = { encoding: 'utf8', mode: 0o666, flag: 'a' };
11281125
} else if (util.isString(options)) {
1129-
options = { encoding: options, mode: 438, flag: 'a' };
1126+
options = { encoding: options, mode: 0o666, flag: 'a' };
11301127
} else if (!util.isObject(options)) {
11311128
throw new TypeError('Bad arguments');
11321129
}
@@ -1138,9 +1135,9 @@ fs.appendFile = function(path, data, options, callback_) {
11381135

11391136
fs.appendFileSync = function(path, data, options) {
11401137
if (!options) {
1141-
options = { encoding: 'utf8', mode: 438 /*=0666*/, flag: 'a' };
1138+
options = { encoding: 'utf8', mode: 0o666, flag: 'a' };
11421139
} else if (util.isString(options)) {
1143-
options = { encoding: options, mode: 438, flag: 'a' };
1140+
options = { encoding: options, mode: 0o666, flag: 'a' };
11441141
} else if (!util.isObject(options)) {
11451142
throw new TypeError('Bad arguments');
11461143
}
@@ -1578,7 +1575,7 @@ function ReadStream(path, options) {
15781575
this.path = path;
15791576
this.fd = options.hasOwnProperty('fd') ? options.fd : null;
15801577
this.flags = options.hasOwnProperty('flags') ? options.flags : 'r';
1581-
this.mode = options.hasOwnProperty('mode') ? options.mode : 438; /*=0666*/
1578+
this.mode = options.hasOwnProperty('mode') ? options.mode : 0o666;
15821579

15831580
this.start = options.hasOwnProperty('start') ? options.start : undefined;
15841581
this.end = options.hasOwnProperty('end') ? options.end : undefined;
@@ -1746,7 +1743,7 @@ function WriteStream(path, options) {
17461743

17471744
this.fd = options.hasOwnProperty('fd') ? options.fd : null;
17481745
this.flags = options.hasOwnProperty('flags') ? options.flags : 'w';
1749-
this.mode = options.hasOwnProperty('mode') ? options.mode : 438; /*=0666*/
1746+
this.mode = options.hasOwnProperty('mode') ? options.mode : 0o666;
17501747

17511748
this.start = options.hasOwnProperty('start') ? options.start : undefined;
17521749
this.pos = undefined;

0 commit comments

Comments
 (0)