123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- #ifndef BASE_FILES_FILE_ENUMERATOR_H_
- #define BASE_FILES_FILE_ENUMERATOR_H_
- #include <stddef.h>
- #include <stdint.h>
- #include <vector>
- #include "base/base_export.h"
- #include "base/containers/stack.h"
- #include "base/files/file.h"
- #include "base/files/file_path.h"
- #include "base/macros.h"
- #include "base/optional.h"
- #include "base/time/time.h"
- #include "build/build_config.h"
- #if defined(OS_WIN)
- #include <windows.h>
- #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
- #include <unistd.h>
- #include <unordered_set>
- #include "base/files/file.h"
- #endif
- namespace base {
- class BASE_EXPORT FileEnumerator {
- public:
-
- class BASE_EXPORT FileInfo {
- public:
- FileInfo();
- ~FileInfo();
- bool IsDirectory() const;
-
-
-
- FilePath GetName() const;
- int64_t GetSize() const;
-
- Time GetLastModifiedTime() const;
- #if defined(OS_WIN)
-
-
-
- const WIN32_FIND_DATA& find_data() const { return find_data_; }
- #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
- const stat_wrapper_t& stat() const { return stat_; }
- #endif
- private:
- friend class FileEnumerator;
- #if defined(OS_WIN)
- WIN32_FIND_DATA find_data_;
- #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
- stat_wrapper_t stat_;
- FilePath filename_;
- #endif
- };
- enum FileType {
- FILES = 1 << 0,
- DIRECTORIES = 1 << 1,
- INCLUDE_DOT_DOT = 1 << 2,
- #if defined(OS_POSIX) || defined(OS_FUCHSIA)
- SHOW_SYM_LINKS = 1 << 4,
- #endif
- };
-
- enum class FolderSearchPolicy {
-
-
-
- MATCH_ONLY,
-
-
- ALL,
- };
-
-
-
- enum class ErrorPolicy {
-
-
- IGNORE_ERRORS,
-
-
-
- STOP_ENUMERATION,
- };
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- FileEnumerator(const FilePath& root_path, bool recursive, int file_type);
- FileEnumerator(const FilePath& root_path,
- bool recursive,
- int file_type,
- const FilePath::StringType& pattern);
- FileEnumerator(const FilePath& root_path,
- bool recursive,
- int file_type,
- const FilePath::StringType& pattern,
- FolderSearchPolicy folder_search_policy);
- FileEnumerator(const FilePath& root_path,
- bool recursive,
- int file_type,
- const FilePath::StringType& pattern,
- FolderSearchPolicy folder_search_policy,
- ErrorPolicy error_policy);
- ~FileEnumerator();
-
-
-
-
-
- FilePath Next();
-
-
-
-
-
- FileInfo GetInfo() const;
-
-
-
-
- File::Error GetError() const { return error_; }
- private:
-
- bool ShouldSkip(const FilePath& path);
- bool IsTypeMatched(bool is_dir) const;
- bool IsPatternMatched(const FilePath& src) const;
- #if defined(OS_WIN)
-
- bool has_find_data_ = false;
- WIN32_FIND_DATA find_data_;
- HANDLE find_handle_ = INVALID_HANDLE_VALUE;
- #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
-
- std::vector<FileInfo> directory_entries_;
-
-
- std::unordered_set<ino_t> visited_directories_;
-
- size_t current_directory_entry_;
- #endif
- FilePath root_path_;
- const bool recursive_;
- const int file_type_;
- FilePath::StringType pattern_;
- const FolderSearchPolicy folder_search_policy_;
- const ErrorPolicy error_policy_;
- File::Error error_ = File::FILE_OK;
-
-
- base::stack<FilePath> pending_paths_;
- DISALLOW_COPY_AND_ASSIGN(FileEnumerator);
- };
- }
- #endif
|