From 51c10e05806df5c3d5da850711fc3578a60c53c0 Mon Sep 17 00:00:00 2001 From: pbouianov Date: Thu, 23 Jan 2014 21:56:04 -0500 Subject: [PATCH] added fs.appendFile support --- src/fs.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/fs.js b/src/fs.js index c76e4f5..35575db 100644 --- a/src/fs.js +++ b/src/fs.js @@ -1714,6 +1714,47 @@ define(function(require) { }); } + function _appendFile(fs, context, path, data, options, callback) { + if(!options) { + options = { encoding: 'utf8', flag: 'a' }; + } else if(typeof options === "function") { + options = { encoding: 'utf8', flag: 'a' }; + } else if(typeof options === "string") { + options = { encoding: options, flag: 'a' }; + } + console.log(options); + + if(!nullCheck(path, callback)) return; + + var flags = validate_flags(options.flag || 'a'); + if(!flags) { + callback(new EInvalid('flags is not valid')); + } + + data = data || ''; + if(typeof data === "number") { + data = '' + data; + } + if(typeof data === "string" && options.encoding === 'utf8') { + data = new TextEncoder('utf-8').encode(data); + } + open_file(context, path, flags, function(err, fileNode) { + if(err) { + return callback(err); + } + var ofd = new OpenFileDescription(fileNode.id, flags, fileNode.size); + var fd = fs.allocDescriptor(ofd); + console.log(fileNode); + write_data(context, ofd, data, 0, data.length, ofd.position, function(err2, nbytes) { + if(err2) { + return callback(err2); + } + fs.releaseDescriptor(fd); + callback(null); + }); + }); + } + function _getxattr (context, path, name, callback) { if (!nullCheck(path, callback)) return; @@ -2179,6 +2220,17 @@ define(function(require) { ); if(error) callback(error); }; + FileSystem.prototype.appendFile = function(path, data, options, callback_) { + var callback = maybeCallback(arguments[arguments.length - 1]); + var fs = this; + var error = fs.queueOrRun( + function() { + var context = fs.provider.getReadWriteContext(); + _appendFile(fs, context, path, data, options, callback); + } + ); + if(error) callback(error); + }; FileSystem.prototype.lseek = function(fd, offset, whence, callback) { callback = maybeCallback(callback); var fs = this;