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