OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/files/file_enumerator.h" | 5 #include "base/files/file_enumerator.h" |
6 | 6 |
7 #include <Shlwapi.h> | |
Mark Mentovai
2017/05/19 17:16:12
We always use lowercase names for these.
ivafanas
2017/05/22 04:42:28
Done.
| |
8 | |
Mark Mentovai
2017/05/19 17:16:12
Why the blank line?
ivafanas
2017/05/22 04:42:28
Done.
| |
7 #include <stdint.h> | 9 #include <stdint.h> |
8 #include <string.h> | 10 #include <string.h> |
9 | 11 |
10 #include "base/logging.h" | 12 #include "base/logging.h" |
11 #include "base/threading/thread_restrictions.h" | 13 #include "base/threading/thread_restrictions.h" |
12 #include "base/win/windows_version.h" | 14 #include "base/win/windows_version.h" |
13 | 15 |
14 namespace base { | 16 namespace base { |
15 | 17 |
18 namespace { | |
19 | |
20 HANDLE FindFirstFileImpl(const FilePath& src, WIN32_FIND_DATA* find_data) { | |
21 if (base::win::GetVersion() >= base::win::VERSION_WIN7) { | |
Mark Mentovai
2017/05/19 17:16:12
Isn’t this our minimum anyway?
ivafanas
2017/05/22 04:42:28
Done.
| |
22 return FindFirstFileEx(src.value().c_str(), | |
23 FindExInfoBasic, // Omit short name. | |
24 find_data, FindExSearchNameMatch, NULL, | |
Mark Mentovai
2017/05/19 17:16:12
nullptr instead of NULL
| |
25 FIND_FIRST_EX_LARGE_FETCH); | |
26 } else { | |
27 return FindFirstFile(src.value().c_str(), find_data); | |
28 } | |
29 } | |
30 | |
31 FilePath BuildSearchFilter(FileEnumerator::FolderSearchPolicy policy, | |
32 const FilePath& root_path, | |
33 const FilePath::StringType& pattern) { | |
34 // MATCH_ONLY policy filters incoming files by pattern on OS side. ALL policy | |
35 // collects all files and filters them manually. | |
36 switch (policy) { | |
37 case FileEnumerator::FolderSearchPolicy::MATCH_ONLY: | |
38 return root_path.Append(pattern); | |
39 case FileEnumerator::FolderSearchPolicy::ALL: | |
40 return root_path.AppendASCII("*"); | |
41 } | |
42 NOTREACHED(); | |
43 return {}; | |
44 } | |
45 | |
46 } // namespace | |
47 | |
16 // FileEnumerator::FileInfo ---------------------------------------------------- | 48 // FileEnumerator::FileInfo ---------------------------------------------------- |
17 | 49 |
18 FileEnumerator::FileInfo::FileInfo() { | 50 FileEnumerator::FileInfo::FileInfo() { |
19 memset(&find_data_, 0, sizeof(find_data_)); | 51 memset(&find_data_, 0, sizeof(find_data_)); |
20 } | 52 } |
21 | 53 |
22 bool FileEnumerator::FileInfo::IsDirectory() const { | 54 bool FileEnumerator::FileInfo::IsDirectory() const { |
23 return (find_data_.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; | 55 return (find_data_.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; |
24 } | 56 } |
25 | 57 |
(...skipping 12 matching lines...) Expand all Loading... | |
38 | 70 |
39 base::Time FileEnumerator::FileInfo::GetLastModifiedTime() const { | 71 base::Time FileEnumerator::FileInfo::GetLastModifiedTime() const { |
40 return base::Time::FromFileTime(find_data_.ftLastWriteTime); | 72 return base::Time::FromFileTime(find_data_.ftLastWriteTime); |
41 } | 73 } |
42 | 74 |
43 // FileEnumerator -------------------------------------------------------------- | 75 // FileEnumerator -------------------------------------------------------------- |
44 | 76 |
45 FileEnumerator::FileEnumerator(const FilePath& root_path, | 77 FileEnumerator::FileEnumerator(const FilePath& root_path, |
46 bool recursive, | 78 bool recursive, |
47 int file_type) | 79 int file_type) |
48 : has_find_data_(false), | 80 : FileEnumerator(root_path, |
49 find_handle_(INVALID_HANDLE_VALUE), | 81 recursive, |
50 recursive_(recursive), | 82 file_type, |
51 file_type_(file_type) { | 83 FilePath::StringType(), |
52 // INCLUDE_DOT_DOT must not be specified if recursive. | 84 FolderSearchPolicy::MATCH_ONLY) {} |
53 DCHECK(!(recursive && (INCLUDE_DOT_DOT & file_type_))); | |
54 memset(&find_data_, 0, sizeof(find_data_)); | |
55 pending_paths_.push(root_path); | |
56 } | |
57 | 85 |
58 FileEnumerator::FileEnumerator(const FilePath& root_path, | 86 FileEnumerator::FileEnumerator(const FilePath& root_path, |
59 bool recursive, | 87 bool recursive, |
60 int file_type, | 88 int file_type, |
61 const FilePath::StringType& pattern) | 89 const FilePath::StringType& pattern) |
62 : has_find_data_(false), | 90 : FileEnumerator(root_path, |
63 find_handle_(INVALID_HANDLE_VALUE), | 91 recursive, |
64 recursive_(recursive), | 92 file_type, |
93 pattern, | |
94 FolderSearchPolicy::MATCH_ONLY) {} | |
95 | |
96 FileEnumerator::FileEnumerator(const FilePath& root_path, | |
97 bool recursive, | |
98 int file_type, | |
99 const FilePath::StringType& pattern, | |
100 FolderSearchPolicy folder_search_policy) | |
101 : recursive_(recursive), | |
65 file_type_(file_type), | 102 file_type_(file_type), |
66 pattern_(pattern) { | 103 pattern_(!pattern.empty() ? pattern : FILE_PATH_LITERAL("*")), |
104 folder_search_policy_(folder_search_policy) { | |
67 // INCLUDE_DOT_DOT must not be specified if recursive. | 105 // INCLUDE_DOT_DOT must not be specified if recursive. |
68 DCHECK(!(recursive && (INCLUDE_DOT_DOT & file_type_))); | 106 DCHECK(!(recursive && (INCLUDE_DOT_DOT & file_type_))); |
69 memset(&find_data_, 0, sizeof(find_data_)); | 107 memset(&find_data_, 0, sizeof(find_data_)); |
70 pending_paths_.push(root_path); | 108 pending_paths_.push(root_path); |
71 } | 109 } |
72 | 110 |
73 FileEnumerator::~FileEnumerator() { | 111 FileEnumerator::~FileEnumerator() { |
74 if (find_handle_ != INVALID_HANDLE_VALUE) | 112 if (find_handle_ != INVALID_HANDLE_VALUE) |
75 FindClose(find_handle_); | 113 FindClose(find_handle_); |
76 } | 114 } |
(...skipping 11 matching lines...) Expand all Loading... | |
88 FilePath FileEnumerator::Next() { | 126 FilePath FileEnumerator::Next() { |
89 base::ThreadRestrictions::AssertIOAllowed(); | 127 base::ThreadRestrictions::AssertIOAllowed(); |
90 | 128 |
91 while (has_find_data_ || !pending_paths_.empty()) { | 129 while (has_find_data_ || !pending_paths_.empty()) { |
92 if (!has_find_data_) { | 130 if (!has_find_data_) { |
93 // The last find FindFirstFile operation is done, prepare a new one. | 131 // The last find FindFirstFile operation is done, prepare a new one. |
94 root_path_ = pending_paths_.top(); | 132 root_path_ = pending_paths_.top(); |
95 pending_paths_.pop(); | 133 pending_paths_.pop(); |
96 | 134 |
97 // Start a new find operation. | 135 // Start a new find operation. |
98 FilePath src = root_path_; | 136 const auto src = |
99 | 137 BuildSearchFilter(folder_search_policy_, root_path_, pattern_); |
100 if (pattern_.empty()) | 138 find_handle_ = FindFirstFileImpl(src, &find_data_); |
101 src = src.Append(L"*"); // No pattern = match everything. | |
102 else | |
103 src = src.Append(pattern_); | |
104 | |
105 if (base::win::GetVersion() >= base::win::VERSION_WIN7) { | |
106 // Use a "large fetch" on newer Windows which should speed up large | |
107 // enumerations (we seldom abort in the middle). | |
108 find_handle_ = FindFirstFileEx(src.value().c_str(), | |
109 FindExInfoBasic, // Omit short name. | |
110 &find_data_, | |
111 FindExSearchNameMatch, | |
112 NULL, | |
113 FIND_FIRST_EX_LARGE_FETCH); | |
114 } else { | |
115 find_handle_ = FindFirstFile(src.value().c_str(), &find_data_); | |
116 } | |
117 has_find_data_ = true; | 139 has_find_data_ = true; |
118 } else { | 140 } else { |
119 // Search for the next file/directory. | 141 // Search for the next file/directory. |
120 if (!FindNextFile(find_handle_, &find_data_)) { | 142 if (!FindNextFile(find_handle_, &find_data_)) { |
121 FindClose(find_handle_); | 143 FindClose(find_handle_); |
122 find_handle_ = INVALID_HANDLE_VALUE; | 144 find_handle_ = INVALID_HANDLE_VALUE; |
123 } | 145 } |
124 } | 146 } |
125 | 147 |
126 if (INVALID_HANDLE_VALUE == find_handle_) { | 148 if (INVALID_HANDLE_VALUE == find_handle_) { |
127 has_find_data_ = false; | 149 has_find_data_ = false; |
128 | 150 |
129 // This is reached when we have finished a directory and are advancing to | 151 // MATCH_ONLY policy clears pattern for matched subfolders. ALL policy |
130 // the next one in the queue. We applied the pattern (if any) to the files | 152 // applies pattern for all subfolders. |
131 // in the root search directory, but for those directories which were | 153 if (folder_search_policy_ == FolderSearchPolicy::MATCH_ONLY) { |
132 // matched, we want to enumerate all files inside them. This will happen | 154 // This is reached when we have finished a directory and are advancing |
133 // when the handle is empty. | 155 // to the next one in the queue. We applied the pattern (if any) to the |
134 pattern_ = FilePath::StringType(); | 156 // files in the root search directory, but for those directories which |
157 // were matched, we want to enumerate all files inside them. This will | |
158 // happen when the handle is empty. | |
159 pattern_ = FILE_PATH_LITERAL("*"); | |
160 } | |
135 | 161 |
136 continue; | 162 continue; |
137 } | 163 } |
138 | 164 |
139 FilePath cur_file(find_data_.cFileName); | 165 const FilePath filename(find_data_.cFileName); |
140 if (ShouldSkip(cur_file)) | 166 if (ShouldSkip(filename)) |
141 continue; | 167 continue; |
142 | 168 |
143 // Construct the absolute filename. | 169 const bool is_dir = |
144 cur_file = root_path_.Append(find_data_.cFileName); | 170 (find_data_.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; |
171 const auto abs_path = root_path_.Append(filename); | |
145 | 172 |
146 if (find_data_.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { | 173 // Check if directory should be processed recursive. |
147 if (recursive_) { | 174 if (is_dir && recursive_) { |
148 // If |cur_file| is a directory, and we are doing recursive searching, | 175 // If |cur_file| is a directory, and we are doing recursive searching, |
149 // add it to pending_paths_ so we scan it after we finish scanning this | 176 // add it to pending_paths_ so we scan it after we finish scanning this |
150 // directory. However, don't do recursion through reparse points or we | 177 // directory. However, don't do recursion through reparse points or we |
151 // may end up with an infinite cycle. | 178 // may end up with an infinite cycle. |
152 DWORD attributes = GetFileAttributes(cur_file.value().c_str()); | 179 DWORD attributes = GetFileAttributes(abs_path.value().c_str()); |
153 if (!(attributes & FILE_ATTRIBUTE_REPARSE_POINT)) | 180 if (!(attributes & FILE_ATTRIBUTE_REPARSE_POINT)) |
154 pending_paths_.push(cur_file); | 181 pending_paths_.push(abs_path); |
155 } | |
156 if (file_type_ & FileEnumerator::DIRECTORIES) | |
157 return cur_file; | |
158 } else if (file_type_ & FileEnumerator::FILES) { | |
159 return cur_file; | |
160 } | 182 } |
183 | |
184 if (IsTypeMatched(is_dir) && IsPatternMatched(filename)) | |
185 return abs_path; | |
161 } | 186 } |
162 | |
163 return FilePath(); | 187 return FilePath(); |
164 } | 188 } |
165 | 189 |
190 bool FileEnumerator::IsPatternMatched(const FilePath& src) const { | |
191 switch (folder_search_policy_) { | |
192 case FolderSearchPolicy::MATCH_ONLY: | |
193 // MATCH_ONLY policy filters by pattern on search request, so all found | |
194 // files already fits to pattern. | |
195 return true; | |
196 case FolderSearchPolicy::ALL: | |
197 // ALL policy enumerates all files, we need to check pattern match | |
198 // manually. | |
199 return pattern_.empty() || | |
200 PathMatchSpec(src.value().c_str(), pattern_.c_str()) == TRUE; | |
201 } | |
202 NOTREACHED(); | |
203 return false; | |
204 } | |
205 | |
166 } // namespace base | 206 } // namespace base |
OLD | NEW |