refactoring to support additional backends

This commit is contained in:
Alan Kligman 2013-11-20 19:24:49 -05:00
parent cfc63d80c1
commit c9d60fbd9b
3 changed files with 1624 additions and 1384 deletions

658
dist/idbfs.js vendored
View File

@ -8305,7 +8305,7 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
function read_parent_directory_data(error, parentDirectoryNode) {
if(error) {
callback(error);
} else if(!parentDirectoryNode.type == MODE_DIRECTORY) {
} else if(parentDirectoryNode.mode !== MODE_DIRECTORY || !parentDirectoryNode.data) {
callback(new ENotDirectory('a component of the path prefix is not a directory'));
} else {
read_object(objectStore, parentDirectoryNode.data, get_node_id_from_parent_directory_data);
@ -8338,53 +8338,24 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
* 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);
}
/*
@ -8964,69 +8935,6 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
*/
function FileSystem(name, flags) {
var format = _(flags).contains(FS_FORMAT);
var that = this;
var deferred = when.defer();
this.promise = deferred.promise;
var openRequest = indexedDB.open(name);
openRequest.onupgradeneeded = function onupgradeneeded(event) {
var db = event.target.result;
if(db.objectStoreNames.contains(FILE_STORE_NAME)) {
db.deleteObjectStore(FILE_STORE_NAME);
}
var files = db.createObjectStore(FILE_STORE_NAME);
if(db.objectStoreNames.contains(METADATA_STORE_NAME)) {
db.deleteObjectStore(METADATA_STORE_NAME);
}
var metadata = db.createObjectStore(METADATA_STORE_NAME);
format = true;
};
openRequest.onsuccess = function onsuccess(event) {
var db = event.target.result;
var transaction = db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME);
function complete(error) {
that.db = db;
if(error) {
that.readyState = FS_ERROR;
deferred.reject(error);
} else {
that.readyState = FS_READY;
deferred.resolve();
}
}
if(format) {
var clearRequest = files.clear();
clearRequest.onsuccess = function onsuccess(event) {
make_root_directory(files, complete);
};
clearRequest.onerror = function onerror(error) {
complete(error);
};
} else {
complete();
}
};
openRequest.onerror = function onerror(error) {
this.readyState = FS_ERROR;
deferred.reject(error);
};
var nextDescriptor = 1;
var openFiles = {};
this.readyState = FS_PENDING;
this.db = null;
this.nextDescriptor = nextDescriptor;
this.openFiles = openFiles;
this.name = name;
}
FileSystem.prototype._allocate_descriptor = function _allocate_descriptor(openFileDescription) {
var fd = this.nextDescriptor ++;
@ -9036,13 +8944,9 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
FileSystem.prototype._release_descriptor = function _release_descriptor(fd) {
delete this.openFiles[fd];
};
FileSystem.prototype.open = function open(path, flags, callback) {
FileSystem.prototype._open = function _open(context, path, flags, 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, fileNode) {
if(error) {
@ -9066,7 +8970,8 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
deferred.reject(new EInvalid('flags is not valid'));
}
open_file(that, files, path, flags, check_result);
open_file(that, context, path, flags, check_result);
deferred.promise.then(
function(result) {
callback(undefined, result);
@ -9075,13 +8980,8 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
callback(error);
}
);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
FileSystem.prototype.close = function close(fd, callback) {
FileSystem.prototype._close = function _close(fd, callback) {
var deferred = when.defer();
if(!_(this.openFiles).has(fd)) {
@ -9100,13 +9000,9 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
}
);
};
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) {
@ -9117,7 +9013,8 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
}
}
make_directory(files, path, check_result);
make_directory(context, path, check_result);
deferred.promise.then(
function() {
callback();
@ -9126,19 +9023,10 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
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) {
@ -9149,7 +9037,8 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
}
}
remove_directory(files, path, check_result);
remove_directory(context, path, check_result);
deferred.promise.then(
function() {
callback();
@ -9158,19 +9047,10 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
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) {
@ -9182,7 +9062,7 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
}
}
stat_file(files, path, check_result);
stat_file(context, path, check_result);
deferred.promise.then(
function(result) {
@ -9192,19 +9072,10 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
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) {
@ -9221,7 +9092,7 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
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(
@ -9232,19 +9103,10 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
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) {
@ -9255,7 +9117,7 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
}
}
link_node(files, oldpath, newpath, check_result);
link_node(context, oldpath, newpath, check_result);
deferred.promise.then(
function(result) {
@ -9265,19 +9127,10 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
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) {
@ -9288,7 +9141,7 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
}
}
unlink_node(files, path, check_result);
unlink_node(context, path, check_result);
deferred.promise.then(
function(result) {
@ -9298,19 +9151,10 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
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;
@ -9331,7 +9175,7 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
} 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(
@ -9342,19 +9186,10 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
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' };
@ -9370,7 +9205,7 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
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);
@ -9378,7 +9213,7 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
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);
@ -9388,7 +9223,7 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
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);
@ -9415,19 +9250,10 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
callback(error);
}
);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
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;
@ -9449,7 +9275,7 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
} 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(
@ -9460,19 +9286,10 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
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' };
@ -9492,7 +9309,7 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
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);
@ -9500,7 +9317,7 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
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);
@ -9518,22 +9335,15 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
callback(error);
}
);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
FileSystem.prototype.getxattr = function getxattr(path, name, callback) {
FileSystem.prototype._getxattr = function _getxattr(path, name, callback) {
};
FileSystem.prototype.setxattr = function setxattr(path, name, value, callback) {
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) {
@ -9578,10 +9388,7 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
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'));
}
@ -9594,19 +9401,10 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
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) {
@ -9617,7 +9415,7 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
}
}
read_directory(files, path, check_result);
read_directory(context, path, check_result);
deferred.promise.then(
function(result) {
@ -9627,31 +9425,22 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
callback(error);
}
);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
FileSystem.prototype.utimes = function utimes(path, atime, mtime, callback) {
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);
}
}
@ -9672,34 +9461,365 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
callback(error);
}
);
};
FileSystem.prototype._truncate = function _truncate(path, length, callback) {
};
FileSystem.prototype._ftruncate = function _ftruncate(fd, length, callback) {
};
FileSystem.prototype._symlink = function _symlink(fd, length, callback) {
};
FileSystem.prototype._readlink = function _readlink(fd, length, callback) {
};
FileSystem.prototype._realpath = function _realpath(fd, length, callback) {
};
FileSystem.prototype._lstat = function _lstat(fd, length, callback) {
};
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;
var deferred = when.defer();
this.promise = deferred.promise;
var openRequest = indexedDB.open(name);
openRequest.onupgradeneeded = function onupgradeneeded(event) {
var db = event.target.result;
if(db.objectStoreNames.contains(FILE_STORE_NAME)) {
db.deleteObjectStore(FILE_STORE_NAME);
}
var files = db.createObjectStore(FILE_STORE_NAME);
if(db.objectStoreNames.contains(METADATA_STORE_NAME)) {
db.deleteObjectStore(METADATA_STORE_NAME);
}
var metadata = db.createObjectStore(METADATA_STORE_NAME);
format = true;
};
openRequest.onsuccess = function onsuccess(event) {
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;
if(error) {
that.readyState = FS_ERROR;
deferred.reject(error);
} else {
that.readyState = FS_READY;
deferred.resolve();
}
}
if(format) {
var clearRequest = files.clear();
clearRequest.onsuccess = function onsuccess(event) {
make_root_directory(context, complete);
};
clearRequest.onerror = function onerror(error) {
complete(error);
};
} else {
complete();
}
};
openRequest.onerror = function onerror(error) {
this.readyState = FS_ERROR;
deferred.reject(error);
};
var nextDescriptor = 1;
var openFiles = {};
this.readyState = FS_PENDING;
this.db = null;
this.nextDescriptor = nextDescriptor;
this.openFiles = openFiles;
this.name = name;
}
IndexedDBFileSystem.prototype = new FileSystem();
IndexedDBFileSystem.prototype.constructor = IndexedDBFileSystem;
IndexedDBFileSystem.prototype.open = function open(path, flags, 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._open(context, path, flags, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
FileSystem.prototype.truncate = function truncate(path, length, callback) {
IndexedDBFileSystem.prototype.close = function close(fd, callback) {
this._close(fd, callback);
};
FileSystem.prototype.ftruncate = function ftruncate(fd, length, callback) {
IndexedDBFileSystem.prototype.mkdir = function 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);
var context = new IndexedDBContext(files);
fs._mkdir(context, path, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
FileSystem.prototype.symlink = function symlink(fd, length, callback) {
IndexedDBFileSystem.prototype.rmdir = function 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);
var context = new IndexedDBContext(files);
fs._rmdir(context, path, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
FileSystem.prototype.readlink = function readlink(fd, length, callback) {
IndexedDBFileSystem.prototype.stat = function 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);
var context = new IndexedDBContext(files);
fs._stat(context, path, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
FileSystem.prototype.realpath = function realpath(fd, length, callback) {
IndexedDBFileSystem.prototype.fstat = function 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);
var context = new IndexedDBContext(files);
fs._fstat(context, fd, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
FileSystem.prototype.lstat = function lstat(fd, length, callback) {
IndexedDBFileSystem.prototype.link = function 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);
var context = new IndexedDBContext(files);
fs._link(context, oldpath, newpath, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
IndexedDBFileSystem.prototype.unlink = function 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);
var context = new IndexedDBContext(files);
fs._unlink(context, path, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
IndexedDBFileSystem.prototype.read = function 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);
var context = new IndexedDBContext(files);
fs._read(context, fd, buffer, offset, length, position, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
IndexedDBFileSystem.prototype.readFile = function 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);
var context = new IndexedDBContext(files);
fs._readFile(context, path, options, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
IndexedDBFileSystem.prototype.write = function 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) {
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) {
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) {
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) {
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) {
};
function WebSQLFileSystem(name, flags) {
}
WebSQLFileSystem.prototype = new FileSystem();
IndexedDBFileSystem.prototype.constructor = WebSQLFileSystem;
return {
FileSystem: FileSystem
FileSystem: IndexedDBFileSystem
};
});

8
dist/idbfs.min.js vendored

File diff suppressed because one or more lines are too long

658
src/fs.js
View File

@ -136,7 +136,7 @@ define(function(require) {
function read_parent_directory_data(error, parentDirectoryNode) {
if(error) {
callback(error);
} else if(!parentDirectoryNode.type == MODE_DIRECTORY) {
} else if(parentDirectoryNode.mode !== MODE_DIRECTORY || !parentDirectoryNode.data) {
callback(new ENotDirectory('a component of the path prefix is not a directory'));
} else {
read_object(objectStore, parentDirectoryNode.data, get_node_id_from_parent_directory_data);
@ -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);
}
/*
@ -795,69 +766,6 @@ define(function(require) {
*/
function FileSystem(name, flags) {
var format = _(flags).contains(FS_FORMAT);
var that = this;
var deferred = when.defer();
this.promise = deferred.promise;
var openRequest = indexedDB.open(name);
openRequest.onupgradeneeded = function onupgradeneeded(event) {
var db = event.target.result;
if(db.objectStoreNames.contains(FILE_STORE_NAME)) {
db.deleteObjectStore(FILE_STORE_NAME);
}
var files = db.createObjectStore(FILE_STORE_NAME);
if(db.objectStoreNames.contains(METADATA_STORE_NAME)) {
db.deleteObjectStore(METADATA_STORE_NAME);
}
var metadata = db.createObjectStore(METADATA_STORE_NAME);
format = true;
};
openRequest.onsuccess = function onsuccess(event) {
var db = event.target.result;
var transaction = db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME);
function complete(error) {
that.db = db;
if(error) {
that.readyState = FS_ERROR;
deferred.reject(error);
} else {
that.readyState = FS_READY;
deferred.resolve();
}
}
if(format) {
var clearRequest = files.clear();
clearRequest.onsuccess = function onsuccess(event) {
make_root_directory(files, complete);
};
clearRequest.onerror = function onerror(error) {
complete(error);
};
} else {
complete();
}
};
openRequest.onerror = function onerror(error) {
this.readyState = FS_ERROR;
deferred.reject(error);
};
var nextDescriptor = 1;
var openFiles = {};
this.readyState = FS_PENDING;
this.db = null;
this.nextDescriptor = nextDescriptor;
this.openFiles = openFiles;
this.name = name;
}
FileSystem.prototype._allocate_descriptor = function _allocate_descriptor(openFileDescription) {
var fd = this.nextDescriptor ++;
@ -867,13 +775,9 @@ define(function(require) {
FileSystem.prototype._release_descriptor = function _release_descriptor(fd) {
delete this.openFiles[fd];
};
FileSystem.prototype.open = function open(path, flags, callback) {
FileSystem.prototype._open = function _open(context, path, flags, 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, fileNode) {
if(error) {
@ -897,7 +801,8 @@ define(function(require) {
deferred.reject(new EInvalid('flags is not valid'));
}
open_file(that, files, path, flags, check_result);
open_file(that, context, path, flags, check_result);
deferred.promise.then(
function(result) {
callback(undefined, result);
@ -906,13 +811,8 @@ define(function(require) {
callback(error);
}
);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
FileSystem.prototype.close = function close(fd, callback) {
FileSystem.prototype._close = function _close(fd, callback) {
var deferred = when.defer();
if(!_(this.openFiles).has(fd)) {
@ -931,13 +831,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) {
@ -948,7 +844,8 @@ define(function(require) {
}
}
make_directory(files, path, check_result);
make_directory(context, path, check_result);
deferred.promise.then(
function() {
callback();
@ -957,19 +854,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) {
@ -980,7 +868,8 @@ define(function(require) {
}
}
remove_directory(files, path, check_result);
remove_directory(context, path, check_result);
deferred.promise.then(
function() {
callback();
@ -989,19 +878,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) {
@ -1013,7 +893,7 @@ define(function(require) {
}
}
stat_file(files, path, check_result);
stat_file(context, path, check_result);
deferred.promise.then(
function(result) {
@ -1023,19 +903,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) {
@ -1052,7 +923,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(
@ -1063,19 +934,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) {
@ -1086,7 +948,7 @@ define(function(require) {
}
}
link_node(files, oldpath, newpath, check_result);
link_node(context, oldpath, newpath, check_result);
deferred.promise.then(
function(result) {
@ -1096,19 +958,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) {
@ -1119,7 +972,7 @@ define(function(require) {
}
}
unlink_node(files, path, check_result);
unlink_node(context, path, check_result);
deferred.promise.then(
function(result) {
@ -1129,19 +982,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;
@ -1162,7 +1006,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(
@ -1173,19 +1017,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' };
@ -1201,7 +1036,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);
@ -1209,7 +1044,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);
@ -1219,7 +1054,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);
@ -1246,19 +1081,10 @@ define(function(require) {
callback(error);
}
);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
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;
@ -1280,7 +1106,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(
@ -1291,19 +1117,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' };
@ -1323,7 +1140,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);
@ -1331,7 +1148,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);
@ -1349,22 +1166,15 @@ define(function(require) {
callback(error);
}
);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
FileSystem.prototype.getxattr = function getxattr(path, name, callback) {
FileSystem.prototype._getxattr = function _getxattr(path, name, callback) {
};
FileSystem.prototype.setxattr = function setxattr(path, name, value, callback) {
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) {
@ -1409,10 +1219,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'));
}
@ -1425,19 +1232,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) {
@ -1448,7 +1246,7 @@ define(function(require) {
}
}
read_directory(files, path, check_result);
read_directory(context, path, check_result);
deferred.promise.then(
function(result) {
@ -1458,31 +1256,22 @@ define(function(require) {
callback(error);
}
);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
FileSystem.prototype.utimes = function utimes(path, atime, mtime, callback) {
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);
}
}
@ -1503,34 +1292,365 @@ define(function(require) {
callback(error);
}
);
};
FileSystem.prototype._truncate = function _truncate(path, length, callback) {
};
FileSystem.prototype._ftruncate = function _ftruncate(fd, length, callback) {
};
FileSystem.prototype._symlink = function _symlink(fd, length, callback) {
};
FileSystem.prototype._readlink = function _readlink(fd, length, callback) {
};
FileSystem.prototype._realpath = function _realpath(fd, length, callback) {
};
FileSystem.prototype._lstat = function _lstat(fd, length, callback) {
};
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;
var deferred = when.defer();
this.promise = deferred.promise;
var openRequest = indexedDB.open(name);
openRequest.onupgradeneeded = function onupgradeneeded(event) {
var db = event.target.result;
if(db.objectStoreNames.contains(FILE_STORE_NAME)) {
db.deleteObjectStore(FILE_STORE_NAME);
}
var files = db.createObjectStore(FILE_STORE_NAME);
if(db.objectStoreNames.contains(METADATA_STORE_NAME)) {
db.deleteObjectStore(METADATA_STORE_NAME);
}
var metadata = db.createObjectStore(METADATA_STORE_NAME);
format = true;
};
openRequest.onsuccess = function onsuccess(event) {
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;
if(error) {
that.readyState = FS_ERROR;
deferred.reject(error);
} else {
that.readyState = FS_READY;
deferred.resolve();
}
}
if(format) {
var clearRequest = files.clear();
clearRequest.onsuccess = function onsuccess(event) {
make_root_directory(context, complete);
};
clearRequest.onerror = function onerror(error) {
complete(error);
};
} else {
complete();
}
};
openRequest.onerror = function onerror(error) {
this.readyState = FS_ERROR;
deferred.reject(error);
};
var nextDescriptor = 1;
var openFiles = {};
this.readyState = FS_PENDING;
this.db = null;
this.nextDescriptor = nextDescriptor;
this.openFiles = openFiles;
this.name = name;
}
IndexedDBFileSystem.prototype = new FileSystem();
IndexedDBFileSystem.prototype.constructor = IndexedDBFileSystem;
IndexedDBFileSystem.prototype.open = function open(path, flags, 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._open(context, path, flags, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
FileSystem.prototype.truncate = function truncate(path, length, callback) {
IndexedDBFileSystem.prototype.close = function close(fd, callback) {
this._close(fd, callback);
};
FileSystem.prototype.ftruncate = function ftruncate(fd, length, callback) {
IndexedDBFileSystem.prototype.mkdir = function 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);
var context = new IndexedDBContext(files);
fs._mkdir(context, path, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
FileSystem.prototype.symlink = function symlink(fd, length, callback) {
IndexedDBFileSystem.prototype.rmdir = function 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);
var context = new IndexedDBContext(files);
fs._rmdir(context, path, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
FileSystem.prototype.readlink = function readlink(fd, length, callback) {
IndexedDBFileSystem.prototype.stat = function 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);
var context = new IndexedDBContext(files);
fs._stat(context, path, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
FileSystem.prototype.realpath = function realpath(fd, length, callback) {
IndexedDBFileSystem.prototype.fstat = function 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);
var context = new IndexedDBContext(files);
fs._fstat(context, fd, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
FileSystem.prototype.lstat = function lstat(fd, length, callback) {
IndexedDBFileSystem.prototype.link = function 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);
var context = new IndexedDBContext(files);
fs._link(context, oldpath, newpath, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
IndexedDBFileSystem.prototype.unlink = function 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);
var context = new IndexedDBContext(files);
fs._unlink(context, path, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
IndexedDBFileSystem.prototype.read = function 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);
var context = new IndexedDBContext(files);
fs._read(context, fd, buffer, offset, length, position, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
IndexedDBFileSystem.prototype.readFile = function 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);
var context = new IndexedDBContext(files);
fs._readFile(context, path, options, callback);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
};
IndexedDBFileSystem.prototype.write = function 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) {
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) {
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) {
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) {
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) {
};
function WebSQLFileSystem(name, flags) {
}
WebSQLFileSystem.prototype = new FileSystem();
IndexedDBFileSystem.prototype.constructor = WebSQLFileSystem;
return {
FileSystem: FileSystem
FileSystem: IndexedDBFileSystem
};
});