| 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 const CREATE_REQUEST = 0; | 7 static const CREATE_REQUEST = 0; |
| 8 static const DELETE_REQUEST = 1; | 8 static const DELETE_REQUEST = 1; |
| 9 static const EXISTS_REQUEST = 2; | 9 static const EXISTS_REQUEST = 2; |
| 10 static const CREATE_TEMP_REQUEST = 3; | 10 static const CREATE_TEMP_REQUEST = 3; |
| 11 static const LIST_REQUEST = 4; | 11 static const LIST_REQUEST = 4; |
| 12 static const RENAME_REQUEST = 5; | 12 static const RENAME_REQUEST = 5; |
| 13 | 13 |
| 14 static const SUCCESS_RESPONSE = 0; | 14 static const SUCCESS_RESPONSE = 0; |
| 15 static const ILLEGAL_ARGUMENT_RESPONSE = 1; | 15 static const ILLEGAL_ARGUMENT_RESPONSE = 1; |
| 16 static const OSERROR_RESPONSE = 2; | 16 static const OSERROR_RESPONSE = 2; |
| 17 | 17 |
| 18 _Directory(String this._path); | 18 _Directory(String this._path); |
| 19 _Directory.fromPath(Path path) : this(path.toNativePath()); | 19 _Directory.fromPath(Path path) : this(path.toNativePath()); |
| 20 _Directory.current() : this(_current()); | 20 _Directory.current() : this(_current()); |
| 21 | 21 |
| 22 static String _current() native "Directory_Current"; | 22 external static String _current(); |
| 23 static _createTemp(String template) native "Directory_CreateTemp"; | 23 external static _createTemp(String template); |
| 24 static int _exists(String path) native "Directory_Exists"; | 24 external static int _exists(String path); |
| 25 static _create(String path) native "Directory_Create"; | 25 external static _create(String path); |
| 26 static _delete(String path, bool recursive) native "Directory_Delete"; | 26 external static _delete(String path, bool recursive); |
| 27 static _rename(String path, String newPath) native "Directory_Rename"; | 27 external static _rename(String path, String newPath); |
| 28 static SendPort _newServicePort() native "Directory_NewServicePort"; | 28 external static SendPort _newServicePort(); |
| 29 | 29 |
| 30 Future<bool> exists() { | 30 Future<bool> exists() { |
| 31 _ensureDirectoryService(); | 31 _ensureDirectoryService(); |
| 32 List request = new List(2); | 32 List request = new List(2); |
| 33 request[0] = EXISTS_REQUEST; | 33 request[0] = EXISTS_REQUEST; |
| 34 request[1] = _path; | 34 request[1] = _path; |
| 35 return _directoryService.call(request).transform((response) { | 35 return _directoryService.call(request).transform((response) { |
| 36 if (_isErrorResponse(response)) { | 36 if (_isErrorResponse(response)) { |
| 37 throw _exceptionFromResponse(response, "Exists failed"); | 37 throw _exceptionOrErrorFromResponse(response, "Exists failed"); |
| 38 } | 38 } |
| 39 return response == 1; | 39 return response == 1; |
| 40 }); | 40 }); |
| 41 } | 41 } |
| 42 | 42 |
| 43 bool existsSync() { | 43 bool existsSync() { |
| 44 if (_path is !String) { | 44 if (_path is !String) { |
| 45 throw new ArgumentError(); | 45 throw new ArgumentError(); |
| 46 } | 46 } |
| 47 var result = _exists(_path); | 47 var result = _exists(_path); |
| 48 if (result is OSError) { | 48 if (result is OSError) { |
| 49 throw new DirectoryIOException("Exists failed", _path, result); | 49 throw new DirectoryIOException("Exists failed", _path, result); |
| 50 } | 50 } |
| 51 return (result == 1); | 51 return (result == 1); |
| 52 } | 52 } |
| 53 | 53 |
| 54 Future<Directory> create() { | 54 Future<Directory> create() { |
| 55 _ensureDirectoryService(); | 55 _ensureDirectoryService(); |
| 56 List request = new List(2); | 56 List request = new List(2); |
| 57 request[0] = CREATE_REQUEST; | 57 request[0] = CREATE_REQUEST; |
| 58 request[1] = _path; | 58 request[1] = _path; |
| 59 return _directoryService.call(request).transform((response) { | 59 return _directoryService.call(request).transform((response) { |
| 60 if (_isErrorResponse(response)) { | 60 if (_isErrorResponse(response)) { |
| 61 throw _exceptionFromResponse(response, "Creation failed"); | 61 throw _exceptionOrErrorFromResponse(response, "Creation failed"); |
| 62 } | 62 } |
| 63 return this; | 63 return this; |
| 64 }); | 64 }); |
| 65 } | 65 } |
| 66 | 66 |
| 67 void createSync() { | 67 void createSync() { |
| 68 if (_path is !String) { | 68 if (_path is !String) { |
| 69 throw new ArgumentError(); | 69 throw new ArgumentError(); |
| 70 } | 70 } |
| 71 var result = _create(_path); | 71 var result = _create(_path); |
| 72 if (result is OSError) { | 72 if (result is OSError) { |
| 73 throw new DirectoryIOException("Creation failed", _path, result); | 73 throw new DirectoryIOException("Creation failed", _path, result); |
| 74 } | 74 } |
| 75 } | 75 } |
| 76 | 76 |
| 77 Future<Directory> createTemp() { | 77 Future<Directory> createTemp() { |
| 78 _ensureDirectoryService(); | 78 _ensureDirectoryService(); |
| 79 List request = new List(2); | 79 List request = new List(2); |
| 80 request[0] = CREATE_TEMP_REQUEST; | 80 request[0] = CREATE_TEMP_REQUEST; |
| 81 request[1] = _path; | 81 request[1] = _path; |
| 82 return _directoryService.call(request).transform((response) { | 82 return _directoryService.call(request).transform((response) { |
| 83 if (_isErrorResponse(response)) { | 83 if (_isErrorResponse(response)) { |
| 84 throw _exceptionFromResponse(response, | 84 throw _exceptionOrErrorFromResponse(response, |
| 85 "Creation of temporary directory failed"); | 85 "Creation of temporary directory failed"); |
| 86 } | 86 } |
| 87 return new Directory(response); | 87 return new Directory(response); |
| 88 }); | 88 }); |
| 89 } | 89 } |
| 90 | 90 |
| 91 Directory createTempSync() { | 91 Directory createTempSync() { |
| 92 if (_path is !String) { | 92 if (_path is !String) { |
| 93 throw new ArgumentError(); | 93 throw new ArgumentError(); |
| 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 return new Directory(result); | 101 return new Directory(result); |
| 102 } | 102 } |
| 103 | 103 |
| 104 Future<Directory> _deleteHelper(bool recursive, String errorMsg) { | 104 Future<Directory> _deleteHelper(bool recursive, String errorMsg) { |
| 105 _ensureDirectoryService(); | 105 _ensureDirectoryService(); |
| 106 List request = new List(3); | 106 List request = new List(3); |
| 107 request[0] = DELETE_REQUEST; | 107 request[0] = DELETE_REQUEST; |
| 108 request[1] = _path; | 108 request[1] = _path; |
| 109 request[2] = recursive; | 109 request[2] = recursive; |
| 110 return _directoryService.call(request).transform((response) { | 110 return _directoryService.call(request).transform((response) { |
| 111 if (_isErrorResponse(response)) { | 111 if (_isErrorResponse(response)) { |
| 112 throw _exceptionFromResponse(response, errorMsg); | 112 throw _exceptionOrErrorFromResponse(response, errorMsg); |
| 113 } | 113 } |
| 114 return this; | 114 return this; |
| 115 }); | 115 }); |
| 116 return completer.future; | |
| 117 } | 116 } |
| 118 | 117 |
| 119 Future<Directory> delete() { | 118 Future<Directory> delete() { |
| 120 return _deleteHelper(false, "Deletion failed"); | 119 return _deleteHelper(false, "Deletion failed"); |
| 121 } | 120 } |
| 122 | 121 |
| 123 void deleteSync() { | 122 void deleteSync() { |
| 124 if (_path is !String) { | 123 if (_path is !String) { |
| 125 throw new ArgumentError(); | 124 throw new ArgumentError(); |
| 126 } | 125 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 145 } | 144 } |
| 146 | 145 |
| 147 Future<Directory> rename(String newPath) { | 146 Future<Directory> rename(String newPath) { |
| 148 _ensureDirectoryService(); | 147 _ensureDirectoryService(); |
| 149 List request = new List(3); | 148 List request = new List(3); |
| 150 request[0] = RENAME_REQUEST; | 149 request[0] = RENAME_REQUEST; |
| 151 request[1] = _path; | 150 request[1] = _path; |
| 152 request[2] = newPath; | 151 request[2] = newPath; |
| 153 return _directoryService.call(request).transform((response) { | 152 return _directoryService.call(request).transform((response) { |
| 154 if (_isErrorResponse(response)) { | 153 if (_isErrorResponse(response)) { |
| 155 throw _exceptionFromResponse(response, "Rename failed"); | 154 throw _exceptionOrErrorFromResponse(response, "Rename failed"); |
| 156 } | 155 } |
| 157 return new Directory(newPath); | 156 return new Directory(newPath); |
| 158 }); | 157 }); |
| 159 } | 158 } |
| 160 | 159 |
| 161 Directory renameSync(String newPath) { | 160 Directory renameSync(String newPath) { |
| 162 if (_path is !String || newPath is !String) { | 161 if (_path is !String || newPath is !String) { |
| 163 throw new ArgumentError(); | 162 throw new ArgumentError(); |
| 164 } | 163 } |
| 165 var result = _rename(_path, newPath); | 164 var result = _rename(_path, newPath); |
| 166 if (result is OSError) { | 165 if (result is OSError) { |
| 167 throw new DirectoryIOException("Rename failed", _path, result); | 166 throw new DirectoryIOException("Rename failed", _path, result); |
| 168 } | 167 } |
| 169 return new Directory(newPath); | 168 return new Directory(newPath); |
| 170 } | 169 } |
| 171 | 170 |
| 172 DirectoryLister list({bool recursive: false}) { | 171 DirectoryLister list({bool recursive: false}) { |
| 173 return new _DirectoryLister(_path, recursive); | 172 return new _DirectoryLister(_path, recursive); |
| 174 } | 173 } |
| 175 | 174 |
| 176 String get path { return _path; } | 175 String get path { return _path; } |
| 177 | 176 |
| 178 bool _isErrorResponse(response) { | 177 bool _isErrorResponse(response) { |
| 179 return response is List && response[0] != _SUCCESS_RESPONSE; | 178 return response is List && response[0] != _SUCCESS_RESPONSE; |
| 180 } | 179 } |
| 181 | 180 |
| 182 Exception _exceptionFromResponse(response, String message) { | 181 _exceptionOrErrorFromResponse(response, String message) { |
| 183 assert(_isErrorResponse(response)); | 182 assert(_isErrorResponse(response)); |
| 184 switch (response[_ERROR_RESPONSE_ERROR_TYPE]) { | 183 switch (response[_ERROR_RESPONSE_ERROR_TYPE]) { |
| 185 case _ILLEGAL_ARGUMENT_RESPONSE: | 184 case _ILLEGAL_ARGUMENT_RESPONSE: |
| 186 return new ArgumentError(); | 185 return new ArgumentError(); |
| 187 case _OSERROR_RESPONSE: | 186 case _OSERROR_RESPONSE: |
| 188 var err = new OSError(response[_OSERROR_RESPONSE_MESSAGE], | 187 var err = new OSError(response[_OSERROR_RESPONSE_MESSAGE], |
| 189 response[_OSERROR_RESPONSE_ERROR_CODE]); | 188 response[_OSERROR_RESPONSE_ERROR_CODE]); |
| 190 return new DirectoryIOException(message, _path, err); | 189 return new DirectoryIOException(message, _path, err); |
| 191 default: | 190 default: |
| 192 return new Exception("Unknown error"); | 191 return new Exception("Unknown error"); |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 } else { | 284 } else { |
| 286 throw e; | 285 throw e; |
| 287 } | 286 } |
| 288 } | 287 } |
| 289 | 288 |
| 290 Function _onDir; | 289 Function _onDir; |
| 291 Function _onFile; | 290 Function _onFile; |
| 292 Function _onDone; | 291 Function _onDone; |
| 293 Function _onError; | 292 Function _onError; |
| 294 } | 293 } |
| OLD | NEW |