OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 /** | 7 /** |
8 * [Link] objects are references to filesystem links. | 8 * [Link] objects are references to filesystem links. |
9 * | 9 * |
10 */ | 10 */ |
(...skipping 16 matching lines...) Expand all Loading... |
27 Future<bool> exists(); | 27 Future<bool> exists(); |
28 | 28 |
29 /** | 29 /** |
30 * Synchronously checks if the link exists. The link may exist, even if | 30 * Synchronously checks if the link exists. The link may exist, even if |
31 * its target is missing or deleted. | 31 * its target is missing or deleted. |
32 */ | 32 */ |
33 bool existsSync(); | 33 bool existsSync(); |
34 | 34 |
35 /** | 35 /** |
36 * Creates a symbolic link. Returns a [:Future<Link>:] that completes with | 36 * Creates a symbolic link. Returns a [:Future<Link>:] that completes with |
37 * the link when it has been created. If the link exists, the function | 37 * the link when it has been created. If the link exists, |
38 * the future will complete with an error. | 38 * the future will complete with an error. |
39 * | 39 * |
40 * On the Windows platform, this will only work with directories, and the | 40 * On the Windows platform, this will only work with directories, and the |
41 * target directory must exist. The link will be created as a Junction. | 41 * target directory must exist. The link will be created as a Junction. |
42 * Only absolute links will be created, and relative paths to the target | 42 * Only absolute links will be created, and relative paths to the target |
43 * will be converted to absolute paths. | 43 * will be converted to absolute paths. |
| 44 * |
| 45 * On other platforms, the posix symlink() call is used to make a symbolic |
| 46 * link containing the string [target]. If [target] is a relative path, |
| 47 * it will be interpreted relative to the directory containing the link. |
44 */ | 48 */ |
45 Future<Link> create(String target); | 49 Future<Link> create(String target); |
46 | 50 |
47 /** | 51 /** |
48 * Synchronously create the link. Calling [createSync] on an existing link | 52 * Synchronously create the link. Calling [createSync] on an existing link |
49 * will throw an exception. | 53 * will throw an exception. |
50 * | 54 * |
51 * On the Windows platform, this will only work with directories, and the | 55 * On the Windows platform, this will only work with directories, and the |
52 * target directory must exist. The link will be created as a Junction. | 56 * target directory must exist. The link will be created as a Junction. |
53 * Only absolute links will be created, and relative paths to the target | 57 * Only absolute links will be created, and relative paths to the target |
54 * will be converted to absolute paths. | 58 * will be converted to absolute paths. |
| 59 * |
| 60 * On other platforms, the posix symlink() call is used to make a symbolic |
| 61 * link containing the string [target]. If [target] is a relative path, |
| 62 * it will be interpreted relative to the directory containing the link. |
55 */ | 63 */ |
56 void createSync(String target); | 64 void createSync(String target); |
57 | 65 |
58 /** | 66 /** |
59 * Synchronously updates the link. Calling [updateSync] on a non-existing link | 67 * Synchronously updates the link. Calling [updateSync] on a non-existing link |
60 * will throw an exception. | 68 * will throw an exception. |
61 * | 69 * |
62 * If [linkRelative] is true, the target argument should be a relative path, | 70 * If [linkRelative] is true, the target argument should be a relative path, |
63 * and the link will interpret the target as a path relative to the link's | 71 * and the link will interpret the target as a path relative to the link's |
64 * directory. | 72 * directory. |
65 * | 73 * |
66 * On the Windows platform, this will only work with directories, and the | 74 * On the Windows platform, this will only work with directories, and the |
67 * target directory must exist. | 75 * target directory must exist. |
68 */ | 76 */ |
69 void updateSync(String target, {bool linkRelative: false }); | 77 void updateSync(String target, {bool linkRelative: false }); |
70 | 78 |
71 /** | 79 /** |
72 * Deletes the link. Returns a [:Future<Link>:] that completes with | 80 * Deletes the link. Returns a [:Future<Link>:] that completes with |
73 * the link when it has been deleted. This does not delete, or otherwise | 81 * the link when it has been deleted. This does not delete, or otherwise |
74 * affect, the target of the link. | 82 * affect, the target of the link. It also works on broken links, but if |
| 83 * the link does not exist or is not actually a link, it completes the |
| 84 * future with a LinkIOException. |
75 */ | 85 */ |
76 Future<Link> delete(); | 86 Future<Link> delete(); |
77 | 87 |
78 /** | 88 /** |
79 * Synchronously deletes the link. This does not delete, or otherwise | 89 * Synchronously deletes the link. This does not delete, or otherwise |
80 * affect, the target of the link. | 90 * affect, the target of the link. It also works on broken links, but if |
| 91 * the link does not exist or is not actually a link, it throws a |
| 92 * LinkIOException. |
81 */ | 93 */ |
82 void deleteSync(); | 94 void deleteSync(); |
83 | 95 |
84 /** | 96 /** |
85 * Gets the target of the link. Returns a future that completes with | 97 * Gets the target of the link. Returns a future that completes with |
86 * the path to the target. | 98 * the path to the target. |
87 * | 99 * |
88 * If the returned target is a relative path, it is relative to the | 100 * If the returned target is a relative path, it is relative to the |
89 * directory containing the link. | 101 * directory containing the link. |
90 * | 102 * |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 } | 209 } |
198 | 210 |
199 String targetSync() { | 211 String targetSync() { |
200 var result = _File._linkTarget(path); | 212 var result = _File._linkTarget(path); |
201 throwIfError(result, "Cannot read link '$path'"); | 213 throwIfError(result, "Cannot read link '$path'"); |
202 return result; | 214 return result; |
203 } | 215 } |
204 | 216 |
205 static throwIfError(Object result, String msg) { | 217 static throwIfError(Object result, String msg) { |
206 if (result is OSError) { | 218 if (result is OSError) { |
207 throw new FileIOException(msg, result); | 219 throw new LinkIOException(msg, result); |
208 } | 220 } |
209 } | 221 } |
210 | 222 |
211 bool _isErrorResponse(response) { | 223 bool _isErrorResponse(response) { |
212 return response is List && response[0] != _SUCCESS_RESPONSE; | 224 return response is List && response[0] != _SUCCESS_RESPONSE; |
213 } | 225 } |
214 | 226 |
215 void _ensureFileService() { | 227 void _ensureFileService() { |
216 if (_fileService == null) { | 228 if (_fileService == null) { |
217 _fileService = _FileUtils._newServicePort(); | 229 _fileService = _FileUtils._newServicePort(); |
218 } | 230 } |
219 } | 231 } |
220 | 232 |
221 _exceptionFromResponse(response, String message) { | 233 _exceptionFromResponse(response, String message) { |
222 assert(_isErrorResponse(response)); | 234 assert(_isErrorResponse(response)); |
223 switch (response[_ERROR_RESPONSE_ERROR_TYPE]) { | 235 switch (response[_ERROR_RESPONSE_ERROR_TYPE]) { |
224 case _ILLEGAL_ARGUMENT_RESPONSE: | 236 case _ILLEGAL_ARGUMENT_RESPONSE: |
225 return new ArgumentError(); | 237 return new ArgumentError(); |
226 case _OSERROR_RESPONSE: | 238 case _OSERROR_RESPONSE: |
227 var err = new OSError(response[_OSERROR_RESPONSE_MESSAGE], | 239 var err = new OSError(response[_OSERROR_RESPONSE_MESSAGE], |
228 response[_OSERROR_RESPONSE_ERROR_CODE]); | 240 response[_OSERROR_RESPONSE_ERROR_CODE]); |
229 return new FileIOException(message, err); | 241 return new LinkIOException(message, err); |
230 case _FILE_CLOSED_RESPONSE: | |
231 return new FileIOException("File closed"); | |
232 default: | 242 default: |
233 return new Exception("Unknown error"); | 243 return new Exception("Unknown error"); |
234 } | 244 } |
235 } | 245 } |
236 } | 246 } |
237 | 247 |
238 | 248 |
239 class LinkIOException implements Exception { | 249 class LinkIOException implements Exception { |
240 const LinkIOException([String this.message = "", | 250 const LinkIOException([String this.message = "", |
241 String this.path = "", | 251 String this.path = "", |
(...skipping 14 matching lines...) Expand all Loading... |
256 if (path != null) { | 266 if (path != null) { |
257 sb.write(", path = $path"); | 267 sb.write(", path = $path"); |
258 } | 268 } |
259 } | 269 } |
260 return sb.toString(); | 270 return sb.toString(); |
261 } | 271 } |
262 final String message; | 272 final String message; |
263 final String path; | 273 final String path; |
264 final OSError osError; | 274 final OSError osError; |
265 } | 275 } |
OLD | NEW |