diff --git a/dist/idbfs.js b/dist/idbfs.js index 21f3d75..ac8cde8 100644 --- a/dist/idbfs.js +++ b/dist/idbfs.js @@ -6206,7 +6206,7 @@ define('src/file-system',['require','lodash','when','src/path','src/path','src/p callback(new ENoEntry('O_CREATE and O_EXCLUSIVE are set, and the named file exists')) } else { directoryEntry = directoryData[name]; - if(directoryEntry.type == MODE_DIRECTORY) { + if(directoryEntry.type == MODE_DIRECTORY && _(flags).contains(O_WRITE)) { callback(new EIsDirectory('the named file is a directory and O_WRITE is set')) } else { read_object(objectStore, directoryEntry.id, set_file_node); @@ -6446,36 +6446,49 @@ define('src/file-system',['require','lodash','when','src/path','src/path','src/p }; FileSystem.prototype.open = function open(path, flags, callback) { var that = this; - var deferred = when.defer(); - var transaction = this.db.transaction([FILE_STORE_NAME], IDB_RW); - var files = transaction.objectStore(FILE_STORE_NAME); + this.promise.then( + function() { + var deferred = when.defer(); + var transaction = that.db.transaction([FILE_STORE_NAME], IDB_RW); + var files = transaction.objectStore(FILE_STORE_NAME); - function check_result(error, fileNode) { - if(error) { - // if(transaction.error) transaction.abort(); - deferred.reject(error); - } else { - var position; - if(_(flags).contains(O_APPEND)) { - position = fileNode.size; + function check_result(error, fileNode) { + if(error) { + // if(transaction.error) transaction.abort(); + deferred.reject(error); + } else { + var position; + if(_(flags).contains(O_APPEND)) { + position = fileNode.size; + } else { + position = 0; + } + var openFileDescription = new OpenFileDescription(fileNode.id, flags, position); + var fd = that._allocate_descriptor(openFileDescription); + deferred.resolve(fd); + } + }; + + if(!_(O_FLAGS).has(flags)) { + deferred.reject(new EInvalid('flags is not valid')); } else { - position = 0; + flags = O_FLAGS[flags]; } - var openFileDescription = new OpenFileDescription(fileNode.id, flags, position); - var fd = that._allocate_descriptor(openFileDescription); - deferred.resolve(fd); + + open_file(this, files, path, flags, check_result); + deferred.promise.then( + function(result) { + callback(undefined, result); + }, + function(error) { + callback(error); + } + ); + }, + function() { + callback(new EFileSystemError('unknown error')); } - }; - - if(!_(O_FLAGS).has(flags)) { - deferred.reject(new EInvalid('flags is not valid')); - } else { - flags = O_FLAGS[flags]; - } - - - open_file(this, files, path, flags, check_result); - deferred.then(callback); + ); }; FileSystem.prototype.close = function close(fd, callback) { var deferred = when.defer(); diff --git a/examples/refactoring-test.html b/examples/refactoring-test.html index ee7a8e2..4f1c45d 100644 --- a/examples/refactoring-test.html +++ b/examples/refactoring-test.html @@ -21,10 +21,8 @@ var flags = 'FORMAT'; //var flags; var fs = new IDBFS.FileSystem('local', flags); -fs.mkdir('/tmp', function(error) { - /*fs.rmdir('/tmp', function(error) { - console.log('!'); - });*/ +fs.open('/', 'r', function(error, result) { + console.log('1', error, result); }); /* diff --git a/src/file-system.js b/src/file-system.js index 1ec9e51..dfadce6 100644 --- a/src/file-system.js +++ b/src/file-system.js @@ -385,7 +385,15 @@ define(function(require) { var fileNode; var fileData; - find_node(objectStore, parentPath, read_directory_data); + if(ROOT_DIRECTORY_NAME == name) { + if(_(flags).contains(O_WRITE)) { + callback(new EIsDirectory('the named file is a directory and O_WRITE is set')) + } else { + find_node(objectStore, path, set_file_node); + } + } else { + find_node(objectStore, parentPath, read_directory_data); + } function read_directory_data(error, result) { if(error) { @@ -406,7 +414,7 @@ define(function(require) { callback(new ENoEntry('O_CREATE and O_EXCLUSIVE are set, and the named file exists')) } else { directoryEntry = directoryData[name]; - if(directoryEntry.type == MODE_DIRECTORY) { + if(directoryEntry.type == MODE_DIRECTORY && _(flags).contains(O_WRITE)) { callback(new EIsDirectory('the named file is a directory and O_WRITE is set')) } else { read_object(objectStore, directoryEntry.id, set_file_node); @@ -646,36 +654,49 @@ define(function(require) { }; FileSystem.prototype.open = function open(path, flags, callback) { var that = this; - var deferred = when.defer(); - var transaction = this.db.transaction([FILE_STORE_NAME], IDB_RW); - var files = transaction.objectStore(FILE_STORE_NAME); + this.promise.then( + function() { + var deferred = when.defer(); + var transaction = that.db.transaction([FILE_STORE_NAME], IDB_RW); + var files = transaction.objectStore(FILE_STORE_NAME); - function check_result(error, fileNode) { - if(error) { - // if(transaction.error) transaction.abort(); - deferred.reject(error); - } else { - var position; - if(_(flags).contains(O_APPEND)) { - position = fileNode.size; + function check_result(error, fileNode) { + if(error) { + // if(transaction.error) transaction.abort(); + deferred.reject(error); + } else { + var position; + if(_(flags).contains(O_APPEND)) { + position = fileNode.size; + } else { + position = 0; + } + var openFileDescription = new OpenFileDescription(fileNode.id, flags, position); + var fd = that._allocate_descriptor(openFileDescription); + deferred.resolve(fd); + } + }; + + if(!_(O_FLAGS).has(flags)) { + deferred.reject(new EInvalid('flags is not valid')); } else { - position = 0; + flags = O_FLAGS[flags]; } - var openFileDescription = new OpenFileDescription(fileNode.id, flags, position); - var fd = that._allocate_descriptor(openFileDescription); - deferred.resolve(fd); + + open_file(this, files, path, flags, check_result); + deferred.promise.then( + function(result) { + callback(undefined, result); + }, + function(error) { + callback(error); + } + ); + }, + function() { + callback(new EFileSystemError('unknown error')); } - }; - - if(!_(O_FLAGS).has(flags)) { - deferred.reject(new EInvalid('flags is not valid')); - } else { - flags = O_FLAGS[flags]; - } - - - open_file(this, files, path, flags, check_result); - deferred.then(callback); + ); }; FileSystem.prototype.close = function close(fd, callback) { var deferred = when.defer(); diff --git a/tests/spec/idbfs.spec.js b/tests/spec/idbfs.spec.js index 28ef2e1..820dabb 100644 --- a/tests/spec/idbfs.spec.js +++ b/tests/spec/idbfs.spec.js @@ -61,7 +61,7 @@ describe("fs", function() { waitsFor(function() { return complete; - }, 'stat to complete', DEFAULT_TIMEOUT); + }, 'test to complete', DEFAULT_TIMEOUT); runs(function() { expect(_result).toBeDefined(); @@ -97,7 +97,7 @@ describe('fs.stat', function() { waitsFor(function() { return complete; - }, 'stat to complete', DEFAULT_TIMEOUT); + }, 'test to complete', DEFAULT_TIMEOUT); runs(function() { expect(_error).toBeDefined(); @@ -119,7 +119,7 @@ describe('fs.stat', function() { waitsFor(function() { return complete; - }, 'stat to complete', DEFAULT_TIMEOUT); + }, 'test to complete', DEFAULT_TIMEOUT); runs(function() { expect(_result).toBeDefined(); @@ -181,7 +181,7 @@ describe('fs.mkdir', function() { waitsFor(function() { return complete; - }, 'stat to complete', DEFAULT_TIMEOUT); + }, 'test to complete', DEFAULT_TIMEOUT); runs(function() { expect(_error).toBeDefined(); @@ -206,7 +206,7 @@ describe('fs.mkdir', function() { waitsFor(function() { return complete; - }, 'stat to complete', DEFAULT_TIMEOUT); + }, 'test to complete', DEFAULT_TIMEOUT); runs(function() { expect(_error).not.toBeDefined(); @@ -231,7 +231,7 @@ describe('fs.rmdir', function() { expect(typeof this.fs.rmdir).toEqual('function'); }); - it('should return an error if part of the path does not exist', function() { + it('should return an error if the path does not exist', function() { var complete = false; var _error; var that = this; @@ -244,7 +244,7 @@ describe('fs.rmdir', function() { waitsFor(function() { return complete; - }, 'rmdir to complete', DEFAULT_TIMEOUT); + }, 'test to complete', DEFAULT_TIMEOUT); runs(function() { expect(_error).toBeDefined(); @@ -264,7 +264,7 @@ describe('fs.rmdir', function() { waitsFor(function() { return complete; - }, 'rmdir to complete', DEFAULT_TIMEOUT); + }, 'test to complete', DEFAULT_TIMEOUT); runs(function() { expect(_error).toBeDefined(); @@ -288,7 +288,7 @@ describe('fs.rmdir', function() { waitsFor(function() { return complete; - }, 'rmdir to complete', DEFAULT_TIMEOUT); + }, 'test to complete', DEFAULT_TIMEOUT); runs(function() { expect(_error).toBeDefined(); @@ -313,11 +313,126 @@ describe('fs.rmdir', function() { waitsFor(function() { return complete; - }, 'rmdir to complete', DEFAULT_TIMEOUT); + }, 'test to complete', DEFAULT_TIMEOUT); runs(function() { expect(_error).not.toBeDefined(); expect(_stat).not.toBeDefined(); }); }); +}); + +describe('fs.open', function() { + beforeEach(function() { + this.db_name = mk_db_name(); + this.fs = new IDBFS.FileSystem(this.db_name, 'FORMAT'); + }); + + afterEach(function() { + indexedDB.deleteDatabase(this.db_name); + delete this.fs; + }); + + it('should be a function', function() { + expect(typeof this.fs.open).toEqual('function'); + }); + + it('should return an error if the parent path does not exist', function() { + var complete = false; + var _error, _result; + var that = this; + + that.fs.open('/tmp/myfile', 'w+', function(error, result) { + _error = error; + _result = result; + + complete = true; + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toBeDefined(); + expect(_result).not.toBeDefined(); + }); + }); + + it('should return an error when flagged for read and the path does not exist', function() { + var complete = false; + var _error, _result; + var that = this; + + that.fs.open('/myfile', 'r+', function(error, result) { + _error = error; + _result = result; + + complete = true; + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toBeDefined(); + expect(_result).not.toBeDefined(); + }); + }); + + + it('should return an error when flagged for write and the path is a directory', function() { + var complete = false; + var _error, _result; + var that = this; + + that.fs.mkdir('/tmp', function(error) { + if(error) throw error; + that.fs.open('/tmp', 'w', function(error, result) { + _error = error; + _result = result; + + complete = true; + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toBeDefined(); + expect(_result).not.toBeDefined(); + }); + }); + + it('should return an error when flagged for append and the path is a directory', function() { + var complete = false; + var _error, _result; + var that = this; + + that.fs.mkdir('/tmp', function(error) { + if(error) throw error; + that.fs.open('/tmp', 'a', function(error, result) { + _error = error; + _result = result; + + complete = true; + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toBeDefined(); + expect(_result).not.toBeDefined(); + }); + }); +/* + it('should return a unique file descriptor', function() { + }); +*/ }); \ No newline at end of file