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
3 // found in the LICENSE file.
5 #ifndef BASE_FILES_FILE_ENUMERATOR_H_
6 #define BASE_FILES_FILE_ENUMERATOR_H_
11 #include "base/base_export.h"
12 #include "base/basictypes.h"
13 #include "base/files/file_path.h"
14 #include "base/time/time.h"
15 #include "build/build_config.h"
19 #elif defined(OS_POSIX)
26 // A class for enumerating the files in a provided path. The order of the
27 // results is not guaranteed.
29 // This is blocking. Do not use on critical threads.
33 // base::FileEnumerator enum(my_dir, false, base::FileEnumerator::FILES,
34 // FILE_PATH_LITERAL("*.txt"));
35 // for (base::FilePath name = enum.Next(); !name.empty(); name = enum.Next())
37 class BASE_EXPORT FileEnumerator
{
39 // Note: copy & assign supported.
40 class BASE_EXPORT FileInfo
{
45 bool IsDirectory() const;
47 // 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
49 // includes the |root_path| passed into the FileEnumerator constructor.
50 FilePath
GetName() const;
52 int64
GetSize() const;
53 Time
GetLastModifiedTime() const;
56 const WIN32_FIND_DATA
& find_data() const { return find_data_
; }
57 #elif defined(OS_POSIX)
58 const struct stat
& stat() const { return stat_
; }
62 friend class FileEnumerator
;
65 WIN32_FIND_DATA find_data_
;
66 #elif defined(OS_POSIX)
75 INCLUDE_DOT_DOT
= 1 << 2,
77 SHOW_SYM_LINKS
= 1 << 4,
81 // |root_path| is the starting directory to search for. It may or may not end
84 // If |recursive| is true, this will enumerate all matches in any
85 // subdirectories matched as well. It does a breadth-first search, so all
86 // files in one directory will be returned before any files in a
89 // |file_type|, a bit mask of FileType, specifies whether the enumerator
90 // should match files, directories, or both.
92 // |pattern| is an optional pattern for which files to match. This
93 // works like shell globbing. For example, "*.txt" or "Foo???.doc".
94 // However, be careful in specifying patterns that aren't cross platform
95 // since the underlying code uses OS-specific matching routines. In general,
96 // Windows matching is less featureful than others, so test there first.
97 // If unspecified, this will match all files.
98 // NOTE: the pattern only matches the contents of root_path, not files in
99 // recursive subdirectories.
100 // TODO(erikkay): Fix the pattern matching to work at all levels.
101 FileEnumerator(const FilePath
& root_path
,
104 FileEnumerator(const FilePath
& root_path
,
107 const FilePath::StringType
& pattern
);
110 // Returns the next file or an empty string if there are no more results.
112 // The returned path will incorporate the |root_path| passed in the
113 // constructor: "<root_path>/file_name.txt". If the |root_path| is absolute,
114 // then so will be the result of Next().
117 // Write the file info into |info|.
118 FileInfo
GetInfo() const;
121 // Returns true if the given path should be skipped in enumeration.
122 bool ShouldSkip(const FilePath
& path
);
125 // True when find_data_ is valid.
127 WIN32_FIND_DATA find_data_
;
129 #elif defined(OS_POSIX)
131 // Read the filenames in source into the vector of DirectoryEntryInfo's
132 static bool ReadDirectory(std::vector
<FileInfo
>* entries
,
133 const FilePath
& source
, bool show_links
);
135 // The files in the current directory
136 std::vector
<FileInfo
> directory_entries_
;
138 // The next entry to use from the directory_entries_ vector
139 size_t current_directory_entry_
;
145 FilePath::StringType pattern_
; // Empty when we want to find everything.
147 // A stack that keeps track of which subdirectories we still need to
148 // enumerate in the breadth-first search.
149 std::stack
<FilePath
> pending_paths_
;
151 DISALLOW_COPY_AND_ASSIGN(FileEnumerator
);
156 #endif // BASE_FILES_FILE_ENUMERATOR_H_