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");
|
2012-12-14 01:24:07 +00:00
|
|
|
var Path = require("src/path");
|
2012-11-08 05:12:41 +00:00
|
|
|
var guid = require("src/guid");
|
2012-11-30 00:46:35 +00:00
|
|
|
var error = require("src/error");
|
2012-11-14 00:05:57 +00:00
|
|
|
require("crypto-js/rollups/sha256"); var Crypto = CryptoJS;
|
2012-11-08 05:12:41 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-30 00:46:35 +00:00
|
|
|
function runcallback(callback) {
|
2012-11-08 05:12:41 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-30 00:46:35 +00:00
|
|
|
function hash(string) {
|
|
|
|
return Crypto.SHA256(string).toString(Crypto.enc.hex);
|
|
|
|
}
|
|
|
|
|
2012-11-14 00:05:57 +00:00
|
|
|
function Data(bytes) {
|
2012-11-08 05:12:41 +00:00
|
|
|
return {
|
|
|
|
bytes: bytes || undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-30 00:46:35 +00:00
|
|
|
var FILE_MIME_TYPE = "application/file";
|
|
|
|
var DIRECTORY_MIME_TYPE = "application/directory";
|
|
|
|
|
|
|
|
var BINARY_MIME_TYPE = "application/octet-stream";
|
|
|
|
var JSON_MIME_TYPE = "application/json";
|
|
|
|
|
|
|
|
var DEFAULT_DATA_TYPE = BINARY_MIME_TYPE;
|
|
|
|
function File(mode, data, type, size, version, atime, ctime, mtime, flags, xattrs, links) {
|
2012-11-08 05:12:41 +00:00
|
|
|
var now = Date.now();
|
|
|
|
return {
|
|
|
|
size: size || 0,
|
|
|
|
atime: atime || now,
|
|
|
|
ctime: ctime || now,
|
|
|
|
mtime: mtime || now,
|
2012-11-30 00:46:35 +00:00
|
|
|
mode: mode || FILE_MIME_TYPE,
|
2012-11-08 05:12:41 +00:00
|
|
|
flags: flags || "",
|
|
|
|
xattrs: xattrs || {},
|
2012-11-14 00:05:57 +00:00
|
|
|
data: data || Crypto.SHA256(guid()).toString(Crypto.enc.hex),
|
2012-11-30 00:46:35 +00:00
|
|
|
type: type || DEFAULT_DATA_TYPE,
|
|
|
|
links: links || 0,
|
|
|
|
version: version || 0,
|
|
|
|
id: Crypto.SHA256(guid()).toString(Crypto.enc.hex)
|
2012-11-08 05:12:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-30 00:46:35 +00:00
|
|
|
function signature(file) {
|
|
|
|
// Compute file signature based on file id and version
|
2012-11-08 05:12:41 +00:00
|
|
|
}
|
|
|
|
|
2012-12-01 00:09:14 +00:00
|
|
|
function Stats(size, handle, atime, ctime, mtime, links) {
|
2012-11-14 00:05:57 +00:00
|
|
|
return {
|
|
|
|
size: size,
|
2012-12-01 00:09:14 +00:00
|
|
|
handle: handle,
|
2012-11-14 00:05:57 +00:00
|
|
|
atime: atime,
|
|
|
|
ctime: ctime,
|
|
|
|
mtime: mtime,
|
|
|
|
links: links
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function Transaction(db, request, stores, mode) {
|
|
|
|
var transaction = db.transaction(stores, mode);
|
|
|
|
var deferred = when.defer();
|
|
|
|
transaction.oncomplete = deferred.resolve;
|
|
|
|
transaction.then = deferred.then;
|
|
|
|
request(transaction);
|
|
|
|
return transaction;
|
|
|
|
}
|
|
|
|
|
2012-11-08 05:12:41 +00:00
|
|
|
function FileSystem(db) {
|
2012-11-09 16:10:55 +00:00
|
|
|
this._db = db;
|
2012-11-08 05:12:41 +00:00
|
|
|
this._pending = 0;
|
|
|
|
this._mounted = true;
|
2012-11-09 16:10:55 +00:00
|
|
|
this._descriptors = {};
|
|
|
|
|
|
|
|
this._deferred = when.defer();
|
|
|
|
this._deferred.resolve();
|
2012-11-08 05:12:41 +00:00
|
|
|
|
2012-11-08 10:29:05 +00:00
|
|
|
this.Context = FileSystemContext.bind(undefined, this);
|
2012-11-14 00:05:57 +00:00
|
|
|
this.Transaction = Transaction.bind(undefined, db, this._request);
|
2012-11-08 05:12:41 +00:00
|
|
|
}
|
|
|
|
FileSystem.prototype._request = function _request(transaction) {
|
|
|
|
var fs = this;
|
2012-11-09 16:10:55 +00:00
|
|
|
if(0 === fs._pending) {
|
2012-11-08 05:12:41 +00:00
|
|
|
fs._deferred = when.defer();
|
|
|
|
}
|
|
|
|
++ fs._pending;
|
2012-11-14 00:05:57 +00:00
|
|
|
transaction.then(function(e) {
|
2012-11-08 05:12:41 +00:00
|
|
|
-- fs._pending;
|
2012-11-09 16:10:55 +00:00
|
|
|
if(0 === fs._pending) {
|
2012-11-08 05:12:41 +00:00
|
|
|
fs._deferred.resolve();
|
|
|
|
}
|
2012-11-14 00:05:57 +00:00
|
|
|
});
|
2012-11-08 05:12:41 +00:00
|
|
|
};
|
2012-11-09 16:10:55 +00:00
|
|
|
var OF_CREATE = "CREATE";
|
|
|
|
var OF_APPEND = "APPEND";
|
|
|
|
var OF_TRUNCATE = "TRUNCATE";
|
|
|
|
var OM_RO = "RO";
|
|
|
|
var OM_RW = "RW";
|
|
|
|
FileSystem.prototype.open = function open(fullpath, flags, mode, callback, optTransaction) {
|
|
|
|
var fs = this;
|
|
|
|
fullpath = Path.normalize(fullpath);
|
|
|
|
|
2012-11-30 00:46:35 +00:00
|
|
|
var transaction = optTransaction || new fs.Transaction([FILE_STORE_NAME], IDB_RW);
|
2012-11-09 16:10:55 +00:00
|
|
|
|
|
|
|
var files = transaction.objectStore(FILE_STORE_NAME);
|
2012-11-08 05:12:41 +00:00
|
|
|
|
2012-11-09 16:10:55 +00:00
|
|
|
flags = parseFlags(flags);
|
|
|
|
mode = mode.toUpperCase();
|
|
|
|
|
2012-11-30 00:46:35 +00:00
|
|
|
var name = Path.basename(fullpath);
|
|
|
|
var parentpath = Path.dirname(fullpath);
|
|
|
|
var parenthandle = hash(parentpath);
|
|
|
|
var getParentRequest = files.get(parenthandle);
|
|
|
|
getParentRequest.onsuccess = function(e) {
|
|
|
|
var parent = e.target.result;
|
|
|
|
var data = parent.data;
|
|
|
|
var file, filehandle;
|
2012-12-14 01:24:07 +00:00
|
|
|
if(name === fullpath) {
|
|
|
|
file = parent;
|
|
|
|
filehandle = parenthandle;
|
|
|
|
if(OM_RW === mode && DIRECTORY_MIME_TYPE === file.mode) {
|
|
|
|
runcallback(callback, new error.EIsDirectory());
|
|
|
|
}
|
|
|
|
_createFileDescriptor(filehandle, file, flags, mode);
|
|
|
|
} else if(!_(data).has(name)) {
|
2012-11-09 16:10:55 +00:00
|
|
|
if(_(flags).contains(OF_CREATE)) {
|
2012-11-30 00:46:35 +00:00
|
|
|
filehandle = data[name] = hash(guid());
|
2012-12-01 00:09:14 +00:00
|
|
|
++ parent.size;
|
|
|
|
++ parent.version;
|
2012-11-09 16:10:55 +00:00
|
|
|
file = new File();
|
2012-11-14 16:19:04 +00:00
|
|
|
++ file.links;
|
2012-11-30 00:46:35 +00:00
|
|
|
var createFileRequest = files.put(file, data[name]);
|
2012-11-09 16:10:55 +00:00
|
|
|
createFileRequest.onsuccess = function(e) {
|
2012-11-30 00:46:35 +00:00
|
|
|
var updateParentRequest = files.put(parent, parenthandle);
|
|
|
|
updateParentRequest.onsuccess = function(e) {
|
|
|
|
_createFileDescriptor(filehandle, file, flags, mode);
|
2012-11-09 16:10:55 +00:00
|
|
|
};
|
2012-11-30 00:46:35 +00:00
|
|
|
updateParentRequest.onerror = function(e) {
|
|
|
|
runcallback(callback, e);
|
2012-11-09 16:10:55 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
createFileRequest.onerror = function(e) {
|
2012-11-30 00:46:35 +00:00
|
|
|
runcallback(callback, e);
|
2012-11-09 16:10:55 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
} else {
|
2012-11-30 00:46:35 +00:00
|
|
|
filehandle = data[name];
|
|
|
|
var getFileRequest = files.get(filehandle);
|
|
|
|
getFileRequest.onsuccess = function(e) {
|
|
|
|
file = e.target.result;
|
|
|
|
if(OM_RW === mode && DIRECTORY_MIME_TYPE === file.mode) {
|
|
|
|
runcallback(callback, new error.EIsDirectory());
|
|
|
|
}
|
|
|
|
_createFileDescriptor(filehandle, file, flags, mode);
|
|
|
|
};
|
|
|
|
getFileRequest.onerror = function(e) {
|
|
|
|
runcallback(callback, e);
|
|
|
|
};
|
2012-11-09 16:10:55 +00:00
|
|
|
}
|
|
|
|
};
|
2012-11-30 00:46:35 +00:00
|
|
|
getParentRequest.onerror = function(e) {
|
|
|
|
runcallback(callback, e);
|
2012-11-09 16:10:55 +00:00
|
|
|
};
|
2012-11-30 00:46:35 +00:00
|
|
|
|
|
|
|
function _createFileDescriptor(handle, file, flags, mode) {
|
|
|
|
var openfile = new OpenFile(fs, handle, file, flags, mode);
|
2012-11-14 00:05:57 +00:00
|
|
|
var descriptor = new FileDescriptor(openfile);
|
|
|
|
fs._descriptors[descriptor] = openfile;
|
2012-11-30 00:46:35 +00:00
|
|
|
runcallback(callback, null, descriptor);
|
2012-11-09 16:10:55 +00:00
|
|
|
}
|
2012-11-08 05:12:41 +00:00
|
|
|
};
|
|
|
|
FileSystem.prototype.close = function close(descriptor, callback) {
|
2012-11-09 16:10:55 +00:00
|
|
|
var fs = this;
|
2012-11-14 00:05:57 +00:00
|
|
|
var openfile = fs._descriptors[descriptor];
|
|
|
|
openfile.valid = false;
|
|
|
|
openfile._deferred.then(callback);
|
2012-11-08 05:12:41 +00:00
|
|
|
};
|
|
|
|
FileSystem.prototype.mkdir = function mkdir(fullpath, callback, optTransaction) {
|
2012-11-08 10:29:05 +00:00
|
|
|
var fs = this;
|
2012-11-09 16:10:55 +00:00
|
|
|
fullpath = Path.normalize(fullpath);
|
2012-11-08 05:12:41 +00:00
|
|
|
|
2012-11-30 00:46:35 +00:00
|
|
|
var transaction = optTransaction || new fs.Transaction([FILE_STORE_NAME], IDB_RW);
|
2012-11-08 05:12:41 +00:00
|
|
|
|
2012-11-30 00:46:35 +00:00
|
|
|
var files = transaction.objectStore(FILE_STORE_NAME);
|
2012-11-08 05:12:41 +00:00
|
|
|
|
2012-11-30 00:46:35 +00:00
|
|
|
var directoryhandle = hash(fullpath);
|
|
|
|
var parentpath = Path.dirname(fullpath);
|
|
|
|
var parenthandle = hash(parentpath);
|
|
|
|
var getDirectoryRequest = files.get(directoryhandle);
|
|
|
|
getDirectoryRequest.onsuccess = function(e) {
|
|
|
|
var directory = e.target.result;
|
|
|
|
if(directory) {
|
|
|
|
runcallback(callback, new error.EPathExists());
|
2012-11-08 05:12:41 +00:00
|
|
|
} else {
|
2012-11-30 00:46:35 +00:00
|
|
|
directory = new File(DIRECTORY_MIME_TYPE, {
|
|
|
|
".": directoryhandle,
|
|
|
|
"..": parenthandle,
|
|
|
|
}, JSON_MIME_TYPE, 2);
|
|
|
|
++ directory.links;
|
|
|
|
var createDirectoryRequest = files.put(directory, directoryhandle);
|
2012-12-14 01:24:07 +00:00
|
|
|
createDirectoryRequest.onsuccess = function(e) {
|
|
|
|
if(directoryhandle !== parenthandle) {
|
|
|
|
var getParentRequest = files.get(parenthandle);
|
|
|
|
getParentRequest.onsuccess = function(e) {
|
|
|
|
var parent = e.target.result;
|
|
|
|
parent.data[Path.basename(fullpath)] = directoryhandle;
|
|
|
|
++ parent.size;
|
|
|
|
++ parent.version;
|
|
|
|
var updateParentRequest = files.put(parent, parenthandle);
|
|
|
|
updateParentRequest.onsuccess = function(e) {
|
|
|
|
runcallback(callback);
|
|
|
|
};
|
|
|
|
updateParentRequest.onerror = function(e) {
|
|
|
|
runcallback(callback, e);
|
|
|
|
};
|
2012-11-30 00:46:35 +00:00
|
|
|
};
|
2012-12-14 01:24:07 +00:00
|
|
|
getParentRequest.onerror = function(e) {
|
2012-11-30 00:46:35 +00:00
|
|
|
runcallback(callback, e);
|
|
|
|
};
|
2012-12-14 01:24:07 +00:00
|
|
|
} else {
|
|
|
|
runcallback(callback);
|
|
|
|
}
|
2012-11-08 05:12:41 +00:00
|
|
|
};
|
2012-11-30 00:46:35 +00:00
|
|
|
createDirectoryRequest.onerror = function(e) {
|
|
|
|
runcallback(callback, e);
|
2012-11-08 05:12:41 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
2012-11-30 00:46:35 +00:00
|
|
|
getDirectoryRequest.onerror = function(e) {
|
|
|
|
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-09 16:10:55 +00:00
|
|
|
fullpath = Path.normalize(fullpath);
|
2012-11-08 05:12:41 +00:00
|
|
|
|
2012-11-30 00:46:35 +00:00
|
|
|
var transaction = optTransaction || new fs.Transaction([FILE_STORE_NAME], IDB_RW);
|
2012-11-08 05:12:41 +00:00
|
|
|
|
2012-11-30 00:46:35 +00:00
|
|
|
var files = transaction.objectStore(FILE_STORE_NAME);
|
2012-11-08 05:12:41 +00:00
|
|
|
|
2012-11-30 00:46:35 +00:00
|
|
|
var directoryhandle = hash(fullpath);
|
|
|
|
var getDirectoryRequest = files.get(directoryhandle);
|
|
|
|
getDirectoryRequest.onsuccess = function(e) {
|
|
|
|
var directory = e.target.result;
|
|
|
|
if(!directory) {
|
|
|
|
runcallback(callback, new error.ENoEntry());
|
2012-11-08 05:12:41 +00:00
|
|
|
} else {
|
2012-11-30 00:46:35 +00:00
|
|
|
var data = directory.data;
|
|
|
|
if(Object.keys(data).length > 2) {
|
|
|
|
runcallback(callback, new error.ENotEmpty());
|
|
|
|
} else {
|
|
|
|
var removeDirectoryRequest = files.delete(directoryhandle);
|
|
|
|
removeDirectoryRequest.onsuccess = function(e) {
|
|
|
|
var parentpath = Path.dirname(fullpath);
|
|
|
|
var parenthandle = hash(parentpath);
|
|
|
|
var getParentRequest = files.get(parenthandle);
|
|
|
|
getParentRequest.onsuccess = function(e) {
|
|
|
|
var parent = e.target.result;
|
|
|
|
delete parent.data[directoryhandle];
|
2012-12-01 00:09:14 +00:00
|
|
|
-- parent.size;
|
2012-11-30 00:46:35 +00:00
|
|
|
++ parent.version;
|
|
|
|
var updateParentRequest = files.put(parent, parenthandle);
|
|
|
|
updateParentRequest.onsuccess = function(e) {
|
|
|
|
runcallback(callback);
|
|
|
|
};
|
|
|
|
updateParentRequest.onerror = function(e) {
|
|
|
|
runcallback(callback, e);
|
|
|
|
};
|
2012-11-08 05:12:41 +00:00
|
|
|
};
|
2012-11-30 00:46:35 +00:00
|
|
|
getParentRequest.onerror = function(e) {
|
|
|
|
runcallback(callback, e);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
removeDirectoryRequest.onerror = function(e) {
|
|
|
|
runcallback(callback, e);
|
|
|
|
};
|
2012-11-08 05:12:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2012-11-30 00:46:35 +00:00
|
|
|
getDirectoryRequest.onerror = function(e) {
|
|
|
|
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;
|
2012-11-09 16:10:55 +00:00
|
|
|
fullpath = Path.normalize(fullpath);
|
2012-11-08 10:29:05 +00:00
|
|
|
|
2012-11-30 00:46:35 +00:00
|
|
|
var transaction = optTransaction || new fs.Transaction([FILE_STORE_NAME], IDB_RO);
|
2012-11-08 05:12:41 +00:00
|
|
|
|
2012-11-14 00:05:57 +00:00
|
|
|
var files = transaction.objectStore(FILE_STORE_NAME);
|
2012-11-08 10:29:05 +00:00
|
|
|
|
2012-11-30 00:46:35 +00:00
|
|
|
var parentpath = Path.dirname(fullpath);
|
|
|
|
var parenthandle = hash(parentpath);
|
2012-12-14 01:24:07 +00:00
|
|
|
var getParentRequest = files.get(parenthandle);
|
|
|
|
var stats, file;
|
2012-11-30 00:46:35 +00:00
|
|
|
getParentRequest.onsuccess = function(e) {
|
|
|
|
var parent = e.target.result;
|
|
|
|
var data = parent.data;
|
|
|
|
var name = Path.basename(fullpath);
|
2012-12-14 01:24:07 +00:00
|
|
|
if(name === fullpath) {
|
|
|
|
file = parent;
|
2012-12-14 20:55:11 +00:00
|
|
|
filehandle = parenthandle;
|
2012-12-14 01:24:07 +00:00
|
|
|
stats = new Stats(file.size, filehandle, file.atime, file.ctime, file.mtime, file.links);
|
|
|
|
runcallback(callback, null, stats);
|
|
|
|
} else if(!_(data).has(name)) {
|
2012-11-30 00:46:35 +00:00
|
|
|
runcallback(callback, new error.ENoEntry());
|
2012-11-08 10:29:05 +00:00
|
|
|
} else {
|
2012-11-30 00:46:35 +00:00
|
|
|
var filehandle = data[name];
|
|
|
|
var getFileRequest = files.get(filehandle);
|
|
|
|
getFileRequest.onsuccess = function(e) {
|
|
|
|
var file = e.target.result;
|
2012-12-14 01:24:07 +00:00
|
|
|
stats = new Stats(file.size, filehandle, file.atime, file.ctime, file.mtime, file.links);
|
2012-11-30 00:46:35 +00:00
|
|
|
runcallback(callback, null, stats);
|
|
|
|
};
|
|
|
|
getFileRequest.onerror = function(e) {
|
|
|
|
runcallback(callback, e);
|
|
|
|
};
|
2012-11-08 10:29:05 +00:00
|
|
|
}
|
|
|
|
};
|
2012-11-30 00:46:35 +00:00
|
|
|
getParentRequest.onerror = function(e) {
|
|
|
|
runcallback(callback, e);
|
|
|
|
};
|
2012-11-08 05:12:41 +00:00
|
|
|
};
|
2012-11-14 16:19:04 +00:00
|
|
|
FileSystem.prototype.link = function link(oldpath, newpath, callback, optTransaction) {
|
|
|
|
var fs = this;
|
|
|
|
oldpath = Path.normalize(oldpath);
|
|
|
|
newpath = Path.normalize(newpath);
|
2012-11-30 00:46:35 +00:00
|
|
|
var oldparentpath = Path.dirname(oldpath);
|
|
|
|
var newparentpath = Path.dirname(newpath);
|
2012-11-14 16:19:04 +00:00
|
|
|
|
2012-11-30 00:46:35 +00:00
|
|
|
var transaction = optTransaction || new fs.Transaction([FILE_STORE_NAME], IDB_RW);
|
2012-11-08 05:12:41 +00:00
|
|
|
|
2012-11-14 16:19:04 +00:00
|
|
|
var files = transaction.objectStore(FILE_STORE_NAME);
|
|
|
|
|
2012-11-30 00:46:35 +00:00
|
|
|
var oldparenthandle = hash(oldparentpath);
|
|
|
|
var newparenthandle = hash(newparentpath);
|
|
|
|
var getOldParentRequest = files.get(oldparenthandle);
|
|
|
|
getOldParentRequest.onsuccess = function(e) {
|
|
|
|
var oldparent = e.target.result;
|
|
|
|
if(!oldparent) {
|
|
|
|
runcallback(callback, new error.ENoEntry());
|
2012-11-14 16:19:04 +00:00
|
|
|
} else {
|
2012-11-30 00:46:35 +00:00
|
|
|
var olddata = oldparent.data;
|
|
|
|
var filehandle = olddata[Path.basename(oldpath)];
|
|
|
|
if(!filehandle) {
|
|
|
|
runcallback(callback, new error.ENoEntry());
|
|
|
|
} else {
|
|
|
|
var getNewParentRequest = files.get(newparenthandle);
|
|
|
|
getNewParentRequest.onsuccess = function(e) {
|
|
|
|
var newparent = e.target.result;
|
|
|
|
if(!newparent) {
|
|
|
|
runcallback(callback, new error.ENoEntry());
|
|
|
|
} else {
|
|
|
|
var getFileRequest = files.get(filehandle);
|
2012-11-14 16:19:04 +00:00
|
|
|
getFileRequest.onsuccess = function(e) {
|
|
|
|
var file = e.target.result;
|
|
|
|
++ file.links;
|
2012-11-30 00:46:35 +00:00
|
|
|
++ file.version;
|
|
|
|
var updateFileRequest = files.put(file, filehandle);
|
|
|
|
updateFileRequest.onsuccess = function(e) {
|
|
|
|
var newdata = newparent.data;
|
|
|
|
var newname = Path.basename(newpath);
|
|
|
|
if(_(newdata).has(newname)) {
|
|
|
|
runcallback(callback, new error.EPathExists());
|
|
|
|
} else {
|
|
|
|
newdata[newname] = filehandle;
|
2012-12-01 00:09:14 +00:00
|
|
|
++ newparent.size;
|
|
|
|
++ newparent.version;
|
2012-11-30 00:46:35 +00:00
|
|
|
var updateNewParentRequest = files.put(newparent, newparenthandle);
|
|
|
|
updateNewParentRequest.onsuccess = function(e) {
|
|
|
|
runcallback(callback);
|
|
|
|
};
|
|
|
|
updateNewParentRequest.onerror = function(e) {
|
|
|
|
runcallback(callback, e);
|
|
|
|
};
|
|
|
|
}
|
2012-11-14 16:19:04 +00:00
|
|
|
};
|
2012-11-30 00:46:35 +00:00
|
|
|
updateFileRequest.onerror = function(e) {
|
|
|
|
runcallback(callback, e);
|
2012-11-14 16:19:04 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
getFileRequest.onerror = function(e) {
|
2012-11-30 00:46:35 +00:00
|
|
|
runcallback(callback, e);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
getNewParentRequest.onerror = function(e) {
|
|
|
|
runcallback(callback, e);
|
|
|
|
};
|
|
|
|
}
|
2012-11-14 16:19:04 +00:00
|
|
|
}
|
|
|
|
};
|
2012-11-30 00:46:35 +00:00
|
|
|
getOldParentRequest.onerror = function(e) {
|
|
|
|
runcallback(callback, e);
|
|
|
|
};
|
2012-11-08 05:12:41 +00:00
|
|
|
};
|
2012-11-14 16:19:04 +00:00
|
|
|
FileSystem.prototype.unlink = function unlink(fullpath, callback, optTransaction) {
|
|
|
|
var fs = this;
|
|
|
|
fullpath = Path.normalize(fullpath);
|
|
|
|
|
2012-11-30 00:46:35 +00:00
|
|
|
var transaction = optTransaction || new fs.Transaction([FILE_STORE_NAME], IDB_RW);
|
2012-11-14 16:19:04 +00:00
|
|
|
|
|
|
|
var files = transaction.objectStore(FILE_STORE_NAME);
|
2012-11-30 00:46:35 +00:00
|
|
|
|
|
|
|
var parentpath = Path.dirname(fullpath);
|
|
|
|
var parenthandle = hash(parentpath);
|
|
|
|
var getParentRequest = files.get(parenthandle);
|
|
|
|
getParentRequest.onsuccess = function(e) {
|
|
|
|
var parent = e.target.result;
|
|
|
|
var data = parent.data;
|
|
|
|
var name = Path.basename(fullpath);
|
|
|
|
if(!_(data).has(name)) {
|
|
|
|
runcallback(callback, new error.ENoEntry());
|
2012-11-14 16:19:04 +00:00
|
|
|
} else {
|
2012-11-30 00:46:35 +00:00
|
|
|
var filehandle = data[name];
|
|
|
|
delete data[name];
|
2012-12-01 00:09:14 +00:00
|
|
|
-- parent.size;
|
|
|
|
++ parent.version;
|
2012-11-30 00:46:35 +00:00
|
|
|
var updateParentRequest = files.put(parent, parenthandle);
|
|
|
|
updateParentRequest.onsuccess = function(e) {
|
|
|
|
var getFileRequest = files.get(filehandle);
|
2012-11-14 16:19:04 +00:00
|
|
|
getFileRequest.onsuccess = function(e) {
|
|
|
|
var file = e.target.result;
|
2012-11-30 00:46:35 +00:00
|
|
|
-- file.links;
|
|
|
|
if(0 === file.links) {
|
|
|
|
var deleteFileRequest = files.delete(filehandle);
|
2012-11-14 16:19:04 +00:00
|
|
|
deleteFileRequest.onsuccess = complete;
|
|
|
|
deleteFileRequest.onerror = function(e) {
|
2012-11-30 00:46:35 +00:00
|
|
|
runcallback(callback, e);
|
2012-11-14 16:19:04 +00:00
|
|
|
};
|
|
|
|
} else {
|
2012-11-30 00:46:35 +00:00
|
|
|
++ file.version;
|
|
|
|
var putFileRequest = files.put(file, filehandle);
|
2012-11-14 16:19:04 +00:00
|
|
|
putFileRequest.onsuccess = complete;
|
|
|
|
putFileRequest.onerror = function(e) {
|
2012-11-30 00:46:35 +00:00
|
|
|
runcallback(callback, e);
|
2012-11-14 16:19:04 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
function complete() {
|
2012-11-30 00:46:35 +00:00
|
|
|
runcallback(callback);
|
2012-11-14 16:19:04 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
getFileRequest.onerror = function(e) {
|
2012-11-30 00:46:35 +00:00
|
|
|
runcallback(callback, e);
|
2012-11-14 16:19:04 +00:00
|
|
|
};
|
|
|
|
};
|
2012-11-30 00:46:35 +00:00
|
|
|
updateParentRequest.onerror = function(e) {
|
|
|
|
runcallback(callback, e);
|
|
|
|
};
|
2012-11-14 16:19:04 +00:00
|
|
|
}
|
|
|
|
};
|
2012-11-30 00:46:35 +00:00
|
|
|
getParentRequest.onerror = function(e) {
|
|
|
|
runcallback(callback, e);
|
2012-11-14 16:19:04 +00:00
|
|
|
};
|
2012-11-08 10:29:05 +00:00
|
|
|
};
|
2012-11-14 16:19:04 +00:00
|
|
|
FileSystem.prototype.setxattr = function setxattr(fullpath, name, value, callback, optTransaction) {
|
2012-11-08 10:29:05 +00:00
|
|
|
};
|
2012-11-30 00:46:35 +00:00
|
|
|
FileSystem.prototype.getxattr = function getxattr(fullpath, name, callback, optTransaction) {
|
2012-11-08 05:12:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function FileSystemContext(fs, optCwd) {
|
|
|
|
this._fs = fs;
|
|
|
|
this._cwd = optCwd || "/";
|
|
|
|
}
|
2012-11-09 16:10:55 +00:00
|
|
|
FileSystemContext.prototype.chdir = function chdir(path) {
|
|
|
|
if(Path.isAbsolute(path)) {
|
|
|
|
this._cwd = Path.normalize(path);
|
|
|
|
} else {
|
|
|
|
this._cwd = Path.normalize(this._cwd + "/" + path);
|
|
|
|
}
|
2012-11-08 05:12:41 +00:00
|
|
|
};
|
|
|
|
FileSystemContext.prototype.getcwd = function getcwd() {
|
|
|
|
return this._cwd;
|
|
|
|
};
|
2012-11-09 16:10:55 +00:00
|
|
|
FileSystemContext.prototype.open = function open(path, flags, mode, callback) {
|
|
|
|
this._fs.open(Path.normalize(this._cwd + "/" + path), flags, mode, callback);
|
2012-11-08 05:12:41 +00:00
|
|
|
};
|
|
|
|
FileSystemContext.prototype.close = function close(descriptor, callback) {
|
2012-11-09 16:10:55 +00:00
|
|
|
this._fs.close(descriptor, callback);
|
2012-11-08 05:12:41 +00:00
|
|
|
};
|
2012-11-09 16:10:55 +00:00
|
|
|
FileSystemContext.prototype.mkdir = function mkdir(path, callback) {
|
|
|
|
this._fs.mkdir(Path.normalize(this._cwd + "/" + path), callback);
|
2012-11-08 10:29:05 +00:00
|
|
|
};
|
2012-11-09 16:10:55 +00:00
|
|
|
FileSystemContext.prototype.rmdir = function rmdir(path, callback) {
|
|
|
|
this._fs.rmdir(Path.normalize(this._cwd + "/" + path), callback);
|
2012-11-08 10:29:05 +00:00
|
|
|
};
|
2012-11-09 16:10:55 +00:00
|
|
|
FileSystemContext.prototype.stat = function stat(path, callback) {
|
|
|
|
this._fs.stat(Path.normalize(this._cwd + "/" + path), 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-14 16:19:04 +00:00
|
|
|
this._fs.link(Path.normalize(this._cwd + "/" + oldpath), Path.normalize(this._cwd + "/" + newpath), callback);
|
2012-11-08 05:12:41 +00:00
|
|
|
};
|
2012-11-09 16:10:55 +00:00
|
|
|
FileSystemContext.prototype.unlink = function unlink(path, callback) {
|
2012-11-14 16:19:04 +00:00
|
|
|
this._fs.unlink(Path.normalize(this._cwd + "/" + path), callback);
|
2012-11-08 05:12:41 +00:00
|
|
|
};
|
2012-11-09 16:10:55 +00:00
|
|
|
FileSystemContext.prototype.setxattr = function setxattr(path, name, value, callback) {
|
2012-11-15 11:24:00 +00:00
|
|
|
this._fs.setxattr(Path.normalize(this._cwd + "/" + path), name, value, callback);
|
2012-11-08 05:12:41 +00:00
|
|
|
};
|
2012-11-09 16:10:55 +00:00
|
|
|
FileSystemContext.prototype.getxattr = function getxattr(path, name, callback) {
|
2012-11-15 11:24:00 +00:00
|
|
|
this._fs.getxattr(Path.normalize(this._cwd + "/" + path), name, callback);
|
2012-11-08 05:12:41 +00:00
|
|
|
};
|
|
|
|
|
2012-11-30 00:46:35 +00:00
|
|
|
function OpenFile(fs, handle, file, flags, mode, size) {
|
2012-11-14 00:05:57 +00:00
|
|
|
this._fs = fs;
|
2012-11-08 05:12:41 +00:00
|
|
|
this._pending = 0;
|
|
|
|
this._valid = true;
|
2012-11-14 00:05:57 +00:00
|
|
|
this._position = 0;
|
2012-11-08 05:12:41 +00:00
|
|
|
this._flags = flags;
|
|
|
|
this._mode = mode;
|
2012-11-14 00:05:57 +00:00
|
|
|
this._size = size;
|
|
|
|
|
2012-11-30 00:46:35 +00:00
|
|
|
this._handle = handle;
|
|
|
|
this._file = file; // Cached file node, might require an update
|
2012-11-09 16:10:55 +00:00
|
|
|
|
|
|
|
this._deferred = when.defer();
|
|
|
|
this._deferred.resolve();
|
2012-11-14 00:05:57 +00:00
|
|
|
|
|
|
|
var openfile = this;
|
|
|
|
this.Transaction = Transaction.bind(undefined, fs._db, function(transaction) {
|
|
|
|
fs._request(transaction);
|
|
|
|
openfile._request(transaction);
|
|
|
|
});
|
2012-11-08 05:12:41 +00:00
|
|
|
}
|
2012-11-14 00:05:57 +00:00
|
|
|
OpenFile.prototype._request = function _request(transaction) {
|
|
|
|
var openfile = this;
|
|
|
|
if(0 === openfile._pending) {
|
|
|
|
openfile._deferred = when.defer();
|
2012-11-09 16:10:55 +00:00
|
|
|
}
|
2012-11-14 00:05:57 +00:00
|
|
|
++ openfile._pending;
|
|
|
|
transaction.then(function(x) {
|
|
|
|
-- openfile._pending;
|
|
|
|
if(0 === openfile._pending) {
|
|
|
|
openfile._deferred.resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
OpenFile.prototype.seek = function seek(offset, origin, callback, optTransaction) {
|
|
|
|
var openfile = this;
|
|
|
|
var fs = openfile._fs;
|
|
|
|
origin = origin.toUpperCase();
|
|
|
|
|
|
|
|
var transaction = optTransaction || new openfile.Transaction([FILE_STORE_NAME], IDB_RO);
|
|
|
|
|
|
|
|
var files = transaction.objectStore(FILE_STORE_NAME);
|
|
|
|
|
|
|
|
if(SK_END === origin) {
|
|
|
|
var getFileRequest = files.get(fd._handle);
|
|
|
|
getFileRequest.onsuccess = function(e) {
|
|
|
|
var file = e.target.result;
|
|
|
|
var size = file.size;
|
|
|
|
offset += size;
|
|
|
|
openfile._position = offset;
|
2012-11-30 00:46:35 +00:00
|
|
|
runcallback(callback, null, offset);
|
2012-11-14 00:05:57 +00:00
|
|
|
};
|
|
|
|
getFileRequest.onerror = function(e) {
|
2012-11-30 00:46:35 +00:00
|
|
|
runcallback(callback, e);
|
2012-11-14 00:05:57 +00:00
|
|
|
};
|
|
|
|
} else if(SK_CURRENT === origin) {
|
|
|
|
openfile._position += offset;
|
2012-11-30 00:46:35 +00:00
|
|
|
runcallback(callback, null, openfile._position);
|
2012-11-14 00:05:57 +00:00
|
|
|
} else if(SK_SET === origin) {
|
|
|
|
openfile._position = offset;
|
2012-11-30 00:46:35 +00:00
|
|
|
runcallback(callback, null, offset);
|
2012-11-14 00:05:57 +00:00
|
|
|
}
|
|
|
|
};
|
2012-12-01 00:09:14 +00:00
|
|
|
OpenFile.prototype.read = function read(buffer, callback, optTransaction) {
|
2012-11-14 00:05:57 +00:00
|
|
|
var openfile = this;
|
|
|
|
var fs = openfile._fs;
|
2012-12-01 00:09:14 +00:00
|
|
|
|
|
|
|
if(DIRECTORY_MIME_TYPE === openfile._file.mode) {
|
|
|
|
return runcallback(callback, new error.EIsDir());
|
|
|
|
}
|
|
|
|
|
2012-11-14 00:05:57 +00:00
|
|
|
var transaction = optTransaction || new openfile.Transaction([FILE_STORE_NAME], IDB_RO);
|
|
|
|
|
|
|
|
var files = transaction.objectStore(FILE_STORE_NAME);
|
|
|
|
|
2012-12-01 00:09:14 +00:00
|
|
|
var getDataRequest = files.get(openfile._file.data);
|
|
|
|
getDataRequest.onsuccess = function(e) {
|
|
|
|
var data = e.target.result;
|
|
|
|
if(!data) {
|
|
|
|
// There's not file data, so return zero bytes read
|
|
|
|
runcallback(callback, null, 0, buffer);
|
|
|
|
} else {
|
|
|
|
// Make sure we won't read past the end of the file
|
|
|
|
var bytes = (openfile._position + buffer.length > data.length) ? data.length - openfile._position : buffer.length;
|
|
|
|
// Copy the desired region from the file into the buffer
|
|
|
|
var dataView = data.subarray(openfile._position, openfile._position + bytes);
|
|
|
|
buffer.set(dataView);
|
|
|
|
openfile._position += bytes;
|
|
|
|
runcallback(callback, null, bytes, buffer);
|
2012-11-14 00:05:57 +00:00
|
|
|
}
|
2012-12-01 00:09:14 +00:00
|
|
|
};
|
|
|
|
getDataRequest.onerror = function(e) {
|
|
|
|
runcallback(callback, e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
OpenFile.prototype.readdir = function readdir(buffer, count, callback, optTransaction) {
|
|
|
|
var openfile = this;
|
|
|
|
var fs = openfile._fs;
|
|
|
|
|
|
|
|
if(!DIRECTORY_MIME_TYPE === openfile._file.mode) {
|
|
|
|
return runcallback(callback, new error.ENotIsDir());
|
2012-11-14 00:05:57 +00:00
|
|
|
}
|
2012-12-01 00:09:14 +00:00
|
|
|
|
|
|
|
var transaction = optTransaction || new openfile.Transaction([FILE_STORE_NAME], IDB_RO);
|
|
|
|
|
|
|
|
var files = transaction.objectStore(FILE_STORE_NAME);
|
|
|
|
|
|
|
|
var getFileRequest = files.get(openfile._handle);
|
|
|
|
getFileRequest.onsuccess = function(e) {
|
|
|
|
var file = e.target.result;
|
|
|
|
var names = _(file.data).keys().sort();
|
2012-12-14 01:24:07 +00:00
|
|
|
count = count || file.size;
|
2012-12-01 00:09:14 +00:00
|
|
|
count = (openfile._position + count > file.size) ? (file.size - openfile._position) : count;
|
|
|
|
for(var i = openfile._position, l = openfile._position + count; i < l; ++ i) {
|
|
|
|
buffer.push(names[i]);
|
|
|
|
}
|
|
|
|
openfile._position += count;
|
|
|
|
runcallback(callback, null, count, buffer);
|
|
|
|
};
|
|
|
|
getFileRequest.onerror = function(e) {
|
|
|
|
runcallback(callback, e);
|
|
|
|
};
|
2012-11-14 00:05:57 +00:00
|
|
|
};
|
|
|
|
OpenFile.prototype.write = function write(buffer, callback, optTransaction) {
|
|
|
|
var openfile = this;
|
|
|
|
var fs = openfile._fs;
|
|
|
|
|
|
|
|
if(OM_RO === openfile._mode) {
|
2012-11-30 00:46:35 +00:00
|
|
|
runcallback(callback, new error.EBadFileDescriptor());
|
2012-11-14 00:05:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-30 00:46:35 +00:00
|
|
|
var transaction = optTransaction || openfile.Transaction([FILE_STORE_NAME], IDB_RW);
|
2012-11-14 00:05:57 +00:00
|
|
|
|
|
|
|
var files = transaction.objectStore(FILE_STORE_NAME);
|
|
|
|
|
2012-11-30 00:46:35 +00:00
|
|
|
var getDataRequest = files.get(openfile._file.data);
|
2012-11-14 00:05:57 +00:00
|
|
|
getDataRequest.onsuccess = function(e) {
|
|
|
|
var data = e.target.result;
|
|
|
|
var bytes = buffer.length;
|
|
|
|
var dataLength = data ? data.length : 0;
|
|
|
|
var size = (dataLength > openfile._position + bytes) ? dataLength : openfile._position + bytes;
|
|
|
|
var newData = new Uint8Array(size);
|
|
|
|
if(data) {
|
|
|
|
newData.set(data);
|
2012-11-09 16:10:55 +00:00
|
|
|
}
|
2012-11-14 00:05:57 +00:00
|
|
|
newData.set(buffer, openfile._position);
|
|
|
|
openfile._position += bytes;
|
2012-11-30 00:46:35 +00:00
|
|
|
var putDataRequest = files.put(newData, openfile._file.data);
|
2012-11-14 00:05:57 +00:00
|
|
|
putDataRequest.onsuccess = function(e) {
|
2012-11-30 00:46:35 +00:00
|
|
|
var getFileRequest = files.get(openfile._handle);
|
2012-11-14 00:05:57 +00:00
|
|
|
getFileRequest.onsuccess = function(e) {
|
|
|
|
var file = e.target.result;
|
|
|
|
file.size = size;
|
|
|
|
file.mtime = Date.now();
|
2012-11-30 00:46:35 +00:00
|
|
|
var putFileRequest = files.put(file, openfile._handle);
|
2012-11-14 00:05:57 +00:00
|
|
|
putFileRequest.onsuccess = function(e) {
|
2012-11-30 00:46:35 +00:00
|
|
|
runcallback(callback, null, size);
|
2012-11-14 00:05:57 +00:00
|
|
|
};
|
|
|
|
putFileRequest.onerror = function(e) {
|
2012-11-30 00:46:35 +00:00
|
|
|
runcallback(callback, e);
|
2012-11-14 00:05:57 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
getFileRequest.onerror = function(e) {
|
2012-11-30 00:46:35 +00:00
|
|
|
runcallback(callback, e);
|
2012-11-14 00:05:57 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
putDataRequest.onerror = function(e) {
|
2012-11-30 00:46:35 +00:00
|
|
|
runcallback(callback, e);
|
2012-11-14 00:05:57 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
getDataRequest.onerror = function(e) {
|
2012-11-30 00:46:35 +00:00
|
|
|
runcallback(callback, e);
|
2012-11-14 00:05:57 +00:00
|
|
|
};
|
2012-11-09 16:10:55 +00:00
|
|
|
};
|
2012-11-08 05:12:41 +00:00
|
|
|
|
|
|
|
function FileDescriptor(openfile, flags) {
|
|
|
|
this._openfile = openfile;
|
2012-11-14 00:05:57 +00:00
|
|
|
this._flags = flags;
|
2012-11-08 05:12:41 +00:00
|
|
|
}
|
2012-11-14 00:05:57 +00:00
|
|
|
var SK_SET = "SET";
|
|
|
|
var SK_CURRENT = "CURRENT";
|
|
|
|
var SK_END = "END";
|
|
|
|
FileDescriptor.prototype.seek = function seek(offset, origin, callback) {
|
|
|
|
this._openfile.seek(offset, origin, callback);
|
2012-11-08 05:12:41 +00:00
|
|
|
};
|
|
|
|
FileDescriptor.prototype.read = function read(buffer, callback) {
|
2012-11-14 00:05:57 +00:00
|
|
|
this._openfile.read(buffer, callback);
|
2012-11-08 05:12:41 +00:00
|
|
|
};
|
2012-12-01 00:09:14 +00:00
|
|
|
FileDescriptor.prototype.readdir = function readdir(buffer, count, callback) {
|
|
|
|
this._openfile.readdir(buffer, count, callback);
|
|
|
|
};
|
2012-11-08 05:12:41 +00:00
|
|
|
FileDescriptor.prototype.write = function write(buffer, callback) {
|
2012-11-14 00:05:57 +00:00
|
|
|
this._openfile.write(buffer, callback);
|
2012-11-08 05:12:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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(FILE_STORE_NAME)) {
|
|
|
|
db.deleteObjectStore(FILE_STORE_NAME);
|
|
|
|
}
|
|
|
|
var files = db.createObjectStore(FILE_STORE_NAME);
|
|
|
|
|
|
|
|
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) {
|
2012-11-30 00:46:35 +00:00
|
|
|
var transaction = db.transaction([FILE_STORE_NAME], IDB_RW);
|
|
|
|
|
|
|
|
var files = transaction.objectStore(FILE_STORE_NAME);
|
2012-11-08 05:12:41 +00:00
|
|
|
|
2012-11-30 00:46:35 +00:00
|
|
|
var clearRequest = files.clear();
|
2012-11-08 05:12:41 +00:00
|
|
|
clearRequest.onsuccess = function() {
|
|
|
|
fs.mkdir("/", function(error) {
|
|
|
|
if(error) {
|
2012-11-30 00:46:35 +00:00
|
|
|
runcallback(callback, error);
|
2012-11-08 05:12:41 +00:00
|
|
|
} else {
|
2012-11-30 00:46:35 +00:00
|
|
|
runcallback(callback, null, context);
|
2012-11-08 05:12:41 +00:00
|
|
|
}
|
|
|
|
}, 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 {
|
2012-11-30 00:46:35 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
});
|