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