| 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 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 | 161 |
| 162 Link get absolute => new Link(_absolutePath); | 162 Link get absolute => new Link(_absolutePath); |
| 163 | 163 |
| 164 Future<Link> create(String target, {bool recursive: false}) { | 164 Future<Link> create(String target, {bool recursive: false}) { |
| 165 if (Platform.isWindows) { | 165 if (Platform.isWindows) { |
| 166 target = _makeWindowsLinkTarget(target); | 166 target = _makeWindowsLinkTarget(target); |
| 167 } | 167 } |
| 168 var result = | 168 var result = |
| 169 recursive ? parent.create(recursive: true) : new Future.value(null); | 169 recursive ? parent.create(recursive: true) : new Future.value(null); |
| 170 return result | 170 return result |
| 171 .then((_) => _IOService._dispatch(_FILE_CREATE_LINK, [path, target])) | 171 .then((_) => _File |
| 172 ._dispatchWithNamespace(_FILE_CREATE_LINK, [null, path, target])) |
| 172 .then((response) { | 173 .then((response) { |
| 173 if (_isErrorResponse(response)) { | 174 if (_isErrorResponse(response)) { |
| 174 throw _exceptionFromResponse( | 175 throw _exceptionFromResponse( |
| 175 response, "Cannot create link to target '$target'", path); | 176 response, "Cannot create link to target '$target'", path); |
| 176 } | 177 } |
| 177 return this; | 178 return this; |
| 178 }); | 179 }); |
| 179 } | 180 } |
| 180 | 181 |
| 181 void createSync(String target, {bool recursive: false}) { | 182 void createSync(String target, {bool recursive: false}) { |
| 182 if (recursive) { | 183 if (recursive) { |
| 183 parent.createSync(recursive: true); | 184 parent.createSync(recursive: true); |
| 184 } | 185 } |
| 185 if (Platform.isWindows) { | 186 if (Platform.isWindows) { |
| 186 target = _makeWindowsLinkTarget(target); | 187 target = _makeWindowsLinkTarget(target); |
| 187 } | 188 } |
| 188 var result = _File._createLink(path, target); | 189 var result = _File._createLink(_Namespace._namespace, path, target); |
| 189 throwIfError(result, "Cannot create link", path); | 190 throwIfError(result, "Cannot create link", path); |
| 190 } | 191 } |
| 191 | 192 |
| 192 // Put target into the form "\??\C:\my\target\dir". | 193 // Put target into the form "\??\C:\my\target\dir". |
| 193 String _makeWindowsLinkTarget(String target) { | 194 String _makeWindowsLinkTarget(String target) { |
| 194 Uri base = new Uri.file('${Directory.current.path}\\'); | 195 Uri base = new Uri.file('${Directory.current.path}\\'); |
| 195 Uri link = new Uri.file(path); | 196 Uri link = new Uri.file(path); |
| 196 Uri destination = new Uri.file(target); | 197 Uri destination = new Uri.file(target); |
| 197 String result = base.resolveUri(link).resolveUri(destination).toFilePath(); | 198 String result = base.resolveUri(link).resolveUri(destination).toFilePath(); |
| 198 if (result.length > 3 && result[1] == ':' && result[2] == '\\') { | 199 if (result.length > 3 && result[1] == ':' && result[2] == '\\') { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 218 // Atomically changing a link can be done by creating the new link, with | 219 // Atomically changing a link can be done by creating the new link, with |
| 219 // a different name, and using the rename() posix call to move it to | 220 // a different name, and using the rename() posix call to move it to |
| 220 // the old name atomically. | 221 // the old name atomically. |
| 221 return delete().then<Link>((_) => create(target)); | 222 return delete().then<Link>((_) => create(target)); |
| 222 } | 223 } |
| 223 | 224 |
| 224 Future<Link> _delete({bool recursive: false}) { | 225 Future<Link> _delete({bool recursive: false}) { |
| 225 if (recursive) { | 226 if (recursive) { |
| 226 return new Directory(path).delete(recursive: true).then((_) => this); | 227 return new Directory(path).delete(recursive: true).then((_) => this); |
| 227 } | 228 } |
| 228 return _IOService._dispatch(_FILE_DELETE_LINK, [path]).then((response) { | 229 return _File._dispatchWithNamespace(_FILE_DELETE_LINK, [null, path]).then( |
| 230 (response) { |
| 229 if (_isErrorResponse(response)) { | 231 if (_isErrorResponse(response)) { |
| 230 throw _exceptionFromResponse(response, "Cannot delete link", path); | 232 throw _exceptionFromResponse(response, "Cannot delete link", path); |
| 231 } | 233 } |
| 232 return this; | 234 return this; |
| 233 }); | 235 }); |
| 234 } | 236 } |
| 235 | 237 |
| 236 void _deleteSync({bool recursive: false}) { | 238 void _deleteSync({bool recursive: false}) { |
| 237 if (recursive) { | 239 if (recursive) { |
| 238 return new Directory(path).deleteSync(recursive: true); | 240 return new Directory(path).deleteSync(recursive: true); |
| 239 } | 241 } |
| 240 var result = _File._deleteLinkNative(path); | 242 var result = _File._deleteLinkNative(_Namespace._namespace, path); |
| 241 throwIfError(result, "Cannot delete link", path); | 243 throwIfError(result, "Cannot delete link", path); |
| 242 } | 244 } |
| 243 | 245 |
| 244 Future<Link> rename(String newPath) { | 246 Future<Link> rename(String newPath) { |
| 245 return _IOService | 247 return _File._dispatchWithNamespace( |
| 246 ._dispatch(_FILE_RENAME_LINK, [path, newPath]).then((response) { | 248 _FILE_RENAME_LINK, [null, path, newPath]).then((response) { |
| 247 if (_isErrorResponse(response)) { | 249 if (_isErrorResponse(response)) { |
| 248 throw _exceptionFromResponse( | 250 throw _exceptionFromResponse( |
| 249 response, "Cannot rename link to '$newPath'", path); | 251 response, "Cannot rename link to '$newPath'", path); |
| 250 } | 252 } |
| 251 return new Link(newPath); | 253 return new Link(newPath); |
| 252 }); | 254 }); |
| 253 } | 255 } |
| 254 | 256 |
| 255 Link renameSync(String newPath) { | 257 Link renameSync(String newPath) { |
| 256 var result = _File._renameLink(path, newPath); | 258 var result = _File._renameLink(_Namespace._namespace, path, newPath); |
| 257 throwIfError(result, "Cannot rename link '$path' to '$newPath'"); | 259 throwIfError(result, "Cannot rename link '$path' to '$newPath'"); |
| 258 return new Link(newPath); | 260 return new Link(newPath); |
| 259 } | 261 } |
| 260 | 262 |
| 261 Future<String> target() { | 263 Future<String> target() { |
| 262 return _IOService._dispatch(_FILE_LINK_TARGET, [path]).then((response) { | 264 return _File._dispatchWithNamespace(_FILE_LINK_TARGET, [null, path]).then( |
| 265 (response) { |
| 263 if (_isErrorResponse(response)) { | 266 if (_isErrorResponse(response)) { |
| 264 throw _exceptionFromResponse( | 267 throw _exceptionFromResponse( |
| 265 response, "Cannot get target of link", path); | 268 response, "Cannot get target of link", path); |
| 266 } | 269 } |
| 267 return response; | 270 return response; |
| 268 }); | 271 }); |
| 269 } | 272 } |
| 270 | 273 |
| 271 String targetSync() { | 274 String targetSync() { |
| 272 var result = _File._linkTarget(path); | 275 var result = _File._linkTarget(_Namespace._namespace, path); |
| 273 throwIfError(result, "Cannot read link", path); | 276 throwIfError(result, "Cannot read link", path); |
| 274 return result; | 277 return result; |
| 275 } | 278 } |
| 276 | 279 |
| 277 static throwIfError(Object result, String msg, [String path = ""]) { | 280 static throwIfError(Object result, String msg, [String path = ""]) { |
| 278 if (result is OSError) { | 281 if (result is OSError) { |
| 279 throw new FileSystemException(msg, path, result); | 282 throw new FileSystemException(msg, path, result); |
| 280 } | 283 } |
| 281 } | 284 } |
| 282 | 285 |
| 283 bool _isErrorResponse(response) { | 286 bool _isErrorResponse(response) { |
| 284 return response is List && response[0] != _SUCCESS_RESPONSE; | 287 return response is List && response[0] != _SUCCESS_RESPONSE; |
| 285 } | 288 } |
| 286 | 289 |
| 287 _exceptionFromResponse(response, String message, String path) { | 290 _exceptionFromResponse(response, String message, String path) { |
| 288 assert(_isErrorResponse(response)); | 291 assert(_isErrorResponse(response)); |
| 289 switch (response[_ERROR_RESPONSE_ERROR_TYPE]) { | 292 switch (response[_ERROR_RESPONSE_ERROR_TYPE]) { |
| 290 case _ILLEGAL_ARGUMENT_RESPONSE: | 293 case _ILLEGAL_ARGUMENT_RESPONSE: |
| 291 return new ArgumentError(); | 294 return new ArgumentError(); |
| 292 case _OSERROR_RESPONSE: | 295 case _OSERROR_RESPONSE: |
| 293 var err = new OSError(response[_OSERROR_RESPONSE_MESSAGE], | 296 var err = new OSError(response[_OSERROR_RESPONSE_MESSAGE], |
| 294 response[_OSERROR_RESPONSE_ERROR_CODE]); | 297 response[_OSERROR_RESPONSE_ERROR_CODE]); |
| 295 return new FileSystemException(message, path, err); | 298 return new FileSystemException(message, path, err); |
| 296 default: | 299 default: |
| 297 return new Exception("Unknown error"); | 300 return new Exception("Unknown error"); |
| 298 } | 301 } |
| 299 } | 302 } |
| 300 } | 303 } |
| OLD | NEW |