Update broken references to image assets
[chromium-blink-merge.git] / base / files / file_enumerator_win.cc
blob90db7f5729efc624b76b0f328bfcfdac86793055
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/files/file_enumerator.h"
7 #include <string.h>
9 #include "base/logging.h"
10 #include "base/threading/thread_restrictions.h"
11 #include "base/win/windows_version.h"
13 namespace base {
15 // FileEnumerator::FileInfo ----------------------------------------------------
17 FileEnumerator::FileInfo::FileInfo() {
18 memset(&find_data_, 0, sizeof(find_data_));
21 bool FileEnumerator::FileInfo::IsDirectory() const {
22 return (find_data_.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
25 FilePath FileEnumerator::FileInfo::GetName() const {
26 return FilePath(find_data_.cFileName);
29 int64 FileEnumerator::FileInfo::GetSize() const {
30 ULARGE_INTEGER size;
31 size.HighPart = find_data_.nFileSizeHigh;
32 size.LowPart = find_data_.nFileSizeLow;
33 DCHECK_LE(size.QuadPart,
34 static_cast<ULONGLONG>(std::numeric_limits<int64>::max()));
35 return static_cast<int64>(size.QuadPart);
38 base::Time FileEnumerator::FileInfo::GetLastModifiedTime() const {
39 return base::Time::FromFileTime(find_data_.ftLastWriteTime);
42 // FileEnumerator --------------------------------------------------------------
44 FileEnumerator::FileEnumerator(const FilePath& root_path,
45 bool recursive,
46 int file_type)
47 : has_find_data_(false),
48 find_handle_(INVALID_HANDLE_VALUE),
49 recursive_(recursive),
50 file_type_(file_type) {
51 // INCLUDE_DOT_DOT must not be specified if recursive.
52 DCHECK(!(recursive && (INCLUDE_DOT_DOT & file_type_)));
53 memset(&find_data_, 0, sizeof(find_data_));
54 pending_paths_.push(root_path);
57 FileEnumerator::FileEnumerator(const FilePath& root_path,
58 bool recursive,
59 int file_type,
60 const FilePath::StringType& pattern)
61 : has_find_data_(false),
62 find_handle_(INVALID_HANDLE_VALUE),
63 recursive_(recursive),
64 file_type_(file_type),
65 pattern_(pattern) {
66 // INCLUDE_DOT_DOT must not be specified if recursive.
67 DCHECK(!(recursive && (INCLUDE_DOT_DOT & file_type_)));
68 memset(&find_data_, 0, sizeof(find_data_));
69 pending_paths_.push(root_path);
72 FileEnumerator::~FileEnumerator() {
73 if (find_handle_ != INVALID_HANDLE_VALUE)
74 FindClose(find_handle_);
77 FileEnumerator::FileInfo FileEnumerator::GetInfo() const {
78 if (!has_find_data_) {
79 NOTREACHED();
80 return FileInfo();
82 FileInfo ret;
83 memcpy(&ret.find_data_, &find_data_, sizeof(find_data_));
84 return ret;
87 FilePath FileEnumerator::Next() {
88 base::ThreadRestrictions::AssertIOAllowed();
90 while (has_find_data_ || !pending_paths_.empty()) {
91 if (!has_find_data_) {
92 // The last find FindFirstFile operation is done, prepare a new one.
93 root_path_ = pending_paths_.top();
94 pending_paths_.pop();
96 // Start a new find operation.
97 FilePath src = root_path_;
99 if (pattern_.empty())
100 src = src.Append(L"*"); // No pattern = match everything.
101 else
102 src = src.Append(pattern_);
104 if (base::win::GetVersion() >= base::win::VERSION_WIN7) {
105 // Use a "large fetch" on newer Windows which should speed up large
106 // enumerations (we seldom abort in the middle).
107 find_handle_ = FindFirstFileEx(src.value().c_str(),
108 FindExInfoBasic, // Omit short name.
109 &find_data_,
110 FindExSearchNameMatch,
111 NULL,
112 FIND_FIRST_EX_LARGE_FETCH);
113 } else {
114 find_handle_ = FindFirstFile(src.value().c_str(), &find_data_);
116 has_find_data_ = true;
117 } else {
118 // Search for the next file/directory.
119 if (!FindNextFile(find_handle_, &find_data_)) {
120 FindClose(find_handle_);
121 find_handle_ = INVALID_HANDLE_VALUE;
125 if (INVALID_HANDLE_VALUE == find_handle_) {
126 has_find_data_ = false;
128 // This is reached when we have finished a directory and are advancing to
129 // the next one in the queue. We applied the pattern (if any) to the files
130 // in the root search directory, but for those directories which were
131 // matched, we want to enumerate all files inside them. This will happen
132 // when the handle is empty.
133 pattern_ = FilePath::StringType();
135 continue;
138 FilePath cur_file(find_data_.cFileName);
139 if (ShouldSkip(cur_file))
140 continue;
142 // Construct the absolute filename.
143 cur_file = root_path_.Append(find_data_.cFileName);
145 if (find_data_.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
146 if (recursive_) {
147 // If |cur_file| is a directory, and we are doing recursive searching,
148 // add it to pending_paths_ so we scan it after we finish scanning this
149 // directory. However, don't do recursion through reparse points or we
150 // may end up with an infinite cycle.
151 DWORD attributes = GetFileAttributes(cur_file.value().c_str());
152 if (!(attributes & FILE_ATTRIBUTE_REPARSE_POINT))
153 pending_paths_.push(cur_file);
155 if (file_type_ & FileEnumerator::DIRECTORIES)
156 return cur_file;
157 } else if (file_type_ & FileEnumerator::FILES) {
158 return cur_file;
162 return FilePath();
165 } // namespace base