Code and tests for fs.open
This commit is contained in:
parent
5853704bab
commit
d3d7dcabde
|
@ -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'))
|
callback(new ENoEntry('O_CREATE and O_EXCLUSIVE are set, and the named file exists'))
|
||||||
} else {
|
} else {
|
||||||
directoryEntry = directoryData[name];
|
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'))
|
callback(new EIsDirectory('the named file is a directory and O_WRITE is set'))
|
||||||
} else {
|
} else {
|
||||||
read_object(objectStore, directoryEntry.id, set_file_node);
|
read_object(objectStore, directoryEntry.id, set_file_node);
|
||||||
|
@ -6446,8 +6446,10 @@ define('src/file-system',['require','lodash','when','src/path','src/path','src/p
|
||||||
};
|
};
|
||||||
FileSystem.prototype.open = function open(path, flags, callback) {
|
FileSystem.prototype.open = function open(path, flags, callback) {
|
||||||
var that = this;
|
var that = this;
|
||||||
|
this.promise.then(
|
||||||
|
function() {
|
||||||
var deferred = when.defer();
|
var deferred = when.defer();
|
||||||
var transaction = this.db.transaction([FILE_STORE_NAME], IDB_RW);
|
var transaction = that.db.transaction([FILE_STORE_NAME], IDB_RW);
|
||||||
var files = transaction.objectStore(FILE_STORE_NAME);
|
var files = transaction.objectStore(FILE_STORE_NAME);
|
||||||
|
|
||||||
function check_result(error, fileNode) {
|
function check_result(error, fileNode) {
|
||||||
|
@ -6473,9 +6475,20 @@ define('src/file-system',['require','lodash','when','src/path','src/path','src/p
|
||||||
flags = O_FLAGS[flags];
|
flags = O_FLAGS[flags];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
open_file(this, files, path, flags, check_result);
|
open_file(this, files, path, flags, check_result);
|
||||||
deferred.then(callback);
|
deferred.promise.then(
|
||||||
|
function(result) {
|
||||||
|
callback(undefined, result);
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
callback(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
},
|
||||||
|
function() {
|
||||||
|
callback(new EFileSystemError('unknown error'));
|
||||||
|
}
|
||||||
|
);
|
||||||
};
|
};
|
||||||
FileSystem.prototype.close = function close(fd, callback) {
|
FileSystem.prototype.close = function close(fd, callback) {
|
||||||
var deferred = when.defer();
|
var deferred = when.defer();
|
||||||
|
|
|
@ -21,10 +21,8 @@ var flags = 'FORMAT';
|
||||||
//var flags;
|
//var flags;
|
||||||
|
|
||||||
var fs = new IDBFS.FileSystem('local', flags);
|
var fs = new IDBFS.FileSystem('local', flags);
|
||||||
fs.mkdir('/tmp', function(error) {
|
fs.open('/', 'r', function(error, result) {
|
||||||
/*fs.rmdir('/tmp', function(error) {
|
console.log('1', error, result);
|
||||||
console.log('!');
|
|
||||||
});*/
|
|
||||||
});
|
});
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -385,7 +385,15 @@ define(function(require) {
|
||||||
var fileNode;
|
var fileNode;
|
||||||
var fileData;
|
var fileData;
|
||||||
|
|
||||||
|
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);
|
find_node(objectStore, parentPath, read_directory_data);
|
||||||
|
}
|
||||||
|
|
||||||
function read_directory_data(error, result) {
|
function read_directory_data(error, result) {
|
||||||
if(error) {
|
if(error) {
|
||||||
|
@ -406,7 +414,7 @@ define(function(require) {
|
||||||
callback(new ENoEntry('O_CREATE and O_EXCLUSIVE are set, and the named file exists'))
|
callback(new ENoEntry('O_CREATE and O_EXCLUSIVE are set, and the named file exists'))
|
||||||
} else {
|
} else {
|
||||||
directoryEntry = directoryData[name];
|
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'))
|
callback(new EIsDirectory('the named file is a directory and O_WRITE is set'))
|
||||||
} else {
|
} else {
|
||||||
read_object(objectStore, directoryEntry.id, set_file_node);
|
read_object(objectStore, directoryEntry.id, set_file_node);
|
||||||
|
@ -646,8 +654,10 @@ define(function(require) {
|
||||||
};
|
};
|
||||||
FileSystem.prototype.open = function open(path, flags, callback) {
|
FileSystem.prototype.open = function open(path, flags, callback) {
|
||||||
var that = this;
|
var that = this;
|
||||||
|
this.promise.then(
|
||||||
|
function() {
|
||||||
var deferred = when.defer();
|
var deferred = when.defer();
|
||||||
var transaction = this.db.transaction([FILE_STORE_NAME], IDB_RW);
|
var transaction = that.db.transaction([FILE_STORE_NAME], IDB_RW);
|
||||||
var files = transaction.objectStore(FILE_STORE_NAME);
|
var files = transaction.objectStore(FILE_STORE_NAME);
|
||||||
|
|
||||||
function check_result(error, fileNode) {
|
function check_result(error, fileNode) {
|
||||||
|
@ -673,9 +683,20 @@ define(function(require) {
|
||||||
flags = O_FLAGS[flags];
|
flags = O_FLAGS[flags];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
open_file(this, files, path, flags, check_result);
|
open_file(this, files, path, flags, check_result);
|
||||||
deferred.then(callback);
|
deferred.promise.then(
|
||||||
|
function(result) {
|
||||||
|
callback(undefined, result);
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
callback(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
},
|
||||||
|
function() {
|
||||||
|
callback(new EFileSystemError('unknown error'));
|
||||||
|
}
|
||||||
|
);
|
||||||
};
|
};
|
||||||
FileSystem.prototype.close = function close(fd, callback) {
|
FileSystem.prototype.close = function close(fd, callback) {
|
||||||
var deferred = when.defer();
|
var deferred = when.defer();
|
||||||
|
|
|
@ -61,7 +61,7 @@ describe("fs", function() {
|
||||||
|
|
||||||
waitsFor(function() {
|
waitsFor(function() {
|
||||||
return complete;
|
return complete;
|
||||||
}, 'stat to complete', DEFAULT_TIMEOUT);
|
}, 'test to complete', DEFAULT_TIMEOUT);
|
||||||
|
|
||||||
runs(function() {
|
runs(function() {
|
||||||
expect(_result).toBeDefined();
|
expect(_result).toBeDefined();
|
||||||
|
@ -97,7 +97,7 @@ describe('fs.stat', function() {
|
||||||
|
|
||||||
waitsFor(function() {
|
waitsFor(function() {
|
||||||
return complete;
|
return complete;
|
||||||
}, 'stat to complete', DEFAULT_TIMEOUT);
|
}, 'test to complete', DEFAULT_TIMEOUT);
|
||||||
|
|
||||||
runs(function() {
|
runs(function() {
|
||||||
expect(_error).toBeDefined();
|
expect(_error).toBeDefined();
|
||||||
|
@ -119,7 +119,7 @@ describe('fs.stat', function() {
|
||||||
|
|
||||||
waitsFor(function() {
|
waitsFor(function() {
|
||||||
return complete;
|
return complete;
|
||||||
}, 'stat to complete', DEFAULT_TIMEOUT);
|
}, 'test to complete', DEFAULT_TIMEOUT);
|
||||||
|
|
||||||
runs(function() {
|
runs(function() {
|
||||||
expect(_result).toBeDefined();
|
expect(_result).toBeDefined();
|
||||||
|
@ -181,7 +181,7 @@ describe('fs.mkdir', function() {
|
||||||
|
|
||||||
waitsFor(function() {
|
waitsFor(function() {
|
||||||
return complete;
|
return complete;
|
||||||
}, 'stat to complete', DEFAULT_TIMEOUT);
|
}, 'test to complete', DEFAULT_TIMEOUT);
|
||||||
|
|
||||||
runs(function() {
|
runs(function() {
|
||||||
expect(_error).toBeDefined();
|
expect(_error).toBeDefined();
|
||||||
|
@ -206,7 +206,7 @@ describe('fs.mkdir', function() {
|
||||||
|
|
||||||
waitsFor(function() {
|
waitsFor(function() {
|
||||||
return complete;
|
return complete;
|
||||||
}, 'stat to complete', DEFAULT_TIMEOUT);
|
}, 'test to complete', DEFAULT_TIMEOUT);
|
||||||
|
|
||||||
runs(function() {
|
runs(function() {
|
||||||
expect(_error).not.toBeDefined();
|
expect(_error).not.toBeDefined();
|
||||||
|
@ -231,7 +231,7 @@ describe('fs.rmdir', function() {
|
||||||
expect(typeof this.fs.rmdir).toEqual('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 complete = false;
|
||||||
var _error;
|
var _error;
|
||||||
var that = this;
|
var that = this;
|
||||||
|
@ -244,7 +244,7 @@ describe('fs.rmdir', function() {
|
||||||
|
|
||||||
waitsFor(function() {
|
waitsFor(function() {
|
||||||
return complete;
|
return complete;
|
||||||
}, 'rmdir to complete', DEFAULT_TIMEOUT);
|
}, 'test to complete', DEFAULT_TIMEOUT);
|
||||||
|
|
||||||
runs(function() {
|
runs(function() {
|
||||||
expect(_error).toBeDefined();
|
expect(_error).toBeDefined();
|
||||||
|
@ -264,7 +264,7 @@ describe('fs.rmdir', function() {
|
||||||
|
|
||||||
waitsFor(function() {
|
waitsFor(function() {
|
||||||
return complete;
|
return complete;
|
||||||
}, 'rmdir to complete', DEFAULT_TIMEOUT);
|
}, 'test to complete', DEFAULT_TIMEOUT);
|
||||||
|
|
||||||
runs(function() {
|
runs(function() {
|
||||||
expect(_error).toBeDefined();
|
expect(_error).toBeDefined();
|
||||||
|
@ -288,7 +288,7 @@ describe('fs.rmdir', function() {
|
||||||
|
|
||||||
waitsFor(function() {
|
waitsFor(function() {
|
||||||
return complete;
|
return complete;
|
||||||
}, 'rmdir to complete', DEFAULT_TIMEOUT);
|
}, 'test to complete', DEFAULT_TIMEOUT);
|
||||||
|
|
||||||
runs(function() {
|
runs(function() {
|
||||||
expect(_error).toBeDefined();
|
expect(_error).toBeDefined();
|
||||||
|
@ -313,7 +313,7 @@ describe('fs.rmdir', function() {
|
||||||
|
|
||||||
waitsFor(function() {
|
waitsFor(function() {
|
||||||
return complete;
|
return complete;
|
||||||
}, 'rmdir to complete', DEFAULT_TIMEOUT);
|
}, 'test to complete', DEFAULT_TIMEOUT);
|
||||||
|
|
||||||
runs(function() {
|
runs(function() {
|
||||||
expect(_error).not.toBeDefined();
|
expect(_error).not.toBeDefined();
|
||||||
|
@ -321,3 +321,118 @@ describe('fs.rmdir', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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() {
|
||||||
|
});
|
||||||
|
*/
|
||||||
|
});
|
Loading…
Reference in New Issue