fixed, added appendFile tests and updated README

This commit is contained in:
pbouianov 2014-01-30 14:12:32 -05:00
parent 51c10e0580
commit 6ec635e551
4 changed files with 213 additions and 26 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;
@ -1715,14 +1713,7 @@ define(function(require) {
} }
function _appendFile(fs, context, path, data, options, callback) { function _appendFile(fs, context, path, data, options, callback) {
if(!options) { options = validate_file_options(options, 'utf8', 'a');
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; if(!nullCheck(path, callback)) return;
@ -1738,13 +1729,14 @@ define(function(require) {
if(typeof data === "string" && options.encoding === 'utf8') { if(typeof data === "string" && options.encoding === 'utf8') {
data = new TextEncoder('utf-8').encode(data); data = new TextEncoder('utf-8').encode(data);
} }
open_file(context, path, flags, function(err, fileNode) { open_file(context, path, flags, function(err, fileNode) {
if(err) { if(err) {
return callback(err); return callback(err);
} }
var ofd = new OpenFileDescription(fileNode.id, flags, fileNode.size); var ofd = new OpenFileDescription(fileNode.id, flags, fileNode.size);
var fd = fs.allocDescriptor(ofd); var fd = fs.allocDescriptor(ofd);
console.log(fileNode);
write_data(context, ofd, data, 0, data.length, ofd.position, function(err2, nbytes) { write_data(context, ofd, data, 0, data.length, ofd.position, function(err2, nbytes) {
if(err2) { if(err2) {
return callback(err2); return callback(err2);
@ -1801,7 +1793,7 @@ define(function(require) {
else { else {
callback(null); callback(null);
} }
}; }
setxattr_file(context, path, name, value, flag, check_result); setxattr_file(context, path, name, value, flag, check_result);
} }

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",