OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 DirectoryException { | 6 class DirectoryException { |
| 7 String toString() { return "DirectoryException: $message"; } |
7 const DirectoryException(String this.message); | 8 const DirectoryException(String this.message); |
8 final String message; | 9 final String message; |
9 } | 10 } |
10 | 11 |
11 | 12 |
12 class _DirectoryListingIsolate extends Isolate { | 13 class _DirectoryListingIsolate extends Isolate { |
13 | 14 |
14 _DirectoryListingIsolate() : super.heavy(); | 15 _DirectoryListingIsolate() : super.heavy(); |
15 | 16 |
16 void main() { | 17 void main() { |
(...skipping 12 matching lines...) Expand all Loading... |
29 bool recursive, | 30 bool recursive, |
30 SendPort dirPort, | 31 SendPort dirPort, |
31 SendPort filePort, | 32 SendPort filePort, |
32 SendPort donePort, | 33 SendPort donePort, |
33 SendPort errorPort) native "Directory_List"; | 34 SendPort errorPort) native "Directory_List"; |
34 } | 35 } |
35 | 36 |
36 | 37 |
37 class _Directory implements Directory { | 38 class _Directory implements Directory { |
38 | 39 |
39 _Directory(String this._dir); | 40 _Directory(String this._path); |
| 41 |
| 42 bool exists() { |
| 43 int exists = _exists(_path); |
| 44 if (exists < 0) { |
| 45 // TODO(ager): Supply a better error message. |
| 46 throw new DirectoryException("Diretory exists test failed: $_path"); |
| 47 } |
| 48 return (exists == 1); |
| 49 } |
| 50 |
| 51 void create() { |
| 52 if (!_create(_path)) { |
| 53 // TODO(ager): Supply a better error message. |
| 54 throw new DirectoryException("Directory creation failed: $_path"); |
| 55 } |
| 56 } |
| 57 |
| 58 void delete() { |
| 59 if (!_delete(_path)) { |
| 60 // TODO(ager): Supply a better error message. |
| 61 throw new DirectoryException("Directory deletion failed: $_path"); |
| 62 } |
| 63 } |
40 | 64 |
41 void list([bool recursive = false]) { | 65 void list([bool recursive = false]) { |
42 new _DirectoryListingIsolate().spawn().then((port) { | 66 new _DirectoryListingIsolate().spawn().then((port) { |
43 // Build a map of parameters to the directory listing isolate. | 67 // Build a map of parameters to the directory listing isolate. |
44 Map listingParameters = new Map(); | 68 Map listingParameters = new Map(); |
45 listingParameters['dir'] = _dir; | 69 listingParameters['dir'] = _path; |
46 listingParameters['recursive'] = recursive; | 70 listingParameters['recursive'] = recursive; |
47 | 71 |
48 // Setup ports to receive messages from listing. | 72 // Setup ports to receive messages from listing. |
49 // TODO(ager): Do not explicitly transform to send ports when | 73 // TODO(ager): Do not explicitly transform to send ports when |
50 // implicit conversions are implemented. | 74 // implicit conversions are implemented. |
51 ReceivePort dirPort; | 75 ReceivePort dirPort; |
52 ReceivePort filePort; | 76 ReceivePort filePort; |
53 ReceivePort donePort; | 77 ReceivePort donePort; |
54 ReceivePort errorPort; | 78 ReceivePort errorPort; |
55 if (_dirHandler !== null) { | 79 if (_dirHandler !== null) { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 void setErrorHandler(void errorHandler(String error)) { | 135 void setErrorHandler(void errorHandler(String error)) { |
112 _errorHandler = errorHandler; | 136 _errorHandler = errorHandler; |
113 } | 137 } |
114 | 138 |
115 void _closePort(ReceivePort port) { | 139 void _closePort(ReceivePort port) { |
116 if (port !== null) { | 140 if (port !== null) { |
117 port.close(); | 141 port.close(); |
118 } | 142 } |
119 } | 143 } |
120 | 144 |
| 145 String get path() { return _path; } |
| 146 |
| 147 bool _exists(String path) native "Directory_Exists"; |
| 148 bool _create(String path) native "Directory_Create"; |
| 149 bool _delete(String path) native "Directory_Delete"; |
| 150 |
121 var _dirHandler; | 151 var _dirHandler; |
122 var _fileHandler; | 152 var _fileHandler; |
123 var _doneHandler; | 153 var _doneHandler; |
124 var _errorHandler; | 154 var _errorHandler; |
125 | 155 |
126 String _dir; | 156 String _path; |
127 } | 157 } |
OLD | NEW |