| 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 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 * | 118 * |
| 119 * If the link does not exist, or is not a link, throws a LinkException. | 119 * If the link does not exist, or is not a link, throws a LinkException. |
| 120 */ | 120 */ |
| 121 String targetSync(); | 121 String targetSync(); |
| 122 } | 122 } |
| 123 | 123 |
| 124 | 124 |
| 125 class _Link extends FileSystemEntity implements Link { | 125 class _Link extends FileSystemEntity implements Link { |
| 126 final String path; | 126 final String path; |
| 127 | 127 |
| 128 SendPort _fileService; | |
| 129 | |
| 130 _Link(String this.path) { | 128 _Link(String this.path) { |
| 131 if (path is! String) { | 129 if (path is! String) { |
| 132 throw new ArgumentError('${Error.safeToString(path)} ' | 130 throw new ArgumentError('${Error.safeToString(path)} ' |
| 133 'is not a String'); | 131 'is not a String'); |
| 134 } | 132 } |
| 135 } | 133 } |
| 136 | 134 |
| 137 | 135 |
| 138 String toString() => "Link: '$path'"; | 136 String toString() => "Link: '$path'"; |
| 139 | 137 |
| 140 Future<bool> exists() => FileSystemEntity.isLink(path); | 138 Future<bool> exists() => FileSystemEntity.isLink(path); |
| 141 | 139 |
| 142 bool existsSync() => FileSystemEntity.isLinkSync(path); | 140 bool existsSync() => FileSystemEntity.isLinkSync(path); |
| 143 | 141 |
| 144 Link get absolute => new Link(_absolutePath); | 142 Link get absolute => new Link(_absolutePath); |
| 145 | 143 |
| 146 Future<FileStat> stat() => FileStat.stat(path); | 144 Future<FileStat> stat() => FileStat.stat(path); |
| 147 | 145 |
| 148 FileStat statSync() => FileStat.statSync(path); | 146 FileStat statSync() => FileStat.statSync(path); |
| 149 | 147 |
| 150 Future<Link> create(String target) { | 148 Future<Link> create(String target) { |
| 151 _ensureFileService(); | |
| 152 if (Platform.operatingSystem == 'windows') { | 149 if (Platform.operatingSystem == 'windows') { |
| 153 target = _makeWindowsLinkTarget(target); | 150 target = _makeWindowsLinkTarget(target); |
| 154 } | 151 } |
| 155 List request = new List(3); | 152 return IOService.dispatch(FILE_CREATE_LINK, [path, target]) |
| 156 request[0] = _CREATE_LINK_REQUEST; | 153 .then((response) { |
| 157 request[1] = path; | 154 if (_isErrorResponse(response)) { |
| 158 request[2] = target; | 155 throw _exceptionFromResponse( |
| 159 return _fileService.call(request).then((response) { | 156 response, "Cannot create link to target '$target'", path); |
| 160 if (_isErrorResponse(response)) { | 157 } |
| 161 throw _exceptionFromResponse( | 158 return this; |
| 162 response, "Cannot create link to target '$target'", path); | 159 }); |
| 163 } | |
| 164 return this; | |
| 165 }); | |
| 166 } | 160 } |
| 167 | 161 |
| 168 void createSync(String target) { | 162 void createSync(String target) { |
| 169 if (Platform.operatingSystem == 'windows') { | 163 if (Platform.operatingSystem == 'windows') { |
| 170 target = _makeWindowsLinkTarget(target); | 164 target = _makeWindowsLinkTarget(target); |
| 171 } | 165 } |
| 172 var result = _File._createLink(path, target); | 166 var result = _File._createLink(path, target); |
| 173 throwIfError(result, "Cannot create link", path); | 167 throwIfError(result, "Cannot create link", path); |
| 174 } | 168 } |
| 175 | 169 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 202 // Atomically changing a link can be done by creating the new link, with | 196 // Atomically changing a link can be done by creating the new link, with |
| 203 // a different name, and using the rename() posix call to move it to | 197 // a different name, and using the rename() posix call to move it to |
| 204 // the old name atomically. | 198 // the old name atomically. |
| 205 return delete().then((_) => create(target)); | 199 return delete().then((_) => create(target)); |
| 206 } | 200 } |
| 207 | 201 |
| 208 Future<Link> _delete({bool recursive: false}) { | 202 Future<Link> _delete({bool recursive: false}) { |
| 209 if (recursive) { | 203 if (recursive) { |
| 210 return new Directory(path).delete(recursive: true).then((_) => this); | 204 return new Directory(path).delete(recursive: true).then((_) => this); |
| 211 } | 205 } |
| 212 _ensureFileService(); | 206 return IOService.dispatch(FILE_DELETE_LINK, [path]).then((response) { |
| 213 List request = new List(2); | |
| 214 request[0] = _DELETE_LINK_REQUEST; | |
| 215 request[1] = path; | |
| 216 return _fileService.call(request).then((response) { | |
| 217 if (_isErrorResponse(response)) { | 207 if (_isErrorResponse(response)) { |
| 218 throw _exceptionFromResponse(response, "Cannot delete link", path); | 208 throw _exceptionFromResponse(response, "Cannot delete link", path); |
| 219 } | 209 } |
| 220 return this; | 210 return this; |
| 221 }); | 211 }); |
| 222 } | 212 } |
| 223 | 213 |
| 224 void _deleteSync({bool recursive: false}) { | 214 void _deleteSync({bool recursive: false}) { |
| 225 if (recursive) { | 215 if (recursive) { |
| 226 return new Directory(path).deleteSync(recursive: true); | 216 return new Directory(path).deleteSync(recursive: true); |
| 227 } | 217 } |
| 228 var result = _File._deleteLinkNative(path); | 218 var result = _File._deleteLinkNative(path); |
| 229 throwIfError(result, "Cannot delete link", path); | 219 throwIfError(result, "Cannot delete link", path); |
| 230 } | 220 } |
| 231 | 221 |
| 232 Future<Link> rename(String newPath) { | 222 Future<Link> rename(String newPath) { |
| 233 _ensureFileService(); | 223 return IOService.dispatch(FILE_RENAME_LINK, [path, newPath]) |
| 234 List request = new List(3); | 224 .then((response) { |
| 235 request[0] = _RENAME_LINK_REQUEST; | 225 if (_isErrorResponse(response)) { |
| 236 request[1] = path; | 226 throw _exceptionFromResponse( |
| 237 request[2] = newPath; | 227 response, "Cannot rename link to '$newPath'", path); |
| 238 return _fileService.call(request).then((response) { | 228 } |
| 239 if (_isErrorResponse(response)) { | 229 return new Link(newPath); |
| 240 throw _exceptionFromResponse( | 230 }); |
| 241 response, "Cannot rename link to '$newPath'", path); | |
| 242 } | |
| 243 return new Link(newPath); | |
| 244 }); | |
| 245 } | 231 } |
| 246 | 232 |
| 247 Link renameSync(String newPath) { | 233 Link renameSync(String newPath) { |
| 248 var result = _File._renameLink(path, newPath); | 234 var result = _File._renameLink(path, newPath); |
| 249 throwIfError(result, "Cannot rename link '$path' to '$newPath'"); | 235 throwIfError(result, "Cannot rename link '$path' to '$newPath'"); |
| 250 return new Link(newPath); | 236 return new Link(newPath); |
| 251 } | 237 } |
| 252 | 238 |
| 253 Future<String> target() { | 239 Future<String> target() { |
| 254 _ensureFileService(); | 240 return IOService.dispatch(FILE_LINK_TARGET, [path]).then((response) { |
| 255 List request = new List(2); | |
| 256 request[0] = _LINK_TARGET_REQUEST; | |
| 257 request[1] = path; | |
| 258 return _fileService.call(request).then((response) { | |
| 259 if (_isErrorResponse(response)) { | 241 if (_isErrorResponse(response)) { |
| 260 throw _exceptionFromResponse( | 242 throw _exceptionFromResponse( |
| 261 response, "Cannot get target of link", path); | 243 response, "Cannot get target of link", path); |
| 262 } | 244 } |
| 263 return response; | 245 return response; |
| 264 }); | 246 }); |
| 265 } | 247 } |
| 266 | 248 |
| 267 String targetSync() { | 249 String targetSync() { |
| 268 var result = _File._linkTarget(path); | 250 var result = _File._linkTarget(path); |
| 269 throwIfError(result, "Cannot read link", path); | 251 throwIfError(result, "Cannot read link", path); |
| 270 return result; | 252 return result; |
| 271 } | 253 } |
| 272 | 254 |
| 273 static throwIfError(Object result, String msg, [String path = ""]) { | 255 static throwIfError(Object result, String msg, [String path = ""]) { |
| 274 if (result is OSError) { | 256 if (result is OSError) { |
| 275 throw new LinkException(msg, path, result); | 257 throw new LinkException(msg, path, result); |
| 276 } | 258 } |
| 277 } | 259 } |
| 278 | 260 |
| 279 bool _isErrorResponse(response) { | 261 bool _isErrorResponse(response) { |
| 280 return response is List && response[0] != _SUCCESS_RESPONSE; | 262 return response is List && response[0] != _SUCCESS_RESPONSE; |
| 281 } | 263 } |
| 282 | 264 |
| 283 void _ensureFileService() { | |
| 284 if (_fileService == null) { | |
| 285 _fileService = _FileUtils._newServicePort(); | |
| 286 } | |
| 287 } | |
| 288 | |
| 289 _exceptionFromResponse(response, String message, String path) { | 265 _exceptionFromResponse(response, String message, String path) { |
| 290 assert(_isErrorResponse(response)); | 266 assert(_isErrorResponse(response)); |
| 291 switch (response[_ERROR_RESPONSE_ERROR_TYPE]) { | 267 switch (response[_ERROR_RESPONSE_ERROR_TYPE]) { |
| 292 case _ILLEGAL_ARGUMENT_RESPONSE: | 268 case _ILLEGAL_ARGUMENT_RESPONSE: |
| 293 return new ArgumentError(); | 269 return new ArgumentError(); |
| 294 case _OSERROR_RESPONSE: | 270 case _OSERROR_RESPONSE: |
| 295 var err = new OSError(response[_OSERROR_RESPONSE_MESSAGE], | 271 var err = new OSError(response[_OSERROR_RESPONSE_MESSAGE], |
| 296 response[_OSERROR_RESPONSE_ERROR_CODE]); | 272 response[_OSERROR_RESPONSE_ERROR_CODE]); |
| 297 return new LinkException(message, path, err); | 273 return new LinkException(message, path, err); |
| 298 default: | 274 default: |
| (...skipping 23 matching lines...) Expand all Loading... |
| 322 if (path != null) { | 298 if (path != null) { |
| 323 sb.write(", path = $path"); | 299 sb.write(", path = $path"); |
| 324 } | 300 } |
| 325 } | 301 } |
| 326 return sb.toString(); | 302 return sb.toString(); |
| 327 } | 303 } |
| 328 final String message; | 304 final String message; |
| 329 final String path; | 305 final String path; |
| 330 final OSError osError; | 306 final OSError osError; |
| 331 } | 307 } |
| OLD | NEW |