diff --git a/examples/test.html b/examples/test.html index 5c70caf..b1adf96 100644 --- a/examples/test.html +++ b/examples/test.html @@ -17,6 +17,7 @@ require(["../../javascript-debug/debug", "src/filesystem"], function(debug, File debug.log("filesystem state: ", fs.state); fs.then(function(fs) { debug.log("filesystem state: ", fs.state); + fs.mkdir("/tmp"); }); }); diff --git a/src/filesystem.js b/src/filesystem.js index 894b776..ebb5b93 100644 --- a/src/filesystem.js +++ b/src/filesystem.js @@ -47,65 +47,26 @@ define(function(require) { FileError.TYPE_MISMATCH_ERR = 11; FileError.PATH_EXISTS_ERR = 12; - function Entry() {} - Entry.prototype.move = function move(parent, newName, successCallback, errorCallback) { - }; - Entry.prototype.copy = function copy(parent, newName, successCallback, errorCallback) { - }; - Entry.prototype.remove = function remove(successCallback, errorCallback) { - }; - Entry.prototype.getParent = function getParent(successCallback, errorCallback) { - }; - Entry.prototype.getMetadata = function getMetadata(successCallback, errorCallback) { - }; - /*Entry.prototype.toUrl = function toUrl(mimeType) { - };*/ - - function DirectoryEntry(fs, path, name, parent) { - this.isFile = false; - this.isDirectory = true; - this.name = name; - this.path = path; // FIXME: account for the parent path - this.filesystem = fs; - this.parent = parent; - } - DirectoryEntry.prototype = new Entry(); - DirectoryEntry.prototype.createReader = function createReader() { - }; - DirectoryEntry.prototype.removeRecursively = function removeRecursively(successCallback, errorCallback) { - }; - DirectoryEntry.prototype.getFile = function getFile(path, options, successCallback, errorCallback) { - }; - DirectoryEntry.prototype.getDirectory = function getDirectory(path, options, successCallback, errorCallback) { - }; - - function FileEntry(fs, path, name, parent) { - this.isFile = true; - this.isDirectory = false; - this.name = name; - this.path = path; // FIXME: account for the parent path - this.filesystem = fs; - this.parent = parent; - } - FileEntry.prototype = new Entry(); - FileEntry.prototype.createWriter = function createWriter(successCallback, errorCallback) { - }; - FileEntry.prototype.file = function file(successCallback, errorCallback) { + function DirectoryError(code) { + this.code = code; + // FIXME: add a message field with the text error }; + DirectoryError.PATH_EXISTS_ERR = 1; + DirectoryError.MISSING_PATH_COMPONENT_ERR = 2; var RO = "readonly", RW = "readwrite"; function FileSystem(name, optFormat) { var fs = this; - var FILES = "files"; + fs.OBJECT_STORE_NAME = "files"; fs.name = name || "default"; fs.pending = {}; fs.state = FileSystem.UNINITIALIZED; var deferred; fs.then; - fs.root; + fs.db; function updateState() { if(FileSystem.READY === fs.state && Object.keys(fs.pending).length > 0) { @@ -145,7 +106,6 @@ define(function(require) { return request; } - var db; var format = undefined !== optFormat ? optFormat : false; var deferred = when.defer(); fs.then = deferred.promise.then; @@ -155,11 +115,12 @@ define(function(require) { fs.state = FileSystem.PENDING; openRequest.onsuccess = function(e) { - db = e.target.result; + fs.db = e.target.result; + var db = fs.db; + var transaction = db.transaction([fs.OBJECT_STORE_NAME], RW); + var store = transaction.objectStore(fs.OBJECT_STORE_NAME); if(format) { - debug.info("format required"); - var transaction = db.transaction([FILES], RW); - var store = transaction.objectStore(FILES); + debug.info("format required"); var formatRequest = queuePending(store.put({}, "/")); formatRequest.onsuccess = function(e) { debug.info("format complete"); @@ -175,8 +136,8 @@ define(function(require) { db.deleteObjectStore("files"); } var store = db.createObjectStore("files"); - store.createIndex("parent", "parent"); - store.createIndex("name", "name"); + store.createIndex("parent", "parent", {unique: false}); + store.createIndex("name", "name", {unique: true}); format = true; }; @@ -184,6 +145,35 @@ define(function(require) { openRequest.onerror = function(e) { clearPending(openRequest); }; + + // API + + this.mkdir = function mkdir(path) { + debug.info("mkdir"); + var db = fs.db; + var d = when.defer(); + var transaction = db.transaction([this.OBJECT_STORE_NAME], RW); + var store = transaction.objectStore(fs.OBJECT_STORE_NAME); + var nameIndex = store.index("name"); + + var getRequest = queuePending(nameIndex.get(path)); + getRequest.onsuccess = function(e) { + clearPending(getRequest); + var result = e.target.result; + if(result) { + debug.info("mkdir PATH_EXISTS_ERR"); + d.reject(new DirectoryError(DirectoryError.PATH_EXISTS_ERR)); + } else { + var directoryRequest = queuePending(store.put({}, path)); + directoryRequest.onsuccess = function(e) { + debug.info("mkdir complete"); + clearPending(directoryRequest); + d.resolve(); + }; + } + }; + return d.promise; + } } FileSystem.READY = 0; FileSystem.PENDING = 1;