From 51c10e05806df5c3d5da850711fc3578a60c53c0 Mon Sep 17 00:00:00 2001 From: pbouianov Date: Thu, 23 Jan 2014 21:56:04 -0500 Subject: [PATCH 1/2] 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; From 6ec635e55137723acb7f60d52c0c11a056ed5b9b Mon Sep 17 00:00:00 2001 From: pbouianov Date: Thu, 30 Jan 2014 14:12:32 -0500 Subject: [PATCH 2/2] fixed, added appendFile tests and updated README --- README.md | 26 ++++- src/fs.js | 42 ++++---- tests/spec/fs.appendFile.spec.js | 170 +++++++++++++++++++++++++++++++ tests/test-manifest.js | 1 + 4 files changed, 213 insertions(+), 26 deletions(-) create mode 100644 tests/spec/fs.appendFile.spec.js diff --git a/README.md b/README.md index 81f1229..6aad26e 100644 --- a/README.md +++ b/README.md @@ -747,7 +747,31 @@ fs.writeFile('/myfile', buffer, function (err) { #### fs.appendFile(filename, data, [options], callback) -NOTE: Not yet implemented, see https://github.com/js-platform/filer/issues/88 +Writes data to the end of a file. `data` can be a string or a buffer, in which case any encoding option is ignored. The `options` argument is optional, and can take the form `"utf8"` (i.e., an encoding) or be an object literal: `{ encoding: "utf8", flag: "w" }`. If no encoding is specified, and `data` is a string, the encoding defaults to `'utf8'`. The callback gets `(error)`. + +Examples: + +```javascript +// Append UTF8 text file +fs.writeFile('/myfile.txt', "More...", function (err) { + if (err) throw err; +}); +fs.appendFile('/myfile.txt', "Data...", function (err) { + if (err) throw err; +}); +// '/myfile.txt' would now read out 'More...Data...' + +// Append binary file +var more = new Uint8Array([1, 2, 3, 4]); +var data = new Uint8Array([5, 6, 7, 8]); +fs.writeFile('/myfile', more, function (err) { + if (err) throw err; +}); +fs.appendFile('/myfile', buffer, function (err) { + if (err) throw err; +}); +// '/myfile' would now contain [1, 2, 3, 4, 5, 6, 7, 8] +``` #### fs.setxattr(path, name, value, [flag], callback) diff --git a/src/fs.js b/src/fs.js index 35575db..2d56f50 100644 --- a/src/fs.js +++ b/src/fs.js @@ -1308,6 +1308,17 @@ define(function(require) { return O_FLAGS[flags]; } + function validate_file_options(options, enc, fileMode){ + if(!options) { + options = { encoding: enc, flag: fileMode }; + } else if(typeof options === "function") { + options = { encoding: enc, flag: fileMode }; + } else if(typeof options === "string") { + options = { encoding: options, flag: fileMode }; + } + return options; + } + // nullCheck from https://github.com/joyent/node/blob/master/lib/fs.js function nullCheck(path, callback) { if (('' + path).indexOf('\u0000') !== -1) { @@ -1598,13 +1609,7 @@ define(function(require) { } function _readFile(fs, context, path, options, callback) { - if(!options) { - options = { encoding: null, flag: 'r' }; - } else if(typeof options === "function") { - options = { encoding: null, flag: 'r' }; - } else if(typeof options === "string") { - options = { encoding: options, flag: 'r' }; - } + options = validate_file_options(options, null, 'r'); if(!nullCheck(path, callback)) return; @@ -1644,7 +1649,6 @@ define(function(require) { callback(null, data); }); }); - }); } @@ -1674,13 +1678,7 @@ define(function(require) { } function _writeFile(fs, context, path, data, options, callback) { - if(!options) { - options = { encoding: 'utf8', flag: 'w' }; - } else if(typeof options === "function") { - options = { encoding: 'utf8', flag: 'w' }; - } else if(typeof options === "string") { - options = { encoding: options, flag: 'w' }; - } + options = validate_file_options(options, 'utf8', 'w'); if(!nullCheck(path, callback)) return; @@ -1715,14 +1713,7 @@ 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); + options = validate_file_options(options, 'utf8', 'a'); if(!nullCheck(path, callback)) return; @@ -1738,13 +1729,14 @@ define(function(require) { 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); @@ -1801,7 +1793,7 @@ define(function(require) { else { callback(null); } - }; + } setxattr_file(context, path, name, value, flag, check_result); } diff --git a/tests/spec/fs.appendFile.spec.js b/tests/spec/fs.appendFile.spec.js new file mode 100644 index 0000000..e7187ed --- /dev/null +++ b/tests/spec/fs.appendFile.spec.js @@ -0,0 +1,170 @@ +define(["Filer"], function(Filer) { + + describe('fs.appendFile', function() { + beforeEach(function() { + this.db_name = mk_db_name(); + this.fs = new Filer.FileSystem({ + name: this.db_name, + flags: 'FORMAT' + }); + this.fs.writeFile('/myfile', "This is a file.", { encoding: 'utf8' }, function(error) { + if(error) throw error; + }); + }); + + afterEach(function() { + indexedDB.deleteDatabase(this.db_name); + delete this.fs; + }); + + it('should be a function', function() { + expect(typeof this.fs.appendFile).toEqual('function'); + }); + + it('should append a utf8 file without specifying utf8 in appendFile', function() { + var complete = false; + var _error, _result; + var that = this; + + var contents = "This is a file."; + var more = " Appended."; + + that.fs.appendFile('/myfile', more, function(error) { + if(error) throw error; + }); + that.fs.readFile('/myfile', 'utf8', function(error, data) { + if(error) throw error; + _result = data; + complete = true; + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(_result).toEqual(contents+more); + }); + }); + + it('should append a utf8 file with "utf8" option to appendFile', function() { + var complete = false; + var _error, _result; + var that = this; + + var contents = "This is a file."; + var more = " Appended."; + + that.fs.appendFile('/myfile', more, 'utf8', function(error) { + if(error) throw error; + }); + that.fs.readFile('/myfile', 'utf8', function(error, data) { + if(error) throw error; + _result = data; + complete = true; + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(_result).toEqual(contents+more); + }); + }); + + it('should append a utf8 file with {encoding: "utf8"} option to appendFile', function() { + var complete = false; + var _error, _result; + var that = this; + + var contents = "This is a file."; + var more = " Appended."; + + that.fs.appendFile('/myfile', more, { encoding: 'utf8' }, function(error) { + if(error) throw error; + }); + that.fs.readFile('/myfile', { encoding: 'utf8' }, function(error, data) { + if(error) throw error; + _result = data; + complete = true; + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(_result).toEqual(contents+more); + }); + }); + + it('should append a binary file', function() { + var complete = false; + var _error, _result; + var that = this; + + // String and utf8 binary encoded versions of the same thing: + var contents = "This is a file."; + var binary = new Uint8Array([84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 102, 105, 108, 101, 46]); + var more = " Appended."; + var binary2 = new Uint8Array([32, 65, 112, 112, 101, 110, 100, 101, 100, 46]); + var binary3 = new Uint8Array([84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 102, 105, 108, 101, 46, + 32, 65, 112, 112, 101, 110, 100, 101, 100, 46]); + + that.fs.writeFile('/mybinaryfile', binary, function(error) { + if(error) throw error; + }); + that.fs.appendFile('/mybinaryfile', binary2, function(error) { + if(error) throw error; + }); + that.fs.readFile('/mybinaryfile', 'ascii', function(error, data) { + if(error) throw error; + _result = data; + complete = true; + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(_result).toEqual(binary3); + }); + }); + + it('should follow symbolic links', function () { + var complete = false; + var _result; + var that = this; + + var contents = "This is a file."; + var more = " Appended."; + + that.fs.symlink('/myfile', '/myFileLink', function (error) { + if (error) throw error; + }); + that.fs.appendFile('/myFileLink', more, 'utf8', function (error) { + if (error) throw error; + }); + that.fs.readFile('/myFileLink', 'utf8', function(error, data) { + if(error) throw error; + _result = data; + complete = true; + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_result).toEqual(contents+more); + }); + }); + }); + +}); \ No newline at end of file diff --git a/tests/test-manifest.js b/tests/test-manifest.js index 9b4b87a..43a549b 100644 --- a/tests/test-manifest.js +++ b/tests/test-manifest.js @@ -18,6 +18,7 @@ define([ "spec/fs.open.spec", "spec/fs.write.spec", "spec/fs.writeFile-readFile.spec", + "spec/fs.appendFile.spec", "spec/fs.read.spec", "spec/fs.close.spec", "spec/fs.link.spec",