Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(23)

Side by Side Diff: sdk/lib/io/directory_impl.dart

Issue 16123036: Clean up dart:io exceptions. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sdk/lib/io/directory.dart ('k') | sdk/lib/io/file.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 part of dart.io; 5 part of dart.io;
6 6
7 class _Directory implements Directory { 7 class _Directory implements Directory {
8 static const CREATE_REQUEST = 0; 8 static const CREATE_REQUEST = 0;
9 static const DELETE_REQUEST = 1; 9 static const DELETE_REQUEST = 1;
10 static const EXISTS_REQUEST = 2; 10 static const EXISTS_REQUEST = 2;
(...skipping 14 matching lines...) Expand all
25 external static List _list(String path, bool recursive, bool followLinks); 25 external static List _list(String path, bool recursive, bool followLinks);
26 external static SendPort _newServicePort(); 26 external static SendPort _newServicePort();
27 27
28 static Directory get current => new _Directory(_current()); 28 static Directory get current => new _Directory(_current());
29 29
30 static void set current(path) { 30 static void set current(path) {
31 if (path is Directory) path = path.path; 31 if (path is Directory) path = path.path;
32 var result = _setCurrent(path); 32 var result = _setCurrent(path);
33 if (result is ArgumentError) throw result; 33 if (result is ArgumentError) throw result;
34 if (result is OSError) { 34 if (result is OSError) {
35 throw new DirectoryIOException( 35 throw new DirectoryException(
36 "Setting current working directory failed", path, result); 36 "Setting current working directory failed", path, result);
37 } 37 }
38 } 38 }
39 39
40 Future<bool> exists() { 40 Future<bool> exists() {
41 _ensureDirectoryService(); 41 _ensureDirectoryService();
42 List request = new List(2); 42 List request = new List(2);
43 request[0] = EXISTS_REQUEST; 43 request[0] = EXISTS_REQUEST;
44 request[1] = _path; 44 request[1] = _path;
45 return _directoryService.call(request).then((response) { 45 return _directoryService.call(request).then((response) {
46 if (_isErrorResponse(response)) { 46 if (_isErrorResponse(response)) {
47 throw _exceptionOrErrorFromResponse(response, "Exists failed"); 47 throw _exceptionOrErrorFromResponse(response, "Exists failed");
48 } 48 }
49 return response == 1; 49 return response == 1;
50 }); 50 });
51 } 51 }
52 52
53 bool existsSync() { 53 bool existsSync() {
54 if (_path is !String) { 54 if (_path is !String) {
55 throw new ArgumentError(); 55 throw new ArgumentError();
56 } 56 }
57 var result = _exists(_path); 57 var result = _exists(_path);
58 if (result is OSError) { 58 if (result is OSError) {
59 throw new DirectoryIOException("Exists failed", _path, result); 59 throw new DirectoryException("Exists failed", _path, result);
60 } 60 }
61 return (result == 1); 61 return (result == 1);
62 } 62 }
63 63
64 Future<FileStat> stat() => FileStat.stat(path); 64 Future<FileStat> stat() => FileStat.stat(path);
65 65
66 FileStat statSync() => FileStat.statSync(path); 66 FileStat statSync() => FileStat.statSync(path);
67 67
68 // Compute the index of the first directory in the list that exists. If 68 // Compute the index of the first directory in the list that exists. If
69 // none of the directories exist dirsToCreate.length is returned. 69 // none of the directories exist dirsToCreate.length is returned.
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 } 148 }
149 } 149 }
150 150
151 void createSync({recursive: false}) { 151 void createSync({recursive: false}) {
152 if (_path is !String) { 152 if (_path is !String) {
153 throw new ArgumentError(); 153 throw new ArgumentError();
154 } 154 }
155 if (recursive) return createRecursivelySync(); 155 if (recursive) return createRecursivelySync();
156 var result = _create(_path); 156 var result = _create(_path);
157 if (result is OSError) { 157 if (result is OSError) {
158 throw new DirectoryIOException("Creation failed", _path, result); 158 throw new DirectoryException("Creation failed", _path, result);
159 } 159 }
160 } 160 }
161 161
162 Future<Directory> createTemp() { 162 Future<Directory> createTemp() {
163 _ensureDirectoryService(); 163 _ensureDirectoryService();
164 List request = new List(2); 164 List request = new List(2);
165 request[0] = CREATE_TEMP_REQUEST; 165 request[0] = CREATE_TEMP_REQUEST;
166 request[1] = _path; 166 request[1] = _path;
167 return _directoryService.call(request).then((response) { 167 return _directoryService.call(request).then((response) {
168 if (_isErrorResponse(response)) { 168 if (_isErrorResponse(response)) {
169 throw _exceptionOrErrorFromResponse(response, 169 throw _exceptionOrErrorFromResponse(response,
170 "Creation of temporary directory failed"); 170 "Creation of temporary directory failed");
171 } 171 }
172 return new Directory(response); 172 return new Directory(response);
173 }); 173 });
174 } 174 }
175 175
176 Directory createTempSync() { 176 Directory createTempSync() {
177 if (_path is !String) { 177 if (_path is !String) {
178 throw new ArgumentError(); 178 throw new ArgumentError();
179 } 179 }
180 var result = _createTemp(path); 180 var result = _createTemp(path);
181 if (result is OSError) { 181 if (result is OSError) {
182 throw new DirectoryIOException("Creation of temporary directory failed", 182 throw new DirectoryException("Creation of temporary directory failed",
183 _path, 183 _path,
184 result); 184 result);
185 } 185 }
186 return new Directory(result); 186 return new Directory(result);
187 } 187 }
188 188
189 Future<Directory> _deleteHelper(bool recursive, String errorMsg) { 189 Future<Directory> _deleteHelper(bool recursive, String errorMsg) {
190 } 190 }
191 191
192 Future<Directory> delete({recursive: false}) { 192 Future<Directory> delete({recursive: false}) {
193 _ensureDirectoryService(); 193 _ensureDirectoryService();
194 List request = new List(3); 194 List request = new List(3);
195 request[0] = DELETE_REQUEST; 195 request[0] = DELETE_REQUEST;
196 request[1] = _path; 196 request[1] = _path;
197 request[2] = recursive; 197 request[2] = recursive;
198 return _directoryService.call(request).then((response) { 198 return _directoryService.call(request).then((response) {
199 if (_isErrorResponse(response)) { 199 if (_isErrorResponse(response)) {
200 throw _exceptionOrErrorFromResponse(response, "Deletion failed"); 200 throw _exceptionOrErrorFromResponse(response, "Deletion failed");
201 } 201 }
202 return this; 202 return this;
203 }); 203 });
204 } 204 }
205 205
206 void deleteSync({recursive: false}) { 206 void deleteSync({recursive: false}) {
207 if (_path is !String) { 207 if (_path is !String) {
208 throw new ArgumentError(); 208 throw new ArgumentError();
209 } 209 }
210 var result = _delete(_path, recursive); 210 var result = _delete(_path, recursive);
211 if (result is OSError) { 211 if (result is OSError) {
212 throw new DirectoryIOException("Deletion failed", _path, result); 212 throw new DirectoryException("Deletion failed", _path, result);
213 } 213 }
214 } 214 }
215 215
216 Future<Directory> rename(String newPath) { 216 Future<Directory> rename(String newPath) {
217 _ensureDirectoryService(); 217 _ensureDirectoryService();
218 List request = new List(3); 218 List request = new List(3);
219 request[0] = RENAME_REQUEST; 219 request[0] = RENAME_REQUEST;
220 request[1] = _path; 220 request[1] = _path;
221 request[2] = newPath; 221 request[2] = newPath;
222 return _directoryService.call(request).then((response) { 222 return _directoryService.call(request).then((response) {
223 if (_isErrorResponse(response)) { 223 if (_isErrorResponse(response)) {
224 throw _exceptionOrErrorFromResponse(response, "Rename failed"); 224 throw _exceptionOrErrorFromResponse(response, "Rename failed");
225 } 225 }
226 return new Directory(newPath); 226 return new Directory(newPath);
227 }); 227 });
228 } 228 }
229 229
230 Directory renameSync(String newPath) { 230 Directory renameSync(String newPath) {
231 if (_path is !String || newPath is !String) { 231 if (_path is !String || newPath is !String) {
232 throw new ArgumentError(); 232 throw new ArgumentError();
233 } 233 }
234 var result = _rename(_path, newPath); 234 var result = _rename(_path, newPath);
235 if (result is OSError) { 235 if (result is OSError) {
236 throw new DirectoryIOException("Rename failed", _path, result); 236 throw new DirectoryException("Rename failed", _path, result);
237 } 237 }
238 return new Directory(newPath); 238 return new Directory(newPath);
239 } 239 }
240 240
241 Stream<FileSystemEntity> list({bool recursive: false, 241 Stream<FileSystemEntity> list({bool recursive: false,
242 bool followLinks: true}) { 242 bool followLinks: true}) {
243 const int LIST_FILE = 0; 243 const int LIST_FILE = 0;
244 const int LIST_DIRECTORY = 1; 244 const int LIST_DIRECTORY = 1;
245 const int LIST_LINK = 2; 245 const int LIST_LINK = 2;
246 const int LIST_ERROR = 3; 246 const int LIST_ERROR = 3;
247 const int LIST_DONE = 4; 247 const int LIST_DONE = 4;
248 248
249 const int RESPONSE_TYPE = 0; 249 const int RESPONSE_TYPE = 0;
250 const int RESPONSE_PATH = 1; 250 const int RESPONSE_PATH = 1;
251 const int RESPONSE_COMPLETE = 1; 251 const int RESPONSE_COMPLETE = 1;
252 const int RESPONSE_ERROR = 2; 252 const int RESPONSE_ERROR = 2;
253 253
254 var controller = new StreamController<FileSystemEntity>(sync: true); 254 var controller = new StreamController<FileSystemEntity>(sync: true);
255 255
256 List request = [ _Directory.LIST_REQUEST, path, recursive, followLinks ]; 256 List request = [ _Directory.LIST_REQUEST, path, recursive, followLinks ];
257 ReceivePort responsePort = new ReceivePort(); 257 ReceivePort responsePort = new ReceivePort();
258 // Use a separate directory service port for each listing as 258 // Use a separate directory service port for each listing as
259 // listing operations on the same directory can run in parallel. 259 // listing operations on the same directory can run in parallel.
260 _Directory._newServicePort().send(request, responsePort.toSendPort()); 260 _Directory._newServicePort().send(request, responsePort.toSendPort());
261 responsePort.receive((message, replyTo) { 261 responsePort.receive((message, replyTo) {
262 if (message is !List || message[RESPONSE_TYPE] is !int) { 262 if (message is !List || message[RESPONSE_TYPE] is !int) {
263 responsePort.close(); 263 responsePort.close();
264 controller.addError(new DirectoryIOException("Internal error")); 264 controller.addError(new DirectoryException("Internal error"));
265 return; 265 return;
266 } 266 }
267 switch (message[RESPONSE_TYPE]) { 267 switch (message[RESPONSE_TYPE]) {
268 case LIST_FILE: 268 case LIST_FILE:
269 controller.add(new File(message[RESPONSE_PATH])); 269 controller.add(new File(message[RESPONSE_PATH]));
270 break; 270 break;
271 case LIST_DIRECTORY: 271 case LIST_DIRECTORY:
272 controller.add(new Directory(message[RESPONSE_PATH])); 272 controller.add(new Directory(message[RESPONSE_PATH]));
273 break; 273 break;
274 case LIST_LINK: 274 case LIST_LINK:
275 controller.add(new Link(message[RESPONSE_PATH])); 275 controller.add(new Link(message[RESPONSE_PATH]));
276 break; 276 break;
277 case LIST_ERROR: 277 case LIST_ERROR:
278 var errorType = 278 var errorType =
279 message[RESPONSE_ERROR][_ERROR_RESPONSE_ERROR_TYPE]; 279 message[RESPONSE_ERROR][_ERROR_RESPONSE_ERROR_TYPE];
280 if (errorType == _ILLEGAL_ARGUMENT_RESPONSE) { 280 if (errorType == _ILLEGAL_ARGUMENT_RESPONSE) {
281 controller.addError(new ArgumentError()); 281 controller.addError(new ArgumentError());
282 } else if (errorType == _OSERROR_RESPONSE) { 282 } else if (errorType == _OSERROR_RESPONSE) {
283 var responseError = message[RESPONSE_ERROR]; 283 var responseError = message[RESPONSE_ERROR];
284 var err = new OSError( 284 var err = new OSError(
285 responseError[_OSERROR_RESPONSE_MESSAGE], 285 responseError[_OSERROR_RESPONSE_MESSAGE],
286 responseError[_OSERROR_RESPONSE_ERROR_CODE]); 286 responseError[_OSERROR_RESPONSE_ERROR_CODE]);
287 var errorPath = message[RESPONSE_PATH]; 287 var errorPath = message[RESPONSE_PATH];
288 if (errorPath == null) errorPath = path; 288 if (errorPath == null) errorPath = path;
289 controller.addError( 289 controller.addError(
290 new DirectoryIOException("Directory listing failed", 290 new DirectoryException("Directory listing failed",
291 errorPath, 291 errorPath,
292 err)); 292 err));
293 } else { 293 } else {
294 controller.addError(new DirectoryIOException("Internal error")); 294 controller.addError(new DirectoryException("Internal error"));
295 } 295 }
296 break; 296 break;
297 case LIST_DONE: 297 case LIST_DONE:
298 responsePort.close(); 298 responsePort.close();
299 controller.close(); 299 controller.close();
300 break; 300 break;
301 } 301 }
302 }); 302 });
303 303
304 return controller.stream; 304 return controller.stream;
(...skipping 15 matching lines...) Expand all
320 } 320 }
321 321
322 _exceptionOrErrorFromResponse(response, String message) { 322 _exceptionOrErrorFromResponse(response, String message) {
323 assert(_isErrorResponse(response)); 323 assert(_isErrorResponse(response));
324 switch (response[_ERROR_RESPONSE_ERROR_TYPE]) { 324 switch (response[_ERROR_RESPONSE_ERROR_TYPE]) {
325 case _ILLEGAL_ARGUMENT_RESPONSE: 325 case _ILLEGAL_ARGUMENT_RESPONSE:
326 return new ArgumentError(); 326 return new ArgumentError();
327 case _OSERROR_RESPONSE: 327 case _OSERROR_RESPONSE:
328 var err = new OSError(response[_OSERROR_RESPONSE_MESSAGE], 328 var err = new OSError(response[_OSERROR_RESPONSE_MESSAGE],
329 response[_OSERROR_RESPONSE_ERROR_CODE]); 329 response[_OSERROR_RESPONSE_ERROR_CODE]);
330 return new DirectoryIOException(message, _path, err); 330 return new DirectoryException(message, _path, err);
331 default: 331 default:
332 return new Exception("Unknown error"); 332 return new Exception("Unknown error");
333 } 333 }
334 } 334 }
335 335
336 void _ensureDirectoryService() { 336 void _ensureDirectoryService() {
337 if (_directoryService == null) { 337 if (_directoryService == null) {
338 _directoryService = _newServicePort(); 338 _directoryService = _newServicePort();
339 } 339 }
340 } 340 }
341 341
342 final String _path; 342 final String _path;
343 SendPort _directoryService; 343 SendPort _directoryService;
344 } 344 }
OLDNEW
« no previous file with comments | « sdk/lib/io/directory.dart ('k') | sdk/lib/io/file.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698