partially done refactoring

This commit is contained in:
Alan Kligman 2013-11-21 01:07:41 -05:00
parent 25342d5876
commit a0e27476b0
1 changed files with 271 additions and 262 deletions

209
src/fs.js
View File

@ -821,8 +821,6 @@ define(function(require) {
FileSystem.prototype._open = function _open(context, path, flags, callback) {
var that = this;
var deferred = when.defer();
// var transaction = that.db.transaction([FILE_STORE_NAME], IDB_RW);
// var files = transaction.objectStore(FILE_STORE_NAME);
function check_result(error, fileNode) {
if(error) {
@ -847,6 +845,7 @@ define(function(require) {
}
open_file(that, context, path, flags, check_result);
deferred.promise.then(
function(result) {
callback(undefined, result);
@ -875,13 +874,9 @@ define(function(require) {
}
);
};
FileSystem.prototype._mkdir = function _mkdir(path, callback) {
FileSystem.prototype._mkdir = function _mkdir(context, path, callback) {
var that = this;
this.promise.then(
function() {
var deferred = when.defer();
var transaction = that.db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME);
function check_result(error) {
if(error) {
@ -892,7 +887,8 @@ define(function(require) {
}
}
make_directory(files, path, check_result);
make_directory(context, path, check_result);
deferred.promise.then(
function() {
callback();
@ -901,19 +897,10 @@ define(function(require) {
callback(error);
}
);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
FileSystem.prototype._rmdir = function _rmdir(path, callback) {
FileSystem.prototype._rmdir = function _rmdir(context, path, callback) {
var that = this;
this.promise.then(
function() {
var deferred = when.defer();
var transaction = that.db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME);
function check_result(error) {
if(error) {
@ -924,7 +911,8 @@ define(function(require) {
}
}
remove_directory(files, path, check_result);
remove_directory(context, path, check_result);
deferred.promise.then(
function() {
callback();
@ -933,19 +921,10 @@ define(function(require) {
callback(error);
}
);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
FileSystem.prototype._stat = function _stat(path, callback) {
FileSystem.prototype._stat = function _stat(context, path, callback) {
var that = this;
this.promise.then(
function() {
var deferred = when.defer();
var transaction = that.db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME);
function check_result(error, result) {
if(error) {
@ -957,7 +936,7 @@ define(function(require) {
}
}
stat_file(files, path, check_result);
stat_file(context, path, check_result);
deferred.promise.then(
function(result) {
@ -967,19 +946,10 @@ define(function(require) {
callback(error);
}
);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
FileSystem.prototype._fstat = function _fstat(fd, callback) {
FileSystem.prototype._fstat = function _fstat(context, fd, callback) {
var that = this;
this.promise.then(
function() {
var deferred = when.defer();
var transaction = that.db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME);
function check_result(error, result) {
if(error) {
@ -996,7 +966,7 @@ define(function(require) {
if(!ofd) {
deferred.reject(new EBadFileDescriptor('invalid file descriptor'));
} else {
fstat_file(files, ofd, check_result);
fstat_file(context, ofd, check_result);
}
deferred.promise.then(
@ -1007,19 +977,10 @@ define(function(require) {
callback(error);
}
);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
FileSystem.prototype._link = function _link(oldpath, newpath, callback) {
FileSystem.prototype._link = function _link(context, oldpath, newpath, callback) {
var that = this;
this.promise.then(
function() {
var deferred = when.defer();
var transaction = that.db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME);
function check_result(error) {
if(error) {
@ -1030,7 +991,7 @@ define(function(require) {
}
}
link_node(files, oldpath, newpath, check_result);
link_node(context, oldpath, newpath, check_result);
deferred.promise.then(
function(result) {
@ -1040,19 +1001,10 @@ define(function(require) {
callback(error);
}
);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
FileSystem.prototype._unlink = function _unlink(path, callback) {
FileSystem.prototype._unlink = function _unlink(context, path, callback) {
var that = this;
this.promise.then(
function() {
var deferred = when.defer();
var transaction = that.db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME);
function check_result(error) {
if(error) {
@ -1063,7 +1015,7 @@ define(function(require) {
}
}
unlink_node(files, path, check_result);
unlink_node(context, path, check_result);
deferred.promise.then(
function(result) {
@ -1073,19 +1025,10 @@ define(function(require) {
callback(error);
}
);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
FileSystem.prototype._read = function _read(fd, buffer, offset, length, position, callback) {
FileSystem.prototype._read = function _read(context, fd, buffer, offset, length, position, callback) {
var that = this;
this.promise.then(
function() {
var deferred = when.defer();
var transaction = that.db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME);
offset = (undefined === offset) ? 0 : offset;
length = (undefined === length) ? buffer.length - offset : length;
@ -1106,7 +1049,7 @@ define(function(require) {
} else if(!_(ofd.flags).contains(O_READ)) {
deferred.reject(new EBadFileDescriptor('descriptor does not permit reading'));
} else {
read_data(files, ofd, buffer, offset, length, position, check_result);
read_data(context, ofd, buffer, offset, length, position, check_result);
}
deferred.promise.then(
@ -1117,19 +1060,10 @@ define(function(require) {
callback(error);
}
);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
FileSystem.prototype._readFile = function _readFile(path, options, callback) {
FileSystem.prototype._readFile = function _readFile(context, path, options, callback) {
var that = this;
this.promise.then(
function() {
var deferred = when.defer();
var transaction = that.db.transaction([FILE_STORE_NAME], IDB_RO);
var files = transaction.objectStore(FILE_STORE_NAME);
if(!options) {
options = { encoding: null, flag: 'r' };
@ -1145,7 +1079,7 @@ define(function(require) {
deferred.reject(new EInvalid('flags is not valid'));
}
open_file(that, files, path, flags, function(err, fileNode) {
open_file(that, context, path, flags, function(err, fileNode) {
if(err) {
// TODO: abort transaction?
return deferred.reject(err);
@ -1153,7 +1087,7 @@ define(function(require) {
var ofd = new OpenFileDescription(fileNode.id, flags, 0);
var fd = that._allocate_descriptor(ofd);
fstat_file(files, ofd, function(err2, fstatResult) {
fstat_file(context, ofd, function(err2, fstatResult) {
if(err2) {
// TODO: abort transaction?
return deferred.reject(err2);
@ -1163,7 +1097,7 @@ define(function(require) {
var size = stats.size;
var buffer = new Uint8Array(size);
read_data(files, ofd, buffer, 0, size, 0, function(err3, nbytes) {
read_data(context, ofd, buffer, 0, size, 0, function(err3, nbytes) {
if(err3) {
// TODO: abort transaction?
return deferred.reject(err3);
@ -1190,11 +1124,6 @@ define(function(require) {
callback(error);
}
);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
FileSystem.prototype._write = function _write(fd, buffer, offset, length, position, callback) {
var that = this;
@ -1556,28 +1485,108 @@ define(function(require) {
this._close(fd, callback);
}
IndexedDBFileSystem.prototype.mkdir = function mkdir(path, callback) {
this._mkdir(path, callback);
var fs = this;
this.promise.then(
function() {
var transaction = fs.db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME);
fs._mkdir(files, path, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
}
IndexedDBFileSystem.prototype.rmdir = function rmdir(path, callback) {
this._rmdir(path, callback);
var fs = this;
this.promise.then(
function() {
var transaction = fs.db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME);
fs._rmdir(files, path, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
}
IndexedDBFileSystem.prototype.stat = function stat(path, callback) {
this._stat(path, callback);
var fs = this;
this.promise.then(
function() {
var transaction = fs.db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME);
fs._stat(files, path, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
}
IndexedDBFileSystem.prototype.fstat = function fstat(fd, callback) {
this._fstat(fd, callback);
var fs = this;
this.promise.then(
function() {
var transaction = fs.db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME);
fs._fstat(files, fd, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
}
IndexedDBFileSystem.prototype.link = function link(oldpath, newpath, callback) {
this._link(oldpath, newpath, callback);
var fs = this;
this.promise.then(
function() {
var transaction = fs.db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME);
fs._link(files, oldpath, newpath, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
}
IndexedDBFileSystem.prototype.unlink = function unlink(path, callback) {
this._unlink(path, callback);
var fs = this;
this.promise.then(
function() {
var transaction = fs.db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME);
fs._unlink(files, path, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
}
IndexedDBFileSystem.prototype.read = function read(fd, buffer, offset, length, position, callback) {
this._read(fd, buffer, offset, length, position, callback);
var fs = this;
this.promise.then(
function() {
var transaction = fs.db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME);
fs._read(files, fd, buffer, offset, length, position, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
}
IndexedDBFileSystem.prototype.readFile = function readFile(path, options, callback) {
this._readFile(path, options, callback);
var fs = this;
this.promise.then(
function() {
var transaction = fs.db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME);
fs._readFile(files, path, options, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
}
IndexedDBFileSystem.prototype.write = function write(fd, buffer, offset, length, position, callback) {
this._write(fd, buffer, offset, length, position, callback);