Synchronous version of `fs.read`. Returns the number of `bytesRead`.
-## fs.readFile(filename, [encoding], [callback])
+## fs.readFile(filename, [options], [callback])
+
+* `filename` {String}
+* `options` {Object}
+ * `encoding` {String | Null} default = `null`
+ * `flag` {String} default = `'r'`
Asynchronously reads the entire contents of a file. Example:
If no encoding is specified, then the raw buffer is returned.
-## fs.readFileSync(filename, [encoding])
+## fs.readFileSync(filename, [options])
Synchronous version of `fs.readFile`. Returns the contents of the `filename`.
-If `encoding` is specified then this function returns a string. Otherwise it
-returns a buffer.
+If the `encoding` option is specified then this function returns a
+string. Otherwise it returns a buffer.
+
+## fs.writeFile(filename, data, [options], [callback])
-## fs.writeFile(filename, data, [encoding], [callback])
+* `filename` {String}
+* `data` {String | Buffer}
+* `options` {Object}
+ * `encoding` {String | Null} default = `'utf8'`
+ * `mode` {Number} default = `438` (aka `0666` in Octal)
+ * `flag` {String} default = `'w'`
Asynchronously writes data to a file, replacing the file if it already exists.
-`data` can be a string or a buffer. The `encoding` argument is ignored if
-`data` is a buffer. It defaults to `'utf8'`.
+`data` can be a string or a buffer.
+
+The `encoding` option is ignored if `data` is a buffer. It defaults
+to `'utf8'`.
Example:
console.log('It\'s saved!');
});
-## fs.writeFileSync(filename, data, [encoding])
+## fs.writeFileSync(filename, data, [options])
The synchronous version of `fs.writeFile`.
-## fs.appendFile(filename, data, encoding='utf8', [callback])
+## fs.appendFile(filename, data, [options], [callback])
+
+* `filename` {String}
+* `data` {String | Buffer}
+* `options` {Object}
+ * `encoding` {String | Null} default = `'utf8'`
+ * `mode` {Number} default = `438` (aka `0666` in Octal)
+ * `flag` {String} default = `'a'`
Asynchronously append data to a file, creating the file if it not yet exists.
-`data` can be a string or a buffer. The `encoding` argument is ignored if
-`data` is a buffer.
+`data` can be a string or a buffer.
Example:
console.log('The "data to append" was appended to file!');
});
-## fs.appendFileSync(filename, data, encoding='utf8')
+## fs.appendFileSync(filename, data, [options])
The synchronous version of `fs.appendFile`.
}
};
-fs.readFile = function(path, encoding_) {
- var encoding = typeof(encoding_) === 'string' ? encoding_ : null;
+fs.readFile = function(path, options, callback_) {
var callback = maybeCallback(arguments[arguments.length - 1]);
+ if (typeof options === 'function' || !options) {
+ options = { encoding: null, flag: 'r' };
+ } else if (typeof options === 'string') {
+ options = { encoding: options, flag: 'r' };
+ } else if (!options) {
+ options = { encoding: null, flag: 'r' };
+ } else if (typeof options !== 'object') {
+ throw new TypeError('Bad arguments');
+ }
+
+ var encoding = options.encoding;
assertEncoding(encoding);
// first, stat the file, so we know the size.
var pos = 0;
var fd;
- fs.open(path, constants.O_RDONLY, 438 /*=0666*/, function(er, fd_) {
+ var flag = options.flag || 'r';
+ fs.open(path, flag, 438 /*=0666*/, function(er, fd_) {
if (er) return callback(er);
fd = fd_;
}
};
-fs.readFileSync = function(path, encoding) {
+fs.readFileSync = function(path, options) {
+ if (!options) {
+ options = { encoding: null, flag: 'r' };
+ } else if (typeof options === 'string') {
+ options = { encoding: options, flag: 'r' };
+ } else if (typeof options !== 'object') {
+ throw new TypeError('Bad arguments');
+ }
+
+ var encoding = options.encoding;
assertEncoding(encoding);
- var fd = fs.openSync(path, constants.O_RDONLY, 438 /*=0666*/);
+ var flag = options.flag || 'r';
+ var fd = fs.openSync(path, flag, 438 /*=0666*/);
var size;
var threw = true;
});
}
-fs.writeFile = function(path, data, encoding_, callback) {
- var encoding = (typeof(encoding_) == 'string' ? encoding_ : 'utf8');
- assertEncoding(encoding);
+fs.writeFile = function(path, data, options, callback) {
+ var callback = maybeCallback(arguments[arguments.length - 1]);
- callback = maybeCallback(arguments[arguments.length - 1]);
- fs.open(path, 'w', 438 /*=0666*/, function(openErr, fd) {
+ if (typeof options === 'function' || !options) {
+ options = { encoding: 'utf8', mode: 438 /*=0666*/, flag: 'w' };
+ } else if (typeof options === 'string') {
+ options = { encoding: options, mode: 438, flag: 'w' };
+ } else if (!options) {
+ options = { encoding: 'utf8', mode: 438 /*=0666*/, flag: 'w' };
+ } else if (typeof options !== 'object') {
+ throw new TypeError('Bad arguments');
+ }
+
+ assertEncoding(options.encoding);
+
+ var flag = options.flag || 'w';
+ fs.open(path, options.flag || 'w', options.mode, function(openErr, fd) {
if (openErr) {
if (callback) callback(openErr);
} else {
var buffer = Buffer.isBuffer(data) ? data : new Buffer('' + data,
- encoding);
- writeAll(fd, buffer, 0, buffer.length, 0, callback);
+ options.encoding || 'utf8');
+ var position = /a/.test(flag) ? null : 0;
+ writeAll(fd, buffer, 0, buffer.length, position, callback);
}
});
};
-fs.writeFileSync = function(path, data, encoding) {
- assertEncoding(encoding);
+fs.writeFileSync = function(path, data, options) {
+ if (!options) {
+ options = { encoding: 'utf8', mode: 438 /*=0666*/, flag: 'w' };
+ } else if (typeof options === 'string') {
+ options = { encoding: options, mode: 438, flag: 'w' };
+ } else if (typeof options !== 'object') {
+ throw new TypeError('Bad arguments');
+ }
- var fd = fs.openSync(path, 'w');
+ assertEncoding(options.encoding);
+
+ var flag = options.flag || 'w';
+ var fd = fs.openSync(path, flag);
if (!Buffer.isBuffer(data)) {
- data = new Buffer('' + data, encoding || 'utf8');
+ data = new Buffer('' + data, options.encoding || 'utf8');
}
var written = 0;
var length = data.length;
+ var position = /a/.test(flag) ? null : 0;
try {
while (written < length) {
- written += fs.writeSync(fd, data, written, length - written, written);
+ written += fs.writeSync(fd, data, written, length - written, position);
+ position += written;
}
} finally {
fs.closeSync(fd);
}
};
-fs.appendFile = function(path, data, encoding_, callback) {
- var encoding = (typeof(encoding_) == 'string' ? encoding_ : 'utf8');
- assertEncoding(encoding);
+fs.appendFile = function(path, data, options, callback_) {
+ var callback = maybeCallback(arguments[arguments.length - 1]);
- callback = maybeCallback(arguments[arguments.length - 1]);
+ if (typeof options === 'function' || !options) {
+ options = { encoding: 'utf8', mode: 438 /*=0666*/, flag: 'a' };
+ } else if (typeof options === 'string') {
+ options = { encoding: options, mode: 438, flag: 'a' };
+ } else if (!options) {
+ options = { encoding: 'utf8', mode: 438 /*=0666*/, flag: 'a' };
+ } else if (typeof options !== 'object') {
+ throw new TypeError('Bad arguments');
+ }
- fs.open(path, 'a', 438 /*=0666*/, function(err, fd) {
- if (err) return callback(err);
- var buffer = Buffer.isBuffer(data) ? data : new Buffer('' + data, encoding);
- writeAll(fd, buffer, 0, buffer.length, null, callback);
- });
+ if (!options.flag)
+ options = util._extend({ flag: 'a' }, options);
+ fs.writeFile(path, data, options, callback);
};
-fs.appendFileSync = function(path, data, encoding) {
- assertEncoding(encoding);
-
- var fd = fs.openSync(path, 'a');
- if (!Buffer.isBuffer(data)) {
- data = new Buffer('' + data, encoding || 'utf8');
+fs.appendFileSync = function(path, data, options) {
+ if (!options) {
+ options = { encoding: 'utf8', mode: 438 /*=0666*/, flag: 'a' };
+ } else if (typeof options === 'string') {
+ options = { encoding: options, mode: 438, flag: 'a' };
+ } else if (typeof options !== 'object') {
+ throw new TypeError('Bad arguments');
}
- var written = 0;
- var position = null;
- var length = data.length;
+ if (!options.flag)
+ options = util._extend({ flag: 'a' }, options);
- try {
- while (written < length) {
- written += fs.writeSync(fd, data, written, length - written, position);
- position += written; // XXX not safe with multiple concurrent writers?
- }
- } finally {
- fs.closeSync(fd);
- }
+ fs.writeFileSync(path, data, options);
};
function errnoException(errorno, syscall) {