2012-11-08 05:12:41 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2012, Alan Kligman
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
|
|
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
|
|
Neither the name of the Mozilla Foundation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
define(function(require) {
|
|
|
|
// 'use strict';
|
|
|
|
|
|
|
|
var when = require("when");
|
|
|
|
var _ = require("lodash");
|
|
|
|
var path = require("src/path");
|
|
|
|
var guid = require("src/guid");
|
|
|
|
|
|
|
|
var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
|
|
|
|
|
|
|
|
var METADATA_STORE_NAME = "metadata";
|
|
|
|
var FILE_STORE_NAME = "files";
|
|
|
|
var PARENT_INDEX = "parent";
|
|
|
|
var PARENT_INDEX_KEY_PATH = "parent";
|
|
|
|
|
|
|
|
var IDB_RO = "readonly";
|
|
|
|
var IDB_RW = "readwrite";
|
|
|
|
|
|
|
|
function parseFlags(flags) {
|
|
|
|
var i;
|
|
|
|
if(undefined === flags) {
|
|
|
|
return [];
|
|
|
|
} else if("string" === typeof flags) {
|
|
|
|
flags = flags.split(",");
|
|
|
|
for(i = 0, l = flags.length; i < l; ++ i) {
|
|
|
|
flags[i] = flags[i].trim().toUpperCase();
|
|
|
|
}
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function runCallback(callback) {
|
|
|
|
if("function" === typeof callback) {
|
2012-11-08 10:29:05 +00:00
|
|
|
callback.apply(undefined, Array.prototype.slice.call(arguments, 1));
|
2012-11-08 05:12:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function Data(handle, bytes) {
|
|
|
|
return {
|
|
|
|
handle: handle || guid(),
|
|
|
|
bytes: bytes || undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function File(handle, size, atime, ctime, mtime, nlinks, type, flags, data, xattrs) {
|
|
|
|
var now = Date.now();
|
|
|
|
return {
|
|
|
|
handle: handle || guid(),
|
|
|
|
size: size || 0,
|
|
|
|
atime: atime || now,
|
|
|
|
ctime: ctime || now,
|
|
|
|
mtime: mtime || now,
|
|
|
|
type: type || "application/octet-stream",
|
|
|
|
flags: flags || "",
|
|
|
|
xattrs: xattrs || {},
|
|
|
|
data: data || guid()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var FILE_ENTRY_MIME_TYPE = "application/file-entry";
|
|
|
|
function FileEntry(fullpath, file) {
|
|
|
|
return {
|
|
|
|
name: fullpath,
|
|
|
|
parent: path.dirname(fullpath),
|
|
|
|
file: file,
|
|
|
|
type: FILE_ENTRY_MIME_TYPE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var DIRECTORY_ENTRY_MIME_TYPE = "application/directory-entry";
|
|
|
|
function DirectoryEntry(fullpath, atime, ctime, mtime, xattrs) {
|
|
|
|
var now = Date.now();
|
|
|
|
return {
|
|
|
|
name: fullpath,
|
|
|
|
parent: path.dirname(fullpath),
|
|
|
|
atime: atime || now,
|
|
|
|
ctime: ctime || now,
|
|
|
|
mtime: mtime || now,
|
|
|
|
xattrs: xattrs || {},
|
|
|
|
type: DIRECTORY_ENTRY_MIME_TYPE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function FileSystem(db) {
|
|
|
|
this._db = db;
|
|
|
|
this._deferred = when.defer();
|
|
|
|
this._deferred.resolve();
|
|
|
|
this._pending = 0;
|
|
|
|
this._mounted = true;
|
|
|
|
|
2012-11-08 10:29:05 +00:00
|
|
|
this.Context = FileSystemContext.bind(undefined, this);
|
2012-11-08 05:12:41 +00:00
|
|
|
}
|
|
|
|
FileSystem.prototype._request = function _request(transaction) {
|
|
|
|
var fs = this;
|
|
|
|
if(!fs._pending) {
|
|
|
|
fs._deferred = when.defer();
|
|
|
|
}
|
|
|
|
++ fs._pending;
|
|
|
|
transaction.oncomplete = function(e) {
|
|
|
|
-- fs._pending;
|
|
|
|
if(!fs._pending) {
|
|
|
|
fs._deferred.resolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
FileSystem.prototype.open = function open(path, flags, mode, callback) {
|
|
|
|
|
|
|
|
};
|
|
|
|
FileSystem.prototype.close = function close(descriptor, callback) {
|
|
|
|
|
|
|
|
};
|
|
|
|
FileSystem.prototype.mkdir = function mkdir(fullpath, callback, optTransaction) {
|
2012-11-08 10:29:05 +00:00
|
|
|
var fs = this;
|
2012-11-08 05:12:41 +00:00
|
|
|
fullpath = path.normalize(fullpath);
|
|
|
|
|
2012-11-08 10:29:05 +00:00
|
|
|
var transaction = optTransaction || fs._db.transaction([METADATA_STORE_NAME], IDB_RW);
|
2012-11-08 05:12:41 +00:00
|
|
|
this._request(transaction);
|
|
|
|
|
|
|
|
var metaStore = transaction.objectStore(METADATA_STORE_NAME);
|
|
|
|
|
|
|
|
var getRequest = metaStore.get(fullpath);
|
2012-11-08 10:29:05 +00:00
|
|
|
getRequest.onsuccess = function(e) {
|
2012-11-08 05:12:41 +00:00
|
|
|
var getResult = e.target.result;
|
|
|
|
if(getResult) {
|
|
|
|
handleError(transaction, new error.EPathExists());
|
|
|
|
} else {
|
|
|
|
var entry = new DirectoryEntry(fullpath);
|
|
|
|
var putRequest = metaStore.put(entry, fullpath);
|
2012-11-08 10:29:05 +00:00
|
|
|
putRequest.onsuccess = function(e) {
|
|
|
|
runCallback(callback);
|
2012-11-08 05:12:41 +00:00
|
|
|
};
|
|
|
|
putRequest.onerror = function(e) {
|
2012-11-08 10:29:05 +00:00
|
|
|
runCallback(callback, e);
|
2012-11-08 05:12:41 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
getRequest.onerror = function(e) {
|
2012-11-08 10:29:05 +00:00
|
|
|
runCallback(callback, e);
|
2012-11-08 05:12:41 +00:00
|
|
|
}
|
|
|
|
};
|
2012-11-08 10:29:05 +00:00
|
|
|
FileSystem.prototype.rmdir = function rmdir(fullpath, callback, optTransaction) {
|
|
|
|
var fs = this;
|
2012-11-08 05:12:41 +00:00
|
|
|
fullpath = path.normalize(fullpath);
|
|
|
|
|
2012-11-08 10:29:05 +00:00
|
|
|
var transaction = optTransaction || fs._db.transaction([METADATA_STORE_NAME], IDB_RW);
|
2012-11-08 05:12:41 +00:00
|
|
|
this._request(transaction);
|
|
|
|
|
|
|
|
var metaStore = transaction.objectStore(METADATA_STORE_NAME);
|
|
|
|
var parentIndex = metaStore.index(PARENT_INDEX);
|
|
|
|
|
|
|
|
var getRequest = metaStore.get(fullpath);
|
|
|
|
getRequest.onsuccess = function(e) {
|
|
|
|
var getResult = e.target.result;
|
|
|
|
if(!getResult) {
|
|
|
|
runCallback(callback, new error.ENoEntry());
|
|
|
|
} else {
|
|
|
|
var contentRequest = parentIndex.get(fullpath);
|
|
|
|
contentRequest.onsuccess = function(e) {
|
|
|
|
var contentResult = e.target.result;
|
|
|
|
if(contentResult) {
|
|
|
|
runCallback(callback, new error.ENotEmpty());
|
|
|
|
} else {
|
|
|
|
var removeRequest = metaStore.delete(fullpath);
|
|
|
|
removeRequest.onsuccess = function(e) {
|
|
|
|
runCallback(callback);
|
|
|
|
};
|
|
|
|
removeRequest.onerror = function(e) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
contentRequest.onerror = function(e) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
getRequest.onerror = function(e) {
|
2012-11-08 10:29:05 +00:00
|
|
|
runCallback(callback, e);
|
2012-11-08 05:12:41 +00:00
|
|
|
}
|
|
|
|
};
|
2012-11-08 10:29:05 +00:00
|
|
|
FileSystem.prototype.stat = function stat(fullpath, callback, optTransaction) {
|
|
|
|
var fs = this;
|
|
|
|
fullpath = path.normalize(fullpath);
|
|
|
|
|
|
|
|
var transaction = optTransaction || fs._db.transaction([METADATA_STORE_NAME], IDB_RW);
|
|
|
|
this._request(transaction);
|
2012-11-08 05:12:41 +00:00
|
|
|
|
2012-11-08 10:29:05 +00:00
|
|
|
var metaStore = transaction.objectStore(METADATA_STORE_NAME);
|
|
|
|
|
|
|
|
var getRequest = metaStore.get(fullpath);
|
|
|
|
getRequest.onsuccess = function(e) {
|
|
|
|
var getResult = e.target.result;
|
|
|
|
if(!getResult) {
|
|
|
|
runCallback(callback, new error.ENoEntry());
|
|
|
|
} else {
|
|
|
|
runCallback(callback, null, getResult);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
getRequest.onerror = function(e) {
|
|
|
|
runCallback(callback, e);
|
|
|
|
};
|
2012-11-08 05:12:41 +00:00
|
|
|
};
|
|
|
|
FileSystem.prototype.link = function link(oldpath, newpath, callback) {
|
|
|
|
|
|
|
|
};
|
2012-11-08 10:29:05 +00:00
|
|
|
FileSystem.prototype.unlink = function unlink(fullpath, callback) {
|
|
|
|
|
|
|
|
};
|
|
|
|
FileSystem.prototype.setxattr = function setxattr(fullpath, name, value, callback) {
|
|
|
|
|
|
|
|
};
|
|
|
|
FileSystem.prototype.getxattr = function getxattr(fullpath, name, callback) {
|
2012-11-08 05:12:41 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
function FileSystemContext(fs, optCwd) {
|
|
|
|
this._fs = fs;
|
|
|
|
this._cwd = optCwd || "/";
|
|
|
|
}
|
2012-11-08 10:29:05 +00:00
|
|
|
FileSystemContext.prototype.chdir = function chdir(rpath) {
|
2012-11-08 05:12:41 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
FileSystemContext.prototype.getcwd = function getcwd() {
|
|
|
|
return this._cwd;
|
|
|
|
};
|
2012-11-08 10:29:05 +00:00
|
|
|
FileSystemContext.prototype.open = function open(rpath, flags, mode, callback) {
|
2012-11-08 05:12:41 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
FileSystemContext.prototype.close = function close(descriptor, callback) {
|
|
|
|
|
|
|
|
};
|
2012-11-08 10:29:05 +00:00
|
|
|
FileSystemContext.prototype.mkdir = function mkdir(rpath, callback) {
|
|
|
|
this._fs.mkdir(path.normalize(this._cwd + "/" + rpath), callback);
|
|
|
|
};
|
|
|
|
FileSystemContext.prototype.rmdir = function rmdir(rpath, callback) {
|
|
|
|
this._fs.rmdir(path.normalize(this._cwd + "/" + rpath), callback);
|
|
|
|
};
|
|
|
|
FileSystemContext.prototype.stat = function stat(rpath, callback) {
|
|
|
|
this._fs.stat(path.normalize(this._cwd + "/" + rpath), callback);
|
2012-11-08 05:12:41 +00:00
|
|
|
};
|
2012-11-08 10:29:05 +00:00
|
|
|
FileSystemContext.prototype.link = function link(oldpath, newpath, callback) {
|
2012-11-08 05:12:41 +00:00
|
|
|
|
|
|
|
};
|
2012-11-08 10:29:05 +00:00
|
|
|
FileSystemContext.prototype.unlink = function unlink(rpath, callback) {
|
2012-11-08 05:12:41 +00:00
|
|
|
|
|
|
|
};
|
2012-11-08 10:29:05 +00:00
|
|
|
FileSystemContext.prototype.setxattr = function setxattr(rpath, name, value, callback) {
|
2012-11-08 05:12:41 +00:00
|
|
|
|
|
|
|
};
|
2012-11-08 10:29:05 +00:00
|
|
|
FileSystemContext.prototype.getxattr = function getxattr(rpath, name, callback) {
|
2012-11-08 05:12:41 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
function OpenFile(handle, flags, mode) {
|
|
|
|
this._pending = 0;
|
|
|
|
this._valid = true;
|
|
|
|
this._pointer = 0;
|
|
|
|
this._flags = flags;
|
|
|
|
this._mode = mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
function FileDescriptor(openfile, flags) {
|
|
|
|
this._openfile = openfile;
|
|
|
|
}
|
|
|
|
FileDescriptor.prototype.seek = function seek(offset, whence) {
|
|
|
|
|
|
|
|
};
|
|
|
|
FileDescriptor.prototype.read = function read(buffer, callback) {
|
|
|
|
|
|
|
|
};
|
|
|
|
FileDescriptor.prototype.write = function write(buffer, callback) {
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
var MNT_FORMAT = "FORMAT";
|
|
|
|
function mount(name, flags, callback) {
|
|
|
|
flags = parseFlags(flags);
|
|
|
|
var format = _(flags).contains(MNT_FORMAT);
|
|
|
|
|
|
|
|
var openRequest = indexedDB.open(name);
|
|
|
|
openRequest.onupgradeneeded = function(e) {
|
|
|
|
var db = e.target.result;
|
|
|
|
|
|
|
|
if(db.objectStoreNames.contains(METADATA_STORE_NAME)) {
|
|
|
|
db.deleteObjectStore(METADATA_STORE_NAME);
|
|
|
|
}
|
|
|
|
if(db.objectStoreNames.contains(FILE_STORE_NAME)) {
|
|
|
|
db.deleteObjectStore(FILE_STORE_NAME);
|
|
|
|
}
|
|
|
|
var metadata = db.createObjectStore(METADATA_STORE_NAME);
|
|
|
|
var files = db.createObjectStore(FILE_STORE_NAME);
|
|
|
|
metadata.createIndex(PARENT_INDEX, PARENT_INDEX_KEY_PATH, {unique: false});
|
|
|
|
|
|
|
|
format = true;
|
|
|
|
};
|
|
|
|
openRequest.onsuccess = function(e) {
|
|
|
|
var db = e.target.result;
|
|
|
|
var fs = new FileSystem(db);
|
2012-11-08 10:29:05 +00:00
|
|
|
var context = new fs.Context();
|
2012-11-08 05:12:41 +00:00
|
|
|
|
|
|
|
if(format) {
|
|
|
|
var transaction = db.transaction([METADATA_STORE_NAME], IDB_RW);
|
|
|
|
var metaStore = transaction.objectStore(METADATA_STORE_NAME);
|
|
|
|
|
|
|
|
var clearRequest = metaStore.clear();
|
|
|
|
clearRequest.onsuccess = function() {
|
|
|
|
fs.mkdir("/", function(error) {
|
|
|
|
if(error) {
|
|
|
|
runCallback(callback, error);
|
|
|
|
} else {
|
|
|
|
runCallback(callback, null, context);
|
|
|
|
}
|
|
|
|
}, transaction);
|
|
|
|
};
|
|
|
|
clearRequest.onerror = function(e) {
|
2012-11-08 10:29:05 +00:00
|
|
|
console.log(e);
|
2012-11-08 05:12:41 +00:00
|
|
|
};
|
2012-11-08 10:29:05 +00:00
|
|
|
} else {
|
|
|
|
runCallback(callback, null, context)
|
2012-11-08 05:12:41 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
openRequest.onerror = function(e) {
|
2012-11-08 10:29:05 +00:00
|
|
|
console.log(e);
|
2012-11-08 05:12:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function umount(fs, callback) {
|
|
|
|
fs._db.close();
|
|
|
|
fs._deferred.then(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
var IDBFS = {
|
|
|
|
mount: mount,
|
|
|
|
umount: umount
|
|
|
|
};
|
|
|
|
|
|
|
|
return IDBFS;
|
|
|
|
|
|
|
|
});
|