Merge pull request #101 from pbouianov/issue88

added fs.appendFile support
This commit is contained in:
Alan K 2014-02-03 19:50:37 -08:00
commit 4137c12420
4 changed files with 255 additions and 16 deletions

View File

@ -747,7 +747,31 @@ fs.writeFile('/myfile', buffer, function (err) {
#### fs.appendFile(filename, data, [options], callback)<a name="appendFile"></a> #### fs.appendFile(filename, data, [options], callback)<a name="appendFile"></a>
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)<a name="setxattr"></a> #### fs.setxattr(path, name, value, [flag], callback)<a name="setxattr"></a>

View File

@ -1308,6 +1308,17 @@ define(function(require) {
return O_FLAGS[flags]; 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 // nullCheck from https://github.com/joyent/node/blob/master/lib/fs.js
function nullCheck(path, callback) { function nullCheck(path, callback) {
if (('' + path).indexOf('\u0000') !== -1) { if (('' + path).indexOf('\u0000') !== -1) {
@ -1598,13 +1609,7 @@ define(function(require) {
} }
function _readFile(fs, context, path, options, callback) { function _readFile(fs, context, path, options, callback) {
if(!options) { options = validate_file_options(options, null, 'r');
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' };
}
if(!nullCheck(path, callback)) return; if(!nullCheck(path, callback)) return;
@ -1644,7 +1649,6 @@ define(function(require) {
callback(null, data); callback(null, data);
}); });
}); });
}); });
} }
@ -1674,13 +1678,7 @@ define(function(require) {
} }
function _writeFile(fs, context, path, data, options, callback) { function _writeFile(fs, context, path, data, options, callback) {
if(!options) { options = validate_file_options(options, 'utf8', 'w');
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' };
}
if(!nullCheck(path, callback)) return; 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) { function _getxattr (context, path, name, callback) {
if (!nullCheck(path, callback)) return; if (!nullCheck(path, callback)) return;
@ -2179,6 +2212,17 @@ define(function(require) {
); );
if(error) callback(error); 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) { FileSystem.prototype.lseek = function(fd, offset, whence, callback) {
callback = maybeCallback(callback); callback = maybeCallback(callback);
var fs = this; var fs = this;

View File

@ -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);
});
});
});
});

View File

@ -18,6 +18,7 @@ define([
"spec/fs.open.spec", "spec/fs.open.spec",
"spec/fs.write.spec", "spec/fs.write.spec",
"spec/fs.writeFile-readFile.spec", "spec/fs.writeFile-readFile.spec",
"spec/fs.appendFile.spec",
"spec/fs.read.spec", "spec/fs.read.spec",
"spec/fs.close.spec", "spec/fs.close.spec",
"spec/fs.link.spec", "spec/fs.link.spec",