summaryrefslogtreecommitdiff
path: root/lib/fs.js
diff options
context:
space:
mode:
authorisaacs <i@izs.me>2013-03-01 09:10:26 -0800
committerisaacs <i@izs.me>2013-03-01 09:48:57 -0800
commit0928a526dd5e15a4792ad8186fc85a9164ff6493 (patch)
tree61688e1d43494d69ba512707789c7b60342c6cd0 /lib/fs.js
parent55aa973bee095d5bd01922cad0bd05305fb5fbad (diff)
downloadnodejs-0928a526dd5e15a4792ad8186fc85a9164ff6493.tar.gz
nodejs-0928a526dd5e15a4792ad8186fc85a9164ff6493.tar.bz2
nodejs-0928a526dd5e15a4792ad8186fc85a9164ff6493.zip
fs: Support mode/flag options to read/append/writeFile
Fix #4841
Diffstat (limited to 'lib/fs.js')
-rw-r--r--lib/fs.js128
1 files changed, 85 insertions, 43 deletions
diff --git a/lib/fs.js b/lib/fs.js
index a1bc487b3..1ad0b2e3c 100644
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -165,10 +165,20 @@ fs.existsSync = function(path) {
}
};
-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.
@@ -178,7 +188,8 @@ fs.readFile = function(path, encoding_) {
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_;
@@ -243,10 +254,20 @@ fs.readFile = function(path, encoding_) {
}
};
-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;
@@ -888,72 +909,93 @@ function writeAll(fd, buffer, offset, length, position, callback) {
});
}
-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) {