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

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

Issue 1781883002: Fixes some memory leaks in //runtime/bin (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Fix tests on Windows Created 4 years, 9 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
« no previous file with comments | « runtime/bin/dartutils.cc ('k') | runtime/bin/directory.cc » ('j') | no next file with comments »
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/thread.h" 10 #include "bin/thread.h"
11 #include "platform/globals.h" 11 #include "platform/globals.h"
12 12
13 13
14 namespace dart { 14 namespace dart {
15 namespace bin { 15 namespace bin {
16 16
17 enum ListType { 17 enum ListType {
18 kListFile = 0, 18 kListFile = 0,
19 kListDirectory = 1, 19 kListDirectory = 1,
20 kListLink = 2, 20 kListLink = 2,
21 kListError = 3, 21 kListError = 3,
22 kListDone = 4 22 kListDone = 4
23 }; 23 };
24 24
25 class PathBuffer { 25 class PathBuffer {
26 public: 26 public:
27 PathBuffer(); 27 PathBuffer();
28 ~PathBuffer() { 28 ~PathBuffer();
29 free(data_);
30 }
31 29
32 bool Add(const char* name); 30 bool Add(const char* name);
33 bool AddW(const wchar_t* name); 31 bool AddW(const wchar_t* name);
34 32
35 char* AsString() const; 33 char* AsString() const;
36 wchar_t* AsStringW() const; 34 wchar_t* AsStringW() const;
37 35
38 void Reset(int new_length); 36 // Makes a scope allocated copy of the string.
37 const char* AsScopedString() const;
39 38
40 int length() const { 39 void Reset(intptr_t new_length);
40
41 intptr_t length() const {
41 return length_; 42 return length_;
42 } 43 }
43 44
44 private: 45 private:
45 void* data_; 46 void* data_;
46 int length_; 47 intptr_t length_;
47 48
48 DISALLOW_COPY_AND_ASSIGN(PathBuffer); 49 DISALLOW_COPY_AND_ASSIGN(PathBuffer);
49 }; 50 };
50 51
51 class DirectoryListing; 52 class DirectoryListing;
52 53
53 struct LinkList; 54 struct LinkList;
54 55
55 // DirectoryListingEntry is used as a stack item, when performing recursive 56 // DirectoryListingEntry is used as a stack item, when performing recursive
56 // directory listing. By using DirectoryListingEntry as stack elements, a 57 // directory listing. By using DirectoryListingEntry as stack elements, a
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 } 106 }
106 Push(new DirectoryListingEntry(NULL)); 107 Push(new DirectoryListingEntry(NULL));
107 } 108 }
108 109
109 virtual ~DirectoryListing() { 110 virtual ~DirectoryListing() {
110 while (!IsEmpty()) { 111 while (!IsEmpty()) {
111 Pop(); 112 Pop();
112 } 113 }
113 } 114 }
114 115
115 virtual bool HandleDirectory(char* dir_name) = 0; 116 virtual bool HandleDirectory(const char* dir_name) = 0;
116 virtual bool HandleFile(char* file_name) = 0; 117 virtual bool HandleFile(const char* file_name) = 0;
117 virtual bool HandleLink(char* link_name) = 0; 118 virtual bool HandleLink(const char* link_name) = 0;
118 virtual bool HandleError(const char* dir_name) = 0; 119 virtual bool HandleError() = 0;
119 virtual void HandleDone() {} 120 virtual void HandleDone() {}
120 121
121 void Push(DirectoryListingEntry* directory) { 122 void Push(DirectoryListingEntry* directory) {
122 top_ = directory; 123 top_ = directory;
123 } 124 }
124 125
125 void Pop() { 126 void Pop() {
126 ASSERT(!IsEmpty()); 127 ASSERT(!IsEmpty());
127 DirectoryListingEntry* current = top_; 128 DirectoryListingEntry* current = top_;
128 top_ = top_->parent(); 129 top_ = top_->parent();
129 delete current; 130 delete current;
130 } 131 }
131 132
132 bool IsEmpty() const { 133 bool IsEmpty() const {
133 return top_ == NULL; 134 return top_ == NULL;
134 } 135 }
135 136
136 DirectoryListingEntry* top() const { 137 DirectoryListingEntry* top() const {
137 return top_; 138 return top_;
138 } 139 }
139 140
140 bool recursive() const { 141 bool recursive() const {
141 return recursive_; 142 return recursive_;
142 } 143 }
143 144
144 bool follow_links() const { 145 bool follow_links() const {
145 return follow_links_; 146 return follow_links_;
146 } 147 }
147 148
148 char* CurrentPath() { 149 const char* CurrentPath() {
149 return path_buffer_.AsString(); 150 return path_buffer_.AsScopedString();
150 } 151 }
151 152
152 PathBuffer& path_buffer() { 153 PathBuffer& path_buffer() {
153 return path_buffer_; 154 return path_buffer_;
154 } 155 }
155 156
156 bool error() const { 157 bool error() const {
157 return error_; 158 return error_;
158 } 159 }
159 160
(...skipping 15 matching lines...) Expand all
175 kListError = 3, 176 kListError = 3,
176 kListDone = 4 177 kListDone = 4
177 }; 178 };
178 179
179 AsyncDirectoryListing(const char* dir_name, 180 AsyncDirectoryListing(const char* dir_name,
180 bool recursive, 181 bool recursive,
181 bool follow_links) 182 bool follow_links)
182 : DirectoryListing(dir_name, recursive, follow_links) {} 183 : DirectoryListing(dir_name, recursive, follow_links) {}
183 184
184 virtual ~AsyncDirectoryListing() {} 185 virtual ~AsyncDirectoryListing() {}
185 virtual bool HandleDirectory(char* dir_name); 186 virtual bool HandleDirectory(const char* dir_name);
186 virtual bool HandleFile(char* file_name); 187 virtual bool HandleFile(const char* file_name);
187 virtual bool HandleLink(char* file_name); 188 virtual bool HandleLink(const char* file_name);
188 virtual bool HandleError(const char* dir_name); 189 virtual bool HandleError();
189 virtual void HandleDone(); 190 virtual void HandleDone();
190 191
191 void SetArray(CObjectArray* array, intptr_t length) { 192 void SetArray(CObjectArray* array, intptr_t length) {
192 ASSERT(length % 2 == 0); 193 ASSERT(length % 2 == 0);
193 array_ = array; 194 array_ = array;
194 index_ = 0; 195 index_ = 0;
195 length_ = length; 196 length_ = length;
196 } 197 }
197 198
198 intptr_t index() const { 199 intptr_t index() const {
199 return index_; 200 return index_;
200 } 201 }
201 202
202 private: 203 private:
203 bool AddFileSystemEntityToResponse(Response response, char* arg); 204 bool AddFileSystemEntityToResponse(Response response, const char* arg);
204 CObjectArray* array_; 205 CObjectArray* array_;
205 intptr_t index_; 206 intptr_t index_;
206 intptr_t length_; 207 intptr_t length_;
207 208
208 DISALLOW_IMPLICIT_CONSTRUCTORS(AsyncDirectoryListing); 209 DISALLOW_IMPLICIT_CONSTRUCTORS(AsyncDirectoryListing);
209 }; 210 };
210 211
211 212
212 class SyncDirectoryListing: public DirectoryListing { 213 class SyncDirectoryListing: public DirectoryListing {
213 public: 214 public:
214 SyncDirectoryListing(Dart_Handle results, 215 SyncDirectoryListing(Dart_Handle results,
215 const char* dir_name, 216 const char* dir_name,
216 bool recursive, 217 bool recursive,
217 bool follow_links) 218 bool follow_links)
218 : DirectoryListing(dir_name, recursive, follow_links), 219 : DirectoryListing(dir_name, recursive, follow_links),
219 results_(results) { 220 results_(results) {
220 add_string_ = DartUtils::NewString("add"); 221 add_string_ = DartUtils::NewString("add");
221 directory_type_ = 222 directory_type_ =
222 DartUtils::GetDartType(DartUtils::kIOLibURL, "Directory"); 223 DartUtils::GetDartType(DartUtils::kIOLibURL, "Directory");
223 file_type_ = 224 file_type_ =
224 DartUtils::GetDartType(DartUtils::kIOLibURL, "File"); 225 DartUtils::GetDartType(DartUtils::kIOLibURL, "File");
225 link_type_ = 226 link_type_ =
226 DartUtils::GetDartType(DartUtils::kIOLibURL, "Link"); 227 DartUtils::GetDartType(DartUtils::kIOLibURL, "Link");
227 } 228 }
228 virtual ~SyncDirectoryListing() {} 229 virtual ~SyncDirectoryListing() {}
229 virtual bool HandleDirectory(char* dir_name); 230 virtual bool HandleDirectory(const char* dir_name);
230 virtual bool HandleFile(char* file_name); 231 virtual bool HandleFile(const char* file_name);
231 virtual bool HandleLink(char* file_name); 232 virtual bool HandleLink(const char* file_name);
232 virtual bool HandleError(const char* dir_name); 233 virtual bool HandleError();
233 234
234 private: 235 private:
235 Dart_Handle results_; 236 Dart_Handle results_;
236 Dart_Handle add_string_; 237 Dart_Handle add_string_;
237 Dart_Handle directory_type_; 238 Dart_Handle directory_type_;
238 Dart_Handle file_type_; 239 Dart_Handle file_type_;
239 Dart_Handle link_type_; 240 Dart_Handle link_type_;
240 241
241 DISALLOW_IMPLICIT_CONSTRUCTORS(SyncDirectoryListing); 242 DISALLOW_IMPLICIT_CONSTRUCTORS(SyncDirectoryListing);
242 }; 243 };
243 244
244 245
245 class Directory { 246 class Directory {
246 public: 247 public:
247 enum ExistsResult { 248 enum ExistsResult {
248 UNKNOWN, 249 UNKNOWN,
249 EXISTS, 250 EXISTS,
250 DOES_NOT_EXIST 251 DOES_NOT_EXIST
251 }; 252 };
252 253
253 static void List(DirectoryListing* listing); 254 static void List(DirectoryListing* listing);
254 static ExistsResult Exists(const char* path); 255 static ExistsResult Exists(const char* path);
255 static char* Current(); 256
257 // Returns the current working directory. The caller must call
258 // free() on the result.
259 static char* CurrentNoScope();
260
261 // Returns the current working directory. The returned string is allocated
262 // with Dart_ScopeAllocate(). It lasts only as long as the current API scope.
263 static const char* Current();
264 static const char* SystemTemp();
265 static const char* CreateTemp(const char* path);
256 static bool SetCurrent(const char* path); 266 static bool SetCurrent(const char* path);
257 static bool Create(const char* path); 267 static bool Create(const char* path);
258 static char* SystemTemp();
259 static char* CreateTemp(const char* path);
260 static bool Delete(const char* path, bool recursive); 268 static bool Delete(const char* path, bool recursive);
261 static bool Rename(const char* path, const char* new_path); 269 static bool Rename(const char* path, const char* new_path);
262 270
263 static CObject* CreateRequest(const CObjectArray& request); 271 static CObject* CreateRequest(const CObjectArray& request);
264 static CObject* DeleteRequest(const CObjectArray& request); 272 static CObject* DeleteRequest(const CObjectArray& request);
265 static CObject* ExistsRequest(const CObjectArray& request); 273 static CObject* ExistsRequest(const CObjectArray& request);
266 static CObject* CreateTempRequest(const CObjectArray& request); 274 static CObject* CreateTempRequest(const CObjectArray& request);
267 static CObject* CreateSystemTempRequest(const CObjectArray& request); 275 static CObject* CreateSystemTempRequest(const CObjectArray& request);
268 static CObject* ListStartRequest(const CObjectArray& request); 276 static CObject* ListStartRequest(const CObjectArray& request);
269 static CObject* ListNextRequest(const CObjectArray& request); 277 static CObject* ListNextRequest(const CObjectArray& request);
270 static CObject* ListStopRequest(const CObjectArray& request); 278 static CObject* ListStopRequest(const CObjectArray& request);
271 static CObject* RenameRequest(const CObjectArray& request); 279 static CObject* RenameRequest(const CObjectArray& request);
272 280
273 private: 281 private:
274 DISALLOW_ALLOCATION(); 282 DISALLOW_ALLOCATION();
275 DISALLOW_IMPLICIT_CONSTRUCTORS(Directory); 283 DISALLOW_IMPLICIT_CONSTRUCTORS(Directory);
276 }; 284 };
277 285
278 } // namespace bin 286 } // namespace bin
279 } // namespace dart 287 } // namespace dart
280 288
281 #endif // BIN_DIRECTORY_H_ 289 #endif // BIN_DIRECTORY_H_
OLDNEW
« no previous file with comments | « runtime/bin/dartutils.cc ('k') | runtime/bin/directory.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698