| 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 <sys/stat.h> |
| 9 #include <unistd.h> |
| 8 #include <stack> | 10 #include <stack> |
| 9 #include <vector> | 11 #include <vector> |
| 10 | 12 |
| 11 #include "base/base_export.h" | 13 #include "base/base_export.h" |
| 12 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
| 13 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
| 14 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 15 #include "build/build_config.h" | 17 #include "build/build_config.h" |
| 16 | 18 |
| 17 #if defined(OS_WIN) | |
| 18 #include <windows.h> | |
| 19 #elif defined(OS_POSIX) | |
| 20 #include <sys/stat.h> | |
| 21 #include <unistd.h> | |
| 22 #endif | |
| 23 | |
| 24 namespace base { | 19 namespace base { |
| 25 | 20 |
| 26 // A class for enumerating the files in a provided path. The order of the | 21 // A class for enumerating the files in a provided path. The order of the |
| 27 // results is not guaranteed. | 22 // results is not guaranteed. |
| 28 // | 23 // |
| 29 // This is blocking. Do not use on critical threads. | 24 // This is blocking. Do not use on critical threads. |
| 30 // | 25 // |
| 31 // Example: | 26 // Example: |
| 32 // | 27 // |
| 33 // base::FileEnumerator enum(my_dir, false, base::FileEnumerator::FILES, | 28 // base::FileEnumerator enum(my_dir, false, base::FileEnumerator::FILES, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 45 bool IsDirectory() const; | 40 bool IsDirectory() const; |
| 46 | 41 |
| 47 // The name of the file. This will not include any path information. This | 42 // The name of the file. This will not include any path information. This |
| 48 // is in constrast to the value returned by FileEnumerator.Next() which | 43 // is in constrast to the value returned by FileEnumerator.Next() which |
| 49 // includes the |root_path| passed into the FileEnumerator constructor. | 44 // includes the |root_path| passed into the FileEnumerator constructor. |
| 50 FilePath GetName() const; | 45 FilePath GetName() const; |
| 51 | 46 |
| 52 int64 GetSize() const; | 47 int64 GetSize() const; |
| 53 Time GetLastModifiedTime() const; | 48 Time GetLastModifiedTime() const; |
| 54 | 49 |
| 55 #if defined(OS_WIN) | |
| 56 // Note that the cAlternateFileName (used to hold the "short" 8.3 name) | |
| 57 // of the WIN32_FIND_DATA will be empty. Since we don't use short file | |
| 58 // names, we tell Windows to omit it which speeds up the query slightly. | |
| 59 const WIN32_FIND_DATA& find_data() const { return find_data_; } | |
| 60 #elif defined(OS_POSIX) | |
| 61 const struct stat& stat() const { return stat_; } | 50 const struct stat& stat() const { return stat_; } |
| 62 #endif | |
| 63 | 51 |
| 64 private: | 52 private: |
| 65 friend class FileEnumerator; | 53 friend class FileEnumerator; |
| 66 | 54 |
| 67 #if defined(OS_WIN) | |
| 68 WIN32_FIND_DATA find_data_; | |
| 69 #elif defined(OS_POSIX) | |
| 70 struct stat stat_; | 55 struct stat stat_; |
| 71 FilePath filename_; | 56 FilePath filename_; |
| 72 #endif | |
| 73 }; | 57 }; |
| 74 | 58 |
| 75 enum FileType { | 59 enum FileType { |
| 76 FILES = 1 << 0, | 60 FILES = 1 << 0, |
| 77 DIRECTORIES = 1 << 1, | 61 DIRECTORIES = 1 << 1, |
| 78 INCLUDE_DOT_DOT = 1 << 2, | 62 INCLUDE_DOT_DOT = 1 << 2, |
| 79 #if defined(OS_POSIX) | 63 #if defined(OS_POSIX) |
| 80 SHOW_SYM_LINKS = 1 << 4, | 64 SHOW_SYM_LINKS = 1 << 4, |
| 81 #endif | 65 #endif |
| 82 }; | 66 }; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 // then so will be the result of Next(). | 101 // then so will be the result of Next(). |
| 118 FilePath Next(); | 102 FilePath Next(); |
| 119 | 103 |
| 120 // Write the file info into |info|. | 104 // Write the file info into |info|. |
| 121 FileInfo GetInfo() const; | 105 FileInfo GetInfo() const; |
| 122 | 106 |
| 123 private: | 107 private: |
| 124 // Returns true if the given path should be skipped in enumeration. | 108 // Returns true if the given path should be skipped in enumeration. |
| 125 bool ShouldSkip(const FilePath& path); | 109 bool ShouldSkip(const FilePath& path); |
| 126 | 110 |
| 127 #if defined(OS_WIN) | 111 #if defined(OS_POSIX) |
| 128 // True when find_data_ is valid. | |
| 129 bool has_find_data_; | |
| 130 WIN32_FIND_DATA find_data_; | |
| 131 HANDLE find_handle_; | |
| 132 #elif defined(OS_POSIX) | |
| 133 | |
| 134 // Read the filenames in source into the vector of DirectoryEntryInfo's | 112 // Read the filenames in source into the vector of DirectoryEntryInfo's |
| 135 static bool ReadDirectory(std::vector<FileInfo>* entries, | 113 static bool ReadDirectory(std::vector<FileInfo>* entries, |
| 136 const FilePath& source, bool show_links); | 114 const FilePath& source, bool show_links); |
| 137 | 115 |
| 138 // The files in the current directory | 116 // The files in the current directory |
| 139 std::vector<FileInfo> directory_entries_; | 117 std::vector<FileInfo> directory_entries_; |
| 140 | 118 |
| 141 // The next entry to use from the directory_entries_ vector | 119 // The next entry to use from the directory_entries_ vector |
| 142 size_t current_directory_entry_; | 120 size_t current_directory_entry_; |
| 143 #endif | 121 #endif |
| 144 | 122 |
| 145 FilePath root_path_; | 123 FilePath root_path_; |
| 146 bool recursive_; | 124 bool recursive_; |
| 147 int file_type_; | 125 int file_type_; |
| 148 FilePath::StringType pattern_; // Empty when we want to find everything. | 126 FilePath::StringType pattern_; // Empty when we want to find everything. |
| 149 | 127 |
| 150 // A stack that keeps track of which subdirectories we still need to | 128 // A stack that keeps track of which subdirectories we still need to |
| 151 // enumerate in the breadth-first search. | 129 // enumerate in the breadth-first search. |
| 152 std::stack<FilePath> pending_paths_; | 130 std::stack<FilePath> pending_paths_; |
| 153 | 131 |
| 154 DISALLOW_COPY_AND_ASSIGN(FileEnumerator); | 132 DISALLOW_COPY_AND_ASSIGN(FileEnumerator); |
| 155 }; | 133 }; |
| 156 | 134 |
| 157 } // namespace base | 135 } // namespace base |
| 158 | 136 |
| 159 #endif // BASE_FILES_FILE_ENUMERATOR_H_ | 137 #endif // BASE_FILES_FILE_ENUMERATOR_H_ |
| OLD | NEW |