Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: base/files/file_enumerator_win.cc

Issue 13247008: base: Extract FileEnumerator out of file_util.h (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: file_enumerator_posix.cc should not compile on win Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/files/file_enumerator_posix.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 #include "base/files/file_enumerator.h"
6
7 #include <string.h>
8
9 #include "base/logging.h"
10 #include "base/threading/thread_restrictions.h"
11 #include "base/time.h"
12
13 using base::FilePath;
14 using base::Time;
15 using base::ThreadRestrictions;
16
17 namespace file_util {
18
19 FileEnumerator::FileEnumerator(const FilePath& root_path,
20 bool recursive,
21 int file_type)
22 : recursive_(recursive),
23 file_type_(file_type),
24 has_find_data_(false),
25 find_handle_(INVALID_HANDLE_VALUE) {
26 // INCLUDE_DOT_DOT must not be specified if recursive.
27 DCHECK(!(recursive && (INCLUDE_DOT_DOT & file_type_)));
28 memset(&find_data_, 0, sizeof(find_data_));
29 pending_paths_.push(root_path);
30 }
31
32 FileEnumerator::FileEnumerator(const FilePath& root_path,
33 bool recursive,
34 int file_type,
35 const FilePath::StringType& pattern)
36 : recursive_(recursive),
37 file_type_(file_type),
38 has_find_data_(false),
39 pattern_(pattern),
40 find_handle_(INVALID_HANDLE_VALUE) {
41 // INCLUDE_DOT_DOT must not be specified if recursive.
42 DCHECK(!(recursive && (INCLUDE_DOT_DOT & file_type_)));
43 memset(&find_data_, 0, sizeof(find_data_));
44 pending_paths_.push(root_path);
45 }
46
47 FileEnumerator::~FileEnumerator() {
48 if (find_handle_ != INVALID_HANDLE_VALUE)
49 FindClose(find_handle_);
50 }
51
52 void FileEnumerator::GetFindInfo(FindInfo* info) {
53 DCHECK(info);
54
55 if (!has_find_data_)
56 return;
57
58 memcpy(info, &find_data_, sizeof(*info));
59 }
60
61 // static
62 bool FileEnumerator::IsDirectory(const FindInfo& info) {
63 return (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
64 }
65
66 // static
67 FilePath FileEnumerator::GetFilename(const FindInfo& find_info) {
68 return FilePath(find_info.cFileName);
69 }
70
71 // static
72 int64 FileEnumerator::GetFilesize(const FindInfo& find_info) {
73 ULARGE_INTEGER size;
74 size.HighPart = find_info.nFileSizeHigh;
75 size.LowPart = find_info.nFileSizeLow;
76 DCHECK_LE(size.QuadPart, std::numeric_limits<int64>::max());
77 return static_cast<int64>(size.QuadPart);
78 }
79
80 // static
81 Time FileEnumerator::GetLastModifiedTime(const FindInfo& find_info) {
82 return Time::FromFileTime(find_info.ftLastWriteTime);
83 }
84
85 FilePath FileEnumerator::Next() {
86 ThreadRestrictions::AssertIOAllowed();
87
88 while (has_find_data_ || !pending_paths_.empty()) {
89 if (!has_find_data_) {
90 // The last find FindFirstFile operation is done, prepare a new one.
91 root_path_ = pending_paths_.top();
92 pending_paths_.pop();
93
94 // Start a new find operation.
95 FilePath src = root_path_;
96
97 if (pattern_.empty())
98 src = src.Append(L"*"); // No pattern = match everything.
99 else
100 src = src.Append(pattern_);
101
102 find_handle_ = FindFirstFile(src.value().c_str(), &find_data_);
103 has_find_data_ = true;
104 } else {
105 // Search for the next file/directory.
106 if (!FindNextFile(find_handle_, &find_data_)) {
107 FindClose(find_handle_);
108 find_handle_ = INVALID_HANDLE_VALUE;
109 }
110 }
111
112 if (INVALID_HANDLE_VALUE == find_handle_) {
113 has_find_data_ = false;
114
115 // This is reached when we have finished a directory and are advancing to
116 // the next one in the queue. We applied the pattern (if any) to the files
117 // in the root search directory, but for those directories which were
118 // matched, we want to enumerate all files inside them. This will happen
119 // when the handle is empty.
120 pattern_ = FilePath::StringType();
121
122 continue;
123 }
124
125 FilePath cur_file(find_data_.cFileName);
126 if (ShouldSkip(cur_file))
127 continue;
128
129 // Construct the absolute filename.
130 cur_file = root_path_.Append(find_data_.cFileName);
131
132 if (find_data_.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
133 if (recursive_) {
134 // If |cur_file| is a directory, and we are doing recursive searching,
135 // add it to pending_paths_ so we scan it after we finish scanning this
136 // directory.
137 pending_paths_.push(cur_file);
138 }
139 if (file_type_ & FileEnumerator::DIRECTORIES)
140 return cur_file;
141 } else if (file_type_ & FileEnumerator::FILES) {
142 return cur_file;
143 }
144 }
145
146 return FilePath();
147 }
148
149 } // namespace file_util
OLDNEW
« no previous file with comments | « base/files/file_enumerator_posix.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698