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 template, bool system); | 19 external static _createTemp(String path); |
| 20 external static String _systemTemp(); |
20 external static int _exists(String path); | 21 external static int _exists(String path); |
21 external static _create(String path); | 22 external static _create(String path); |
22 external static _deleteNative(String path, bool recursive); | 23 external static _deleteNative(String path, bool recursive); |
23 external static _rename(String path, String newPath); | 24 external static _rename(String path, String newPath); |
24 external static List _list(String path, bool recursive, bool followLinks); | 25 external static List _list(String path, bool recursive, bool followLinks); |
25 | 26 |
26 static Directory get current { | 27 static Directory get current { |
27 var result = _current(); | 28 var result = _current(); |
28 if (result is OSError) { | 29 if (result is OSError) { |
29 throw new DirectoryException( | 30 throw new DirectoryException( |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 } | 143 } |
143 | 144 |
144 void createSync({bool recursive: false}) { | 145 void createSync({bool recursive: false}) { |
145 if (recursive) return createRecursivelySync(); | 146 if (recursive) return createRecursivelySync(); |
146 var result = _create(path); | 147 var result = _create(path); |
147 if (result is OSError) { | 148 if (result is OSError) { |
148 throw new DirectoryException("Creation failed", path, result); | 149 throw new DirectoryException("Creation failed", path, result); |
149 } | 150 } |
150 } | 151 } |
151 | 152 |
152 // TODO(13720): Make template argument mandatory on Oct 18, 2013. | 153 static Directory get systemTemp => new Directory(_systemTemp()); |
153 Future<Directory> createTemp([String template]) { | 154 |
| 155 Future<Directory> createTemp([String prefix]) { |
| 156 if (prefix == null) prefix = ''; |
154 if (path == '') { | 157 if (path == '') { |
155 if (template == null) template = ''; | 158 return systemTemp.createTemp(prefix); |
156 return createSystemTemp(template); | |
157 // TODO(13720): On Oct 18, 2013, replace this with | 159 // TODO(13720): On Oct 18, 2013, replace this with |
158 // an error. createTemp cannot be called on a Directory with empty path. | 160 // an error. createTemp cannot be called on a Directory with empty path. |
159 } | 161 } |
160 String fullTemplate = "$path${Platform.pathSeparator}"; | 162 String fullPrefix; |
161 if (template != null) fullTemplate = "$fullTemplate$template"; | 163 if (path.endsWith('/') || (Platform.isWindows && path.endsWith('\\'))) { |
162 return _IOService.dispatch(_DIRECTORY_CREATE_TEMP, [fullTemplate]) | 164 fullPrefix = "$path$prefix"; |
| 165 } else { |
| 166 fullPrefix = "$path${Platform.pathSeparator}$prefix"; |
| 167 } |
| 168 return _IOService.dispatch(_DIRECTORY_CREATE_TEMP, [fullPrefix]) |
163 .then((response) { | 169 .then((response) { |
164 if (_isErrorResponse(response)) { | 170 if (_isErrorResponse(response)) { |
165 throw _exceptionOrErrorFromResponse( | 171 throw _exceptionOrErrorFromResponse( |
166 response, "Creation of temporary directory failed"); | 172 response, "Creation of temporary directory failed"); |
167 } | 173 } |
168 return new Directory(response); | 174 return new Directory(response); |
169 }); | 175 }); |
170 } | 176 } |
171 | 177 |
172 // TODO(13720): Make template argument mandatory on Oct 18, 2013. | 178 Directory createTempSync([String prefix]) { |
173 Directory createTempSync([String template]) { | 179 if (prefix == null) prefix = ''; |
174 if (path == '') { | 180 if (path == '') { |
175 if (template == null) template = ''; | 181 return systemTemp.createTempSync(prefix); |
176 return createSystemTempSync(template); | |
177 // TODO(13720): On Oct 18, 2013, replace this with | 182 // TODO(13720): On Oct 18, 2013, replace this with |
178 // an error. createTemp cannot be called on a Directory with empty path. | 183 // an error. createTemp cannot be called on a Directory with empty path. |
179 } | 184 } |
180 String fullTemplate = "$path${Platform.pathSeparator}"; | 185 String fullPrefix; |
181 if (template != null) fullTemplate = "$fullTemplate$template"; | 186 if (path.endsWith('/') || (Platform.isWindows && path.endsWith('\\'))) { |
182 var result = _createTemp(fullTemplate, false); | 187 fullPrefix = "$path$prefix"; |
| 188 } else { |
| 189 fullPrefix = "$path${Platform.pathSeparator}$prefix"; |
| 190 } |
| 191 var result = _createTemp(fullPrefix); |
183 if (result is OSError) { | 192 if (result is OSError) { |
184 throw new DirectoryException("Creation of temporary directory failed", | 193 throw new DirectoryException("Creation of temporary directory failed", |
185 fullTemplate, | 194 fullPrefix, |
186 result); | 195 result); |
187 } | 196 } |
188 return new Directory(result); | 197 return new Directory(result); |
189 } | |
190 | |
191 static Future<Directory> createSystemTemp(String template) { | |
192 return _IOService.dispatch(_DIRECTORY_CREATE_SYSTEM_TEMP, | |
193 [template]).then((response) { | |
194 if (response is List && response[0] != _SUCCESS_RESPONSE) { | |
195 throw new _Directory(template)._exceptionOrErrorFromResponse( | |
196 response, "Creation of temporary directory failed"); | |
197 } | |
198 return new Directory(response); | |
199 }); | |
200 } | |
201 | |
202 static Directory createSystemTempSync(String template) { | |
203 var result = _createTemp(template, true); | |
204 if (result is OSError) { | |
205 throw new DirectoryException("Creation of temporary directory failed", | |
206 template, | |
207 result); | |
208 } | |
209 return new Directory(result); | |
210 } | 198 } |
211 | 199 |
212 Future<Directory> _delete({bool recursive: false}) { | 200 Future<Directory> _delete({bool recursive: false}) { |
213 return _IOService.dispatch(_DIRECTORY_DELETE, [path, recursive]) | 201 return _IOService.dispatch(_DIRECTORY_DELETE, [path, recursive]) |
214 .then((response) { | 202 .then((response) { |
215 if (_isErrorResponse(response)) { | 203 if (_isErrorResponse(response)) { |
216 throw _exceptionOrErrorFromResponse(response, "Deletion failed"); | 204 throw _exceptionOrErrorFromResponse(response, "Deletion failed"); |
217 } | 205 } |
218 return this; | 206 return this; |
219 }); | 207 }); |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 controller.addError( | 396 controller.addError( |
409 new DirectoryException("Directory listing failed", | 397 new DirectoryException("Directory listing failed", |
410 errorPath, | 398 errorPath, |
411 err)); | 399 err)); |
412 } else { | 400 } else { |
413 controller.addError( | 401 controller.addError( |
414 new DirectoryException("Internal error")); | 402 new DirectoryException("Internal error")); |
415 } | 403 } |
416 } | 404 } |
417 } | 405 } |
OLD | NEW |