OLD | NEW |
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 RUNTIME_BIN_DIRECTORY_H_ | 5 #ifndef RUNTIME_BIN_DIRECTORY_H_ |
6 #define RUNTIME_BIN_DIRECTORY_H_ | 6 #define RUNTIME_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/reference_counting.h" | 10 #include "bin/reference_counting.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 bool AddW(const wchar_t* name); | 31 bool AddW(const wchar_t* name); |
32 | 32 |
33 char* AsString() const; | 33 char* AsString() const; |
34 wchar_t* AsStringW() const; | 34 wchar_t* AsStringW() const; |
35 | 35 |
36 // Makes a scope allocated copy of the string. | 36 // Makes a scope allocated copy of the string. |
37 const char* AsScopedString() const; | 37 const char* AsScopedString() const; |
38 | 38 |
39 void Reset(intptr_t new_length); | 39 void Reset(intptr_t new_length); |
40 | 40 |
41 intptr_t length() const { | 41 intptr_t length() const { return length_; } |
42 return length_; | |
43 } | |
44 | 42 |
45 private: | 43 private: |
46 void* data_; | 44 void* data_; |
47 intptr_t length_; | 45 intptr_t length_; |
48 | 46 |
49 DISALLOW_COPY_AND_ASSIGN(PathBuffer); | 47 DISALLOW_COPY_AND_ASSIGN(PathBuffer); |
50 }; | 48 }; |
51 | 49 |
52 class DirectoryListing; | 50 class DirectoryListing; |
53 | 51 |
54 struct LinkList; | 52 struct LinkList; |
55 | 53 |
56 // DirectoryListingEntry is used as a stack item, when performing recursive | 54 // DirectoryListingEntry is used as a stack item, when performing recursive |
57 // directory listing. By using DirectoryListingEntry as stack elements, a | 55 // directory listing. By using DirectoryListingEntry as stack elements, a |
58 // directory listing can be paused e.g. when a buffer is full, and resumed | 56 // directory listing can be paused e.g. when a buffer is full, and resumed |
59 // later on. | 57 // later on. |
60 // | 58 // |
61 // The stack is managed by the DirectoryListing's PathBuffer. Each | 59 // The stack is managed by the DirectoryListing's PathBuffer. Each |
62 // DirectoryListingEntry stored a entry-length, that it'll reset the PathBuffer | 60 // DirectoryListingEntry stored a entry-length, that it'll reset the PathBuffer |
63 // to on each call to Next. | 61 // to on each call to Next. |
64 class DirectoryListingEntry { | 62 class DirectoryListingEntry { |
65 public: | 63 public: |
66 explicit DirectoryListingEntry(DirectoryListingEntry* parent) | 64 explicit DirectoryListingEntry(DirectoryListingEntry* parent) |
67 : parent_(parent), lister_(0), done_(false), link_(NULL) {} | 65 : parent_(parent), lister_(0), done_(false), link_(NULL) {} |
68 | 66 |
69 ~DirectoryListingEntry(); | 67 ~DirectoryListingEntry(); |
70 | 68 |
71 ListType Next(DirectoryListing* listing); | 69 ListType Next(DirectoryListing* listing); |
72 | 70 |
73 DirectoryListingEntry* parent() const { | 71 DirectoryListingEntry* parent() const { return parent_; } |
74 return parent_; | |
75 } | |
76 | 72 |
77 LinkList* link() { | 73 LinkList* link() { return link_; } |
78 return link_; | |
79 } | |
80 | 74 |
81 void set_link(LinkList* link) { | 75 void set_link(LinkList* link) { link_ = link; } |
82 link_ = link; | |
83 } | |
84 | 76 |
85 void ResetLink(); | 77 void ResetLink(); |
86 | 78 |
87 private: | 79 private: |
88 DirectoryListingEntry* parent_; | 80 DirectoryListingEntry* parent_; |
89 intptr_t lister_; | 81 intptr_t lister_; |
90 bool done_; | 82 bool done_; |
91 int path_length_; | 83 int path_length_; |
92 LinkList* link_; | 84 LinkList* link_; |
93 | 85 |
94 DISALLOW_COPY_AND_ASSIGN(DirectoryListingEntry); | 86 DISALLOW_COPY_AND_ASSIGN(DirectoryListingEntry); |
95 }; | 87 }; |
96 | 88 |
97 class DirectoryListing { | 89 class DirectoryListing { |
98 public: | 90 public: |
99 DirectoryListing(const char* dir_name, bool recursive, bool follow_links) | 91 DirectoryListing(const char* dir_name, bool recursive, bool follow_links) |
100 : top_(NULL), | 92 : top_(NULL), |
101 error_(false), | 93 error_(false), |
102 recursive_(recursive), | 94 recursive_(recursive), |
103 follow_links_(follow_links) { | 95 follow_links_(follow_links) { |
104 if (!path_buffer_.Add(dir_name)) { | 96 if (!path_buffer_.Add(dir_name)) { |
105 error_ = true; | 97 error_ = true; |
106 } | 98 } |
107 Push(new DirectoryListingEntry(NULL)); | 99 Push(new DirectoryListingEntry(NULL)); |
108 } | 100 } |
109 | 101 |
110 virtual ~DirectoryListing() { | 102 virtual ~DirectoryListing() { PopAll(); } |
111 PopAll(); | |
112 } | |
113 | 103 |
114 virtual bool HandleDirectory(const char* dir_name) = 0; | 104 virtual bool HandleDirectory(const char* dir_name) = 0; |
115 virtual bool HandleFile(const char* file_name) = 0; | 105 virtual bool HandleFile(const char* file_name) = 0; |
116 virtual bool HandleLink(const char* link_name) = 0; | 106 virtual bool HandleLink(const char* link_name) = 0; |
117 virtual bool HandleError() = 0; | 107 virtual bool HandleError() = 0; |
118 virtual void HandleDone() {} | 108 virtual void HandleDone() {} |
119 | 109 |
120 void Push(DirectoryListingEntry* directory) { | 110 void Push(DirectoryListingEntry* directory) { top_ = directory; } |
121 top_ = directory; | |
122 } | |
123 | 111 |
124 void Pop() { | 112 void Pop() { |
125 ASSERT(!IsEmpty()); | 113 ASSERT(!IsEmpty()); |
126 DirectoryListingEntry* current = top_; | 114 DirectoryListingEntry* current = top_; |
127 top_ = top_->parent(); | 115 top_ = top_->parent(); |
128 delete current; | 116 delete current; |
129 } | 117 } |
130 | 118 |
131 bool IsEmpty() const { | 119 bool IsEmpty() const { return top_ == NULL; } |
132 return top_ == NULL; | |
133 } | |
134 | 120 |
135 void PopAll() { | 121 void PopAll() { |
136 while (!IsEmpty()) { | 122 while (!IsEmpty()) { |
137 Pop(); | 123 Pop(); |
138 } | 124 } |
139 } | 125 } |
140 | 126 |
141 DirectoryListingEntry* top() const { | 127 DirectoryListingEntry* top() const { return top_; } |
142 return top_; | |
143 } | |
144 | 128 |
145 bool recursive() const { | 129 bool recursive() const { return recursive_; } |
146 return recursive_; | |
147 } | |
148 | 130 |
149 bool follow_links() const { | 131 bool follow_links() const { return follow_links_; } |
150 return follow_links_; | |
151 } | |
152 | 132 |
153 const char* CurrentPath() { | 133 const char* CurrentPath() { return path_buffer_.AsScopedString(); } |
154 return path_buffer_.AsScopedString(); | |
155 } | |
156 | 134 |
157 PathBuffer& path_buffer() { | 135 PathBuffer& path_buffer() { return path_buffer_; } |
158 return path_buffer_; | |
159 } | |
160 | 136 |
161 bool error() const { | 137 bool error() const { return error_; } |
162 return error_; | |
163 } | |
164 | 138 |
165 private: | 139 private: |
166 PathBuffer path_buffer_; | 140 PathBuffer path_buffer_; |
167 DirectoryListingEntry* top_; | 141 DirectoryListingEntry* top_; |
168 bool error_; | 142 bool error_; |
169 bool recursive_; | 143 bool recursive_; |
170 bool follow_links_; | 144 bool follow_links_; |
171 }; | 145 }; |
172 | 146 |
173 | 147 |
174 class AsyncDirectoryListing : public ReferenceCounted<AsyncDirectoryListing>, | 148 class AsyncDirectoryListing : public ReferenceCounted<AsyncDirectoryListing>, |
175 public DirectoryListing { | 149 public DirectoryListing { |
176 public: | 150 public: |
177 enum Response { | 151 enum Response { |
178 kListFile = 0, | 152 kListFile = 0, |
179 kListDirectory = 1, | 153 kListDirectory = 1, |
180 kListLink = 2, | 154 kListLink = 2, |
181 kListError = 3, | 155 kListError = 3, |
182 kListDone = 4 | 156 kListDone = 4 |
183 }; | 157 }; |
184 | 158 |
185 AsyncDirectoryListing(const char* dir_name, | 159 AsyncDirectoryListing(const char* dir_name, bool recursive, bool follow_links) |
186 bool recursive, | |
187 bool follow_links) | |
188 : ReferenceCounted(), | 160 : ReferenceCounted(), |
189 DirectoryListing(dir_name, recursive, follow_links), | 161 DirectoryListing(dir_name, recursive, follow_links), |
190 array_(NULL), | 162 array_(NULL), |
191 index_(0), | 163 index_(0), |
192 length_(0) {} | 164 length_(0) {} |
193 | 165 |
194 virtual bool HandleDirectory(const char* dir_name); | 166 virtual bool HandleDirectory(const char* dir_name); |
195 virtual bool HandleFile(const char* file_name); | 167 virtual bool HandleFile(const char* file_name); |
196 virtual bool HandleLink(const char* file_name); | 168 virtual bool HandleLink(const char* file_name); |
197 virtual bool HandleError(); | 169 virtual bool HandleError(); |
198 virtual void HandleDone(); | 170 virtual void HandleDone(); |
199 | 171 |
200 void SetArray(CObjectArray* array, intptr_t length) { | 172 void SetArray(CObjectArray* array, intptr_t length) { |
201 ASSERT(length % 2 == 0); | 173 ASSERT(length % 2 == 0); |
202 array_ = array; | 174 array_ = array; |
203 index_ = 0; | 175 index_ = 0; |
204 length_ = length; | 176 length_ = length; |
205 } | 177 } |
206 | 178 |
207 intptr_t index() const { | 179 intptr_t index() const { return index_; } |
208 return index_; | |
209 } | |
210 | 180 |
211 private: | 181 private: |
212 virtual ~AsyncDirectoryListing() {} | 182 virtual ~AsyncDirectoryListing() {} |
213 bool AddFileSystemEntityToResponse(Response response, const char* arg); | 183 bool AddFileSystemEntityToResponse(Response response, const char* arg); |
214 CObjectArray* array_; | 184 CObjectArray* array_; |
215 intptr_t index_; | 185 intptr_t index_; |
216 intptr_t length_; | 186 intptr_t length_; |
217 | 187 |
218 friend class ReferenceCounted<AsyncDirectoryListing>; | 188 friend class ReferenceCounted<AsyncDirectoryListing>; |
219 DISALLOW_IMPLICIT_CONSTRUCTORS(AsyncDirectoryListing); | 189 DISALLOW_IMPLICIT_CONSTRUCTORS(AsyncDirectoryListing); |
220 }; | 190 }; |
221 | 191 |
222 | 192 |
223 class SyncDirectoryListing: public DirectoryListing { | 193 class SyncDirectoryListing : public DirectoryListing { |
224 public: | 194 public: |
225 SyncDirectoryListing(Dart_Handle results, | 195 SyncDirectoryListing(Dart_Handle results, |
226 const char* dir_name, | 196 const char* dir_name, |
227 bool recursive, | 197 bool recursive, |
228 bool follow_links) | 198 bool follow_links) |
229 : DirectoryListing(dir_name, recursive, follow_links), | 199 : DirectoryListing(dir_name, recursive, follow_links), results_(results) { |
230 results_(results) { | |
231 add_string_ = DartUtils::NewString("add"); | 200 add_string_ = DartUtils::NewString("add"); |
232 directory_type_ = | 201 directory_type_ = DartUtils::GetDartType(DartUtils::kIOLibURL, "Directory"); |
233 DartUtils::GetDartType(DartUtils::kIOLibURL, "Directory"); | 202 file_type_ = DartUtils::GetDartType(DartUtils::kIOLibURL, "File"); |
234 file_type_ = | 203 link_type_ = DartUtils::GetDartType(DartUtils::kIOLibURL, "Link"); |
235 DartUtils::GetDartType(DartUtils::kIOLibURL, "File"); | |
236 link_type_ = | |
237 DartUtils::GetDartType(DartUtils::kIOLibURL, "Link"); | |
238 } | 204 } |
239 virtual ~SyncDirectoryListing() {} | 205 virtual ~SyncDirectoryListing() {} |
240 virtual bool HandleDirectory(const char* dir_name); | 206 virtual bool HandleDirectory(const char* dir_name); |
241 virtual bool HandleFile(const char* file_name); | 207 virtual bool HandleFile(const char* file_name); |
242 virtual bool HandleLink(const char* file_name); | 208 virtual bool HandleLink(const char* file_name); |
243 virtual bool HandleError(); | 209 virtual bool HandleError(); |
244 | 210 |
245 private: | 211 private: |
246 Dart_Handle results_; | 212 Dart_Handle results_; |
247 Dart_Handle add_string_; | 213 Dart_Handle add_string_; |
248 Dart_Handle directory_type_; | 214 Dart_Handle directory_type_; |
249 Dart_Handle file_type_; | 215 Dart_Handle file_type_; |
250 Dart_Handle link_type_; | 216 Dart_Handle link_type_; |
251 | 217 |
252 DISALLOW_ALLOCATION() | 218 DISALLOW_ALLOCATION() |
253 DISALLOW_IMPLICIT_CONSTRUCTORS(SyncDirectoryListing); | 219 DISALLOW_IMPLICIT_CONSTRUCTORS(SyncDirectoryListing); |
254 }; | 220 }; |
255 | 221 |
256 | 222 |
257 class Directory { | 223 class Directory { |
258 public: | 224 public: |
259 enum ExistsResult { | 225 enum ExistsResult { UNKNOWN, EXISTS, DOES_NOT_EXIST }; |
260 UNKNOWN, | |
261 EXISTS, | |
262 DOES_NOT_EXIST | |
263 }; | |
264 | 226 |
265 static void List(DirectoryListing* listing); | 227 static void List(DirectoryListing* listing); |
266 static ExistsResult Exists(const char* path); | 228 static ExistsResult Exists(const char* path); |
267 | 229 |
268 // Returns the current working directory. The caller must call | 230 // Returns the current working directory. The caller must call |
269 // free() on the result. | 231 // free() on the result. |
270 static char* CurrentNoScope(); | 232 static char* CurrentNoScope(); |
271 | 233 |
272 // Returns the current working directory. The returned string is allocated | 234 // Returns the current working directory. The returned string is allocated |
273 // with Dart_ScopeAllocate(). It lasts only as long as the current API scope. | 235 // with Dart_ScopeAllocate(). It lasts only as long as the current API scope. |
(...skipping 20 matching lines...) Expand all Loading... |
294 private: | 256 private: |
295 static char* system_temp_path_override_; | 257 static char* system_temp_path_override_; |
296 DISALLOW_ALLOCATION(); | 258 DISALLOW_ALLOCATION(); |
297 DISALLOW_IMPLICIT_CONSTRUCTORS(Directory); | 259 DISALLOW_IMPLICIT_CONSTRUCTORS(Directory); |
298 }; | 260 }; |
299 | 261 |
300 } // namespace bin | 262 } // namespace bin |
301 } // namespace dart | 263 } // namespace dart |
302 | 264 |
303 #endif // RUNTIME_BIN_DIRECTORY_H_ | 265 #endif // RUNTIME_BIN_DIRECTORY_H_ |
OLD | NEW |