Bug 840887 - Fix behavior of DirectoryIterator when the directory doesn't exist - Unix version. r=froydnj

This commit is contained in:
David Rajchenbach-Teller 2013-02-26 12:07:42 -05:00
parent c45bf4ae63
commit 89fd452b5b
2 changed files with 31 additions and 4 deletions

View File

@ -314,4 +314,8 @@ if (typeof Components != "undefined") {
OSError.exists = function exists(operation) {
return new OSError(operation, OS.Constants.libc.EEXIST);
};
OSError.noSuchFile = function noSuchFile(operation) {
return new OSError(operation, OS.Constants.libc.ENOENT);
};
})(this);

View File

@ -607,9 +607,19 @@
*/
File.DirectoryIterator = function DirectoryIterator(path, options) {
exports.OS.Shared.AbstractFile.AbstractIterator.call(this);
let dir = throw_on_null("DirectoryIterator", UnixFile.opendir(path));
this._dir = dir;
this._path = path;
this._dir = UnixFile.opendir(this._path);
if (this._dir == null) {
let error = ctypes.errno;
if (error != OS.Constants.libc.ENOENT) {
throw new File.Error("DirectoryIterator", error);
}
this._exists = false;
this._closed = true;
} else {
this._exists = true;
this._closed = false;
}
};
File.DirectoryIterator.prototype = Object.create(exports.OS.Shared.AbstractFile.AbstractIterator.prototype);
@ -624,7 +634,10 @@
* encountered.
*/
File.DirectoryIterator.prototype.next = function next() {
if (!this._dir) {
if (!this._exists) {
throw File.Error.noSuchFile("DirectoryIterator.prototype.next");
}
if (this._closed) {
throw StopIteration;
}
for (let entry = UnixFile.readdir(this._dir);
@ -648,11 +661,21 @@
* You should call this once you have finished iterating on a directory.
*/
File.DirectoryIterator.prototype.close = function close() {
if (!this._dir) return;
if (this._closed) return;
this._closed = true;
UnixFile.closedir(this._dir);
this._dir = null;
};
/**
* Determine whether the directory exists.
*
* @return {boolean}
*/
File.DirectoryIterator.prototype.exists = function exists() {
return this._exists;
};
/**
* Return directory as |File|
*/