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 31e2167..0ca1c5f 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;
@@ -1714,6 +1712,41 @@ define(function(require) {
});
}
+ function _appendFile(fs, context, path, data, options, callback) {
+ options = validate_file_options(options, 'utf8', 'a');
+
+ 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);
+
+ 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 +2212,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;
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",