Merge pull request #269 from humphd/issue266

Fix #266 - let FileSystem ctor figure out if an existing filesystem exists
This commit is contained in:
Alan K 2014-08-20 21:27:04 -04:00
commit 16dfb79de9
7 changed files with 72 additions and 51 deletions

View File

@ -187,9 +187,8 @@ function FileSystem(options, callback) {
} }
// Open file system storage provider // Open file system storage provider
provider.open(function(err, needsFormatting) { provider.open(function(err) {
function complete(error) { function complete(error) {
function wrappedContext(methodName) { function wrappedContext(methodName) {
var context = provider[methodName](); var context = provider[methodName]();
context.flags = flags; context.flags = flags;
@ -232,20 +231,22 @@ function FileSystem(options, callback) {
return complete(err); return complete(err);
} }
// If we don't need or want formatting, we're done
if(!(forceFormatting || needsFormatting)) {
return complete(null);
}
// otherwise format the fs first
var context = provider.getReadWriteContext(); var context = provider.getReadWriteContext();
context.guid = wrappedGuidFn(context); context.guid = wrappedGuidFn(context);
// Mount the filesystem, formatting if necessary
if(forceFormatting) {
// Wipe the storage provider, then write root block
context.clear(function(err) { context.clear(function(err) {
if(err) { if(err) {
complete(err); return complete(err);
return;
} }
impl.ensureRootDirectory(context, complete); impl.ensureRootDirectory(context, complete);
}); });
} else {
// Use existing (or create new) root and mount
impl.ensureRootDirectory(context, complete);
}
}); });
} }

View File

@ -105,14 +105,9 @@ IndexedDB.prototype.open = function(callback) {
// Bail if we already have a db open // Bail if we already have a db open
if(that.db) { if(that.db) {
callback(null, false); return callback();
return;
} }
// Keep track of whether we're accessing this db for the first time
// and therefore needs to get formatted.
var firstAccess = false;
// NOTE: we're not using versioned databases. // NOTE: we're not using versioned databases.
var openRequest = indexedDB.open(that.name); var openRequest = indexedDB.open(that.name);
@ -124,13 +119,11 @@ IndexedDB.prototype.open = function(callback) {
db.deleteObjectStore(FILE_STORE_NAME); db.deleteObjectStore(FILE_STORE_NAME);
} }
db.createObjectStore(FILE_STORE_NAME); db.createObjectStore(FILE_STORE_NAME);
firstAccess = true;
}; };
openRequest.onsuccess = function onsuccess(event) { openRequest.onsuccess = function onsuccess(event) {
that.db = event.target.result; that.db = event.target.result;
callback(null, firstAccess); callback();
}; };
openRequest.onerror = function onerror(error) { openRequest.onerror = function onerror(error) {
callback(new Errors.EINVAL('IndexedDB cannot be accessed. If private browsing is enabled, disable it.')); callback(new Errors.EINVAL('IndexedDB cannot be accessed. If private browsing is enabled, disable it.'));

View File

@ -9,14 +9,10 @@ var asyncCallback = require('../../lib/async.js').setImmediate;
var createDB = (function() { var createDB = (function() {
var pool = {}; var pool = {};
return function getOrCreate(name) { return function getOrCreate(name) {
var firstAccess = !pool.hasOwnProperty(name); if(!pool.hasOwnProperty(name)) {
if(firstAccess) {
pool[name] = {}; pool[name] = {};
} }
return { return pool[name];
firstAccess: firstAccess,
db: pool[name]
};
}; };
}()); }());
@ -81,11 +77,8 @@ Memory.isSupported = function() {
}; };
Memory.prototype.open = function(callback) { Memory.prototype.open = function(callback) {
var result = createDB(this.name); this.db = createDB(this.name);
this.db = result.db; asyncCallback(callback);
asyncCallback(function() {
callback(null, result.firstAccess);
});
}; };
Memory.prototype.getReadOnlyContext = function() { Memory.prototype.getReadOnlyContext = function() {
return new MemoryContext(this.db, true); return new MemoryContext(this.db, true);

View File

@ -130,8 +130,7 @@ WebSQL.prototype.open = function(callback) {
// Bail if we already have a db open // Bail if we already have a db open
if(that.db) { if(that.db) {
callback(null, false); return callback();
return;
} }
var db = global.openDatabase(that.name, WSQL_VERSION, WSQL_DESC, WSQL_SIZE); var db = global.openDatabase(that.name, WSQL_VERSION, WSQL_DESC, WSQL_SIZE);
@ -148,18 +147,7 @@ WebSQL.prototype.open = function(callback) {
} }
function onSuccess(transaction, result) { function onSuccess(transaction, result) {
that.db = db; that.db = db;
callback();
function gotCount(transaction, result) {
var firstAccess = result.rows.item(0).count === 0;
callback(null, firstAccess);
}
function onError(transaction, error) {
callback(error);
}
// Keep track of whether we're accessing this db for the first time
// and therefore needs to get formatted.
transaction.executeSql("SELECT COUNT(id) AS count FROM " + FILE_STORE_NAME + ";",
[], gotCount, onError);
} }
// Create the table and index we'll need to store the fs data. // Create the table and index we'll need to store the fs data.

View File

@ -7,6 +7,7 @@
require("./spec/filer.spec"); require("./spec/filer.spec");
// Filer.FileSystem.* // Filer.FileSystem.*
require("./spec/filer.filesystem.spec");
require("./spec/fs.spec"); require("./spec/fs.spec");
require("./spec/fs.stat.spec"); require("./spec/fs.stat.spec");
require("./spec/fs.lstat.spec"); require("./spec/fs.lstat.spec");

View File

@ -0,0 +1,46 @@
var Filer = require('../..');
var util = require('../lib/test-utils.js');
var expect = require('chai').expect;
describe("Filer.FileSystem", function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should properly mount new or existing filesystem', function(done) {
var provider = util.provider().provider;
// 1) Should be able to open a new filesystem, and get empty root
var fs1 = new Filer.FileSystem({provider: provider}, function() {
fs1.readdir('/', function(err, entries) {
expect(err).not.to.exist;
expect(entries).to.be.an('array');
expect(entries.length).to.equal(0);
fs1.writeFile('/file', 'data', function(err) {
if(err) throw err;
// 2) Should be able to open an existing filesystem
var fs2 = new Filer.FileSystem({provider: provider}, function() {
fs2.readdir('/', function(err, entries) {
expect(err).not.to.exist;
expect(entries).to.be.an('array');
expect(entries.length).to.equal(1);
expect(entries[0]).to.equal('file');
// 3) FORMAT flag should wipe an existing filesystem
var fs3 = new Filer.FileSystem({provider: provider, flags: ['FORMAT']}, function() {
fs3.readdir('/', function(err, entries) {
expect(err).not.to.exist;
expect(entries).to.be.an('array');
expect(entries.length).to.equal(0);
done();
});
});
});
});
});
});
});
});
});

View File

@ -35,10 +35,9 @@ module.exports = function createProviderTestsFor(providerName, testProvider) {
expect(provider.getReadWriteContext).to.be.a('function'); expect(provider.getReadWriteContext).to.be.a('function');
}); });
it("should open a new IndexedDB database", function(done) { it("should open a new provider database", function(done) {
provider.open(function(error, firstAccess) { provider.open(function(error) {
expect(error).not.to.exist; expect(error).not.to.exist;
expect(firstAccess).to.be.true;
done(); done();
}); });
}); });