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 // 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_
; }
65 friend class FileEnumerator
;
68 WIN32_FIND_DATA find_data_
;
69 #elif defined(OS_POSIX)
78 INCLUDE_DOT_DOT
= 1 << 2,
80 SHOW_SYM_LINKS
= 1 << 4,
84 // |root_path| is the starting directory to search for. It may or may not end
87 // If |recursive| is true, this will enumerate all matches in any
88 // subdirectories matched as well. It does a breadth-first search, so all
89 // files in one directory will be returned before any files in a
92 // |file_type|, a bit mask of FileType, specifies whether the enumerator
93 // should match files, directories, or both.
95 // |pattern| is an optional pattern for which files to match. This
96 // works like shell globbing. For example, "*.txt" or "Foo???.doc".
97 // However, be careful in specifying patterns that aren't cross platform
98 // since the underlying code uses OS-specific matching routines. In general,
99 // Windows matching is less featureful than others, so test there first.
100 // If unspecified, this will match all files.
101 // NOTE: the pattern only matches the contents of root_path, not files in
102 // recursive subdirectories.
103 // TODO(erikkay): Fix the pattern matching to work at all levels.
104 FileEnumerator(const FilePath
& root_path
,
107 FileEnumerator(const FilePath
& root_path
,
110 const FilePath::StringType
& pattern
);
113 // Returns the next file or an empty string if there are no more results.
115 // The returned path will incorporate the |root_path| passed in the
116 // constructor: "<root_path>/file_name.txt". If the |root_path| is absolute,
117 // then so will be the result of Next().
120 // Write the file info into |info|.
121 FileInfo
GetInfo() const;
124 // Returns true if the given path should be skipped in enumeration.
125 bool ShouldSkip(const FilePath
& path
);
128 // True when find_data_ is valid.
130 WIN32_FIND_DATA find_data_
;
132 #elif defined(OS_POSIX)
134 // Read the filenames in source into the vector of DirectoryEntryInfo's
135 static bool ReadDirectory(std::vector
<FileInfo
>* entries
,
136 const FilePath
& source
, bool show_links
);
138 // The files in the current directory
139 std::vector
<FileInfo
> directory_entries_
;
141 // The next entry to use from the directory_entries_ vector
142 size_t current_directory_entry_
;
148 FilePath::StringType pattern_
; // Empty when we want to find everything.
150 // A stack that keeps track of which subdirectories we still need to
151 // enumerate in the breadth-first search.
152 std::stack
<FilePath
> pending_paths_
;
154 DISALLOW_COPY_AND_ASSIGN(FileEnumerator
);
159 #endif // BASE_FILES_FILE_ENUMERATOR_H_