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

Side by Side Diff: runtime/bin/directory.h

Issue 16813006: Make Directory.list pull-based, making it possible to pause, resume and cancel directory listing. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Make the code more Windows-friendly. Created 7 years, 6 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 | « no previous file | runtime/bin/directory.cc » ('j') | runtime/bin/directory.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #ifndef BIN_DIRECTORY_H_ 5 #ifndef BIN_DIRECTORY_H_
6 #define BIN_DIRECTORY_H_ 6 #define BIN_DIRECTORY_H_
7 7
8 #include "bin/builtin.h" 8 #include "bin/builtin.h"
9 #include "bin/dartutils.h" 9 #include "bin/dartutils.h"
10 #include "bin/native_service.h" 10 #include "bin/native_service.h"
11 #include "platform/globals.h" 11 #include "platform/globals.h"
12 #include "platform/thread.h" 12 #include "platform/thread.h"
13 13
14 14
15 namespace dart { 15 namespace dart {
16 namespace bin { 16 namespace bin {
17 17
18 enum ListType {
19 kListFile = 0,
20 kListDirectory = 1,
21 kListLink = 2,
22 kListError = 3,
23 kListDone = 4
24 };
25
26 class PathBuffer {
27 public:
28 PathBuffer();
29 ~PathBuffer() {
30 free(data_);
31 }
32
33 bool Add(const char* name);
34 bool AddW(const wchar_t* name);
35
36 char* AsString() const;
37 wchar_t* AsStringW() const;
38
39 void Reset(int new_length);
40
41 int length() const {
42 return length_;
43 }
44
45 private:
46 void* data_;
47 int length_;
48 };
49
50 class DirectoryListing;
51
52 class DirectoryListingEntry {
53 public:
54 explicit DirectoryListingEntry(void* link);
55
56 ~DirectoryListingEntry() {
57 if (parent_ == NULL || parent_->link_ != link_) {
58 free(link_);
59 }
60 }
61
62 ListType Next(DirectoryListing* listing);
63
64 DirectoryListingEntry* parent() const {
65 return parent_;
66 }
67
68 void set_parent(DirectoryListingEntry* parent) {
69 parent_ = parent;
70 }
71
72 void* link() {
73 return link_;
74 }
75
76 private:
77 DirectoryListingEntry* parent_;
78 void* lister_;
Søren Gjesse 2013/06/12 12:52:41 lister_, done_ and path_length_ are not used.
Anders Johnsen 2013/06/13 08:38:24 They are, in Next.
79 bool done_;
80 int path_length_;
81 void* link_;
82 };
83
18 class DirectoryListing { 84 class DirectoryListing {
19 public: 85 public:
20 virtual ~DirectoryListing() {} 86 DirectoryListing(const char* dir_name, bool recursive, bool follow_links)
87 : top_(NULL),
88 error_(false),
89 recursive_(recursive),
90 follow_links_(follow_links) {
91 if (!path_buffer_.Add(dir_name)) {
92 error_ = true;
93 }
94 Push(new DirectoryListingEntry(NULL));
95 }
96
97 virtual ~DirectoryListing() {
98 while (!IsEmpty()) {
99 Pop();
100 }
101 }
102
21 virtual bool HandleDirectory(char* dir_name) = 0; 103 virtual bool HandleDirectory(char* dir_name) = 0;
22 virtual bool HandleFile(char* file_name) = 0; 104 virtual bool HandleFile(char* file_name) = 0;
23 virtual bool HandleLink(char* file_name) = 0; 105 virtual bool HandleLink(char* link_name) = 0;
24 virtual bool HandleError(const char* dir_name) = 0; 106 virtual bool HandleError(const char* dir_name) = 0;
107 virtual void HandleDone() {}
108
109 void Push(DirectoryListingEntry* directory) {
110 directory->set_parent(top_);
111 top_ = directory;
112 }
113
114 void Pop() {
115 ASSERT(!IsEmpty());
116 DirectoryListingEntry* current = top_;
117 top_ = top_->parent();
118 delete current;
119 }
120
121 bool IsEmpty() const {
122 return top_ == NULL;
123 }
124
125 DirectoryListingEntry* top() const {
126 return top_;
127 }
128
129 bool recursive() const {
130 return recursive_;
131 }
132
133 bool follow_links() const {
134 return follow_links_;
135 }
136
137 char* CurrentPath() {
138 return path_buffer_.AsString();
139 }
140
141 PathBuffer& path_buffer() {
Søren Gjesse 2013/06/12 12:52:41 Why not PathBuffer* ?
Anders Johnsen 2013/06/13 08:38:24 A pointer would indicated that it could be kept on
142 return path_buffer_;
143 }
144
145 bool error() const {
146 return error_;
147 }
148
149 private:
150 PathBuffer path_buffer_;
151 DirectoryListingEntry* top_;
152 bool error_;
153 bool recursive_;
154 bool follow_links_;
25 }; 155 };
26 156
27 157
28 class AsyncDirectoryListing : public DirectoryListing { 158 class AsyncDirectoryListing : public DirectoryListing {
29 public: 159 public:
30 enum Response { 160 enum Response {
31 kListFile = 0, 161 kListFile = 0,
32 kListDirectory = 1, 162 kListDirectory = 1,
33 kListLink = 2, 163 kListLink = 2,
34 kListError = 3, 164 kListError = 3,
35 kListDone = 4 165 kListDone = 4
36 }; 166 };
37 167
38 explicit AsyncDirectoryListing(Dart_Port response_port) 168 explicit AsyncDirectoryListing(const char* dir_name,
Søren Gjesse 2013/06/12 12:52:41 No need for explicit when there is more than one a
Anders Johnsen 2013/06/13 08:38:24 Done.
39 : response_port_(response_port) {} 169 bool recursive,
170 bool follow_links)
171 : DirectoryListing(dir_name, recursive, follow_links) {
172 }
173
40 virtual ~AsyncDirectoryListing() {} 174 virtual ~AsyncDirectoryListing() {}
41 virtual bool HandleDirectory(char* dir_name); 175 virtual bool HandleDirectory(char* dir_name);
42 virtual bool HandleFile(char* file_name); 176 virtual bool HandleFile(char* file_name);
43 virtual bool HandleLink(char* file_name); 177 virtual bool HandleLink(char* file_name);
44 virtual bool HandleError(const char* dir_name); 178 virtual bool HandleError(const char* dir_name);
179 virtual void HandleDone();
180
181 void set_array(CObjectArray* array, intptr_t length) {
Søren Gjesse 2013/06/12 12:52:41 This should be named SetArray as it does not just
Anders Johnsen 2013/06/13 08:38:24 Done.
182 array_ = array;
183 index_ = 0;
184 length_ = length;
185 }
186
187 intptr_t index() const {
188 return index_;
189 }
45 190
46 private: 191 private:
47 CObjectArray* NewResponse(Response response, char* arg); 192 bool NewResponse(Response response, char* arg);
48 Dart_Port response_port_; 193 CObjectArray* array_;
194 intptr_t index_;
195 intptr_t length_;
49 196
50 DISALLOW_IMPLICIT_CONSTRUCTORS(AsyncDirectoryListing); 197 DISALLOW_IMPLICIT_CONSTRUCTORS(AsyncDirectoryListing);
51 }; 198 };
52 199
53 200
54 class SyncDirectoryListing: public DirectoryListing { 201 class SyncDirectoryListing: public DirectoryListing {
55 public: 202 public:
56 explicit SyncDirectoryListing(Dart_Handle results) 203 explicit SyncDirectoryListing(Dart_Handle results,
Søren Gjesse 2013/06/12 12:52:41 No need for explicit when there is more than one a
Anders Johnsen 2013/06/13 08:38:24 Done.
57 : results_(results) { 204 const char* dir_name,
205 bool recursive,
206 bool follow_links)
207 : DirectoryListing(dir_name, recursive, follow_links),
208 results_(results) {
58 add_string_ = DartUtils::NewString("add"); 209 add_string_ = DartUtils::NewString("add");
59 directory_class_ = 210 directory_class_ =
60 DartUtils::GetDartClass(DartUtils::kIOLibURL, "Directory"); 211 DartUtils::GetDartClass(DartUtils::kIOLibURL, "Directory");
61 file_class_ = 212 file_class_ =
62 DartUtils::GetDartClass(DartUtils::kIOLibURL, "File"); 213 DartUtils::GetDartClass(DartUtils::kIOLibURL, "File");
63 link_class_ = 214 link_class_ =
64 DartUtils::GetDartClass(DartUtils::kIOLibURL, "Link"); 215 DartUtils::GetDartClass(DartUtils::kIOLibURL, "Link");
65 } 216 }
66 virtual ~SyncDirectoryListing() {} 217 virtual ~SyncDirectoryListing() {}
67 virtual bool HandleDirectory(char* dir_name); 218 virtual bool HandleDirectory(char* dir_name);
(...skipping 20 matching lines...) Expand all
88 DOES_NOT_EXIST 239 DOES_NOT_EXIST
89 }; 240 };
90 241
91 // This enum must be kept in sync with the request values in 242 // This enum must be kept in sync with the request values in
92 // directory_impl.dart. 243 // directory_impl.dart.
93 enum DirectoryRequest { 244 enum DirectoryRequest {
94 kCreateRequest = 0, 245 kCreateRequest = 0,
95 kDeleteRequest = 1, 246 kDeleteRequest = 1,
96 kExistsRequest = 2, 247 kExistsRequest = 2,
97 kCreateTempRequest = 3, 248 kCreateTempRequest = 3,
98 kListRequest = 4, 249 kListStartRequest = 4,
99 kRenameRequest = 5 250 kListNextRequest = 5,
251 kListStopRequest = 6,
252 kRenameRequest = 7
100 }; 253 };
101 254
102 static bool List(const char* path, 255 static void List(DirectoryListing* listing);
103 bool recursive,
104 bool follow_links,
105 DirectoryListing* listing);
106 static ExistsResult Exists(const char* path); 256 static ExistsResult Exists(const char* path);
107 static char* Current(); 257 static char* Current();
108 static bool SetCurrent(const char* path); 258 static bool SetCurrent(const char* path);
109 static bool Create(const char* path); 259 static bool Create(const char* path);
110 static char* CreateTemp(const char* const_template); 260 static char* CreateTemp(const char* const_template);
111 static bool Delete(const char* path, bool recursive); 261 static bool Delete(const char* path, bool recursive);
112 static bool Rename(const char* path, const char* new_path); 262 static bool Rename(const char* path, const char* new_path);
113 static Dart_Port GetServicePort(); 263 static Dart_Port GetServicePort();
114 264
115 private: 265 private:
116 static NativeService directory_service_; 266 static NativeService directory_service_;
117 267
118 DISALLOW_ALLOCATION(); 268 DISALLOW_ALLOCATION();
119 DISALLOW_IMPLICIT_CONSTRUCTORS(Directory); 269 DISALLOW_IMPLICIT_CONSTRUCTORS(Directory);
120 }; 270 };
121 271
122 } // namespace bin 272 } // namespace bin
123 } // namespace dart 273 } // namespace dart
124 274
125 #endif // BIN_DIRECTORY_H_ 275 #endif // BIN_DIRECTORY_H_
OLDNEW
« no previous file with comments | « no previous file | runtime/bin/directory.cc » ('j') | runtime/bin/directory.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698