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 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_; | |
Søren Gjesse
2013/06/17 06:37:47
Add DISALLOW_COPY_AND_ASSIGN(PathBuffer);
Also be
Anders Johnsen
2013/06/17 07:27:11
Done.
| |
48 }; | |
49 | |
50 class DirectoryListing; | |
51 | |
52 class LinkList; | |
53 | |
Søren Gjesse
2013/06/17 06:40:03
Could you add a comment on how the current state i
Anders Johnsen
2013/06/17 07:27:11
Done.
| |
54 class DirectoryListingEntry { | |
55 public: | |
56 explicit DirectoryListingEntry(DirectoryListingEntry* parent) | |
57 : parent_(parent), lister_(0), done_(false), link_(NULL) {} | |
58 | |
59 ~DirectoryListingEntry() { | |
60 ResetLink(); | |
61 } | |
62 | |
63 ListType Next(DirectoryListing* listing); | |
64 | |
65 DirectoryListingEntry* parent() const { | |
66 return parent_; | |
67 } | |
68 | |
69 LinkList* link() { | |
70 return link_; | |
71 } | |
72 | |
73 void set_link(LinkList* link) { | |
74 link_ = link; | |
75 } | |
76 | |
77 void ResetLink() { | |
78 if (link_ != NULL && (parent_ == NULL || parent_->link_ != link_)) { | |
79 free(link_); | |
80 link_ = NULL; | |
81 } | |
82 if (parent_ != NULL) { | |
83 link_ = parent_->link_; | |
84 } | |
85 } | |
86 | |
87 private: | |
88 DirectoryListingEntry* parent_; | |
89 intptr_t lister_; | |
90 bool done_; | |
91 int path_length_; | |
92 LinkList* link_; | |
93 }; | |
94 | |
18 class DirectoryListing { | 95 class DirectoryListing { |
19 public: | 96 public: |
20 virtual ~DirectoryListing() {} | 97 DirectoryListing(const char* dir_name, bool recursive, bool follow_links) |
98 : top_(NULL), | |
99 error_(false), | |
100 recursive_(recursive), | |
101 follow_links_(follow_links) { | |
102 if (!path_buffer_.Add(dir_name)) { | |
103 error_ = true; | |
104 } | |
105 Push(new DirectoryListingEntry(NULL)); | |
106 } | |
107 | |
108 virtual ~DirectoryListing() { | |
109 while (!IsEmpty()) { | |
110 Pop(); | |
111 } | |
112 } | |
113 | |
21 virtual bool HandleDirectory(char* dir_name) = 0; | 114 virtual bool HandleDirectory(char* dir_name) = 0; |
22 virtual bool HandleFile(char* file_name) = 0; | 115 virtual bool HandleFile(char* file_name) = 0; |
23 virtual bool HandleLink(char* file_name) = 0; | 116 virtual bool HandleLink(char* link_name) = 0; |
24 virtual bool HandleError(const char* dir_name) = 0; | 117 virtual bool HandleError(const char* dir_name) = 0; |
118 virtual void HandleDone() {} | |
119 | |
120 void Push(DirectoryListingEntry* directory) { | |
121 top_ = directory; | |
122 } | |
123 | |
124 void Pop() { | |
125 ASSERT(!IsEmpty()); | |
126 DirectoryListingEntry* current = top_; | |
127 top_ = top_->parent(); | |
128 delete current; | |
129 } | |
130 | |
131 bool IsEmpty() const { | |
132 return top_ == NULL; | |
133 } | |
134 | |
135 DirectoryListingEntry* top() const { | |
136 return top_; | |
137 } | |
138 | |
139 bool recursive() const { | |
140 return recursive_; | |
141 } | |
142 | |
143 bool follow_links() const { | |
144 return follow_links_; | |
145 } | |
146 | |
147 char* CurrentPath() { | |
148 return path_buffer_.AsString(); | |
149 } | |
150 | |
151 PathBuffer& path_buffer() { | |
152 return path_buffer_; | |
153 } | |
154 | |
155 bool error() const { | |
156 return error_; | |
157 } | |
158 | |
159 private: | |
160 PathBuffer path_buffer_; | |
161 DirectoryListingEntry* top_; | |
162 bool error_; | |
163 bool recursive_; | |
164 bool follow_links_; | |
25 }; | 165 }; |
26 | 166 |
27 | 167 |
28 class AsyncDirectoryListing : public DirectoryListing { | 168 class AsyncDirectoryListing : public DirectoryListing { |
29 public: | 169 public: |
30 enum Response { | 170 enum Response { |
31 kListFile = 0, | 171 kListFile = 0, |
32 kListDirectory = 1, | 172 kListDirectory = 1, |
33 kListLink = 2, | 173 kListLink = 2, |
34 kListError = 3, | 174 kListError = 3, |
35 kListDone = 4 | 175 kListDone = 4 |
36 }; | 176 }; |
37 | 177 |
38 explicit AsyncDirectoryListing(Dart_Port response_port) | 178 AsyncDirectoryListing(const char* dir_name, |
39 : response_port_(response_port) {} | 179 bool recursive, |
180 bool follow_links) | |
181 : DirectoryListing(dir_name, recursive, follow_links) {} | |
182 | |
40 virtual ~AsyncDirectoryListing() {} | 183 virtual ~AsyncDirectoryListing() {} |
41 virtual bool HandleDirectory(char* dir_name); | 184 virtual bool HandleDirectory(char* dir_name); |
42 virtual bool HandleFile(char* file_name); | 185 virtual bool HandleFile(char* file_name); |
43 virtual bool HandleLink(char* file_name); | 186 virtual bool HandleLink(char* file_name); |
44 virtual bool HandleError(const char* dir_name); | 187 virtual bool HandleError(const char* dir_name); |
188 virtual void HandleDone(); | |
189 | |
190 void SetArray(CObjectArray* array, intptr_t length) { | |
191 ASSERT(length % 2 == 0); | |
192 array_ = array; | |
193 index_ = 0; | |
194 length_ = length; | |
195 } | |
196 | |
197 intptr_t index() const { | |
198 return index_; | |
199 } | |
45 | 200 |
46 private: | 201 private: |
47 CObjectArray* NewResponse(Response response, char* arg); | 202 bool AddFileSystemEntityToResponse(Response response, char* arg); |
48 Dart_Port response_port_; | 203 CObjectArray* array_; |
204 intptr_t index_; | |
205 intptr_t length_; | |
49 | 206 |
50 DISALLOW_IMPLICIT_CONSTRUCTORS(AsyncDirectoryListing); | 207 DISALLOW_IMPLICIT_CONSTRUCTORS(AsyncDirectoryListing); |
51 }; | 208 }; |
52 | 209 |
53 | 210 |
54 class SyncDirectoryListing: public DirectoryListing { | 211 class SyncDirectoryListing: public DirectoryListing { |
55 public: | 212 public: |
56 explicit SyncDirectoryListing(Dart_Handle results) | 213 SyncDirectoryListing(Dart_Handle results, |
57 : results_(results) { | 214 const char* dir_name, |
215 bool recursive, | |
216 bool follow_links) | |
217 : DirectoryListing(dir_name, recursive, follow_links), | |
218 results_(results) { | |
58 add_string_ = DartUtils::NewString("add"); | 219 add_string_ = DartUtils::NewString("add"); |
59 directory_class_ = | 220 directory_class_ = |
60 DartUtils::GetDartClass(DartUtils::kIOLibURL, "Directory"); | 221 DartUtils::GetDartClass(DartUtils::kIOLibURL, "Directory"); |
61 file_class_ = | 222 file_class_ = |
62 DartUtils::GetDartClass(DartUtils::kIOLibURL, "File"); | 223 DartUtils::GetDartClass(DartUtils::kIOLibURL, "File"); |
63 link_class_ = | 224 link_class_ = |
64 DartUtils::GetDartClass(DartUtils::kIOLibURL, "Link"); | 225 DartUtils::GetDartClass(DartUtils::kIOLibURL, "Link"); |
65 } | 226 } |
66 virtual ~SyncDirectoryListing() {} | 227 virtual ~SyncDirectoryListing() {} |
67 virtual bool HandleDirectory(char* dir_name); | 228 virtual bool HandleDirectory(char* dir_name); |
(...skipping 20 matching lines...) Expand all Loading... | |
88 DOES_NOT_EXIST | 249 DOES_NOT_EXIST |
89 }; | 250 }; |
90 | 251 |
91 // This enum must be kept in sync with the request values in | 252 // This enum must be kept in sync with the request values in |
92 // directory_impl.dart. | 253 // directory_impl.dart. |
93 enum DirectoryRequest { | 254 enum DirectoryRequest { |
94 kCreateRequest = 0, | 255 kCreateRequest = 0, |
95 kDeleteRequest = 1, | 256 kDeleteRequest = 1, |
96 kExistsRequest = 2, | 257 kExistsRequest = 2, |
97 kCreateTempRequest = 3, | 258 kCreateTempRequest = 3, |
98 kListRequest = 4, | 259 kListStartRequest = 4, |
99 kRenameRequest = 5 | 260 kListNextRequest = 5, |
261 kListStopRequest = 6, | |
262 kRenameRequest = 7 | |
100 }; | 263 }; |
101 | 264 |
102 static bool List(const char* path, | 265 static void List(DirectoryListing* listing); |
103 bool recursive, | |
104 bool follow_links, | |
105 DirectoryListing* listing); | |
106 static ExistsResult Exists(const char* path); | 266 static ExistsResult Exists(const char* path); |
107 static char* Current(); | 267 static char* Current(); |
108 static bool SetCurrent(const char* path); | 268 static bool SetCurrent(const char* path); |
109 static bool Create(const char* path); | 269 static bool Create(const char* path); |
110 static char* CreateTemp(const char* const_template); | 270 static char* CreateTemp(const char* const_template); |
111 static bool Delete(const char* path, bool recursive); | 271 static bool Delete(const char* path, bool recursive); |
112 static bool Rename(const char* path, const char* new_path); | 272 static bool Rename(const char* path, const char* new_path); |
113 static Dart_Port GetServicePort(); | 273 static Dart_Port GetServicePort(); |
114 | 274 |
115 private: | 275 private: |
116 static NativeService directory_service_; | 276 static NativeService directory_service_; |
117 | 277 |
118 DISALLOW_ALLOCATION(); | 278 DISALLOW_ALLOCATION(); |
119 DISALLOW_IMPLICIT_CONSTRUCTORS(Directory); | 279 DISALLOW_IMPLICIT_CONSTRUCTORS(Directory); |
120 }; | 280 }; |
121 | 281 |
122 } // namespace bin | 282 } // namespace bin |
123 } // namespace dart | 283 } // namespace dart |
124 | 284 |
125 #endif // BIN_DIRECTORY_H_ | 285 #endif // BIN_DIRECTORY_H_ |
OLD | NEW |