Chromium Code Reviews| 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 // TODO(13720): Make prefix argument mandatory on Oct 18, 2013. | |
|
Søren Gjesse
2013/10/02 16:57:37
Why do we want to make prefix mandatory? Isn't it
Bill Hesse
2013/10/03 12:15:19
Ok, it will remain optional.
| |
| 156 Future<Directory> createTemp([String prefix]) { | |
| 157 if (prefix == null) prefix = ''; | |
| 154 if (path == '') { | 158 if (path == '') { |
| 155 if (template == null) template = ''; | 159 return systemTemp.createTemp(prefix); |
| 156 return createSystemTemp(template); | |
| 157 // TODO(13720): On Oct 18, 2013, replace this with | 160 // TODO(13720): On Oct 18, 2013, replace this with |
| 158 // an error. createTemp cannot be called on a Directory with empty path. | 161 // an error. createTemp cannot be called on a Directory with empty path. |
| 159 } | 162 } |
| 160 String fullTemplate = "$path${Platform.pathSeparator}"; | 163 String fullPrefix = "$path${Platform.pathSeparator}$prefix"; |
| 161 if (template != null) fullTemplate = "$fullTemplate$template"; | 164 return _IOService.dispatch(_DIRECTORY_CREATE_TEMP, [fullPrefix]) |
| 162 return _IOService.dispatch(_DIRECTORY_CREATE_TEMP, [fullTemplate]) | |
| 163 .then((response) { | 165 .then((response) { |
| 164 if (_isErrorResponse(response)) { | 166 if (_isErrorResponse(response)) { |
| 165 throw _exceptionOrErrorFromResponse( | 167 throw _exceptionOrErrorFromResponse( |
| 166 response, "Creation of temporary directory failed"); | 168 response, "Creation of temporary directory failed"); |
| 167 } | 169 } |
| 168 return new Directory(response); | 170 return new Directory(response); |
| 169 }); | 171 }); |
| 170 } | 172 } |
| 171 | 173 |
| 172 // TODO(13720): Make template argument mandatory on Oct 18, 2013. | 174 // TODO(13720): Make prefix argument mandatory on Oct 18, 2013. |
| 173 Directory createTempSync([String template]) { | 175 Directory createTempSync([String prefix]) { |
| 176 if (prefix == null) prefix = ''; | |
| 174 if (path == '') { | 177 if (path == '') { |
| 175 if (template == null) template = ''; | 178 return systemTemp.createTempSync(prefix); |
| 176 return createSystemTempSync(template); | |
| 177 // TODO(13720): On Oct 18, 2013, replace this with | 179 // TODO(13720): On Oct 18, 2013, replace this with |
| 178 // an error. createTemp cannot be called on a Directory with empty path. | 180 // an error. createTemp cannot be called on a Directory with empty path. |
| 179 } | 181 } |
| 180 String fullTemplate = "$path${Platform.pathSeparator}"; | 182 String fullPrefix = "$path${Platform.pathSeparator}$prefix"; |
| 181 if (template != null) fullTemplate = "$fullTemplate$template"; | 183 var result = _createTemp(fullPrefix); |
| 182 var result = _createTemp(fullTemplate, false); | |
| 183 if (result is OSError) { | 184 if (result is OSError) { |
| 184 throw new DirectoryException("Creation of temporary directory failed", | 185 throw new DirectoryException("Creation of temporary directory failed", |
| 185 fullTemplate, | 186 fullPrefix, |
| 186 result); | 187 result); |
| 187 } | 188 } |
| 188 return new Directory(result); | 189 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 } | 190 } |
| 211 | 191 |
| 212 Future<Directory> _delete({bool recursive: false}) { | 192 Future<Directory> _delete({bool recursive: false}) { |
| 213 return _IOService.dispatch(_DIRECTORY_DELETE, [path, recursive]) | 193 return _IOService.dispatch(_DIRECTORY_DELETE, [path, recursive]) |
| 214 .then((response) { | 194 .then((response) { |
| 215 if (_isErrorResponse(response)) { | 195 if (_isErrorResponse(response)) { |
| 216 throw _exceptionOrErrorFromResponse(response, "Deletion failed"); | 196 throw _exceptionOrErrorFromResponse(response, "Deletion failed"); |
| 217 } | 197 } |
| 218 return this; | 198 return this; |
| 219 }); | 199 }); |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 408 controller.addError( | 388 controller.addError( |
| 409 new DirectoryException("Directory listing failed", | 389 new DirectoryException("Directory listing failed", |
| 410 errorPath, | 390 errorPath, |
| 411 err)); | 391 err)); |
| 412 } else { | 392 } else { |
| 413 controller.addError( | 393 controller.addError( |
| 414 new DirectoryException("Internal error")); | 394 new DirectoryException("Internal error")); |
| 415 } | 395 } |
| 416 } | 396 } |
| 417 } | 397 } |
| OLD | NEW |