diff options
author | dumi@chromium.org <dumi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-14 23:43:29 +0000 |
---|---|---|
committer | dumi@chromium.org <dumi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-14 23:43:29 +0000 |
commit | a82026e0a7c0b5f3672f92242d4180cc52328add (patch) | |
tree | 30c2625d8d44d1b12c79e963256b655d28a45a71 /base/file_util_win.cc | |
parent | 1346ecce2c718b2237cbb8ba95e9674f2e381943 (diff) | |
download | chromium_src-a82026e0a7c0b5f3672f92242d4180cc52328add.zip chromium_src-a82026e0a7c0b5f3672f92242d4180cc52328add.tar.gz chromium_src-a82026e0a7c0b5f3672f92242d4180cc52328add.tar.bz2 |
Un-recursify FileEnumerator::Next().
BUG=58368
TEST=file_util_unittest.cc passes
Review URL: http://codereview.chromium.org/3751001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@62677 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/file_util_win.cc')
-rw-r--r-- | base/file_util_win.cc | 103 |
1 files changed, 52 insertions, 51 deletions
diff --git a/base/file_util_win.cc b/base/file_util_win.cc index dec4be6..c601f46 100644 --- a/base/file_util_win.cc +++ b/base/file_util_win.cc @@ -772,7 +772,7 @@ FileEnumerator::FileEnumerator(const FilePath& root_path, FileEnumerator::FILE_TYPE file_type) : recursive_(recursive), file_type_(file_type), - is_in_find_op_(false), + has_find_data_(false), find_handle_(INVALID_HANDLE_VALUE) { // INCLUDE_DOT_DOT must not be specified if recursive. DCHECK(!(recursive && (INCLUDE_DOT_DOT & file_type_))); @@ -785,7 +785,7 @@ FileEnumerator::FileEnumerator(const FilePath& root_path, const FilePath::StringType& pattern) : recursive_(recursive), file_type_(file_type), - is_in_find_op_(false), + has_find_data_(false), pattern_(pattern), find_handle_(INVALID_HANDLE_VALUE) { // INCLUDE_DOT_DOT must not be specified if recursive. @@ -801,7 +801,7 @@ FileEnumerator::~FileEnumerator() { void FileEnumerator::GetFindInfo(FindInfo* info) { DCHECK(info); - if (!is_in_find_op_) + if (!has_find_data_) return; memcpy(info, &find_data_, sizeof(*info)); @@ -817,63 +817,64 @@ FilePath FileEnumerator::GetFilename(const FindInfo& find_info) { } FilePath FileEnumerator::Next() { - if (!is_in_find_op_) { - if (pending_paths_.empty()) - return FilePath(); - - // The last find FindFirstFile operation is done, prepare a new one. - root_path_ = pending_paths_.top(); - pending_paths_.pop(); - - // Start a new find operation. - FilePath src = root_path_; + while (has_find_data_ || !pending_paths_.empty()) { + if (!has_find_data_) { + // The last find FindFirstFile operation is done, prepare a new one. + root_path_ = pending_paths_.top(); + pending_paths_.pop(); + + // Start a new find operation. + FilePath src = root_path_; + + if (pattern_.empty()) + src = src.Append(L"*"); // No pattern = match everything. + else + src = src.Append(pattern_); + + find_handle_ = FindFirstFile(src.value().c_str(), &find_data_); + has_find_data_ = true; + } else { + // Search for the next file/directory. + if (!FindNextFile(find_handle_, &find_data_)) { + FindClose(find_handle_); + find_handle_ = INVALID_HANDLE_VALUE; + } + } - if (pattern_.empty()) - src = src.Append(L"*"); // No pattern = match everything. - else - src = src.Append(pattern_); + if (INVALID_HANDLE_VALUE == find_handle_) { + has_find_data_ = false; - find_handle_ = FindFirstFile(src.value().c_str(), &find_data_); - is_in_find_op_ = true; + // This is reached when we have finished a directory and are advancing to + // the next one in the queue. We applied the pattern (if any) to the files + // in the root search directory, but for those directories which were + // matched, we want to enumerate all files inside them. This will happen + // when the handle is empty. + pattern_ = FilePath::StringType(); - } else { - // Search for the next file/directory. - if (!FindNextFile(find_handle_, &find_data_)) { - FindClose(find_handle_); - find_handle_ = INVALID_HANDLE_VALUE; + continue; } - } - if (INVALID_HANDLE_VALUE == find_handle_) { - is_in_find_op_ = false; + FilePath cur_file(find_data_.cFileName); + if (ShouldSkip(cur_file)) + continue; - // This is reached when we have finished a directory and are advancing to - // the next one in the queue. We applied the pattern (if any) to the files - // in the root search directory, but for those directories which were - // matched, we want to enumerate all files inside them. This will happen - // when the handle is empty. - pattern_ = FilePath::StringType(); + // Construct the absolute filename. + cur_file = root_path_.Append(find_data_.cFileName); - return Next(); + if (find_data_.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { + if (recursive_) { + // If |cur_file| is a directory, and we are doing recursive searching, + // add it to pending_paths_ so we scan it after we finish scanning this + // directory. + pending_paths_.push(cur_file); + } + if (file_type_ & FileEnumerator::DIRECTORIES) + return cur_file; + } else if (file_type_ & FileEnumerator::FILES) + return cur_file; } - FilePath cur_file(find_data_.cFileName); - if (ShouldSkip(cur_file)) - return Next(); - - // Construct the absolute filename. - cur_file = root_path_.Append(cur_file); - - if (find_data_.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { - if (recursive_) { - // If |cur_file| is a directory, and we are doing recursive searching, add - // it to pending_paths_ so we scan it after we finish scanning this - // directory. - pending_paths_.push(cur_file); - } - return (file_type_ & FileEnumerator::DIRECTORIES) ? cur_file : Next(); - } - return (file_type_ & FileEnumerator::FILES) ? cur_file : Next(); + return FilePath(); } /////////////////////////////////////////////// |