OLD | NEW |
(Empty) | |
| 1 // Copyright 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. |
| 4 |
| 5 #ifndef BASE_FILES_FILE_ENUMERATOR_H_ |
| 6 #define BASE_FILES_FILE_ENUMERATOR_H_ |
| 7 |
| 8 #include <stack> |
| 9 #include <string> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/base_export.h" |
| 13 #include "base/basictypes.h" |
| 14 #include "base/files/file_path.h" |
| 15 #include "build/build_config.h" |
| 16 |
| 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 { |
| 25 class Time; |
| 26 } |
| 27 |
| 28 // TODO(tfarina): Move this into base namespace. |
| 29 namespace file_util { |
| 30 |
| 31 // A class for enumerating the files in a provided path. The order of the |
| 32 // results is not guaranteed. |
| 33 // |
| 34 // DO NOT USE FROM THE MAIN THREAD of your application unless it is a test |
| 35 // program where latency does not matter. This class is blocking. |
| 36 class BASE_EXPORT FileEnumerator { |
| 37 public: |
| 38 #if defined(OS_WIN) |
| 39 typedef WIN32_FIND_DATA FindInfo; |
| 40 #elif defined(OS_POSIX) |
| 41 struct FindInfo { |
| 42 struct stat stat; |
| 43 std::string filename; |
| 44 }; |
| 45 #endif |
| 46 |
| 47 enum FileType { |
| 48 FILES = 1 << 0, |
| 49 DIRECTORIES = 1 << 1, |
| 50 INCLUDE_DOT_DOT = 1 << 2, |
| 51 #if defined(OS_POSIX) |
| 52 SHOW_SYM_LINKS = 1 << 4, |
| 53 #endif |
| 54 }; |
| 55 |
| 56 // |root_path| is the starting directory to search for. It may or may not end |
| 57 // in a slash. |
| 58 // |
| 59 // If |recursive| is true, this will enumerate all matches in any |
| 60 // subdirectories matched as well. It does a breadth-first search, so all |
| 61 // files in one directory will be returned before any files in a |
| 62 // subdirectory. |
| 63 // |
| 64 // |file_type|, a bit mask of FileType, specifies whether the enumerator |
| 65 // should match files, directories, or both. |
| 66 // |
| 67 // |pattern| is an optional pattern for which files to match. This |
| 68 // works like shell globbing. For example, "*.txt" or "Foo???.doc". |
| 69 // However, be careful in specifying patterns that aren't cross platform |
| 70 // since the underlying code uses OS-specific matching routines. In general, |
| 71 // Windows matching is less featureful than others, so test there first. |
| 72 // If unspecified, this will match all files. |
| 73 // NOTE: the pattern only matches the contents of root_path, not files in |
| 74 // recursive subdirectories. |
| 75 // TODO(erikkay): Fix the pattern matching to work at all levels. |
| 76 FileEnumerator(const base::FilePath& root_path, |
| 77 bool recursive, |
| 78 int file_type); |
| 79 FileEnumerator(const base::FilePath& root_path, |
| 80 bool recursive, |
| 81 int file_type, |
| 82 const base::FilePath::StringType& pattern); |
| 83 ~FileEnumerator(); |
| 84 |
| 85 // Returns an empty string if there are no more results. |
| 86 base::FilePath Next(); |
| 87 |
| 88 // Write the file info into |info|. |
| 89 void GetFindInfo(FindInfo* info); |
| 90 |
| 91 // Looks inside a FindInfo and determines if it's a directory. |
| 92 static bool IsDirectory(const FindInfo& info); |
| 93 |
| 94 static base::FilePath GetFilename(const FindInfo& find_info); |
| 95 static int64 GetFilesize(const FindInfo& find_info); |
| 96 static base::Time GetLastModifiedTime(const FindInfo& find_info); |
| 97 |
| 98 private: |
| 99 // Returns true if the given path should be skipped in enumeration. |
| 100 bool ShouldSkip(const base::FilePath& path); |
| 101 |
| 102 #if defined(OS_WIN) |
| 103 // True when find_data_ is valid. |
| 104 bool has_find_data_; |
| 105 WIN32_FIND_DATA find_data_; |
| 106 HANDLE find_handle_; |
| 107 #elif defined(OS_POSIX) |
| 108 struct DirectoryEntryInfo { |
| 109 base::FilePath filename; |
| 110 struct stat stat; |
| 111 }; |
| 112 |
| 113 // Read the filenames in source into the vector of DirectoryEntryInfo's |
| 114 static bool ReadDirectory(std::vector<DirectoryEntryInfo>* entries, |
| 115 const base::FilePath& source, |
| 116 bool show_links); |
| 117 |
| 118 // The files in the current directory |
| 119 std::vector<DirectoryEntryInfo> directory_entries_; |
| 120 |
| 121 // The next entry to use from the directory_entries_ vector |
| 122 size_t current_directory_entry_; |
| 123 #endif |
| 124 |
| 125 base::FilePath root_path_; |
| 126 bool recursive_; |
| 127 int file_type_; |
| 128 base::FilePath::StringType pattern_; // Empty when we want to find |
| 129 // everything. |
| 130 |
| 131 // A stack that keeps track of which subdirectories we still need to |
| 132 // enumerate in the breadth-first search. |
| 133 std::stack<base::FilePath> pending_paths_; |
| 134 |
| 135 DISALLOW_COPY_AND_ASSIGN(FileEnumerator); |
| 136 }; |
| 137 |
| 138 } // namespace file_util |
| 139 |
| 140 #endif // BASE_FILES_FILE_ENUMERATOR_H_ |
OLD | NEW |