support for readdir

This commit is contained in:
Alan Kligman 2013-11-07 15:55:14 -05:00
parent d46e6a1d42
commit 7e59e3a684
6 changed files with 187 additions and 3 deletions

View File

@ -106,3 +106,7 @@ The callback gets `(error, nbytes)`, where `nbytes` is the number of bytes read.
#### fs.lseek(fd, offset, whence, callback) #### fs.lseek(fd, offset, whence, callback)
Asynchronous lseek(2), where `whence` can be `SET`, `CUR`, or `END`. Callback gets `(error, pos)`, where `pos` is the resulting offset, in bytes, from the beginning of the file. Asynchronous lseek(2), where `whence` can be `SET`, `CUR`, or `END`. Callback gets `(error, pos)`, where `pos` is the resulting offset, in bytes, from the beginning of the file.
#### fs.readdir(path, callback)
Asynchronous readdir(3). Reads the contents of a directory. Callback gets `(error, files)`, where `files` is an array containing the names of each file in the directory, excluding `.` and `..`.

59
dist/idbfs.js vendored
View File

@ -6548,6 +6548,35 @@ define('src/file-system',['require','lodash','when','src/path','src/path','src/p
} }
}; };
function read_directory(objectStore, path, callback) {
path = normalize(path);
var name = basename(path);
var directoryNode;
var directoryData;
find_node(objectStore, path, read_directory_data);
function read_directory_data(error, result) {
if(error) {
callback(error);
} else {
directoryNode = result;
read_object(objectStore, directoryNode.data, handle_directory_data);
}
};
function handle_directory_data(error, result) {
if(error) {
callback(error);
} else {
directoryData = result;
var files = Object.keys(directoryData);
callback(undefined, files);
}
};
};
/* /*
* FileSystem * FileSystem
*/ */
@ -7059,7 +7088,37 @@ define('src/file-system',['require','lodash','when','src/path','src/path','src/p
); );
}; };
FileSystem.prototype.readdir = function readdir(path, callback) { FileSystem.prototype.readdir = function readdir(path, callback) {
var that = this;
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, files) {
if(error) {
// if(transaction.error) transaction.abort();
deferred.reject(error);
} else {
deferred.resolve(files);
}
};
read_directory(files, path, check_result);
deferred.promise.then(
function(result) {
callback(undefined, result);
},
function(error) {
callback(error);
}
);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
}; };
FileSystem.prototype.utimes = function utimes(path, atime, mtime, callback) { FileSystem.prototype.utimes = function utimes(path, atime, mtime, callback) {

3
dist/idbfs.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -38,6 +38,10 @@ fs.open('/tmp', 'w+', function(error, fd) {
fs.stat('/tmp', function(error, stats) { fs.stat('/tmp', function(error, stats) {
if(error) throw error; if(error) throw error;
console.log('stats:', stats); console.log('stats:', stats);
fs.readdir('/', function(error, files) {
if(error) throw error;
console.log('ls:', files);
});
}); });
}); });
}); });

View File

@ -748,6 +748,35 @@ define(function(require) {
} }
}; };
function read_directory(objectStore, path, callback) {
path = normalize(path);
var name = basename(path);
var directoryNode;
var directoryData;
find_node(objectStore, path, read_directory_data);
function read_directory_data(error, result) {
if(error) {
callback(error);
} else {
directoryNode = result;
read_object(objectStore, directoryNode.data, handle_directory_data);
}
};
function handle_directory_data(error, result) {
if(error) {
callback(error);
} else {
directoryData = result;
var files = Object.keys(directoryData);
callback(undefined, files);
}
};
};
/* /*
* FileSystem * FileSystem
*/ */
@ -1259,7 +1288,37 @@ define(function(require) {
); );
}; };
FileSystem.prototype.readdir = function readdir(path, callback) { FileSystem.prototype.readdir = function readdir(path, callback) {
var that = this;
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, files) {
if(error) {
// if(transaction.error) transaction.abort();
deferred.reject(error);
} else {
deferred.resolve(files);
}
};
read_directory(files, path, check_result);
deferred.promise.then(
function(result) {
callback(undefined, result);
},
function(error) {
callback(error);
}
);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
}; };
FileSystem.prototype.utimes = function utimes(path, atime, mtime, callback) { FileSystem.prototype.utimes = function utimes(path, atime, mtime, callback) {

View File

@ -284,6 +284,67 @@ describe('fs.mkdir', function() {
}); });
}); });
describe('fs.readdir', 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.readdir).toEqual('function');
});
it('should return an error if the path does not exist', function() {
var complete = false;
var _error;
var that = this;
that.fs.readdir('/tmp/mydir', function(error) {
_error = error;
complete = true;
});
waitsFor(function() {
return complete;
}, 'test to complete', DEFAULT_TIMEOUT);
runs(function() {
expect(_error).toBeDefined();
});
});
it('should return a list of files from an existing directory', function() {
var complete = false;
var _error, _files;
var that = this;
that.fs.mkdir('/tmp', function(error) {
that.fs.readdir('/', function(error, result) {
_error = error;
_files = result;
complete = true;
});
});
waitsFor(function() {
return complete;
}, 'test to complete', DEFAULT_TIMEOUT);
runs(function() {
expect(_error).not.toBeDefined();
expect(_files.length).toEqual(1);
expect(_files[0]).toEqual('tmp');
});
});
});
describe('fs.rmdir', function() { describe('fs.rmdir', function() {
beforeEach(function() { beforeEach(function() {
this.db_name = mk_db_name(); this.db_name = mk_db_name();