From ac1b70af1289cc875cfcfbb8bbb8a395c4327f0f Mon Sep 17 00:00:00 2001 From: David Rajchenbach-Teller Date: Tue, 26 Feb 2013 12:07:42 -0500 Subject: [PATCH] Bug 840887 - Fix behavior of DirectoryIterator when the directory doesn't exist - Unix version. r=froydnj --- .../osfile/osfile_unix_allthreads.jsm | 4 +++ .../components/osfile/osfile_unix_front.jsm | 31 ++++++++++++++++--- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/toolkit/components/osfile/osfile_unix_allthreads.jsm b/toolkit/components/osfile/osfile_unix_allthreads.jsm index e8b09f656e41..98ff13d18b3d 100644 --- a/toolkit/components/osfile/osfile_unix_allthreads.jsm +++ b/toolkit/components/osfile/osfile_unix_allthreads.jsm @@ -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); diff --git a/toolkit/components/osfile/osfile_unix_front.jsm b/toolkit/components/osfile/osfile_unix_front.jsm index f235131085db..95f504cda85e 100644 --- a/toolkit/components/osfile/osfile_unix_front.jsm +++ b/toolkit/components/osfile/osfile_unix_front.jsm @@ -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| */