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 _DirectoryListingIsolate extends Isolate { | 6 class _DirectoryListingIsolate extends Isolate { |
7 | 7 |
8 _DirectoryListingIsolate() : super.heavy(); | 8 _DirectoryListingIsolate() : super.heavy(); |
9 | 9 |
10 void main() { | 10 void main() { |
(...skipping 11 matching lines...) Expand all Loading... |
22 | 22 |
23 bool _list(String dir, | 23 bool _list(String dir, |
24 bool recursive, | 24 bool recursive, |
25 SendPort dirPort, | 25 SendPort dirPort, |
26 SendPort filePort, | 26 SendPort filePort, |
27 SendPort donePort, | 27 SendPort donePort, |
28 SendPort errorPort) native "Directory_List"; | 28 SendPort errorPort) native "Directory_List"; |
29 } | 29 } |
30 | 30 |
31 | 31 |
| 32 class _DirectoryCreateTempIsolate extends Isolate { |
| 33 |
| 34 _DirectoryCreateTempIsolate() : super.heavy(); |
| 35 |
| 36 void main() { |
| 37 port.receive((path, replyTo) { |
| 38 // Call function to get file name |
| 39 replyTo.send(_createTemp(path, (Math.random() * 0x8000000).toInt())); |
| 40 port.close(); |
| 41 }); |
| 42 } |
| 43 |
| 44 String _createTemp(String template, int num) native "Directory_CreateTemp"; |
| 45 } |
| 46 |
| 47 |
32 class _Directory implements Directory { | 48 class _Directory implements Directory { |
33 | 49 |
34 _Directory(String this._path); | 50 _Directory(String this._path); |
35 | 51 |
36 bool existsSync() { | 52 bool existsSync() { |
37 int exists = _exists(_path); | 53 int exists = _exists(_path); |
38 if (exists < 0) { | 54 if (exists < 0) { |
39 throw new DirectoryException("Diretory exists test failed: $_path"); | 55 throw new DirectoryException("Diretory exists test failed: $_path"); |
40 } | 56 } |
41 return (exists == 1); | 57 return (exists == 1); |
42 } | 58 } |
43 | 59 |
44 void createSync() { | 60 void createSync() { |
45 if (!_create(_path)) { | 61 if (!_create(_path)) { |
46 throw new DirectoryException("Directory creation failed: $_path"); | 62 throw new DirectoryException("Directory creation failed: $_path"); |
47 } | 63 } |
48 } | 64 } |
49 | 65 |
| 66 void createTemp() { |
| 67 new _DirectoryCreateTempIsolate().spawn().then((port) { |
| 68 port.call(_path).receive((result, ignored) { |
| 69 if (result != '') { |
| 70 _path = result; |
| 71 if (_createTempHandler !== null) { |
| 72 _createTempHandler(); |
| 73 } |
| 74 } else { |
| 75 if (_errorHandler !== null) { |
| 76 _errorHandler("Could not create temporary directory: $_path"); |
| 77 } |
| 78 } |
| 79 }); |
| 80 }); |
| 81 } |
| 82 |
50 void deleteSync() { | 83 void deleteSync() { |
51 if (!_delete(_path)) { | 84 if (!_delete(_path)) { |
52 throw new DirectoryException("Directory deletion failed: $_path"); | 85 throw new DirectoryException("Directory deletion failed: $_path"); |
53 } | 86 } |
54 } | 87 } |
55 | 88 |
56 void list([bool recursive = false]) { | 89 void list([bool recursive = false]) { |
57 new _DirectoryListingIsolate().spawn().then((port) { | 90 new _DirectoryListingIsolate().spawn().then((port) { |
58 // Build a map of parameters to the directory listing isolate. | 91 // Build a map of parameters to the directory listing isolate. |
59 Map listingParameters = new Map(); | 92 Map listingParameters = new Map(); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 } | 155 } |
123 | 156 |
124 void set fileHandler(void fileHandler(String file)) { | 157 void set fileHandler(void fileHandler(String file)) { |
125 _fileHandler = fileHandler; | 158 _fileHandler = fileHandler; |
126 } | 159 } |
127 | 160 |
128 void set doneHandler(void doneHandler(bool completed)) { | 161 void set doneHandler(void doneHandler(bool completed)) { |
129 _doneHandler = doneHandler; | 162 _doneHandler = doneHandler; |
130 } | 163 } |
131 | 164 |
| 165 void set createTempHandler(void createTempHandler()) { |
| 166 _createTempHandler = createTempHandler; |
| 167 } |
| 168 |
132 void set errorHandler(void errorHandler(String error)) { | 169 void set errorHandler(void errorHandler(String error)) { |
133 _errorHandler = errorHandler; | 170 _errorHandler = errorHandler; |
134 } | 171 } |
135 | 172 |
136 void _closePort(ReceivePort port) { | 173 void _closePort(ReceivePort port) { |
137 if (port !== null) { | 174 if (port !== null) { |
138 port.close(); | 175 port.close(); |
139 } | 176 } |
140 } | 177 } |
141 | 178 |
142 String get path() { return _path; } | 179 String get path() { return _path; } |
143 | 180 |
144 int _exists(String path) native "Directory_Exists"; | 181 int _exists(String path) native "Directory_Exists"; |
145 bool _create(String path) native "Directory_Create"; | 182 bool _create(String path) native "Directory_Create"; |
146 bool _delete(String path) native "Directory_Delete"; | 183 bool _delete(String path) native "Directory_Delete"; |
147 | 184 |
148 var _dirHandler; | 185 var _dirHandler; |
149 var _fileHandler; | 186 var _fileHandler; |
150 var _doneHandler; | 187 var _doneHandler; |
| 188 var _createTempHandler; |
151 var _errorHandler; | 189 var _errorHandler; |
152 | 190 |
153 String _path; | 191 String _path; |
154 } | 192 } |
OLD | NEW |