finish refactoring

This commit is contained in:
Alan Kligman 2013-11-21 13:45:04 -05:00
parent a0e27476b0
commit 1d6ddeae35
1 changed files with 309 additions and 256 deletions

271
src/fs.js
View File

@ -169,53 +169,24 @@ define(function(require) {
* read_object
*/
function read_object(objectStore, id, callback) {
try {
var getRequest = objectStore.get(id);
getRequest.onsuccess = function onsuccess(event) {
var result = event.target.result;
callback(undefined, result);
};
getRequest.onerror = function onerror(error) {
callback(error);
};
} catch(error) {
callback(new EIO(error.message));
}
function read_object(context, id, callback) {
context.get(id, callback);
}
/*
* write_object
*/
function write_object(objectStore, object, id, callback) {
try {
var putRequest = objectStore.put(object, id);
putRequest.onsuccess = function onsuccess(event) {
var result = event.target.result;
callback(undefined, result);
};
putRequest.onerror = function onerror(error) {
callback(error);
};
} catch(error) {
callback(new EIO(error.message));
}
function write_object(context, object, id, callback) {
context.put(id, object, callback);
}
/*
* delete_object
*/
function delete_object(objectStore, id, callback) {
var deleteRequest = objectStore.delete(id);
deleteRequest.onsuccess = function onsuccess(event) {
var result = event.target.result;
callback(undefined, result);
};
deleteRequest.onerror = function(error) {
callback(error);
};
function delete_object(context, id, callback) {
context.delete(id, callback);
}
/*
@ -1125,13 +1096,9 @@ define(function(require) {
}
);
};
FileSystem.prototype._write = function _write(fd, buffer, offset, length, position, callback) {
FileSystem.prototype._write = function _write(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;
@ -1153,7 +1120,7 @@ define(function(require) {
} else if(buffer.length - offset < length) {
deferred.reject(new EIO('intput buffer is too small'));
} else {
write_data(files, ofd, buffer, offset, length, position, check_result);
write_data(context, ofd, buffer, offset, length, position, check_result);
}
deferred.promise.then(
@ -1164,19 +1131,10 @@ define(function(require) {
callback(error);
}
);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
FileSystem.prototype._writeFile = function _writeFile(path, data, options, callback) {
FileSystem.prototype._writeFile = function _writeFile(context, path, data, options, 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);
if(!options) {
options = { encoding: 'utf8', flag: 'w' };
@ -1196,7 +1154,7 @@ define(function(require) {
data = new TextEncoder('utf-8').encode(data);
}
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);
@ -1204,7 +1162,7 @@ define(function(require) {
var ofd = new OpenFileDescription(fileNode.id, flags, 0);
var fd = that._allocate_descriptor(ofd);
write_data(files, ofd, data, 0, data.length, 0, function(err2, nbytes) {
write_data(context, ofd, data, 0, data.length, 0, function(err2, nbytes) {
if(err2) {
// TODO: abort transaction?
return deferred.reject(err2);
@ -1222,11 +1180,6 @@ define(function(require) {
callback(error);
}
);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
FileSystem.prototype._getxattr = function _getxattr(path, name, callback) {
@ -1234,10 +1187,8 @@ define(function(require) {
FileSystem.prototype._setxattr = function _setxattr(path, name, value, callback) {
};
FileSystem.prototype._lseek = function _lseek(fd, offset, whence, callback) {
FileSystem.prototype._lseek = function _lseek(context, fd, offset, whence, callback) {
var that = this;
this.promise.then(
function() {
var deferred = when.defer();
function check_result(error, offset) {
@ -1282,10 +1233,7 @@ define(function(require) {
deferred.resolve(ofd.position);
}
} else if('END' === whence) {
var transaction = that.db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME);
fstat_file(files, ofd, update_descriptor_position);
fstat_file(context, ofd, update_descriptor_position);
} else {
deferred.reject(new EInvalid('whence argument is not a proper value'));
}
@ -1298,19 +1246,10 @@ define(function(require) {
callback(error);
}
);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
FileSystem.prototype._readdir = function _readdir(path, callback) {
FileSystem.prototype._readdir = function _readdir(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, files) {
if(error) {
@ -1321,7 +1260,7 @@ define(function(require) {
}
}
read_directory(files, path, check_result);
read_directory(context, path, check_result);
deferred.promise.then(
function(result) {
@ -1331,31 +1270,22 @@ define(function(require) {
callback(error);
}
);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
FileSystem.prototype._utimes = function _utimes(path, atime, mtime, callback) {
};
FileSystem.prototype._rename = function _rename(oldpath, newpath, callback) {
FileSystem.prototype._rename = function _rename(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);
link_node(files, oldpath, newpath, unlink_old_node);
link_node(context, oldpath, newpath, unlink_old_node);
function unlink_old_node(error) {
if(error) {
// if(transaction.error) transaction.abort();
deferred.reject(error);
} else {
unlink_node(files, oldpath, check_result);
unlink_node(context, oldpath, check_result);
}
}
@ -1376,11 +1306,6 @@ define(function(require) {
callback(error);
}
);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
FileSystem.prototype._truncate = function _truncate(path, length, callback) {
@ -1401,6 +1326,48 @@ define(function(require) {
};
function IndexedDBContext(objectStore) {
this.objectStore = objectStore;
}
IndexedDBContext.prototype.get = function(key, callback) {
try {
var request = this.objectStore.get(key);
request.onsuccess = function onsuccess(event) {
var result = event.target.result;
callback(undefined, result);
}
request.onerror = function onerror(error) {
callback(error);
}
} catch(error) {
callback(new EIO(error.message));
}
}
IndexedDBContext.prototype.put = function(key, value, callback) {
try {
var request = this.objectStore.put(value, key);
request.onsuccess = function onsuccess(event) {
var result = event.target.result;
callback(undefined, result);
};
request.onerror = function onerror(error) {
callback(error);
};
} catch(error) {
callback(new EIO(error.message));
}
}
IndexedDBContext.prototype.delete = function(key, callback) {
var request = this.objectStore.delete(key);
request.onsuccess = function onsuccess(event) {
var result = event.target.result;
callback(undefined, result);
};
request.onerror = function(error) {
callback(error);
};
}
function IndexedDBFileSystem(name, flags) {
var format = _(flags).contains(FS_FORMAT);
var that = this;
@ -1428,6 +1395,7 @@ define(function(require) {
var db = event.target.result;
var transaction = db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME);
var context = new IndexedDBContext(files);
function complete(error) {
that.db = db;
@ -1443,7 +1411,7 @@ define(function(require) {
if(format) {
var clearRequest = files.clear();
clearRequest.onsuccess = function onsuccess(event) {
make_root_directory(files, complete);
make_root_directory(context, complete);
};
clearRequest.onerror = function onerror(error) {
complete(error);
@ -1474,7 +1442,8 @@ define(function(require) {
function() {
var transaction = fs.db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME);
fs._open(files, path, flags, callback);
var context = new IndexedDBContext(files);
fs._open(context, path, flags, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
@ -1490,7 +1459,8 @@ define(function(require) {
function() {
var transaction = fs.db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME);
fs._mkdir(files, path, callback);
var context = new IndexedDBContext(files);
fs._mkdir(context, path, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
@ -1503,7 +1473,8 @@ define(function(require) {
function() {
var transaction = fs.db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME);
fs._rmdir(files, path, callback);
var context = new IndexedDBContext(files);
fs._rmdir(context, path, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
@ -1516,7 +1487,8 @@ define(function(require) {
function() {
var transaction = fs.db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME);
fs._stat(files, path, callback);
var context = new IndexedDBContext(files);
fs._stat(context, path, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
@ -1529,7 +1501,8 @@ define(function(require) {
function() {
var transaction = fs.db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME);
fs._fstat(files, fd, callback);
var context = new IndexedDBContext(files);
fs._fstat(context, fd, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
@ -1542,7 +1515,8 @@ define(function(require) {
function() {
var transaction = fs.db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME);
fs._link(files, oldpath, newpath, callback);
var context = new IndexedDBContext(files);
fs._link(context, oldpath, newpath, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
@ -1555,7 +1529,8 @@ define(function(require) {
function() {
var transaction = fs.db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME);
fs._unlink(files, path, callback);
var context = new IndexedDBContext(files);
fs._unlink(context, path, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
@ -1568,7 +1543,8 @@ define(function(require) {
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);
var context = new IndexedDBContext(files);
fs._read(context, fd, buffer, offset, length, position, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
@ -1581,7 +1557,8 @@ define(function(require) {
function() {
var transaction = fs.db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME);
fs._readFile(files, path, options, callback);
var context = new IndexedDBContext(files);
fs._readFile(context, path, options, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
@ -1589,22 +1566,98 @@ define(function(require) {
);
}
IndexedDBFileSystem.prototype.write = function write(fd, buffer, offset, length, position, callback) {
this._write(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);
var context = new IndexedDBContext(files);
fs._write(context, fd, buffer, offset, length, position, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
}
IndexedDBFileSystem.prototype.writeFile = function writeFile(path, data, options, callback) {
this._writeFile(path, data, 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);
var context = new IndexedDBContext(files);
fs._writeFile(context, path, data, options, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
}
IndexedDBFileSystem.prototype.lseek = function lseek(fd, offset, whence, callback) {
this._lseek(fd, offset, whence, 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);
var context = new IndexedDBContext(files);
fs._lseek(context, fd, offset, whence, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
}
IndexedDBFileSystem.prototype.readdir = function readdir(path, callback) {
this._readdir(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);
var context = new IndexedDBContext(files);
fs._readdir(context, path, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
}
IndexedDBFileSystem.prototype.rename = function rename(oldpath, newpath, callback) {
this._rename(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);
var context = new IndexedDBContext(files);
fs._rename(context, oldpath, newpath, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
}
// FIXME: WebSQL stuff, this needs implementation
function WebSQLContext(transaction) {
this.transaction = transaction;
}
WebSQLContext.prototype.get = function(key, callback) {
try {
} catch(error) {
callback(new EIO(error.message));
}
}
WebSQLContext.prototype.put = function(key, value, callback) {
try {
} catch(error) {
callback(new EIO(error.message));
}
}
WebSQLContext.prototype.delete = function(key, callback) {
}
// FIXME: this needs implementation
function WebSQLFileSystem(name, flags) {
}
WebSQLFileSystem.prototype = new FileSystem();