Added write() support.

This commit is contained in:
Alan Kligman 2012-10-12 17:46:12 -04:00
parent 714e249266
commit d21adc32d7
2 changed files with 80 additions and 28 deletions

View File

@ -20,15 +20,25 @@ require(["src/filesystem"], function(IDBFS) {
IDBFS.mount("default", function(error, fs) { IDBFS.mount("default", function(error, fs) {
fs.mkdir("/tmp", function(error) { fs.mkdir("/tmp", function(error) {
fs.open("/tmp/0", "CREATE", "RW", function(error, fd) { fs.open("/tmp/0", "CREATE", "RW", function(error, fd) {
var buffer = new Uint8Array(16);
fd.read(buffer, 0, 16, function(error, bytes_read, buffer) { var writeBuffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
fd.write(writeBuffer, function(error, bytes_written, buffer) {
if(!error) { if(!error) {
console.log("bytes read:", bytes_read); console.log("bytes written:", bytes_written);
} }
fs.close(fd, function(error) { fd.seek(-bytes_written);
fs.dump(document.getElementById("stdout")); var readBuffer = new Uint8Array(8);
fd.read(readBuffer, function(error, bytes_read, buffer) {
if(!error) {
console.log("bytes read:", bytes_read);
console.log("buffer:", buffer);
}
fs.close(fd, function(error) {
fs.dump(document.getElementById("stdout"));
});
}); });
}); });
}); });
}); });
}, true); }, true);

View File

@ -82,6 +82,7 @@ define(function(require) {
var E_NOTEMPTY = 0x4; var E_NOTEMPTY = 0x4;
var E_NOTDIR = 0x5; var E_NOTDIR = 0x5;
var E_BADF = 0x6; var E_BADF = 0x6;
var E_NOT_IMPLEMENTED = 0x7;
function genericIDBErrorHandler(scope, callback) { function genericIDBErrorHandler(scope, callback) {
return function(error) { return function(error) {
@ -130,7 +131,7 @@ define(function(require) {
} }
} }
function read(buffer, offset, bytes, callback) { function read(buffer, callback) {
debug.info("read -->"); debug.info("read -->");
var onerror = genericIDBErrorHandler("read", callback); var onerror = genericIDBErrorHandler("read", callback);
if(!start()) { if(!start()) {
@ -144,8 +145,8 @@ define(function(require) {
var oid = ofd.entry["object-id"]; var oid = ofd.entry["object-id"];
var getRequest = store.get(oid); var getRequest = store.get(oid);
getRequest.onsuccess = function(e) { getRequest.onsuccess = function(e) {
var file = e.target.result; var storedBuffer = e.target.result;
if(!file) { if(!storedBuffer) {
// There's no file data, so return zero bytes read // There's no file data, so return zero bytes read
end(); end();
debug.info("read <--"); debug.info("read <--");
@ -154,47 +155,88 @@ define(function(require) {
} }
} else { } else {
// Make sure we're not going to read past the end of the file // Make sure we're not going to read past the end of the file
bytes = (ofd.pointer + bytes > file.size) ? file.size - ofd.pointer : bytes; var bytes = (ofd.pointer + buffer.length > storedBuffer.length) ? (storedBuffer.length - ofd.pointer) : buffer.length;
var reader = new FileReader(); // Copy the desired region from the file into the buffer supplied
reader.readAsArrayBuffer(file); var storedBufferView = storedBuffer.subarray(ofd.pointer, ofd.pointer + bytes);
reader.onload = function(e) { buffer.set(storedBufferView);
// Copy the desired region from the file into the buffer supplied ofd.pointer += bytes;
var source = e.target.result; end();
var sourceView = new Uint8Array(data).subarray(ofd.pointer, ofd.pointer + bytes); debug.info("read <--");
var target = buffer; if(callback && "function" === typeof callback) {
target.set(sourceView, offset); callback.call(undefined, undefined, bytes, buffer);
end();
debug.info("read <--");
if(callback && "function" === typeof callback) {
callback.call(undefined, undefined, bytes, buffer);
}
} }
reader.onerror = onerror;
} }
}; };
getRequest.onerror = onerror; getRequest.onerror = onerror;
} else if(MIME_DIRECTORY === ofd.entry["content-type"]) { } else if(MIME_DIRECTORY === ofd.entry["content-type"]) {
// NOT IMPLEMENTED
onerror(new IDBFSError(T_READ, E_NOT_IMPLEMENTED))
} }
} }
function write(buffer, offset, bytes, callback) { function write(buffer, callback) {
debug.info("write -->"); debug.info("write -->");
var onerror = genericIDBErrorHandler("read", callback); var onerror = genericIDBErrorHandler("write", callback);
if(!start()) { if(!start()) {
onerror(new IDBFSError(T_READ, E_BADF)); onerror(new IDBFSError(T_READ, E_BADF));
return; return;
} }
if(OM_RO === ofd.mode) {
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);
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 getRequest = fileStore.get(oid);
getRequest.onsuccess = function(e) {
var storedBuffer = e.target.result;
if(!storedBuffer) {
storedBuffer = new Uint8Array();
}
var bytes = buffer.length;
var size = (storedBuffer.length > ofd.pointer + bytes) ? storedBuffer.length : ofd.pointer + bytes;
var writeBuffer = new Uint8Array(size);
writeBuffer.set(storedBuffer);
writeBuffer.set(buffer);
ofd.pointer += bytes;
var putRequest = fileStore.put(writeBuffer, oid);
putRequest.onsuccess = function(e) {
var readMetadataRequest = metaStore.get(ofd.entry["name"]);
readMetadataRequest.onsuccess = function(e) {
var entry = e.target.result;
entry = makeFileEntry(entry["name"], entry["object-id"], size);
ofd.entry = entry;
var writeMetadataRequest = metaStore.put(entry, entry["name"]);
writeMetadataRequest.onsuccess = function(e) {
end();
debug.info("write <--");
if(callback && "function" === typeof callback) {
callback.call(undefined, undefined, size, buffer);
}
};
writeMetadataRequest.onerror = onerror;
}
readMetadataRequest.onerror = onerror;
};
putRequest.onerror = onerror;
};
getRequest.onerror = onerror;
} }
var SW_SET = "SET"; var SW_SET = "SET";
var SW_CURRENT = "CURRENT"; var SW_CURRENT = "CURRENT";
var SW_END = "END"; var SW_END = "END";
function seek(offset, whence, callback) { function seek(offset, whence) {
whence = whence || SW_CURRENT;
if(SW_SET === whence) {
ofd.pointer = offset;
} else if(SW_CURRENT === whence) {
ofd.pointer += offset;
} else if(SW_END === whence) {
ofd.pointer = ofd.entry["size"] + offset;
}
} }
this.read = read; this.read = read;