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 part of dart.io; | 5 part of dart.io; |
6 | 6 |
7 class _Directory extends FileSystemEntity implements Directory { | 7 class _Directory extends FileSystemEntity implements Directory { |
8 final String path; | 8 final String path; |
9 | 9 |
10 _Directory(String this.path) { | 10 _Directory(String this.path) { |
11 if (path is! String) { | 11 if (path is! String) { |
12 throw new ArgumentError('${Error.safeToString(path)} ' | 12 throw new ArgumentError('${Error.safeToString(path)} ' |
13 'is not a String'); | 13 'is not a String'); |
14 } | 14 } |
15 } | 15 } |
16 | 16 |
17 external static _current(); | 17 external static _current(); |
18 external static _setCurrent(path); | 18 external static _setCurrent(path); |
19 external static _createTemp(String path); | 19 external static _createTemp(String path); |
20 external static String _systemTemp(); | 20 external static String _systemTemp(); |
21 external static int _exists(String path); | 21 external static int _exists(String path); |
22 external static _create(String path); | 22 external static _create(String path); |
23 external static _deleteNative(String path, bool recursive); | 23 external static _deleteNative(String path, bool recursive); |
24 external static _rename(String path, String newPath); | 24 external static _rename(String path, String newPath); |
25 external static List _list(String path, bool recursive, bool followLinks); | 25 external static List _list(String path, bool recursive, bool followLinks); |
26 | 26 |
27 static Directory get current { | 27 static Directory get current { |
28 var result = _current(); | 28 var result = _current(); |
29 if (result is OSError) { | 29 if (result is OSError) { |
30 throw new DirectoryException( | 30 throw new FileSystemException( |
31 "Getting current working directory failed", "", result); | 31 "Getting current working directory failed", "", result); |
32 } | 32 } |
33 return new _Directory(result); | 33 return new _Directory(result); |
34 } | 34 } |
35 | 35 |
36 static void set current(path) { | 36 static void set current(path) { |
37 if (path is Directory) path = path.path; | 37 if (path is Directory) path = path.path; |
38 var result = _setCurrent(path); | 38 var result = _setCurrent(path); |
39 if (result is ArgumentError) throw result; | 39 if (result is ArgumentError) throw result; |
40 if (result is OSError) { | 40 if (result is OSError) { |
41 throw new DirectoryException( | 41 throw new FileSystemException( |
42 "Setting current working directory failed", path, result); | 42 "Setting current working directory failed", path, result); |
43 } | 43 } |
44 } | 44 } |
45 | 45 |
46 Future<bool> exists() { | 46 Future<bool> exists() { |
47 return _IOService.dispatch(_DIRECTORY_EXISTS, [path]).then((response) { | 47 return _IOService.dispatch(_DIRECTORY_EXISTS, [path]).then((response) { |
48 if (_isErrorResponse(response)) { | 48 if (_isErrorResponse(response)) { |
49 throw _exceptionOrErrorFromResponse(response, "Exists failed"); | 49 throw _exceptionOrErrorFromResponse(response, "Exists failed"); |
50 } | 50 } |
51 return response == 1; | 51 return response == 1; |
52 }); | 52 }); |
53 } | 53 } |
54 | 54 |
55 bool existsSync() { | 55 bool existsSync() { |
56 var result = _exists(path); | 56 var result = _exists(path); |
57 if (result is OSError) { | 57 if (result is OSError) { |
58 throw new DirectoryException("Exists failed", path, result); | 58 throw new FileSystemException("Exists failed", path, result); |
59 } | 59 } |
60 return (result == 1); | 60 return (result == 1); |
61 } | 61 } |
62 | 62 |
63 Directory get absolute => new Directory(_absolutePath); | 63 Directory get absolute => new Directory(_absolutePath); |
64 | 64 |
65 Future<FileStat> stat() => FileStat.stat(path); | 65 Future<FileStat> stat() => FileStat.stat(path); |
66 | 66 |
67 FileStat statSync() => FileStat.statSync(path); | 67 FileStat statSync() => FileStat.statSync(path); |
68 | 68 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 } | 136 } |
137 for (var i = dirsToCreate.length - 1; i >= 0; i--) { | 137 for (var i = dirsToCreate.length - 1; i >= 0; i--) { |
138 dirsToCreate[i].createSync(); | 138 dirsToCreate[i].createSync(); |
139 } | 139 } |
140 } | 140 } |
141 | 141 |
142 void createSync({bool recursive: false}) { | 142 void createSync({bool recursive: false}) { |
143 if (recursive) return createRecursivelySync(); | 143 if (recursive) return createRecursivelySync(); |
144 var result = _create(path); | 144 var result = _create(path); |
145 if (result is OSError) { | 145 if (result is OSError) { |
146 throw new DirectoryException("Creation failed", path, result); | 146 throw new FileSystemException("Creation failed", path, result); |
147 } | 147 } |
148 } | 148 } |
149 | 149 |
150 static Directory get systemTemp => new Directory(_systemTemp()); | 150 static Directory get systemTemp => new Directory(_systemTemp()); |
151 | 151 |
152 Future<Directory> createTemp([String prefix]) { | 152 Future<Directory> createTemp([String prefix]) { |
153 if (prefix == null) prefix = ''; | 153 if (prefix == null) prefix = ''; |
154 if (path == '') { | 154 if (path == '') { |
155 throw new ArgumentError( | 155 throw new ArgumentError( |
156 "Directory.createTemp called with an empty path. " | 156 "Directory.createTemp called with an empty path. " |
(...skipping 23 matching lines...) Expand all Loading... |
180 "To use the system temp directory, use Directory.systemTemp"); | 180 "To use the system temp directory, use Directory.systemTemp"); |
181 } | 181 } |
182 String fullPrefix; | 182 String fullPrefix; |
183 if (path.endsWith('/') || (Platform.isWindows && path.endsWith('\\'))) { | 183 if (path.endsWith('/') || (Platform.isWindows && path.endsWith('\\'))) { |
184 fullPrefix = "$path$prefix"; | 184 fullPrefix = "$path$prefix"; |
185 } else { | 185 } else { |
186 fullPrefix = "$path${Platform.pathSeparator}$prefix"; | 186 fullPrefix = "$path${Platform.pathSeparator}$prefix"; |
187 } | 187 } |
188 var result = _createTemp(fullPrefix); | 188 var result = _createTemp(fullPrefix); |
189 if (result is OSError) { | 189 if (result is OSError) { |
190 throw new DirectoryException("Creation of temporary directory failed", | 190 throw new FileSystemException("Creation of temporary directory failed", |
191 fullPrefix, | 191 fullPrefix, |
192 result); | 192 result); |
193 } | 193 } |
194 return new Directory(result); | 194 return new Directory(result); |
195 } | 195 } |
196 | 196 |
197 Future<Directory> _delete({bool recursive: false}) { | 197 Future<Directory> _delete({bool recursive: false}) { |
198 return _IOService.dispatch(_DIRECTORY_DELETE, [path, recursive]) | 198 return _IOService.dispatch(_DIRECTORY_DELETE, [path, recursive]) |
199 .then((response) { | 199 .then((response) { |
200 if (_isErrorResponse(response)) { | 200 if (_isErrorResponse(response)) { |
201 throw _exceptionOrErrorFromResponse(response, "Deletion failed"); | 201 throw _exceptionOrErrorFromResponse(response, "Deletion failed"); |
202 } | 202 } |
203 return this; | 203 return this; |
204 }); | 204 }); |
205 } | 205 } |
206 | 206 |
207 void _deleteSync({bool recursive: false}) { | 207 void _deleteSync({bool recursive: false}) { |
208 var result = _deleteNative(path, recursive); | 208 var result = _deleteNative(path, recursive); |
209 if (result is OSError) { | 209 if (result is OSError) { |
210 throw new DirectoryException("Deletion failed", path, result); | 210 throw new FileSystemException("Deletion failed", path, result); |
211 } | 211 } |
212 } | 212 } |
213 | 213 |
214 Future<Directory> rename(String newPath) { | 214 Future<Directory> rename(String newPath) { |
215 return _IOService.dispatch(_DIRECTORY_RENAME, [path, newPath]) | 215 return _IOService.dispatch(_DIRECTORY_RENAME, [path, newPath]) |
216 .then((response) { | 216 .then((response) { |
217 if (_isErrorResponse(response)) { | 217 if (_isErrorResponse(response)) { |
218 throw _exceptionOrErrorFromResponse(response, "Rename failed"); | 218 throw _exceptionOrErrorFromResponse(response, "Rename failed"); |
219 } | 219 } |
220 return new Directory(newPath); | 220 return new Directory(newPath); |
221 }); | 221 }); |
222 } | 222 } |
223 | 223 |
224 Directory renameSync(String newPath) { | 224 Directory renameSync(String newPath) { |
225 if (newPath is !String) { | 225 if (newPath is !String) { |
226 throw new ArgumentError(); | 226 throw new ArgumentError(); |
227 } | 227 } |
228 var result = _rename(path, newPath); | 228 var result = _rename(path, newPath); |
229 if (result is OSError) { | 229 if (result is OSError) { |
230 throw new DirectoryException("Rename failed", path, result); | 230 throw new FileSystemException("Rename failed", path, result); |
231 } | 231 } |
232 return new Directory(newPath); | 232 return new Directory(newPath); |
233 } | 233 } |
234 | 234 |
235 Stream<FileSystemEntity> list({bool recursive: false, | 235 Stream<FileSystemEntity> list({bool recursive: false, |
236 bool followLinks: true}) { | 236 bool followLinks: true}) { |
237 return new _AsyncDirectoryLister( | 237 return new _AsyncDirectoryLister( |
238 FileSystemEntity._trimTrailingPathSeparators(path), | 238 FileSystemEntity._trimTrailingPathSeparators(path), |
239 recursive, | 239 recursive, |
240 followLinks).stream; | 240 followLinks).stream; |
(...skipping 16 matching lines...) Expand all Loading... |
257 } | 257 } |
258 | 258 |
259 _exceptionOrErrorFromResponse(response, String message) { | 259 _exceptionOrErrorFromResponse(response, String message) { |
260 assert(_isErrorResponse(response)); | 260 assert(_isErrorResponse(response)); |
261 switch (response[_ERROR_RESPONSE_ERROR_TYPE]) { | 261 switch (response[_ERROR_RESPONSE_ERROR_TYPE]) { |
262 case _ILLEGAL_ARGUMENT_RESPONSE: | 262 case _ILLEGAL_ARGUMENT_RESPONSE: |
263 return new ArgumentError(); | 263 return new ArgumentError(); |
264 case _OSERROR_RESPONSE: | 264 case _OSERROR_RESPONSE: |
265 var err = new OSError(response[_OSERROR_RESPONSE_MESSAGE], | 265 var err = new OSError(response[_OSERROR_RESPONSE_MESSAGE], |
266 response[_OSERROR_RESPONSE_ERROR_CODE]); | 266 response[_OSERROR_RESPONSE_ERROR_CODE]); |
267 return new DirectoryException(message, path, err); | 267 return new FileSystemException(message, path, err); |
268 default: | 268 default: |
269 return new Exception("Unknown error"); | 269 return new Exception("Unknown error"); |
270 } | 270 } |
271 } | 271 } |
272 } | 272 } |
273 | 273 |
274 class _AsyncDirectoryLister { | 274 class _AsyncDirectoryLister { |
275 static const int LIST_FILE = 0; | 275 static const int LIST_FILE = 0; |
276 static const int LIST_DIRECTORY = 1; | 276 static const int LIST_DIRECTORY = 1; |
277 static const int LIST_LINK = 2; | 277 static const int LIST_LINK = 2; |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 break; | 355 break; |
356 case LIST_ERROR: | 356 case LIST_ERROR: |
357 error(result[i]); | 357 error(result[i]); |
358 break; | 358 break; |
359 case LIST_DONE: | 359 case LIST_DONE: |
360 close(); | 360 close(); |
361 return; | 361 return; |
362 } | 362 } |
363 } | 363 } |
364 } else { | 364 } else { |
365 controller.addError(new DirectoryException("Internal error")); | 365 controller.addError(new FileSystemException("Internal error")); |
366 } | 366 } |
367 nextRunning = false; | 367 nextRunning = false; |
368 next(); | 368 next(); |
369 }); | 369 }); |
370 } | 370 } |
371 | 371 |
372 void close() { | 372 void close() { |
373 if (closed) return; | 373 if (closed) return; |
374 if (id == null) return; | 374 if (id == null) return; |
375 closed = true; | 375 closed = true; |
376 _IOService.dispatch(_DIRECTORY_LIST_STOP, [id]).then((_) { | 376 _IOService.dispatch(_DIRECTORY_LIST_STOP, [id]).then((_) { |
377 controller.close(); | 377 controller.close(); |
378 }); | 378 }); |
379 } | 379 } |
380 | 380 |
381 void error(message) { | 381 void error(message) { |
382 var errorType = | 382 var errorType = |
383 message[RESPONSE_ERROR][_ERROR_RESPONSE_ERROR_TYPE]; | 383 message[RESPONSE_ERROR][_ERROR_RESPONSE_ERROR_TYPE]; |
384 if (errorType == _ILLEGAL_ARGUMENT_RESPONSE) { | 384 if (errorType == _ILLEGAL_ARGUMENT_RESPONSE) { |
385 controller.addError(new ArgumentError()); | 385 controller.addError(new ArgumentError()); |
386 } else if (errorType == _OSERROR_RESPONSE) { | 386 } else if (errorType == _OSERROR_RESPONSE) { |
387 var responseError = message[RESPONSE_ERROR]; | 387 var responseError = message[RESPONSE_ERROR]; |
388 var err = new OSError( | 388 var err = new OSError( |
389 responseError[_OSERROR_RESPONSE_MESSAGE], | 389 responseError[_OSERROR_RESPONSE_MESSAGE], |
390 responseError[_OSERROR_RESPONSE_ERROR_CODE]); | 390 responseError[_OSERROR_RESPONSE_ERROR_CODE]); |
391 var errorPath = message[RESPONSE_PATH]; | 391 var errorPath = message[RESPONSE_PATH]; |
392 if (errorPath == null) errorPath = path; | 392 if (errorPath == null) errorPath = path; |
393 controller.addError( | 393 controller.addError( |
394 new DirectoryException("Directory listing failed", | 394 new FileSystemException("Directory listing failed", |
395 errorPath, | 395 errorPath, |
396 err)); | 396 err)); |
397 } else { | 397 } else { |
398 controller.addError( | 398 controller.addError( |
399 new DirectoryException("Internal error")); | 399 new FileSystemException("Internal error")); |
400 } | 400 } |
401 } | 401 } |
402 } | 402 } |
OLD | NEW |