Index: base/files/file_enumerator.h |
diff --git a/base/files/file_enumerator.h b/base/files/file_enumerator.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d727ef58ca0e75d959e49d9a43db4e0a46e37e63 |
--- /dev/null |
+++ b/base/files/file_enumerator.h |
@@ -0,0 +1,140 @@ |
+// Copyright 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef BASE_FILES_FILE_ENUMERATOR_H_ |
+#define BASE_FILES_FILE_ENUMERATOR_H_ |
+ |
+#include <stack> |
+#include <string> |
+#include <vector> |
+ |
+#include "base/base_export.h" |
+#include "base/basictypes.h" |
+#include "base/files/file_path.h" |
+#include "build/build_config.h" |
+ |
+#if defined(OS_WIN) |
+#include <windows.h> |
+#elif defined(OS_POSIX) |
+#include <sys/stat.h> |
+#include <unistd.h> |
+#endif |
+ |
+namespace base { |
+class Time; |
+} |
+ |
+// TODO(tfarina): Move this into base namespace. |
+namespace file_util { |
+ |
+// A class for enumerating the files in a provided path. The order of the |
+// results is not guaranteed. |
+// |
+// DO NOT USE FROM THE MAIN THREAD of your application unless it is a test |
+// program where latency does not matter. This class is blocking. |
+class BASE_EXPORT FileEnumerator { |
+ public: |
+#if defined(OS_WIN) |
+ typedef WIN32_FIND_DATA FindInfo; |
+#elif defined(OS_POSIX) |
+ struct FindInfo { |
+ struct stat stat; |
+ std::string filename; |
+ }; |
+#endif |
+ |
+ enum FileType { |
+ FILES = 1 << 0, |
+ DIRECTORIES = 1 << 1, |
+ INCLUDE_DOT_DOT = 1 << 2, |
+#if defined(OS_POSIX) |
+ SHOW_SYM_LINKS = 1 << 4, |
+#endif |
+ }; |
+ |
+ // |root_path| is the starting directory to search for. It may or may not end |
+ // in a slash. |
+ // |
+ // If |recursive| is true, this will enumerate all matches in any |
+ // subdirectories matched as well. It does a breadth-first search, so all |
+ // files in one directory will be returned before any files in a |
+ // subdirectory. |
+ // |
+ // |file_type|, a bit mask of FileType, specifies whether the enumerator |
+ // should match files, directories, or both. |
+ // |
+ // |pattern| is an optional pattern for which files to match. This |
+ // works like shell globbing. For example, "*.txt" or "Foo???.doc". |
+ // However, be careful in specifying patterns that aren't cross platform |
+ // since the underlying code uses OS-specific matching routines. In general, |
+ // Windows matching is less featureful than others, so test there first. |
+ // If unspecified, this will match all files. |
+ // NOTE: the pattern only matches the contents of root_path, not files in |
+ // recursive subdirectories. |
+ // TODO(erikkay): Fix the pattern matching to work at all levels. |
+ FileEnumerator(const base::FilePath& root_path, |
+ bool recursive, |
+ int file_type); |
+ FileEnumerator(const base::FilePath& root_path, |
+ bool recursive, |
+ int file_type, |
+ const base::FilePath::StringType& pattern); |
+ ~FileEnumerator(); |
+ |
+ // Returns an empty string if there are no more results. |
+ base::FilePath Next(); |
+ |
+ // Write the file info into |info|. |
+ void GetFindInfo(FindInfo* info); |
+ |
+ // Looks inside a FindInfo and determines if it's a directory. |
+ static bool IsDirectory(const FindInfo& info); |
+ |
+ static base::FilePath GetFilename(const FindInfo& find_info); |
+ static int64 GetFilesize(const FindInfo& find_info); |
+ static base::Time GetLastModifiedTime(const FindInfo& find_info); |
+ |
+ private: |
+ // Returns true if the given path should be skipped in enumeration. |
+ bool ShouldSkip(const base::FilePath& path); |
+ |
+#if defined(OS_WIN) |
+ // True when find_data_ is valid. |
+ bool has_find_data_; |
+ WIN32_FIND_DATA find_data_; |
+ HANDLE find_handle_; |
+#elif defined(OS_POSIX) |
+ struct DirectoryEntryInfo { |
+ base::FilePath filename; |
+ struct stat stat; |
+ }; |
+ |
+ // Read the filenames in source into the vector of DirectoryEntryInfo's |
+ static bool ReadDirectory(std::vector<DirectoryEntryInfo>* entries, |
+ const base::FilePath& source, |
+ bool show_links); |
+ |
+ // The files in the current directory |
+ std::vector<DirectoryEntryInfo> directory_entries_; |
+ |
+ // The next entry to use from the directory_entries_ vector |
+ size_t current_directory_entry_; |
+#endif |
+ |
+ base::FilePath root_path_; |
+ bool recursive_; |
+ int file_type_; |
+ base::FilePath::StringType pattern_; // Empty when we want to find |
+ // everything. |
+ |
+ // A stack that keeps track of which subdirectories we still need to |
+ // enumerate in the breadth-first search. |
+ std::stack<base::FilePath> pending_paths_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(FileEnumerator); |
+}; |
+ |
+} // namespace file_util |
+ |
+#endif // BASE_FILES_FILE_ENUMERATOR_H_ |