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 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 }); | 150 }); |
151 } | 151 } |
152 } | 152 } |
153 | 153 |
154 Queue<_DirectoryOperation> _queue; | 154 Queue<_DirectoryOperation> _queue; |
155 _DirectoryOperationIsolate _isolate; | 155 _DirectoryOperationIsolate _isolate; |
156 } | 156 } |
157 | 157 |
158 | 158 |
159 class _Directory implements Directory { | 159 class _Directory implements Directory { |
| 160 static final int kCreateService = 0; |
| 161 static final int kDeleteService = 1; |
160 | 162 |
161 _Directory(String this._path) | 163 _Directory(String this._path) |
162 : _scheduler = new _DirectoryOperationScheduler(); | 164 : _scheduler = new _DirectoryOperationScheduler(); |
163 | 165 |
164 static String _createTemp(String template, | 166 static String _createTemp(String template, |
165 _OSStatus status) native "Directory_CreateTemp"; | 167 _OSStatus status) native "Directory_CreateTemp"; |
166 static int _exists(String path) native "Directory_Exists"; | 168 static int _exists(String path) native "Directory_Exists"; |
167 static bool _create(String path) native "Directory_Create"; | 169 static bool _create(String path) native "Directory_Create"; |
168 static bool _delete(String path) native "Directory_Delete"; | 170 static bool _delete(String path) native "Directory_Delete"; |
| 171 static SendPort _newServicePort(int service_id) |
| 172 native "Directory_NewServicePort"; |
169 | 173 |
170 void exists() { | 174 void exists() { |
171 var handler = (_existsHandler != null) ? _existsHandler : (result) => null; | 175 var handler = (_existsHandler != null) ? _existsHandler : (result) => null; |
172 var operation = new _DirExistsOperation(_path); | 176 var operation = new _DirExistsOperation(_path); |
173 _scheduler.enqueue(operation, (result, ignored) { | 177 _scheduler.enqueue(operation, (result, ignored) { |
174 if (result < 0) { | 178 if (result < 0) { |
175 if (_errorHandler != null) { | 179 if (_errorHandler != null) { |
176 _errorHandler("Diretory exists test failed: $_path"); | 180 _errorHandler("Diretory exists test failed: $_path"); |
177 } | 181 } |
178 } else { | 182 } else { |
179 handler(result == 1); | 183 handler(result == 1); |
180 } | 184 } |
181 }); | 185 }); |
182 } | 186 } |
183 | 187 |
184 bool existsSync() { | 188 bool existsSync() { |
185 int exists = _exists(_path); | 189 int exists = _exists(_path); |
186 if (exists < 0) { | 190 if (exists < 0) { |
187 throw new DirectoryException("Diretory exists test failed: $_path"); | 191 throw new DirectoryException("Diretory exists test failed: $_path"); |
188 } | 192 } |
189 return (exists == 1); | 193 return (exists == 1); |
190 } | 194 } |
191 | 195 |
192 void create() { | 196 void create() { |
193 var handler = (_createHandler != null) ? _createHandler : () => null; | 197 if (_directoryCreateService == null) { |
194 var operation = new _DirCreateOperation(_path); | 198 _directoryCreateService = _newServicePort(kCreateService); |
195 _scheduler.enqueue(operation, (result, ignored) { | 199 } |
196 if (result) { | 200 _directoryCreateService.call(_path).receive((result, replyTo) { |
197 handler(); | 201 if (result) { |
198 } else if (_errorHandler != null) { | 202 if (_createHandler != null) _createHandler(); |
199 _errorHandler("Directory creation failed: $_path"); | 203 } else if (_errorHandler != null) { |
200 } | 204 _errorHandler("Directory creation failed: $_path"); |
201 }); | 205 } |
| 206 }); |
202 } | 207 } |
203 | 208 |
204 void createSync() { | 209 void createSync() { |
205 if (!_create(_path)) { | 210 if (!_create(_path)) { |
206 throw new DirectoryException("Directory creation failed: $_path"); | 211 throw new DirectoryException("Directory creation failed: $_path"); |
207 } | 212 } |
208 } | 213 } |
209 | 214 |
210 void createTemp() { | 215 void createTemp() { |
211 var handler = | 216 var handler = |
(...skipping 17 matching lines...) Expand all Loading... |
229 _path = result; | 234 _path = result; |
230 } else { | 235 } else { |
231 throw new DirectoryException( | 236 throw new DirectoryException( |
232 "Could not create temporary directory [$_path]: " + | 237 "Could not create temporary directory [$_path]: " + |
233 "${status._errorMessage}", | 238 "${status._errorMessage}", |
234 status._errorCode); | 239 status._errorCode); |
235 } | 240 } |
236 } | 241 } |
237 | 242 |
238 void delete() { | 243 void delete() { |
239 var handler = (_deleteHandler != null) ? _deleteHandler : () => null; | 244 if (_directoryDeleteService == null) { |
240 var operation = new _DirDeleteOperation(_path); | 245 _directoryDeleteService = _newServicePort(kDeleteService); |
241 _scheduler.enqueue(operation, (result, ignored) { | 246 } |
242 if (result) { | 247 _directoryDeleteService.call(_path).receive((result, replyTo) { |
243 handler(); | 248 if (result) { |
244 } else if (_errorHandler != null) { | 249 if (_deleteHandler != null) _deleteHandler(); |
245 _errorHandler("Directory deletion failed: $_path"); | 250 } else if (_errorHandler != null) { |
246 } | 251 _errorHandler("Directory deletion failed: $_path"); |
247 }); | 252 } |
| 253 }); |
248 } | 254 } |
249 | 255 |
250 void deleteSync() { | 256 void deleteSync() { |
251 if (!_delete(_path)) { | 257 if (!_delete(_path)) { |
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) { |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 var _fileHandler; | 367 var _fileHandler; |
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; |
| 377 |
| 378 static SendPort _directoryCreateService; |
| 379 static SendPort _directoryDeleteService; |
371 } | 380 } |
OLD | NEW |