| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 | 29 |
| 30 bool _list(String dir, | 30 bool _list(String dir, |
| 31 bool recursive, | 31 bool recursive, |
| 32 SendPort dirPort, | 32 SendPort dirPort, |
| 33 SendPort filePort, | 33 SendPort filePort, |
| 34 SendPort donePort, | 34 SendPort donePort, |
| 35 SendPort errorPort) native "Directory_List"; | 35 SendPort errorPort) native "Directory_List"; |
| 36 } | 36 } |
| 37 | 37 |
| 38 | 38 |
| 39 class _DirectoryCreateTempIsolate extends Isolate { | 39 class _DirectoryOperation { |
| 40 abstract void execute(ReceivePort port); |
| 40 | 41 |
| 41 _DirectoryCreateTempIsolate() : super.heavy(); | 42 SendPort set replyPort(SendPort port) { |
| 43 _replyPort = port; |
| 44 } |
| 45 |
| 46 SendPort _replyPort; |
| 47 } |
| 48 |
| 49 |
| 50 class _DirExitOperation extends _DirectoryOperation { |
| 51 void execute(ReceivePort port) { |
| 52 port.close(); |
| 53 } |
| 54 } |
| 55 |
| 56 |
| 57 class _DirExistsOperation extends _DirectoryOperation { |
| 58 _DirExistsOperation(String this._path); |
| 59 |
| 60 void execute(ReceivePort port) { |
| 61 _replyPort.send(_Directory._exists(_path), port.toSendPort()); |
| 62 } |
| 63 |
| 64 String _path; |
| 65 } |
| 66 |
| 67 |
| 68 class _DirCreateOperation extends _DirectoryOperation { |
| 69 _DirCreateOperation(String this._path); |
| 70 |
| 71 void execute(ReceivePort port) { |
| 72 _replyPort.send(_Directory._create(_path), port.toSendPort()); |
| 73 } |
| 74 |
| 75 String _path; |
| 76 } |
| 77 |
| 78 |
| 79 class _DirCreateTempOperation extends _DirectoryOperation { |
| 80 _DirCreateTempOperation(String this._path); |
| 81 |
| 82 void execute(ReceivePort port) { |
| 83 var status = new _OSStatus(); |
| 84 var result = _Directory._createTemp(_path, status); |
| 85 if (result == null) { |
| 86 _replyPort.send(status, port.toSendPort()); |
| 87 } else { |
| 88 _replyPort.send(result, port.toSendPort()); |
| 89 } |
| 90 } |
| 91 |
| 92 String _path; |
| 93 } |
| 94 |
| 95 |
| 96 class _DirDeleteOperation extends _DirectoryOperation { |
| 97 _DirDeleteOperation(String this._path); |
| 98 |
| 99 void execute(ReceivePort port) { |
| 100 _replyPort.send(_Directory._delete(_path), port.toSendPort()); |
| 101 } |
| 102 |
| 103 String _path; |
| 104 } |
| 105 |
| 106 |
| 107 class _DirectoryOperationIsolate extends Isolate { |
| 108 _DirectoryOperationIsolate() : super.heavy(); |
| 109 |
| 110 void handleOperation(_DirectoryOperation message, SendPort ignored) { |
| 111 message.execute(port); |
| 112 port.receive(handleOperation); |
| 113 } |
| 42 | 114 |
| 43 void main() { | 115 void main() { |
| 44 port.receive((path, replyTo) { | 116 port.receive(handleOperation); |
| 45 // Call function to get file name | |
| 46 var status = new _OSStatus(); | |
| 47 var result = _Directory._createTemp(path, status); | |
| 48 if (result == null) { | |
| 49 replyTo.send(status); | |
| 50 } else { | |
| 51 replyTo.send(result); | |
| 52 } | |
| 53 port.close(); | |
| 54 }); | |
| 55 } | 117 } |
| 56 } | 118 } |
| 57 | 119 |
| 58 | 120 |
| 121 class _DirectoryOperationScheduler { |
| 122 _DirectoryOperationScheduler() : _queue = new Queue(); |
| 123 |
| 124 void schedule(SendPort port) { |
| 125 assert(_isolate != null); |
| 126 if (_queue.isEmpty()) { |
| 127 port.send(new _DirExitOperation()); |
| 128 _isolate = null; |
| 129 } else { |
| 130 port.send(_queue.removeFirst()); |
| 131 } |
| 132 } |
| 133 |
| 134 void scheduleWrap(void callback(result, ignored)) { |
| 135 return (result, replyTo) { |
| 136 callback(result, replyTo); |
| 137 schedule(replyTo); |
| 138 }; |
| 139 } |
| 140 |
| 141 void enqueue(_DirectoryOperation operation, void callback(result, ignored)) { |
| 142 ReceivePort replyPort = new ReceivePort.singleShot(); |
| 143 replyPort.receive(scheduleWrap(callback)); |
| 144 operation.replyPort = replyPort.toSendPort(); |
| 145 _queue.addLast(operation); |
| 146 if (_isolate == null) { |
| 147 _isolate = new _FileOperationIsolate(); |
| 148 _isolate.spawn().then((port) { |
| 149 schedule(port); |
| 150 }); |
| 151 } |
| 152 } |
| 153 |
| 154 Queue<_DirectoryOperation> _queue; |
| 155 _DirectoryOperationIsolate _isolate; |
| 156 } |
| 157 |
| 158 |
| 59 class _Directory implements Directory { | 159 class _Directory implements Directory { |
| 60 | 160 |
| 61 _Directory(String this._path); | 161 _Directory(String this._path) |
| 162 : _scheduler = new _DirectoryOperationScheduler(); |
| 62 | 163 |
| 63 static String _createTemp(String template, | 164 static String _createTemp(String template, |
| 64 _OSStatus status) native "Directory_CreateTemp"; | 165 _OSStatus status) native "Directory_CreateTemp"; |
| 166 static int _exists(String path) native "Directory_Exists"; |
| 167 static bool _create(String path) native "Directory_Create"; |
| 168 static bool _delete(String path) native "Directory_Delete"; |
| 169 |
| 170 void exists() { |
| 171 var handler = (_existsHandler != null) ? _existsHandler : (result) => null; |
| 172 var operation = new _DirExistsOperation(_path); |
| 173 _scheduler.enqueue(operation, (result, ignored) { |
| 174 if (result < 0) { |
| 175 if (_errorHandler != null) { |
| 176 _errorHandler("Diretory exists test failed: $_path"); |
| 177 } |
| 178 } else { |
| 179 handler(result == 1); |
| 180 } |
| 181 }); |
| 182 } |
| 65 | 183 |
| 66 bool existsSync() { | 184 bool existsSync() { |
| 67 int exists = _exists(_path); | 185 int exists = _exists(_path); |
| 68 if (exists < 0) { | 186 if (exists < 0) { |
| 69 throw new DirectoryException("Diretory exists test failed: $_path"); | 187 throw new DirectoryException("Diretory exists test failed: $_path"); |
| 70 } | 188 } |
| 71 return (exists == 1); | 189 return (exists == 1); |
| 72 } | 190 } |
| 73 | 191 |
| 192 void create() { |
| 193 var handler = (_createHandler != null) ? _createHandler : () => null; |
| 194 var operation = new _DirCreateOperation(_path); |
| 195 _scheduler.enqueue(operation, (result, ignored) { |
| 196 if (result) { |
| 197 handler(); |
| 198 } else if (_errorHandler != null) { |
| 199 _errorHandler("Directory creation failed: $_path"); |
| 200 } |
| 201 }); |
| 202 } |
| 203 |
| 74 void createSync() { | 204 void createSync() { |
| 75 if (!_create(_path)) { | 205 if (!_create(_path)) { |
| 76 throw new DirectoryException("Directory creation failed: $_path"); | 206 throw new DirectoryException("Directory creation failed: $_path"); |
| 77 } | 207 } |
| 78 } | 208 } |
| 79 | 209 |
| 80 void createTemp() { | 210 void createTemp() { |
| 81 new _DirectoryCreateTempIsolate().spawn().then((port) { | 211 var handler = |
| 82 port.call(_path).receive((result, ignored) { | 212 (_createTempHandler != null) ? _createTempHandler : () => null; |
| 83 if (result is !_OSStatus) { | 213 var operation = new _DirCreateTempOperation(_path); |
| 84 _path = result; | 214 _scheduler.enqueue(operation, (result, ignored) { |
| 85 if (_createTempHandler !== null) { | 215 if (result is !_OSStatus) { |
| 86 _createTempHandler(); | 216 _path = result; |
| 87 } | 217 handler(); |
| 88 } else { | 218 } else if (_errorHandler !== null) { |
| 89 if (_errorHandler !== null) { | 219 _errorHandler("Could not create temporary directory [$_path]: " + |
| 90 _errorHandler("Could not create temporary directory [$_path]: " + | 220 "${result._errorMessage}"); |
| 91 "${result._errorMessage}"); | 221 } |
| 92 } | |
| 93 } | |
| 94 }); | |
| 95 }); | 222 }); |
| 96 } | 223 } |
| 97 | 224 |
| 98 void createTempSync() { | 225 void createTempSync() { |
| 99 var status = new _OSStatus(); | 226 var status = new _OSStatus(); |
| 100 var result = _createTemp(path, status); | 227 var result = _createTemp(path, status); |
| 101 if (result != null) { | 228 if (result != null) { |
| 102 _path = result; | 229 _path = result; |
| 103 } else { | 230 } else { |
| 104 throw new DirectoryException( | 231 throw new DirectoryException( |
| 105 "Could not create temporary directory [$_path]: " + | 232 "Could not create temporary directory [$_path]: " + |
| 106 "${status._errorMessage}", | 233 "${status._errorMessage}", |
| 107 status._errorCode); | 234 status._errorCode); |
| 108 } | 235 } |
| 109 } | 236 } |
| 110 | 237 |
| 238 void delete() { |
| 239 var handler = (_deleteHandler != null) ? _deleteHandler : () => null; |
| 240 var operation = new _DirDeleteOperation(_path); |
| 241 _scheduler.enqueue(operation, (result, ignored) { |
| 242 if (result) { |
| 243 handler(); |
| 244 } else if (_errorHandler != null) { |
| 245 _errorHandler("Directory deletion failed: $_path"); |
| 246 } |
| 247 }); |
| 248 } |
| 249 |
| 111 void deleteSync() { | 250 void deleteSync() { |
| 112 if (!_delete(_path)) { | 251 if (!_delete(_path)) { |
| 113 throw new DirectoryException("Directory deletion failed: $_path"); | 252 throw new DirectoryException("Directory deletion failed: $_path"); |
| 114 } | 253 } |
| 115 } | 254 } |
| 116 | 255 |
| 117 void list([bool recursive = false]) { | 256 void list([bool recursive = false]) { |
| 118 new _DirectoryListingIsolate().spawn().then((port) { | 257 new _DirectoryListingIsolate().spawn().then((port) { |
| 119 // Build a map of parameters to the directory listing isolate. | 258 // Build a map of parameters to the directory listing isolate. |
| 120 Map listingParameters = new Map(); | 259 Map listingParameters = new Map(); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 } | 322 } |
| 184 | 323 |
| 185 void set fileHandler(void fileHandler(String file)) { | 324 void set fileHandler(void fileHandler(String file)) { |
| 186 _fileHandler = fileHandler; | 325 _fileHandler = fileHandler; |
| 187 } | 326 } |
| 188 | 327 |
| 189 void set doneHandler(void doneHandler(bool completed)) { | 328 void set doneHandler(void doneHandler(bool completed)) { |
| 190 _doneHandler = doneHandler; | 329 _doneHandler = doneHandler; |
| 191 } | 330 } |
| 192 | 331 |
| 332 void set existsHandler(void existsHandler(bool exists)) { |
| 333 _existsHandler = existsHandler; |
| 334 } |
| 335 |
| 336 void set createHandler(void createHandler()) { |
| 337 _createHandler = createHandler; |
| 338 } |
| 339 |
| 193 void set createTempHandler(void createTempHandler()) { | 340 void set createTempHandler(void createTempHandler()) { |
| 194 _createTempHandler = createTempHandler; | 341 _createTempHandler = createTempHandler; |
| 195 } | 342 } |
| 196 | 343 |
| 344 void set deleteHandler(void deleteHandler()) { |
| 345 _deleteHandler = deleteHandler; |
| 346 } |
| 347 |
| 197 void set errorHandler(void errorHandler(String error)) { | 348 void set errorHandler(void errorHandler(String error)) { |
| 198 _errorHandler = errorHandler; | 349 _errorHandler = errorHandler; |
| 199 } | 350 } |
| 200 | 351 |
| 201 void _closePort(ReceivePort port) { | 352 void _closePort(ReceivePort port) { |
| 202 if (port !== null) { | 353 if (port !== null) { |
| 203 port.close(); | 354 port.close(); |
| 204 } | 355 } |
| 205 } | 356 } |
| 206 | 357 |
| 207 String get path() { return _path; } | 358 String get path() { return _path; } |
| 208 | 359 |
| 209 int _exists(String path) native "Directory_Exists"; | |
| 210 bool _create(String path) native "Directory_Create"; | |
| 211 bool _delete(String path) native "Directory_Delete"; | |
| 212 | |
| 213 var _dirHandler; | 360 var _dirHandler; |
| 214 var _fileHandler; | 361 var _fileHandler; |
| 215 var _doneHandler; | 362 var _doneHandler; |
| 363 var _existsHandler; |
| 364 var _createHandler; |
| 216 var _createTempHandler; | 365 var _createTempHandler; |
| 366 var _deleteHandler; |
| 217 var _errorHandler; | 367 var _errorHandler; |
| 218 | 368 |
| 219 String _path; | 369 String _path; |
| 370 _DirectoryOperationScheduler _scheduler; |
| 220 } | 371 } |
| OLD | NEW |