OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef BASE_FILES_FILE_ENUMERATOR_H_ | 5 #ifndef BASE_FILES_FILE_ENUMERATOR_H_ |
6 #define BASE_FILES_FILE_ENUMERATOR_H_ | 6 #define BASE_FILES_FILE_ENUMERATOR_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 | 77 |
78 enum FileType { | 78 enum FileType { |
79 FILES = 1 << 0, | 79 FILES = 1 << 0, |
80 DIRECTORIES = 1 << 1, | 80 DIRECTORIES = 1 << 1, |
81 INCLUDE_DOT_DOT = 1 << 2, | 81 INCLUDE_DOT_DOT = 1 << 2, |
82 #if defined(OS_POSIX) | 82 #if defined(OS_POSIX) |
83 SHOW_SYM_LINKS = 1 << 4, | 83 SHOW_SYM_LINKS = 1 << 4, |
84 #endif | 84 #endif |
85 }; | 85 }; |
86 | 86 |
| 87 // Search policy for intermediate folders. |
| 88 enum class FolderSearchPolicy { |
| 89 // Recursive search will pass through folders whose names match the |
| 90 // pattern. Inside each one, all files will be returned. Folders with names |
| 91 // that do not match the pattern will be ignored within their interior. |
| 92 MATCH_ONLY, |
| 93 // Recursive search will pass through every folder and perform pattern |
| 94 // matching inside each one. |
| 95 ALL, |
| 96 }; |
| 97 |
87 // |root_path| is the starting directory to search for. It may or may not end | 98 // |root_path| is the starting directory to search for. It may or may not end |
88 // in a slash. | 99 // in a slash. |
89 // | 100 // |
90 // If |recursive| is true, this will enumerate all matches in any | 101 // If |recursive| is true, this will enumerate all matches in any |
91 // subdirectories matched as well. It does a breadth-first search, so all | 102 // subdirectories matched as well. It does a breadth-first search, so all |
92 // files in one directory will be returned before any files in a | 103 // files in one directory will be returned before any files in a |
93 // subdirectory. | 104 // subdirectory. |
94 // | 105 // |
95 // |file_type|, a bit mask of FileType, specifies whether the enumerator | 106 // |file_type|, a bit mask of FileType, specifies whether the enumerator |
96 // should match files, directories, or both. | 107 // should match files, directories, or both. |
97 // | 108 // |
98 // |pattern| is an optional pattern for which files to match. This | 109 // |pattern| is an optional pattern for which files to match. This |
99 // works like shell globbing. For example, "*.txt" or "Foo???.doc". | 110 // works like shell globbing. For example, "*.txt" or "Foo???.doc". |
100 // However, be careful in specifying patterns that aren't cross platform | 111 // However, be careful in specifying patterns that aren't cross platform |
101 // since the underlying code uses OS-specific matching routines. In general, | 112 // since the underlying code uses OS-specific matching routines. In general, |
102 // Windows matching is less featureful than others, so test there first. | 113 // Windows matching is less featureful than others, so test there first. |
103 // If unspecified, this will match all files. | 114 // If unspecified, this will match all files. |
104 // NOTE: the pattern only matches the contents of root_path, not files in | |
105 // recursive subdirectories. | |
106 // TODO(erikkay): Fix the pattern matching to work at all levels. | |
107 FileEnumerator(const FilePath& root_path, | 115 FileEnumerator(const FilePath& root_path, |
108 bool recursive, | 116 bool recursive, |
109 int file_type); | 117 int file_type); |
110 FileEnumerator(const FilePath& root_path, | 118 FileEnumerator(const FilePath& root_path, |
111 bool recursive, | 119 bool recursive, |
112 int file_type, | 120 int file_type, |
113 const FilePath::StringType& pattern); | 121 const FilePath::StringType& pattern); |
| 122 FileEnumerator(const FilePath& root_path, |
| 123 bool recursive, |
| 124 int file_type, |
| 125 const FilePath::StringType& pattern, |
| 126 FolderSearchPolicy folder_search_policy); |
114 ~FileEnumerator(); | 127 ~FileEnumerator(); |
115 | 128 |
116 // Returns the next file or an empty string if there are no more results. | 129 // Returns the next file or an empty string if there are no more results. |
117 // | 130 // |
118 // The returned path will incorporate the |root_path| passed in the | 131 // The returned path will incorporate the |root_path| passed in the |
119 // constructor: "<root_path>/file_name.txt". If the |root_path| is absolute, | 132 // constructor: "<root_path>/file_name.txt". If the |root_path| is absolute, |
120 // then so will be the result of Next(). | 133 // then so will be the result of Next(). |
121 FilePath Next(); | 134 FilePath Next(); |
122 | 135 |
123 // Write the file info into |info|. | 136 // Write the file info into |info|. |
124 FileInfo GetInfo() const; | 137 FileInfo GetInfo() const; |
125 | 138 |
126 private: | 139 private: |
127 // Returns true if the given path should be skipped in enumeration. | 140 // Returns true if the given path should be skipped in enumeration. |
128 bool ShouldSkip(const FilePath& path); | 141 bool ShouldSkip(const FilePath& path); |
129 | 142 |
| 143 bool IsTypeMatched(bool is_dir) const; |
| 144 |
| 145 bool IsPatternMatched(const FilePath& src) const; |
| 146 |
130 #if defined(OS_WIN) | 147 #if defined(OS_WIN) |
131 // True when find_data_ is valid. | 148 // True when find_data_ is valid. |
132 bool has_find_data_; | 149 bool has_find_data_ = false; |
133 WIN32_FIND_DATA find_data_; | 150 WIN32_FIND_DATA find_data_; |
134 HANDLE find_handle_; | 151 HANDLE find_handle_ = INVALID_HANDLE_VALUE; |
135 #elif defined(OS_POSIX) | 152 #elif defined(OS_POSIX) |
136 | |
137 // Read the filenames in source into the vector of DirectoryEntryInfo's | |
138 static bool ReadDirectory(std::vector<FileInfo>* entries, | |
139 const FilePath& source, bool show_links); | |
140 | |
141 // The files in the current directory | 153 // The files in the current directory |
142 std::vector<FileInfo> directory_entries_; | 154 std::vector<FileInfo> directory_entries_; |
143 | 155 |
144 // The next entry to use from the directory_entries_ vector | 156 // The next entry to use from the directory_entries_ vector |
145 size_t current_directory_entry_; | 157 size_t current_directory_entry_; |
146 #endif | 158 #endif |
147 | |
148 FilePath root_path_; | 159 FilePath root_path_; |
149 bool recursive_; | 160 const bool recursive_; |
150 int file_type_; | 161 const int file_type_; |
151 FilePath::StringType pattern_; // Empty when we want to find everything. | 162 FilePath::StringType pattern_; |
| 163 const FolderSearchPolicy folder_search_policy_; |
152 | 164 |
153 // A stack that keeps track of which subdirectories we still need to | 165 // A stack that keeps track of which subdirectories we still need to |
154 // enumerate in the breadth-first search. | 166 // enumerate in the breadth-first search. |
155 std::stack<FilePath> pending_paths_; | 167 std::stack<FilePath> pending_paths_; |
156 | 168 |
157 DISALLOW_COPY_AND_ASSIGN(FileEnumerator); | 169 DISALLOW_COPY_AND_ASSIGN(FileEnumerator); |
158 }; | 170 }; |
159 | 171 |
160 } // namespace base | 172 } // namespace base |
161 | 173 |
162 #endif // BASE_FILES_FILE_ENUMERATOR_H_ | 174 #endif // BASE_FILES_FILE_ENUMERATOR_H_ |
OLD | NEW |