removed a bunch of promisey code; converted to callbacks

This commit is contained in:
Alan Kligman 2013-11-21 16:15:30 -05:00
parent 612410ae67
commit 291ef6bc94
3 changed files with 107 additions and 409 deletions

259
dist/idbfs.js vendored
View File

@ -8946,12 +8946,11 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
}; };
FileSystem.prototype._open = function _open(context, path, flags, callback) { FileSystem.prototype._open = function _open(context, path, flags, callback) {
var that = this; var that = this;
var deferred = when.defer();
function check_result(error, fileNode) { function check_result(error, fileNode) {
if(error) { if(error) {
// if(transaction.error) transaction.abort(); // if(transaction.error) transaction.abort();
deferred.reject(error); callback(error);
} else { } else {
var position; var position;
if(_(flags).contains(O_APPEND)) { if(_(flags).contains(O_APPEND)) {
@ -8961,200 +8960,119 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
} }
var openFileDescription = new OpenFileDescription(fileNode.id, flags, position); var openFileDescription = new OpenFileDescription(fileNode.id, flags, position);
var fd = that._allocate_descriptor(openFileDescription); var fd = that._allocate_descriptor(openFileDescription);
deferred.resolve(fd); callback(undefined, fd);
} }
} }
flags = validate_flags(flags); flags = validate_flags(flags);
if(!flags) { if(!flags) {
deferred.reject(new EInvalid('flags is not valid')); callback(new EInvalid('flags is not valid'));
} }
open_file(that, context, path, flags, check_result); open_file(that, context, path, flags, check_result);
deferred.promise.then(
function(result) {
callback(undefined, result);
},
function(error) {
callback(error);
}
);
}; };
FileSystem.prototype._close = function _close(fd, callback) { FileSystem.prototype._close = function _close(fd, callback) {
var deferred = when.defer();
if(!_(this.openFiles).has(fd)) { if(!_(this.openFiles).has(fd)) {
deferred.reject(new EBadFileDescriptor('invalid file descriptor')); callback(new EBadFileDescriptor('invalid file descriptor'));
} else { } else {
this._release_descriptor(fd); this._release_descriptor(fd);
deferred.resolve(); callback(undefined);
} }
deferred.promise.then(
function() {
callback();
},
function(error) {
callback(error);
}
);
}; };
FileSystem.prototype._mkdir = function _mkdir(context, path, callback) { FileSystem.prototype._mkdir = function _mkdir(context, path, callback) {
var that = this; var that = this;
var deferred = when.defer();
function check_result(error) { function check_result(error) {
if(error) { if(error) {
// if(transaction.error) transaction.abort(); // if(transaction.error) transaction.abort();
deferred.reject(error); callback(error);
} else { } else {
deferred.resolve(); callback(undefined);
} }
} }
make_directory(context, path, check_result); make_directory(context, path, check_result);
deferred.promise.then(
function() {
callback();
},
function(error) {
callback(error);
}
);
}; };
FileSystem.prototype._rmdir = function _rmdir(context, path, callback) { FileSystem.prototype._rmdir = function _rmdir(context, path, callback) {
var that = this; var that = this;
var deferred = when.defer();
function check_result(error) { function check_result(error) {
if(error) { if(error) {
// if(transaction.error) transaction.abort(); // if(transaction.error) transaction.abort();
deferred.reject(error); callback(error);
} else { } else {
deferred.resolve(); callback(undefined);
} }
} }
remove_directory(context, path, check_result); remove_directory(context, path, check_result);
deferred.promise.then(
function() {
callback();
},
function(error) {
callback(error);
}
);
}; };
FileSystem.prototype._stat = function _stat(context, path, callback) { FileSystem.prototype._stat = function _stat(context, path, callback) {
var that = this; var that = this;
var deferred = when.defer();
function check_result(error, result) { function check_result(error, result) {
if(error) { if(error) {
// if(transaction.error) transaction.abort(); // if(transaction.error) transaction.abort();
deferred.reject(error); callback(error);
} else { } else {
var stats = new Stats(result, that.name); var stats = new Stats(result, that.name);
deferred.resolve(stats); callback(undefined, stats);
} }
} }
stat_file(context, path, check_result); stat_file(context, path, check_result);
deferred.promise.then(
function(result) {
callback(undefined, result);
},
function(error) {
callback(error);
}
);
}; };
FileSystem.prototype._fstat = function _fstat(context, fd, callback) { FileSystem.prototype._fstat = function _fstat(context, fd, callback) {
var that = this; var that = this;
var deferred = when.defer();
function check_result(error, result) { function check_result(error, result) {
if(error) { if(error) {
// if(transaction.error) transaction.abort(); // if(transaction.error) transaction.abort();
deferred.reject(error); callback(error);
} else { } else {
var stats = new Stats(result, that.name); var stats = new Stats(result, that.name);
deferred.resolve(stats); callback(undefined, stats);
} }
} }
var ofd = that.openFiles[fd]; var ofd = that.openFiles[fd];
if(!ofd) { if(!ofd) {
deferred.reject(new EBadFileDescriptor('invalid file descriptor')); callback(new EBadFileDescriptor('invalid file descriptor'));
} else { } else {
fstat_file(context, ofd, check_result); fstat_file(context, ofd, check_result);
} }
deferred.promise.then(
function(result) {
callback(undefined, result);
},
function(error) {
callback(error);
}
);
}; };
FileSystem.prototype._link = function _link(context, oldpath, newpath, callback) { FileSystem.prototype._link = function _link(context, oldpath, newpath, callback) {
var that = this; var that = this;
var deferred = when.defer();
function check_result(error) { function check_result(error) {
if(error) { if(error) {
// if(transaction.error) transaction.abort(); // if(transaction.error) transaction.abort();
deferred.reject(error); callback(error);
} else { } else {
deferred.resolve(); callback(undefined);
} }
} }
link_node(context, oldpath, newpath, check_result); link_node(context, oldpath, newpath, check_result);
deferred.promise.then(
function(result) {
callback();
},
function(error) {
callback(error);
}
);
}; };
FileSystem.prototype._unlink = function _unlink(context, path, callback) { FileSystem.prototype._unlink = function _unlink(context, path, callback) {
var that = this; var that = this;
var deferred = when.defer();
function check_result(error) { function check_result(error) {
if(error) { if(error) {
// if(transaction.error) transaction.abort(); // if(transaction.error) transaction.abort();
deferred.reject(error); callback(error);
} else { } else {
deferred.resolve(); callback(undefined);
} }
} }
unlink_node(context, path, check_result); unlink_node(context, path, check_result);
deferred.promise.then(
function(result) {
callback();
},
function(error) {
callback(error);
}
);
}; };
FileSystem.prototype._read = function _read(context, fd, buffer, offset, length, position, callback) { FileSystem.prototype._read = function _read(context, fd, buffer, offset, length, position, callback) {
var that = this; var that = this;
var deferred = when.defer();
offset = (undefined === offset) ? 0 : offset; offset = (undefined === offset) ? 0 : offset;
length = (undefined === length) ? buffer.length - offset : length; length = (undefined === length) ? buffer.length - offset : length;
@ -9162,34 +9080,24 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
function check_result(error, nbytes) { function check_result(error, nbytes) {
if(error) { if(error) {
// if(transaction.error) transaction.abort(); // if(transaction.error) transaction.abort();
deferred.reject(error); callback(error);
} else { } else {
deferred.resolve(nbytes); callback(undefined, nbytes);
} }
} }
var ofd = that.openFiles[fd]; var ofd = that.openFiles[fd];
if(!ofd) { if(!ofd) {
deferred.reject(new EBadFileDescriptor('invalid file descriptor')); callback(new EBadFileDescriptor('invalid file descriptor'));
} else if(!_(ofd.flags).contains(O_READ)) { } else if(!_(ofd.flags).contains(O_READ)) {
deferred.reject(new EBadFileDescriptor('descriptor does not permit reading')); callback(new EBadFileDescriptor('descriptor does not permit reading'));
} else { } else {
read_data(context, ofd, buffer, offset, length, position, check_result); read_data(context, ofd, buffer, offset, length, position, check_result);
} }
deferred.promise.then(
function(result) {
callback(undefined, result);
},
function(error) {
callback(error);
}
);
}; };
FileSystem.prototype._readFile = function _readFile(context, path, options, callback) { FileSystem.prototype._readFile = function _readFile(context, path, options, callback) {
var that = this; var that = this;
var deferred = when.defer();
if(!options) { if(!options) {
options = { encoding: null, flag: 'r' }; options = { encoding: null, flag: 'r' };
@ -9202,13 +9110,13 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
var flags = validate_flags(options.flag || 'r'); var flags = validate_flags(options.flag || 'r');
if(!flags) { if(!flags) {
deferred.reject(new EInvalid('flags is not valid')); callback(new EInvalid('flags is not valid'));
} }
open_file(that, context, path, flags, function(err, fileNode) { open_file(that, context, path, flags, function(err, fileNode) {
if(err) { if(err) {
// TODO: abort transaction? // TODO: abort transaction?
return deferred.reject(err); return callback(err);
} }
var ofd = new OpenFileDescription(fileNode.id, flags, 0); var ofd = new OpenFileDescription(fileNode.id, flags, 0);
var fd = that._allocate_descriptor(ofd); var fd = that._allocate_descriptor(ofd);
@ -9216,7 +9124,7 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
fstat_file(context, ofd, function(err2, fstatResult) { fstat_file(context, ofd, function(err2, fstatResult) {
if(err2) { if(err2) {
// TODO: abort transaction? // TODO: abort transaction?
return deferred.reject(err2); return callback(err2);
} }
var stats = new Stats(fstatResult, that.name); var stats = new Stats(fstatResult, that.name);
@ -9226,7 +9134,7 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
read_data(context, ofd, buffer, 0, size, 0, function(err3, nbytes) { read_data(context, ofd, buffer, 0, size, 0, function(err3, nbytes) {
if(err3) { if(err3) {
// TODO: abort transaction? // TODO: abort transaction?
return deferred.reject(err3); return callback(err3);
} }
that._release_descriptor(fd); that._release_descriptor(fd);
@ -9236,60 +9144,40 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
} else { } else {
data = buffer; data = buffer;
} }
deferred.resolve(data); callback(undefined, data);
}); });
}); });
}); });
deferred.promise.then(
function(result) {
callback(undefined, result);
},
function(error) {
callback(error);
}
);
}; };
FileSystem.prototype._write = function _write(context, fd, buffer, offset, length, position, callback) { FileSystem.prototype._write = function _write(context, fd, buffer, offset, length, position, callback) {
var that = this; var that = this;
var deferred = when.defer();
offset = (undefined === offset) ? 0 : offset; offset = (undefined === offset) ? 0 : offset;
length = (undefined === length) ? buffer.length - offset : length; length = (undefined === length) ? buffer.length - offset : length;
function check_result(error, nbytes) { function check_result(error, nbytes) {
if(error) { if(error) {
deferred.reject(error); callback(error);
} else { } else {
deferred.resolve(nbytes); callback(undefined, nbytes);
} }
} }
var ofd = that.openFiles[fd]; var ofd = that.openFiles[fd];
if(!ofd) { if(!ofd) {
deferred.reject(new EBadFileDescriptor('invalid file descriptor')); callback(new EBadFileDescriptor('invalid file descriptor'));
} else if(!_(ofd.flags).contains(O_WRITE)) { } else if(!_(ofd.flags).contains(O_WRITE)) {
deferred.reject(new EBadFileDescriptor('descriptor does not permit writing')); callback(new EBadFileDescriptor('descriptor does not permit writing'));
} else if(buffer.length - offset < length) { } else if(buffer.length - offset < length) {
deferred.reject(new EIO('intput buffer is too small')); callback(new EIO('intput buffer is too small'));
} else { } else {
write_data(context, ofd, buffer, offset, length, position, check_result); write_data(context, ofd, buffer, offset, length, position, check_result);
} }
deferred.promise.then(
function(result) {
callback(undefined, result);
},
function(error) {
callback(error);
}
);
}; };
FileSystem.prototype._writeFile = function _writeFile(context, path, data, options, callback) { FileSystem.prototype._writeFile = function _writeFile(context, path, data, options, callback) {
var that = this; var that = this;
var deferred = when.defer();
if(!options) { if(!options) {
options = { encoding: 'utf8', flag: 'w' }; options = { encoding: 'utf8', flag: 'w' };
@ -9302,7 +9190,7 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
var flags = validate_flags(options.flag || 'w'); var flags = validate_flags(options.flag || 'w');
if(!flags) { if(!flags) {
deferred.reject(new EInvalid('flags is not valid')); callback(new EInvalid('flags is not valid'));
} }
if(typeof data === "string" && options.encoding === 'utf8') { if(typeof data === "string" && options.encoding === 'utf8') {
@ -9312,7 +9200,7 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
open_file(that, context, path, flags, function(err, fileNode) { open_file(that, context, path, flags, function(err, fileNode) {
if(err) { if(err) {
// TODO: abort transaction? // TODO: abort transaction?
return deferred.reject(err); return callback(err);
} }
var ofd = new OpenFileDescription(fileNode.id, flags, 0); var ofd = new OpenFileDescription(fileNode.id, flags, 0);
var fd = that._allocate_descriptor(ofd); var fd = that._allocate_descriptor(ofd);
@ -9320,21 +9208,12 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
write_data(context, ofd, data, 0, data.length, 0, function(err2, nbytes) { write_data(context, ofd, data, 0, data.length, 0, function(err2, nbytes) {
if(err2) { if(err2) {
// TODO: abort transaction? // TODO: abort transaction?
return deferred.reject(err2); return callback(err2);
} }
that._release_descriptor(fd); that._release_descriptor(fd);
deferred.resolve();
});
});
deferred.promise.then(
function() {
callback(undefined); callback(undefined);
}, });
function(error) { });
callback(error);
}
);
}; };
FileSystem.prototype._getxattr = function _getxattr(path, name, callback) { FileSystem.prototype._getxattr = function _getxattr(path, name, callback) {
@ -9344,25 +9223,24 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
}; };
FileSystem.prototype._lseek = function _lseek(context, fd, offset, whence, callback) { FileSystem.prototype._lseek = function _lseek(context, fd, offset, whence, callback) {
var that = this; var that = this;
var deferred = when.defer();
function check_result(error, offset) { function check_result(error, offset) {
if(error) { if(error) {
deferred.reject(error); callback(error);
} else { } else {
deferred.resolve(offset); callback(offset);
} }
} }
function update_descriptor_position(error, stats) { function update_descriptor_position(error, stats) {
if(error) { if(error) {
deferred.reject(error); callback(error);
} else { } else {
if(stats.size + offset < 0) { if(stats.size + offset < 0) {
deferred.reject(new EInvalid('resulting file offset would be negative')); callback(new EInvalid('resulting file offset would be negative'));
} else { } else {
ofd.position = stats.size + offset; ofd.position = stats.size + offset;
deferred.resolve(ofd.position); callback(undefined, ofd.position);
} }
} }
} }
@ -9370,75 +9248,55 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
var ofd = that.openFiles[fd]; var ofd = that.openFiles[fd];
if(!ofd) { if(!ofd) {
deferred.reject(new EBadFileDescriptor('invalid file descriptor')); callback(new EBadFileDescriptor('invalid file descriptor'));
} }
if('SET' === whence) { if('SET' === whence) {
if(offset < 0) { if(offset < 0) {
deferred.reject(new EInvalid('resulting file offset would be negative')); callback(new EInvalid('resulting file offset would be negative'));
} else { } else {
ofd.position = offset; ofd.position = offset;
deferred.resolve(ofd.position); callback(undefined, ofd.position);
} }
} else if('CUR' === whence) { } else if('CUR' === whence) {
if(ofd.position + offset < 0) { if(ofd.position + offset < 0) {
deferred.reject(new EInvalid('resulting file offset would be negative')); callback(new EInvalid('resulting file offset would be negative'));
} else { } else {
ofd.position += offset; ofd.position += offset;
deferred.resolve(ofd.position); callback(undefined, ofd.position);
} }
} else if('END' === whence) { } else if('END' === whence) {
fstat_file(context, ofd, update_descriptor_position); fstat_file(context, ofd, update_descriptor_position);
} else { } else {
deferred.reject(new EInvalid('whence argument is not a proper value')); callback(new EInvalid('whence argument is not a proper value'));
} }
deferred.promise.then(
function(result) {
callback(undefined, result);
},
function(error) {
callback(error);
}
);
}; };
FileSystem.prototype._readdir = function _readdir(context, path, callback) { FileSystem.prototype._readdir = function _readdir(context, path, callback) {
var that = this; var that = this;
var deferred = when.defer();
function check_result(error, files) { function check_result(error, files) {
if(error) { if(error) {
// if(transaction.error) transaction.abort(); // if(transaction.error) transaction.abort();
deferred.reject(error); callback(error);
} else { } else {
deferred.resolve(files); callback(undefined, files);
} }
} }
read_directory(context, path, check_result); read_directory(context, path, check_result);
deferred.promise.then(
function(result) {
callback(undefined, result);
},
function(error) {
callback(error);
}
);
}; };
FileSystem.prototype._utimes = function _utimes(path, atime, mtime, callback) { FileSystem.prototype._utimes = function _utimes(path, atime, mtime, callback) {
}; };
FileSystem.prototype._rename = function _rename(context, oldpath, newpath, callback) { FileSystem.prototype._rename = function _rename(context, oldpath, newpath, callback) {
var that = this; var that = this;
var deferred = when.defer();
link_node(context, oldpath, newpath, unlink_old_node); link_node(context, oldpath, newpath, unlink_old_node);
function unlink_old_node(error) { function unlink_old_node(error) {
if(error) { if(error) {
// if(transaction.error) transaction.abort(); // if(transaction.error) transaction.abort();
deferred.reject(error); callback(error);
} else { } else {
unlink_node(context, oldpath, check_result); unlink_node(context, oldpath, check_result);
} }
@ -9447,20 +9305,11 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
function check_result(error) { function check_result(error) {
if(error) { if(error) {
// if(transaction.error) transaction.abort(); // if(transaction.error) transaction.abort();
deferred.reject(error);
} else {
deferred.resolve();
}
}
deferred.promise.then(
function(result) {
callback();
},
function(error) {
callback(error); callback(error);
} else {
callback(undefined);
}
} }
);
}; };
FileSystem.prototype._truncate = function _truncate(path, length, callback) { FileSystem.prototype._truncate = function _truncate(path, length, callback) {

6
dist/idbfs.min.js vendored

File diff suppressed because one or more lines are too long

259
src/fs.js
View File

@ -777,12 +777,11 @@ define(function(require) {
}; };
FileSystem.prototype._open = function _open(context, path, flags, callback) { FileSystem.prototype._open = function _open(context, path, flags, callback) {
var that = this; var that = this;
var deferred = when.defer();
function check_result(error, fileNode) { function check_result(error, fileNode) {
if(error) { if(error) {
// if(transaction.error) transaction.abort(); // if(transaction.error) transaction.abort();
deferred.reject(error); callback(error);
} else { } else {
var position; var position;
if(_(flags).contains(O_APPEND)) { if(_(flags).contains(O_APPEND)) {
@ -792,200 +791,119 @@ define(function(require) {
} }
var openFileDescription = new OpenFileDescription(fileNode.id, flags, position); var openFileDescription = new OpenFileDescription(fileNode.id, flags, position);
var fd = that._allocate_descriptor(openFileDescription); var fd = that._allocate_descriptor(openFileDescription);
deferred.resolve(fd); callback(undefined, fd);
} }
} }
flags = validate_flags(flags); flags = validate_flags(flags);
if(!flags) { if(!flags) {
deferred.reject(new EInvalid('flags is not valid')); callback(new EInvalid('flags is not valid'));
} }
open_file(that, context, path, flags, check_result); open_file(that, context, path, flags, check_result);
deferred.promise.then(
function(result) {
callback(undefined, result);
},
function(error) {
callback(error);
}
);
}; };
FileSystem.prototype._close = function _close(fd, callback) { FileSystem.prototype._close = function _close(fd, callback) {
var deferred = when.defer();
if(!_(this.openFiles).has(fd)) { if(!_(this.openFiles).has(fd)) {
deferred.reject(new EBadFileDescriptor('invalid file descriptor')); callback(new EBadFileDescriptor('invalid file descriptor'));
} else { } else {
this._release_descriptor(fd); this._release_descriptor(fd);
deferred.resolve(); callback(undefined);
} }
deferred.promise.then(
function() {
callback();
},
function(error) {
callback(error);
}
);
}; };
FileSystem.prototype._mkdir = function _mkdir(context, path, callback) { FileSystem.prototype._mkdir = function _mkdir(context, path, callback) {
var that = this; var that = this;
var deferred = when.defer();
function check_result(error) { function check_result(error) {
if(error) { if(error) {
// if(transaction.error) transaction.abort(); // if(transaction.error) transaction.abort();
deferred.reject(error); callback(error);
} else { } else {
deferred.resolve(); callback(undefined);
} }
} }
make_directory(context, path, check_result); make_directory(context, path, check_result);
deferred.promise.then(
function() {
callback();
},
function(error) {
callback(error);
}
);
}; };
FileSystem.prototype._rmdir = function _rmdir(context, path, callback) { FileSystem.prototype._rmdir = function _rmdir(context, path, callback) {
var that = this; var that = this;
var deferred = when.defer();
function check_result(error) { function check_result(error) {
if(error) { if(error) {
// if(transaction.error) transaction.abort(); // if(transaction.error) transaction.abort();
deferred.reject(error); callback(error);
} else { } else {
deferred.resolve(); callback(undefined);
} }
} }
remove_directory(context, path, check_result); remove_directory(context, path, check_result);
deferred.promise.then(
function() {
callback();
},
function(error) {
callback(error);
}
);
}; };
FileSystem.prototype._stat = function _stat(context, path, callback) { FileSystem.prototype._stat = function _stat(context, path, callback) {
var that = this; var that = this;
var deferred = when.defer();
function check_result(error, result) { function check_result(error, result) {
if(error) { if(error) {
// if(transaction.error) transaction.abort(); // if(transaction.error) transaction.abort();
deferred.reject(error); callback(error);
} else { } else {
var stats = new Stats(result, that.name); var stats = new Stats(result, that.name);
deferred.resolve(stats); callback(undefined, stats);
} }
} }
stat_file(context, path, check_result); stat_file(context, path, check_result);
deferred.promise.then(
function(result) {
callback(undefined, result);
},
function(error) {
callback(error);
}
);
}; };
FileSystem.prototype._fstat = function _fstat(context, fd, callback) { FileSystem.prototype._fstat = function _fstat(context, fd, callback) {
var that = this; var that = this;
var deferred = when.defer();
function check_result(error, result) { function check_result(error, result) {
if(error) { if(error) {
// if(transaction.error) transaction.abort(); // if(transaction.error) transaction.abort();
deferred.reject(error); callback(error);
} else { } else {
var stats = new Stats(result, that.name); var stats = new Stats(result, that.name);
deferred.resolve(stats); callback(undefined, stats);
} }
} }
var ofd = that.openFiles[fd]; var ofd = that.openFiles[fd];
if(!ofd) { if(!ofd) {
deferred.reject(new EBadFileDescriptor('invalid file descriptor')); callback(new EBadFileDescriptor('invalid file descriptor'));
} else { } else {
fstat_file(context, ofd, check_result); fstat_file(context, ofd, check_result);
} }
deferred.promise.then(
function(result) {
callback(undefined, result);
},
function(error) {
callback(error);
}
);
}; };
FileSystem.prototype._link = function _link(context, oldpath, newpath, callback) { FileSystem.prototype._link = function _link(context, oldpath, newpath, callback) {
var that = this; var that = this;
var deferred = when.defer();
function check_result(error) { function check_result(error) {
if(error) { if(error) {
// if(transaction.error) transaction.abort(); // if(transaction.error) transaction.abort();
deferred.reject(error); callback(error);
} else { } else {
deferred.resolve(); callback(undefined);
} }
} }
link_node(context, oldpath, newpath, check_result); link_node(context, oldpath, newpath, check_result);
deferred.promise.then(
function(result) {
callback();
},
function(error) {
callback(error);
}
);
}; };
FileSystem.prototype._unlink = function _unlink(context, path, callback) { FileSystem.prototype._unlink = function _unlink(context, path, callback) {
var that = this; var that = this;
var deferred = when.defer();
function check_result(error) { function check_result(error) {
if(error) { if(error) {
// if(transaction.error) transaction.abort(); // if(transaction.error) transaction.abort();
deferred.reject(error); callback(error);
} else { } else {
deferred.resolve(); callback(undefined);
} }
} }
unlink_node(context, path, check_result); unlink_node(context, path, check_result);
deferred.promise.then(
function(result) {
callback();
},
function(error) {
callback(error);
}
);
}; };
FileSystem.prototype._read = function _read(context, fd, buffer, offset, length, position, callback) { FileSystem.prototype._read = function _read(context, fd, buffer, offset, length, position, callback) {
var that = this; var that = this;
var deferred = when.defer();
offset = (undefined === offset) ? 0 : offset; offset = (undefined === offset) ? 0 : offset;
length = (undefined === length) ? buffer.length - offset : length; length = (undefined === length) ? buffer.length - offset : length;
@ -993,34 +911,24 @@ define(function(require) {
function check_result(error, nbytes) { function check_result(error, nbytes) {
if(error) { if(error) {
// if(transaction.error) transaction.abort(); // if(transaction.error) transaction.abort();
deferred.reject(error); callback(error);
} else { } else {
deferred.resolve(nbytes); callback(undefined, nbytes);
} }
} }
var ofd = that.openFiles[fd]; var ofd = that.openFiles[fd];
if(!ofd) { if(!ofd) {
deferred.reject(new EBadFileDescriptor('invalid file descriptor')); callback(new EBadFileDescriptor('invalid file descriptor'));
} else if(!_(ofd.flags).contains(O_READ)) { } else if(!_(ofd.flags).contains(O_READ)) {
deferred.reject(new EBadFileDescriptor('descriptor does not permit reading')); callback(new EBadFileDescriptor('descriptor does not permit reading'));
} else { } else {
read_data(context, ofd, buffer, offset, length, position, check_result); read_data(context, ofd, buffer, offset, length, position, check_result);
} }
deferred.promise.then(
function(result) {
callback(undefined, result);
},
function(error) {
callback(error);
}
);
}; };
FileSystem.prototype._readFile = function _readFile(context, path, options, callback) { FileSystem.prototype._readFile = function _readFile(context, path, options, callback) {
var that = this; var that = this;
var deferred = when.defer();
if(!options) { if(!options) {
options = { encoding: null, flag: 'r' }; options = { encoding: null, flag: 'r' };
@ -1033,13 +941,13 @@ define(function(require) {
var flags = validate_flags(options.flag || 'r'); var flags = validate_flags(options.flag || 'r');
if(!flags) { if(!flags) {
deferred.reject(new EInvalid('flags is not valid')); callback(new EInvalid('flags is not valid'));
} }
open_file(that, context, path, flags, function(err, fileNode) { open_file(that, context, path, flags, function(err, fileNode) {
if(err) { if(err) {
// TODO: abort transaction? // TODO: abort transaction?
return deferred.reject(err); return callback(err);
} }
var ofd = new OpenFileDescription(fileNode.id, flags, 0); var ofd = new OpenFileDescription(fileNode.id, flags, 0);
var fd = that._allocate_descriptor(ofd); var fd = that._allocate_descriptor(ofd);
@ -1047,7 +955,7 @@ define(function(require) {
fstat_file(context, ofd, function(err2, fstatResult) { fstat_file(context, ofd, function(err2, fstatResult) {
if(err2) { if(err2) {
// TODO: abort transaction? // TODO: abort transaction?
return deferred.reject(err2); return callback(err2);
} }
var stats = new Stats(fstatResult, that.name); var stats = new Stats(fstatResult, that.name);
@ -1057,7 +965,7 @@ define(function(require) {
read_data(context, ofd, buffer, 0, size, 0, function(err3, nbytes) { read_data(context, ofd, buffer, 0, size, 0, function(err3, nbytes) {
if(err3) { if(err3) {
// TODO: abort transaction? // TODO: abort transaction?
return deferred.reject(err3); return callback(err3);
} }
that._release_descriptor(fd); that._release_descriptor(fd);
@ -1067,60 +975,40 @@ define(function(require) {
} else { } else {
data = buffer; data = buffer;
} }
deferred.resolve(data); callback(undefined, data);
}); });
}); });
}); });
deferred.promise.then(
function(result) {
callback(undefined, result);
},
function(error) {
callback(error);
}
);
}; };
FileSystem.prototype._write = function _write(context, fd, buffer, offset, length, position, callback) { FileSystem.prototype._write = function _write(context, fd, buffer, offset, length, position, callback) {
var that = this; var that = this;
var deferred = when.defer();
offset = (undefined === offset) ? 0 : offset; offset = (undefined === offset) ? 0 : offset;
length = (undefined === length) ? buffer.length - offset : length; length = (undefined === length) ? buffer.length - offset : length;
function check_result(error, nbytes) { function check_result(error, nbytes) {
if(error) { if(error) {
deferred.reject(error); callback(error);
} else { } else {
deferred.resolve(nbytes); callback(undefined, nbytes);
} }
} }
var ofd = that.openFiles[fd]; var ofd = that.openFiles[fd];
if(!ofd) { if(!ofd) {
deferred.reject(new EBadFileDescriptor('invalid file descriptor')); callback(new EBadFileDescriptor('invalid file descriptor'));
} else if(!_(ofd.flags).contains(O_WRITE)) { } else if(!_(ofd.flags).contains(O_WRITE)) {
deferred.reject(new EBadFileDescriptor('descriptor does not permit writing')); callback(new EBadFileDescriptor('descriptor does not permit writing'));
} else if(buffer.length - offset < length) { } else if(buffer.length - offset < length) {
deferred.reject(new EIO('intput buffer is too small')); callback(new EIO('intput buffer is too small'));
} else { } else {
write_data(context, ofd, buffer, offset, length, position, check_result); write_data(context, ofd, buffer, offset, length, position, check_result);
} }
deferred.promise.then(
function(result) {
callback(undefined, result);
},
function(error) {
callback(error);
}
);
}; };
FileSystem.prototype._writeFile = function _writeFile(context, path, data, options, callback) { FileSystem.prototype._writeFile = function _writeFile(context, path, data, options, callback) {
var that = this; var that = this;
var deferred = when.defer();
if(!options) { if(!options) {
options = { encoding: 'utf8', flag: 'w' }; options = { encoding: 'utf8', flag: 'w' };
@ -1133,7 +1021,7 @@ define(function(require) {
var flags = validate_flags(options.flag || 'w'); var flags = validate_flags(options.flag || 'w');
if(!flags) { if(!flags) {
deferred.reject(new EInvalid('flags is not valid')); callback(new EInvalid('flags is not valid'));
} }
if(typeof data === "string" && options.encoding === 'utf8') { if(typeof data === "string" && options.encoding === 'utf8') {
@ -1143,7 +1031,7 @@ define(function(require) {
open_file(that, context, path, flags, function(err, fileNode) { open_file(that, context, path, flags, function(err, fileNode) {
if(err) { if(err) {
// TODO: abort transaction? // TODO: abort transaction?
return deferred.reject(err); return callback(err);
} }
var ofd = new OpenFileDescription(fileNode.id, flags, 0); var ofd = new OpenFileDescription(fileNode.id, flags, 0);
var fd = that._allocate_descriptor(ofd); var fd = that._allocate_descriptor(ofd);
@ -1151,21 +1039,12 @@ define(function(require) {
write_data(context, ofd, data, 0, data.length, 0, function(err2, nbytes) { write_data(context, ofd, data, 0, data.length, 0, function(err2, nbytes) {
if(err2) { if(err2) {
// TODO: abort transaction? // TODO: abort transaction?
return deferred.reject(err2); return callback(err2);
} }
that._release_descriptor(fd); that._release_descriptor(fd);
deferred.resolve();
});
});
deferred.promise.then(
function() {
callback(undefined); callback(undefined);
}, });
function(error) { });
callback(error);
}
);
}; };
FileSystem.prototype._getxattr = function _getxattr(path, name, callback) { FileSystem.prototype._getxattr = function _getxattr(path, name, callback) {
@ -1175,25 +1054,24 @@ define(function(require) {
}; };
FileSystem.prototype._lseek = function _lseek(context, fd, offset, whence, callback) { FileSystem.prototype._lseek = function _lseek(context, fd, offset, whence, callback) {
var that = this; var that = this;
var deferred = when.defer();
function check_result(error, offset) { function check_result(error, offset) {
if(error) { if(error) {
deferred.reject(error); callback(error);
} else { } else {
deferred.resolve(offset); callback(offset);
} }
} }
function update_descriptor_position(error, stats) { function update_descriptor_position(error, stats) {
if(error) { if(error) {
deferred.reject(error); callback(error);
} else { } else {
if(stats.size + offset < 0) { if(stats.size + offset < 0) {
deferred.reject(new EInvalid('resulting file offset would be negative')); callback(new EInvalid('resulting file offset would be negative'));
} else { } else {
ofd.position = stats.size + offset; ofd.position = stats.size + offset;
deferred.resolve(ofd.position); callback(undefined, ofd.position);
} }
} }
} }
@ -1201,75 +1079,55 @@ define(function(require) {
var ofd = that.openFiles[fd]; var ofd = that.openFiles[fd];
if(!ofd) { if(!ofd) {
deferred.reject(new EBadFileDescriptor('invalid file descriptor')); callback(new EBadFileDescriptor('invalid file descriptor'));
} }
if('SET' === whence) { if('SET' === whence) {
if(offset < 0) { if(offset < 0) {
deferred.reject(new EInvalid('resulting file offset would be negative')); callback(new EInvalid('resulting file offset would be negative'));
} else { } else {
ofd.position = offset; ofd.position = offset;
deferred.resolve(ofd.position); callback(undefined, ofd.position);
} }
} else if('CUR' === whence) { } else if('CUR' === whence) {
if(ofd.position + offset < 0) { if(ofd.position + offset < 0) {
deferred.reject(new EInvalid('resulting file offset would be negative')); callback(new EInvalid('resulting file offset would be negative'));
} else { } else {
ofd.position += offset; ofd.position += offset;
deferred.resolve(ofd.position); callback(undefined, ofd.position);
} }
} else if('END' === whence) { } else if('END' === whence) {
fstat_file(context, ofd, update_descriptor_position); fstat_file(context, ofd, update_descriptor_position);
} else { } else {
deferred.reject(new EInvalid('whence argument is not a proper value')); callback(new EInvalid('whence argument is not a proper value'));
} }
deferred.promise.then(
function(result) {
callback(undefined, result);
},
function(error) {
callback(error);
}
);
}; };
FileSystem.prototype._readdir = function _readdir(context, path, callback) { FileSystem.prototype._readdir = function _readdir(context, path, callback) {
var that = this; var that = this;
var deferred = when.defer();
function check_result(error, files) { function check_result(error, files) {
if(error) { if(error) {
// if(transaction.error) transaction.abort(); // if(transaction.error) transaction.abort();
deferred.reject(error); callback(error);
} else { } else {
deferred.resolve(files); callback(undefined, files);
} }
} }
read_directory(context, path, check_result); read_directory(context, path, check_result);
deferred.promise.then(
function(result) {
callback(undefined, result);
},
function(error) {
callback(error);
}
);
}; };
FileSystem.prototype._utimes = function _utimes(path, atime, mtime, callback) { FileSystem.prototype._utimes = function _utimes(path, atime, mtime, callback) {
}; };
FileSystem.prototype._rename = function _rename(context, oldpath, newpath, callback) { FileSystem.prototype._rename = function _rename(context, oldpath, newpath, callback) {
var that = this; var that = this;
var deferred = when.defer();
link_node(context, oldpath, newpath, unlink_old_node); link_node(context, oldpath, newpath, unlink_old_node);
function unlink_old_node(error) { function unlink_old_node(error) {
if(error) { if(error) {
// if(transaction.error) transaction.abort(); // if(transaction.error) transaction.abort();
deferred.reject(error); callback(error);
} else { } else {
unlink_node(context, oldpath, check_result); unlink_node(context, oldpath, check_result);
} }
@ -1278,20 +1136,11 @@ define(function(require) {
function check_result(error) { function check_result(error) {
if(error) { if(error) {
// if(transaction.error) transaction.abort(); // if(transaction.error) transaction.abort();
deferred.reject(error);
} else {
deferred.resolve();
}
}
deferred.promise.then(
function(result) {
callback();
},
function(error) {
callback(error); callback(error);
} else {
callback(undefined);
}
} }
);
}; };
FileSystem.prototype._truncate = function _truncate(path, length, callback) { FileSystem.prototype._truncate = function _truncate(path, length, callback) {