Support for unlink().

This commit is contained in:
Alan Kligman 2012-10-12 18:54:12 -04:00
parent d21adc32d7
commit 4dc7474fd5
2 changed files with 84 additions and 35 deletions

View File

@ -34,7 +34,9 @@ require(["src/filesystem"], function(IDBFS) {
console.log("buffer:", buffer); console.log("buffer:", buffer);
} }
fs.close(fd, function(error) { fs.close(fd, function(error) {
fs.dump(document.getElementById("stdout")); fs.unlink("/tmp/0", function(error) {
fs.dump(document.getElementById("stdout"));
});
}); });
}); });
}); });

View File

@ -33,8 +33,8 @@ define(function(require) {
return { return {
"parent": parent, "parent": parent,
"name": name, "name": name,
"content-type": "application/directory", "contenttype": "application/directory",
"last-modified": modtime || Date.now() "lastmodified": modtime || Date.now()
} }
} }
@ -43,10 +43,10 @@ define(function(require) {
return { return {
"parent": parent, "parent": parent,
"name": name, "name": name,
"content-type": "application/file", "contenttype": "application/file",
"last-modified": modtime || Date.now(), "lastmodified": modtime || Date.now(),
"size": size || 0, "size": size || 0,
"object-id": oid || guid() "oid": oid || guid()
} }
} }
@ -56,6 +56,8 @@ define(function(require) {
var NAME_INDEX_KEY_PATH = "name"; var NAME_INDEX_KEY_PATH = "name";
var PARENT_INDEX = "parent"; var PARENT_INDEX = "parent";
var PARENT_INDEX_KEY_PATH = "parent"; var PARENT_INDEX_KEY_PATH = "parent";
var OBJECT_ID_INDEX = "oid";
var OBJECT_ID_INDEX_KEY_PATH = "oid";
var MIME_DIRECTORY = "application/directory"; var MIME_DIRECTORY = "application/directory";
var MIME_FILE = "application/file"; var MIME_FILE = "application/file";
@ -74,6 +76,7 @@ define(function(require) {
var T_RMDIR = 0x4; var T_RMDIR = 0x4;
var T_CLOSE = 0x5; var T_CLOSE = 0x5;
var T_READ = 0x6; var T_READ = 0x6;
var T_UNLINK = 0x7;
// Codes // Codes
var E_EXIST = 0x0; var E_EXIST = 0x0;
var E_ISDIR = 0x1; var E_ISDIR = 0x1;
@ -139,10 +142,13 @@ define(function(require) {
return; return;
} }
transaction = db.transaction([FILE_STORE_NAME], IDB_RO); transaction = db.transaction([FILE_STORE_NAME], IDB_RO);
transaction.oncomplete = function(e) {
end();
}
var store = transaction.objectStore(FILE_STORE_NAME); var store = transaction.objectStore(FILE_STORE_NAME);
if(MIME_FILE === ofd.entry["content-type"]) { if(MIME_FILE === ofd.entry["contenttype"]) {
var oid = ofd.entry["object-id"]; var oid = ofd.entry["oid"];
var getRequest = store.get(oid); var getRequest = store.get(oid);
getRequest.onsuccess = function(e) { getRequest.onsuccess = function(e) {
var storedBuffer = e.target.result; var storedBuffer = e.target.result;
@ -160,7 +166,6 @@ define(function(require) {
var storedBufferView = storedBuffer.subarray(ofd.pointer, ofd.pointer + bytes); var storedBufferView = storedBuffer.subarray(ofd.pointer, ofd.pointer + bytes);
buffer.set(storedBufferView); buffer.set(storedBufferView);
ofd.pointer += bytes; ofd.pointer += bytes;
end();
debug.info("read <--"); debug.info("read <--");
if(callback && "function" === typeof callback) { if(callback && "function" === typeof callback) {
callback.call(undefined, undefined, bytes, buffer); callback.call(undefined, undefined, bytes, buffer);
@ -168,7 +173,7 @@ define(function(require) {
} }
}; };
getRequest.onerror = onerror; getRequest.onerror = onerror;
} else if(MIME_DIRECTORY === ofd.entry["content-type"]) { } else if(MIME_DIRECTORY === ofd.entry["contenttype"]) {
// NOT IMPLEMENTED // NOT IMPLEMENTED
onerror(new IDBFSError(T_READ, E_NOT_IMPLEMENTED)) onerror(new IDBFSError(T_READ, E_NOT_IMPLEMENTED))
} }
@ -177,18 +182,21 @@ define(function(require) {
function write(buffer, callback) { function write(buffer, callback) {
debug.info("write -->"); debug.info("write -->");
var onerror = genericIDBErrorHandler("write", callback); var onerror = genericIDBErrorHandler("write", callback);
if(!start()) {
onerror(new IDBFSError(T_READ, E_BADF));
return;
}
if(OM_RO === ofd.mode) { if(OM_RO === ofd.mode) {
onerror(new IDBFSError(T_READ, E_BADF)); onerror(new IDBFSError(T_READ, E_BADF));
return; return;
} }
if(!start()) {
onerror(new IDBFSError(T_READ, E_BADF));
return;
}
transaction = db.transaction([METADATA_STORE_NAME, FILE_STORE_NAME], IDB_RW); transaction = db.transaction([METADATA_STORE_NAME, FILE_STORE_NAME], IDB_RW);
transaction.oncomplete = function(e) {
end();
}
var metaStore = transaction.objectStore(METADATA_STORE_NAME); var metaStore = transaction.objectStore(METADATA_STORE_NAME);
var fileStore = transaction.objectStore(FILE_STORE_NAME); var fileStore = transaction.objectStore(FILE_STORE_NAME);
var oid = ofd.entry["object-id"]; var oid = ofd.entry["oid"];
var getRequest = fileStore.get(oid); var getRequest = fileStore.get(oid);
getRequest.onsuccess = function(e) { getRequest.onsuccess = function(e) {
var storedBuffer = e.target.result; var storedBuffer = e.target.result;
@ -206,11 +214,10 @@ define(function(require) {
var readMetadataRequest = metaStore.get(ofd.entry["name"]); var readMetadataRequest = metaStore.get(ofd.entry["name"]);
readMetadataRequest.onsuccess = function(e) { readMetadataRequest.onsuccess = function(e) {
var entry = e.target.result; var entry = e.target.result;
entry = makeFileEntry(entry["name"], entry["object-id"], size); entry = makeFileEntry(entry["name"], entry["oid"], size);
ofd.entry = entry; ofd.entry = entry;
var writeMetadataRequest = metaStore.put(entry, entry["name"]); var writeMetadataRequest = metaStore.put(entry, entry["name"]);
writeMetadataRequest.onsuccess = function(e) { writeMetadataRequest.onsuccess = function(e) {
end();
debug.info("write <--"); debug.info("write <--");
if(callback && "function" === typeof callback) { if(callback && "function" === typeof callback) {
callback.call(undefined, undefined, size, buffer); callback.call(undefined, undefined, size, buffer);
@ -289,7 +296,7 @@ define(function(require) {
createRequest.onerror = onerror; createRequest.onerror = onerror;
} }
} else { } else {
if(entry["content-type"] === MIME_DIRECTORY && mode === OM_RW) { if(entry["contenttype"] === MIME_DIRECTORY && mode === OM_RW) {
onerror(new IDBFSError(T_OPEN, E_ISDIR)); onerror(new IDBFSError(T_OPEN, E_ISDIR));
return; return;
} else { } else {
@ -414,6 +421,7 @@ define(function(require) {
} }
} }
}; };
getRequest.onerror = onerror;
} }
function link(oldpath, newpath, callback) { function link(oldpath, newpath, callback) {
@ -421,7 +429,38 @@ define(function(require) {
} }
function unlink(pathname, callback) { function unlink(pathname, callback) {
debug.info("unlink -->");
pathname = path.normalize(pathname);
var transaction = db.transaction([METADATA_STORE_NAME], IDB_RW);
var metaStore = transaction.objectStore(METADATA_STORE_NAME);
var nameIndex = metaStore.index(NAME_INDEX);
var onerror = genericIDBErrorHandler("unlink", callback);
stat(transaction, pathname, function(error, entry) {
if(error) {
onerror(new IDBFSError(T_UNLINK, error));
return;
}
if(MIME_DIRECTORY === entry["contenttype"]) {
onerror(new IDBFSError(T_UNLINK, E_ISDIR));
return;
}
var unlinkRequest = metaStore.delete(entry["name"]);
unlinkRequest.onsuccess = function(e) {
// We don't support links, so this entry is the only entry for the file data
var transaction = db.transaction([FILE_STORE_NAME], IDB_RW);
var fileStore = transaction.objectStore(FILE_STORE_NAME);
var deleteRequest = fileStore.delete(entry["oid"]);
deleteRequest.onsuccess = function(e) {
debug.info("unlink <--");
if(callback && "function" === typeof callback) {
callback.call(undefined, undefined);
}
};
deleteRequest.onerror = onerror;
};
unlinkRequest.onerror = onerror;
});
} }
function api() { function api() {
@ -442,25 +481,32 @@ define(function(require) {
this.mkdir = mkdir; this.mkdir = mkdir;
this.rmdir = rmdir; this.rmdir = rmdir;
this.stat = stat; this.stat = stat;
this.link = link; // this.link = link;
this.unlink = unlink; this.unlink = unlink;
this.api = api; this.api = api;
// DEBUG // DEBUG
function dump(element) { function dump(element) {
element.innerHTML = ""; element.innerHTML = "Metadata://<br>";
var transaction = db.transaction([METADATA_STORE_NAME], IDB_RO); var transaction = db.transaction([METADATA_STORE_NAME, FILE_STORE_NAME], IDB_RO);
var store = transaction.objectStore(METADATA_STORE_NAME); var metaStore = transaction.objectStore(METADATA_STORE_NAME);
var cursorRequest = store.openCursor(); var fileStore = transaction.objectStore(FILE_STORE_NAME);
cursorRequest.onsuccess = function(e) { var metaRequest = metaStore.openCursor();
var cursor = e.target.result; metaRequest.onsuccess = function(e) {
if(cursor) { var metaCursor = e.target.result;
var getRequest = store.get(cursor.key); if(metaCursor) {
getRequest.onsuccess = function(e) { element.innerHTML += JSON.stringify(metaCursor.value) + "<br>";
var result = e.target.result; metaCursor.continue();
element.innerHTML += JSON.stringify(result) + "<br>"; } else {
cursor.continue(); element.innerHTML += "Files://<br>"
}; fileRequest = fileStore.openCursor();
fileRequest.onsuccess = function(e) {
var fileCursor = e.target.result;
if(fileCursor) {
element.innerHTML += JSON.stringify(fileCursor.key) + "<br>";
fileCursor.continue();
}
}
} }
}; };
} }
@ -483,6 +529,7 @@ define(function(require) {
var metadata = db.createObjectStore(METADATA_STORE_NAME); var metadata = db.createObjectStore(METADATA_STORE_NAME);
metadata.createIndex(PARENT_INDEX, PARENT_INDEX_KEY_PATH, {unique: false}); metadata.createIndex(PARENT_INDEX, PARENT_INDEX_KEY_PATH, {unique: false});
metadata.createIndex(NAME_INDEX, NAME_INDEX_KEY_PATH, {unique: true}); metadata.createIndex(NAME_INDEX, NAME_INDEX_KEY_PATH, {unique: true});
metadata.createIndex(OBJECT_ID_INDEX, OBJECT_ID_INDEX_KEY_PATH, {unique: false});
var files = db.createObjectStore(FILE_STORE_NAME); var files = db.createObjectStore(FILE_STORE_NAME);
optFormat = true; optFormat = true;