| 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 | 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 |
| 12 | 12 |
| 13 class _Directory implements Directory { | 13 class _Directory implements Directory { |
| 14 static final kCreateRequest = 0; | 14 static final kCreateRequest = 0; |
| 15 static final kDeleteRequest = 1; | 15 static final kDeleteRequest = 1; |
| 16 static final kExistsRequest = 2; | 16 static final kExistsRequest = 2; |
| 17 static final kCreateTempRequest = 3; | 17 static final kCreateTempRequest = 3; |
| 18 static final kListRequest = 4; | 18 static final kListRequest = 4; |
| 19 | 19 |
| 20 _Directory(String this._path); | 20 _Directory(String this._path); |
| 21 | 21 |
| 22 static String _createTemp(String template, | 22 static String _createTemp(String template, |
| 23 _OSStatus status) native "Directory_CreateTemp"; | 23 _OSStatus status) native "Directory_CreateTemp"; |
| 24 static int _exists(String path) native "Directory_Exists"; | 24 static int _exists(String path) native "Directory_Exists"; |
| 25 static bool _create(String path) native "Directory_Create"; | 25 static bool _create(String path) native "Directory_Create"; |
| 26 static bool _delete(String path, bool recursive) native "Directory_Delete"; | 26 static bool _delete(String path, bool recursive) native "Directory_Delete"; |
| 27 static SendPort _newServicePort() native "Directory_NewServicePort"; | 27 static SendPort _newServicePort() native "Directory_NewServicePort"; |
| 28 | 28 |
| 29 void exists(void callback(bool exists)) { | 29 void exists(void callback(bool exists)) { |
| 30 if (_directoryService == null) { | 30 _ensureDirectoryService(); |
| 31 _directoryService = _newServicePort(); | |
| 32 } | |
| 33 List request = new List(2); | 31 List request = new List(2); |
| 34 request[0] = kExistsRequest; | 32 request[0] = kExistsRequest; |
| 35 request[1] = _path; | 33 request[1] = _path; |
| 36 _directoryService.call(request).receive((result, replyTo) { | 34 _directoryService.call(request).receive((result, replyTo) { |
| 37 if (result < 0) { | 35 if (result < 0) { |
| 38 if (_onError != null) { | 36 if (_onError != null) { |
| 39 _onError("Diretory exists test failed: $_path"); | 37 _onError("Diretory exists test failed: $_path"); |
| 40 } | 38 } |
| 41 } else { | 39 } else { |
| 42 callback(result == 1); | 40 callback(result == 1); |
| 43 } | 41 } |
| 44 }); | 42 }); |
| 45 } | 43 } |
| 46 | 44 |
| 47 bool existsSync() { | 45 bool existsSync() { |
| 48 int exists = _exists(_path); | 46 int exists = _exists(_path); |
| 49 if (exists < 0) { | 47 if (exists < 0) { |
| 50 throw new DirectoryException("Diretory exists test failed: $_path"); | 48 throw new DirectoryException("Diretory exists test failed: $_path"); |
| 51 } | 49 } |
| 52 return (exists == 1); | 50 return (exists == 1); |
| 53 } | 51 } |
| 54 | 52 |
| 55 void create(void callback()) { | 53 void create(void callback()) { |
| 56 if (_directoryService == null) { | 54 _ensureDirectoryService(); |
| 57 _directoryService = _newServicePort(); | |
| 58 } | |
| 59 List request = new List(2); | 55 List request = new List(2); |
| 60 request[0] = kCreateRequest; | 56 request[0] = kCreateRequest; |
| 61 request[1] = _path; | 57 request[1] = _path; |
| 62 _directoryService.call(request).receive((result, replyTo) { | 58 _directoryService.call(request).receive((result, replyTo) { |
| 63 if (result) { | 59 if (result) { |
| 64 callback(); | 60 callback(); |
| 65 } else if (_onError != null) { | 61 } else if (_onError != null) { |
| 66 _onError("Directory creation failed: $_path"); | 62 _onError("Directory creation failed: $_path"); |
| 67 } | 63 } |
| 68 }); | 64 }); |
| 69 } | 65 } |
| 70 | 66 |
| 71 void createSync() { | 67 void createSync() { |
| 72 if (!_create(_path)) { | 68 if (!_create(_path)) { |
| 73 throw new DirectoryException("Directory creation failed: $_path"); | 69 throw new DirectoryException("Directory creation failed: $_path"); |
| 74 } | 70 } |
| 75 } | 71 } |
| 76 | 72 |
| 77 void createTemp(void callback()) { | 73 void createTemp(void callback()) { |
| 78 if (_directoryService == null) { | 74 _ensureDirectoryService(); |
| 79 _directoryService = _newServicePort(); | |
| 80 } | |
| 81 List request = new List(2); | 75 List request = new List(2); |
| 82 request[0] = kCreateTempRequest; | 76 request[0] = kCreateTempRequest; |
| 83 request[1] = _path; | 77 request[1] = _path; |
| 84 _directoryService.call(request).receive((result, replyTo) { | 78 _directoryService.call(request).receive((result, replyTo) { |
| 85 if (result is !List) { | 79 if (result is !List) { |
| 86 _path = result; | 80 _path = result; |
| 87 callback(); | 81 callback(); |
| 88 } else if (_onError != null) { | 82 } else if (_onError != null) { |
| 89 _onError("Could not create temporary directory [$_path]: " + | 83 _onError("Could not create temporary directory [$_path]: " + |
| 90 "${result[1]}"); | 84 "${result[1]}"); |
| 91 } | 85 } |
| 92 }); | 86 }); |
| 93 } | 87 } |
| 94 | 88 |
| 95 void createTempSync() { | 89 void createTempSync() { |
| 96 var status = new _OSStatus(); | 90 var status = new _OSStatus(); |
| 97 var result = _createTemp(path, status); | 91 var result = _createTemp(path, status); |
| 98 if (result != null) { | 92 if (result != null) { |
| 99 _path = result; | 93 _path = result; |
| 100 } else { | 94 } else { |
| 101 throw new DirectoryException( | 95 throw new DirectoryException( |
| 102 "Could not create temporary directory [$_path]: " + | 96 "Could not create temporary directory [$_path]: " + |
| 103 "${status._errorMessage}", | 97 "${status._errorMessage}", |
| 104 status._errorCode); | 98 status._errorCode); |
| 105 } | 99 } |
| 106 } | 100 } |
| 107 | 101 |
| 108 void _deleteHelper(bool recursive, String errorMsg, void callback()) { | 102 void _deleteHelper(bool recursive, String errorMsg, void callback()) { |
| 109 if (_directoryService == null) { | 103 _ensureDirectoryService(); |
| 110 _directoryService = _newServicePort(); | |
| 111 } | |
| 112 List request = new List(3); | 104 List request = new List(3); |
| 113 request[0] = kDeleteRequest; | 105 request[0] = kDeleteRequest; |
| 114 request[1] = _path; | 106 request[1] = _path; |
| 115 request[2] = recursive; | 107 request[2] = recursive; |
| 116 _directoryService.call(request).receive((result, replyTo) { | 108 _directoryService.call(request).receive((result, replyTo) { |
| 117 if (result) { | 109 if (result) { |
| 118 callback(); | 110 callback(); |
| 119 } else if (_onError != null) { | 111 } else if (_onError != null) { |
| 120 _onError("${errorMsg}: $_path"); | 112 _onError("${errorMsg}: $_path"); |
| 121 } | 113 } |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 } | 184 } |
| 193 | 185 |
| 194 void set onDone(void onDone(bool completed)) { | 186 void set onDone(void onDone(bool completed)) { |
| 195 _onDone = onDone; | 187 _onDone = onDone; |
| 196 } | 188 } |
| 197 | 189 |
| 198 void set onError(void onError(String error)) { | 190 void set onError(void onError(String error)) { |
| 199 _onError = onError; | 191 _onError = onError; |
| 200 } | 192 } |
| 201 | 193 |
| 202 void _closePort(ReceivePort port) { | |
| 203 if (port !== null) { | |
| 204 port.close(); | |
| 205 } | |
| 206 } | |
| 207 | |
| 208 String get path() { return _path; } | 194 String get path() { return _path; } |
| 209 | 195 |
| 196 void _ensureDirectoryService() { |
| 197 if (_directoryService == null) { |
| 198 _directoryService = _newServicePort(); |
| 199 } |
| 200 } |
| 201 |
| 210 var _onDir; | 202 var _onDir; |
| 211 var _onFile; | 203 var _onFile; |
| 212 var _onDone; | 204 var _onDone; |
| 213 var _onError; | 205 var _onError; |
| 214 | 206 |
| 215 String _path; | 207 String _path; |
| 216 SendPort _directoryService; | 208 SendPort _directoryService; |
| 217 } | 209 } |
| OLD | NEW |