| 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 class _Directory implements Directory { | 6 class _Directory implements Directory { |
| 7 static final kCreateRequest = 0; | 7 static final kCreateRequest = 0; |
| 8 static final kDeleteRequest = 1; | 8 static final kDeleteRequest = 1; |
| 9 static final kExistsRequest = 2; | 9 static final kExistsRequest = 2; |
| 10 static final kCreateTempRequest = 3; | 10 static final kCreateTempRequest = 3; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 static _delete(String path, bool recursive) native "Directory_Delete"; | 24 static _delete(String path, bool recursive) native "Directory_Delete"; |
| 25 static SendPort _newServicePort() native "Directory_NewServicePort"; | 25 static SendPort _newServicePort() native "Directory_NewServicePort"; |
| 26 | 26 |
| 27 void exists(void callback(bool exists)) { | 27 void exists(void callback(bool exists)) { |
| 28 _ensureDirectoryService(); | 28 _ensureDirectoryService(); |
| 29 List request = new List(2); | 29 List request = new List(2); |
| 30 request[0] = kExistsRequest; | 30 request[0] = kExistsRequest; |
| 31 request[1] = _path; | 31 request[1] = _path; |
| 32 _directoryService.call(request).then((response) { | 32 _directoryService.call(request).then((response) { |
| 33 if (_isErrorResponse(response)) { | 33 if (_isErrorResponse(response)) { |
| 34 _reportError(response, "Exists failed"); | 34 _handleErrorResponse(response, "Exists failed"); |
| 35 } else { | 35 } else { |
| 36 callback(response == 1); | 36 callback(response == 1); |
| 37 } | 37 } |
| 38 }); | 38 }); |
| 39 } | 39 } |
| 40 | 40 |
| 41 bool existsSync() { | 41 bool existsSync() { |
| 42 if (_path is !String) { | 42 if (_path is !String) { |
| 43 throw new IllegalArgumentException(); | 43 throw new IllegalArgumentException(); |
| 44 } | 44 } |
| 45 var result = _exists(_path); | 45 var result = _exists(_path); |
| 46 if (result is OSError) { | 46 if (result is OSError) { |
| 47 throw new DirectoryIOException("Exists failed", _path, result); | 47 throw new DirectoryIOException("Exists failed", _path, result); |
| 48 } | 48 } |
| 49 return (result == 1); | 49 return (result == 1); |
| 50 } | 50 } |
| 51 | 51 |
| 52 void create(void callback()) { | 52 void create(void callback()) { |
| 53 _ensureDirectoryService(); | 53 _ensureDirectoryService(); |
| 54 List request = new List(2); | 54 List request = new List(2); |
| 55 request[0] = kCreateRequest; | 55 request[0] = kCreateRequest; |
| 56 request[1] = _path; | 56 request[1] = _path; |
| 57 _directoryService.call(request).then((response) { | 57 _directoryService.call(request).then((response) { |
| 58 if (_isErrorResponse(response)) { | 58 if (_isErrorResponse(response)) { |
| 59 _reportError(response, "Creation failed"); | 59 _handleErrorResponse(response, "Creation failed"); |
| 60 } else { | 60 } else { |
| 61 callback(); | 61 callback(); |
| 62 } | 62 } |
| 63 }); | 63 }); |
| 64 } | 64 } |
| 65 | 65 |
| 66 void createSync() { | 66 void createSync() { |
| 67 if (_path is !String) { | 67 if (_path is !String) { |
| 68 throw new IllegalArgumentException(); | 68 throw new IllegalArgumentException(); |
| 69 } | 69 } |
| 70 var result = _create(_path); | 70 var result = _create(_path); |
| 71 if (result is OSError) { | 71 if (result is OSError) { |
| 72 throw new DirectoryIOException("Creation failed", _path, result); | 72 throw new DirectoryIOException("Creation failed", _path, result); |
| 73 } | 73 } |
| 74 } | 74 } |
| 75 | 75 |
| 76 void createTemp(void callback()) { | 76 void createTemp(void callback()) { |
| 77 _ensureDirectoryService(); | 77 _ensureDirectoryService(); |
| 78 List request = new List(2); | 78 List request = new List(2); |
| 79 request[0] = kCreateTempRequest; | 79 request[0] = kCreateTempRequest; |
| 80 request[1] = _path; | 80 request[1] = _path; |
| 81 _directoryService.call(request).then((response) { | 81 _directoryService.call(request).then((response) { |
| 82 if (_isErrorResponse(response)) { | 82 if (_isErrorResponse(response)) { |
| 83 _reportError(response, "Creation of temporary directory failed"); | 83 _handleErrorResponse(response, "Creation of temporary directory failed")
; |
| 84 } else { | 84 } else { |
| 85 _path = response; | 85 _path = response; |
| 86 callback(); | 86 callback(); |
| 87 } | 87 } |
| 88 }); | 88 }); |
| 89 } | 89 } |
| 90 | 90 |
| 91 void createTempSync() { | 91 void createTempSync() { |
| 92 if (_path is !String) { | 92 if (_path is !String) { |
| 93 throw new IllegalArgumentException(); | 93 throw new IllegalArgumentException(); |
| 94 } | 94 } |
| 95 var result = _createTemp(path); | 95 var result = _createTemp(path); |
| 96 if (result is OSError) { | 96 if (result is OSError) { |
| 97 throw new DirectoryIOException("Creation of temporary directory failed", | 97 throw new DirectoryIOException("Creation of temporary directory failed", |
| 98 _path, | 98 _path, |
| 99 result); | 99 result); |
| 100 } | 100 } |
| 101 _path = result; | 101 _path = result; |
| 102 } | 102 } |
| 103 | 103 |
| 104 void _deleteHelper(bool recursive, String errorMsg, void callback()) { | 104 void _deleteHelper(bool recursive, String errorMsg, void callback()) { |
| 105 _ensureDirectoryService(); | 105 _ensureDirectoryService(); |
| 106 List request = new List(3); | 106 List request = new List(3); |
| 107 request[0] = kDeleteRequest; | 107 request[0] = kDeleteRequest; |
| 108 request[1] = _path; | 108 request[1] = _path; |
| 109 request[2] = recursive; | 109 request[2] = recursive; |
| 110 _directoryService.call(request).then((response) { | 110 _directoryService.call(request).then((response) { |
| 111 if (_isErrorResponse(response)) { | 111 if (_isErrorResponse(response)) { |
| 112 _reportError(response, errorMsg); | 112 _handleErrorResponse(response, errorMsg); |
| 113 } else { | 113 } else { |
| 114 callback(); | 114 callback(); |
| 115 } | 115 } |
| 116 }); | 116 }); |
| 117 } | 117 } |
| 118 | 118 |
| 119 void delete(void callback()) { | 119 void delete(void callback()) { |
| 120 _deleteHelper(false, "Deletion failed", callback); | 120 _deleteHelper(false, "Deletion failed", callback); |
| 121 } | 121 } |
| 122 | 122 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 request[0] = kListRequest; | 159 request[0] = kListRequest; |
| 160 request[1] = _path; | 160 request[1] = _path; |
| 161 request[2] = recursive; | 161 request[2] = recursive; |
| 162 ReceivePort responsePort = new ReceivePort(); | 162 ReceivePort responsePort = new ReceivePort(); |
| 163 // Use a separate directory service port for each listing as | 163 // Use a separate directory service port for each listing as |
| 164 // listing operations on the same directory can run in parallel. | 164 // listing operations on the same directory can run in parallel. |
| 165 _newServicePort().send(request, responsePort.toSendPort()); | 165 _newServicePort().send(request, responsePort.toSendPort()); |
| 166 responsePort.receive((message, replyTo) { | 166 responsePort.receive((message, replyTo) { |
| 167 if (message is !List || message[kResponseType] is !int) { | 167 if (message is !List || message[kResponseType] is !int) { |
| 168 responsePort.close(); | 168 responsePort.close(); |
| 169 if (_onError != null) { | 169 _reportError(new DirectoryIOException("Internal error")); |
| 170 _onError(new DirectoryIOException("Internal error")); | |
| 171 } | |
| 172 return; | 170 return; |
| 173 } | 171 } |
| 174 switch (message[kResponseType]) { | 172 switch (message[kResponseType]) { |
| 175 case kListDirectory: | 173 case kListDirectory: |
| 176 if (_onDir != null) _onDir(message[kResponsePath]); | 174 if (_onDir != null) _onDir(message[kResponsePath]); |
| 177 break; | 175 break; |
| 178 case kListFile: | 176 case kListFile: |
| 179 if (_onFile != null) _onFile(message[kResponsePath]); | 177 if (_onFile != null) _onFile(message[kResponsePath]); |
| 180 break; | 178 break; |
| 181 case kListError: | 179 case kListError: |
| 182 if (_onError != null) { | 180 var errorType = |
| 183 var errorType = | 181 message[kResponseError][_FileUtils.kErrorResponseErrorType]; |
| 184 message[kResponseError][_FileUtils.kErrorResponseErrorType]; | 182 if (errorType == _FileUtils.kIllegalArgumentResponse) { |
| 185 if (errorType == _FileUtils.kIllegalArgumentResponse) { | 183 _reportError(new IllegalArgumentException()); |
| 186 _onError(new IllegalArgumentException()); | 184 } else if (errorType == _FileUtils.kOSErrorResponse) { |
| 187 } else if (errorType == _FileUtils.kOSErrorResponse) { | 185 var err = new OSError( |
| 188 var err = new OSError( | 186 message[kResponseError][_FileUtils.kOSErrorResponseMessage], |
| 189 message[kResponseError][_FileUtils.kOSErrorResponseMessage], | 187 message[kResponseError][_FileUtils.kOSErrorResponseErrorCode]); |
| 190 message[kResponseError][_FileUtils.kOSErrorResponseErrorCode])
; | 188 _reportError(new DirectoryIOException("Directory listing failed", |
| 191 _onError(new DirectoryIOException("Directory listing failed", | 189 message[kResponsePath], |
| 192 message[kResponsePath], | 190 err)); |
| 193 err)); | 191 } else { |
| 194 } else { | 192 _reportError(new DirectoryIOException("Internal error")); |
| 195 _onError(new DirectoryIOException("Internal error")); | |
| 196 } | |
| 197 } | 193 } |
| 198 break; | 194 break; |
| 199 case kListDone: | 195 case kListDone: |
| 200 responsePort.close(); | 196 responsePort.close(); |
| 201 if (_onDone != null) _onDone(message[kResponseComplete]); | 197 if (_onDone != null) _onDone(message[kResponseComplete]); |
| 202 break; | 198 break; |
| 203 } | 199 } |
| 204 }); | 200 }); |
| 205 } | 201 } |
| 206 | 202 |
| 207 void set onDir(void onDir(String dir)) { | 203 void set onDir(void onDir(String dir)) { |
| 208 _onDir = onDir; | 204 _onDir = onDir; |
| 209 } | 205 } |
| 210 | 206 |
| 211 void set onFile(void onFile(String file)) { | 207 void set onFile(void onFile(String file)) { |
| 212 _onFile = onFile; | 208 _onFile = onFile; |
| 213 } | 209 } |
| 214 | 210 |
| 215 void set onDone(void onDone(bool completed)) { | 211 void set onDone(void onDone(bool completed)) { |
| 216 _onDone = onDone; | 212 _onDone = onDone; |
| 217 } | 213 } |
| 218 | 214 |
| 219 void set onError(void onError(Exception e)) { | 215 void set onError(void onError(e)) { |
| 220 _onError = onError; | 216 _onError = onError; |
| 221 } | 217 } |
| 222 | 218 |
| 223 String get path() { return _path; } | 219 String get path() { return _path; } |
| 224 | 220 |
| 225 void _ensureDirectoryService() { | 221 void _ensureDirectoryService() { |
| 226 if (_directoryService == null) { | 222 if (_directoryService == null) { |
| 227 _directoryService = _newServicePort(); | 223 _directoryService = _newServicePort(); |
| 228 } | 224 } |
| 229 } | 225 } |
| 230 | 226 |
| 227 void _reportError(e) { |
| 228 if (_onError != null) { |
| 229 _onError(e); |
| 230 } else { |
| 231 throw e; |
| 232 } |
| 233 } |
| 234 |
| 231 bool _isErrorResponse(response) { | 235 bool _isErrorResponse(response) { |
| 232 return response is List && response[0] != _FileUtils.kSuccessResponse; | 236 return response is List && response[0] != _FileUtils.kSuccessResponse; |
| 233 } | 237 } |
| 234 | 238 |
| 235 bool _reportError(response, String message) { | 239 void _handleErrorResponse(response, String message) { |
| 236 assert(_isErrorResponse(response)); | 240 assert(_isErrorResponse(response)); |
| 237 if (_onError != null) { | 241 switch (response[_FileUtils.kErrorResponseErrorType]) { |
| 238 switch (response[_FileUtils.kErrorResponseErrorType]) { | 242 case _FileUtils.kIllegalArgumentResponse: |
| 239 case _FileUtils.kIllegalArgumentResponse: | 243 _reportError(new IllegalArgumentException()); |
| 240 _onError(new IllegalArgumentException()); | 244 break; |
| 241 break; | 245 case _FileUtils.kOSErrorResponse: |
| 242 case _FileUtils.kOSErrorResponse: | 246 var err = new OSError(response[_FileUtils.kOSErrorResponseMessage], |
| 243 var err = new OSError(response[_FileUtils.kOSErrorResponseMessage], | 247 response[_FileUtils.kOSErrorResponseErrorCode]); |
| 244 response[_FileUtils.kOSErrorResponseErrorCode]); | 248 _reportError(new DirectoryIOException(message, _path, err)); |
| 245 _onError(new DirectoryIOException(message, _path, err)); | 249 break; |
| 246 break; | 250 default: |
| 247 default: | 251 _reportError(new Exception("Unknown error")); |
| 248 _onError(new Exception("Unknown error")); | 252 break; |
| 249 break; | |
| 250 } | |
| 251 } | 253 } |
| 252 } | 254 } |
| 253 | 255 |
| 254 Function _onDir; | 256 Function _onDir; |
| 255 Function _onFile; | 257 Function _onFile; |
| 256 Function _onDone; | 258 Function _onDone; |
| 257 Function _onError; | 259 Function _onError; |
| 258 | 260 |
| 259 String _path; | 261 String _path; |
| 260 SendPort _directoryService; | 262 SendPort _directoryService; |
| 261 } | 263 } |
| OLD | NEW |