OLD | NEW |
1 // Copyright (c) 2011, 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 | 5 |
6 // Used for holding error code and error message for failed OS system calls. | 6 // Used for holding error code and error message for failed OS system calls. |
7 class _OSStatus { | 7 class _OSStatus { |
8 int _errorCode; | 8 int _errorCode; |
9 String _errorMessage; | 9 String _errorMessage; |
10 } | 10 } |
11 | 11 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 } else { | 87 } else { |
88 _replyPort.send(result, port.toSendPort()); | 88 _replyPort.send(result, port.toSendPort()); |
89 } | 89 } |
90 } | 90 } |
91 | 91 |
92 String _path; | 92 String _path; |
93 } | 93 } |
94 | 94 |
95 | 95 |
96 class _DirDeleteOperation extends _DirectoryOperation { | 96 class _DirDeleteOperation extends _DirectoryOperation { |
97 _DirDeleteOperation(String this._path); | 97 _DirDeleteOperation(String this._path, bool this._recursive); |
98 | 98 |
99 void execute(ReceivePort port) { | 99 void execute(ReceivePort port) { |
100 _replyPort.send(_Directory._delete(_path), port.toSendPort()); | 100 _replyPort.send(_Directory._delete(_path, _recursive), port.toSendPort()); |
101 } | 101 } |
102 | 102 |
103 String _path; | 103 String _path; |
| 104 bool _recursive; |
104 } | 105 } |
105 | 106 |
106 | 107 |
107 class _DirectoryOperationIsolate extends Isolate { | 108 class _DirectoryOperationIsolate extends Isolate { |
108 _DirectoryOperationIsolate() : super.heavy(); | 109 _DirectoryOperationIsolate() : super.heavy(); |
109 | 110 |
110 void handleOperation(_DirectoryOperation message, SendPort ignored) { | 111 void handleOperation(_DirectoryOperation message, SendPort ignored) { |
111 message.execute(port); | 112 message.execute(port); |
112 port.receive(handleOperation); | 113 port.receive(handleOperation); |
113 } | 114 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 | 159 |
159 class _Directory implements Directory { | 160 class _Directory implements Directory { |
160 | 161 |
161 _Directory(String this._path) | 162 _Directory(String this._path) |
162 : _scheduler = new _DirectoryOperationScheduler(); | 163 : _scheduler = new _DirectoryOperationScheduler(); |
163 | 164 |
164 static String _createTemp(String template, | 165 static String _createTemp(String template, |
165 _OSStatus status) native "Directory_CreateTemp"; | 166 _OSStatus status) native "Directory_CreateTemp"; |
166 static int _exists(String path) native "Directory_Exists"; | 167 static int _exists(String path) native "Directory_Exists"; |
167 static bool _create(String path) native "Directory_Create"; | 168 static bool _create(String path) native "Directory_Create"; |
168 static bool _delete(String path) native "Directory_Delete"; | 169 static bool _delete(String path, bool recursive) native "Directory_Delete"; |
169 | 170 |
170 void exists() { | 171 void exists() { |
171 var handler = (_existsHandler != null) ? _existsHandler : (result) => null; | |
172 var operation = new _DirExistsOperation(_path); | 172 var operation = new _DirExistsOperation(_path); |
173 _scheduler.enqueue(operation, (result, ignored) { | 173 _scheduler.enqueue(operation, (result, ignored) { |
| 174 var handler = |
| 175 (_existsHandler != null) ? _existsHandler : (result) => null; |
174 if (result < 0) { | 176 if (result < 0) { |
175 if (_errorHandler != null) { | 177 if (_errorHandler != null) { |
176 _errorHandler("Diretory exists test failed: $_path"); | 178 _errorHandler("Diretory exists test failed: $_path"); |
177 } | 179 } |
178 } else { | 180 } else { |
179 handler(result == 1); | 181 handler(result == 1); |
180 } | 182 } |
181 }); | 183 }); |
182 } | 184 } |
183 | 185 |
184 bool existsSync() { | 186 bool existsSync() { |
185 int exists = _exists(_path); | 187 int exists = _exists(_path); |
186 if (exists < 0) { | 188 if (exists < 0) { |
187 throw new DirectoryException("Diretory exists test failed: $_path"); | 189 throw new DirectoryException("Diretory exists test failed: $_path"); |
188 } | 190 } |
189 return (exists == 1); | 191 return (exists == 1); |
190 } | 192 } |
191 | 193 |
192 void create() { | 194 void create() { |
193 var handler = (_createHandler != null) ? _createHandler : () => null; | |
194 var operation = new _DirCreateOperation(_path); | 195 var operation = new _DirCreateOperation(_path); |
195 _scheduler.enqueue(operation, (result, ignored) { | 196 _scheduler.enqueue(operation, (result, ignored) { |
| 197 var handler = (_createHandler != null) ? _createHandler : () => null; |
196 if (result) { | 198 if (result) { |
197 handler(); | 199 handler(); |
198 } else if (_errorHandler != null) { | 200 } else if (_errorHandler != null) { |
199 _errorHandler("Directory creation failed: $_path"); | 201 _errorHandler("Directory creation failed: $_path"); |
200 } | 202 } |
201 }); | 203 }); |
202 } | 204 } |
203 | 205 |
204 void createSync() { | 206 void createSync() { |
205 if (!_create(_path)) { | 207 if (!_create(_path)) { |
206 throw new DirectoryException("Directory creation failed: $_path"); | 208 throw new DirectoryException("Directory creation failed: $_path"); |
207 } | 209 } |
208 } | 210 } |
209 | 211 |
210 void createTemp() { | 212 void createTemp() { |
211 var handler = | |
212 (_createTempHandler != null) ? _createTempHandler : () => null; | |
213 var operation = new _DirCreateTempOperation(_path); | 213 var operation = new _DirCreateTempOperation(_path); |
214 _scheduler.enqueue(operation, (result, ignored) { | 214 _scheduler.enqueue(operation, (result, ignored) { |
| 215 var handler = |
| 216 (_createTempHandler != null) ? _createTempHandler : () => null; |
215 if (result is !_OSStatus) { | 217 if (result is !_OSStatus) { |
216 _path = result; | 218 _path = result; |
217 handler(); | 219 handler(); |
218 } else if (_errorHandler !== null) { | 220 } else if (_errorHandler !== null) { |
219 _errorHandler("Could not create temporary directory [$_path]: " + | 221 _errorHandler("Could not create temporary directory [$_path]: " + |
220 "${result._errorMessage}"); | 222 "${result._errorMessage}"); |
221 } | 223 } |
222 }); | 224 }); |
223 } | 225 } |
224 | 226 |
225 void createTempSync() { | 227 void createTempSync() { |
226 var status = new _OSStatus(); | 228 var status = new _OSStatus(); |
227 var result = _createTemp(path, status); | 229 var result = _createTemp(path, status); |
228 if (result != null) { | 230 if (result != null) { |
229 _path = result; | 231 _path = result; |
230 } else { | 232 } else { |
231 throw new DirectoryException( | 233 throw new DirectoryException( |
232 "Could not create temporary directory [$_path]: " + | 234 "Could not create temporary directory [$_path]: " + |
233 "${status._errorMessage}", | 235 "${status._errorMessage}", |
234 status._errorCode); | 236 status._errorCode); |
235 } | 237 } |
236 } | 238 } |
237 | 239 |
238 void delete() { | 240 void delete([bool recursive = false]) { |
239 var handler = (_deleteHandler != null) ? _deleteHandler : () => null; | 241 var operation = new _DirDeleteOperation(_path, recursive); |
240 var operation = new _DirDeleteOperation(_path); | |
241 _scheduler.enqueue(operation, (result, ignored) { | 242 _scheduler.enqueue(operation, (result, ignored) { |
| 243 var handler = (_deleteHandler != null) ? _deleteHandler : () => null; |
242 if (result) { | 244 if (result) { |
243 handler(); | 245 handler(); |
244 } else if (_errorHandler != null) { | 246 } else if (_errorHandler != null) { |
245 _errorHandler("Directory deletion failed: $_path"); | 247 if (recursive) { |
| 248 _errorHandler("Recursive directory deletion failed: $_path"); |
| 249 } else { |
| 250 _errorHandler("Non-recursive directory deletion failed: $_path"); |
| 251 } |
246 } | 252 } |
247 }); | 253 }); |
248 } | 254 } |
249 | 255 |
250 void deleteSync() { | 256 void deleteSync([bool recursive = false]) { |
251 if (!_delete(_path)) { | 257 if (!_delete(_path, recursive)) { |
252 throw new DirectoryException("Directory deletion failed: $_path"); | 258 throw new DirectoryException("Directory deletion failed: $_path"); |
253 } | 259 } |
254 } | 260 } |
255 | 261 |
256 void list([bool recursive = false]) { | 262 void list([bool recursive = false]) { |
257 new _DirectoryListingIsolate().spawn().then((port) { | 263 new _DirectoryListingIsolate().spawn().then((port) { |
258 // Build a map of parameters to the directory listing isolate. | 264 // Build a map of parameters to the directory listing isolate. |
259 Map listingParameters = new Map(); | 265 Map listingParameters = new Map(); |
260 listingParameters['dir'] = _path; | 266 listingParameters['dir'] = _path; |
261 listingParameters['recursive'] = recursive; | 267 listingParameters['recursive'] = recursive; |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 var _doneHandler; | 368 var _doneHandler; |
363 var _existsHandler; | 369 var _existsHandler; |
364 var _createHandler; | 370 var _createHandler; |
365 var _createTempHandler; | 371 var _createTempHandler; |
366 var _deleteHandler; | 372 var _deleteHandler; |
367 var _errorHandler; | 373 var _errorHandler; |
368 | 374 |
369 String _path; | 375 String _path; |
370 _DirectoryOperationScheduler _scheduler; | 376 _DirectoryOperationScheduler _scheduler; |
371 } | 377 } |
OLD | NEW |