OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 part of dart.io; |
| 6 |
| 7 /** |
| 8 * [Link] objects are references to filesystem links. |
| 9 * |
| 10 */ |
| 11 abstract class Link extends FileSystemEntity { |
| 12 /** |
| 13 * Create a Link object. |
| 14 */ |
| 15 factory Link(String path) => new _Link(path); |
| 16 |
| 17 /** |
| 18 * Create a Link object from a Path object. |
| 19 */ |
| 20 factory Link.fromPath(Path path) => new _Link.fromPath(path); |
| 21 |
| 22 /** |
| 23 * Check if the link exists. The link may exist, even if its target |
| 24 * is missing or deleted. |
| 25 * Returns a [:Future<bool>:] that completes when the answer is known. |
| 26 */ |
| 27 Future<bool> exists(); |
| 28 |
| 29 /** |
| 30 * Synchronously check if the link exists. The link may exist, even if |
| 31 * its target is missing or deleted. |
| 32 */ |
| 33 bool existsSync(); |
| 34 |
| 35 /** |
| 36 * Create a symbolic link. Returns a [:Future<Link>:] that completes with |
| 37 * the link when it has been created. If the link exists, the function |
| 38 * the future will complete with an error. |
| 39 * |
| 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. |
| 42 * Only absolute links will be created, and relative paths to the target |
| 43 * will be converted to absolute paths. |
| 44 */ |
| 45 Future<Link> create(String target); |
| 46 |
| 47 /** |
| 48 * Synchronously create the link. Calling [createSync] on an existing link |
| 49 * will throw an exception. |
| 50 * |
| 51 * 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. |
| 53 * Only absolute links will be created, and relative paths to the target |
| 54 * will be converted to absolute paths. |
| 55 */ |
| 56 void createSync(String target); |
| 57 |
| 58 /** |
| 59 * Synchronously update the link. Calling [updateSync] on a non-existing link |
| 60 * will throw an exception. |
| 61 * |
| 62 * 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 |
| 64 * directory. |
| 65 * |
| 66 * On the Windows platform, this will only work with directories, and the |
| 67 * target directory must exist. |
| 68 */ |
| 69 void updateSync(String target, {bool linkRelative: false }); |
| 70 |
| 71 /** |
| 72 * Delete the link. Returns a [:Future<Link>:] that completes with |
| 73 * the link when it has been deleted. This does not delete, or otherwise |
| 74 * affect, the target of the link. |
| 75 */ |
| 76 Future<Link> delete(); |
| 77 |
| 78 /** |
| 79 * Synchronously delete the link. This does not delete, or otherwise |
| 80 * affect, the target of the link. |
| 81 */ |
| 82 void deleteSync(); |
| 83 } |
| 84 |
| 85 |
| 86 class _Link extends FileSystemEntity implements Link { |
| 87 final String path; |
| 88 |
| 89 _Link(String this.path); |
| 90 |
| 91 _Link.fromPath(Path inputPath) : path = inputPath.toNativePath(); |
| 92 |
| 93 Future<bool> exists() { |
| 94 // TODO(whesse): Replace with asynchronous version. |
| 95 return new Future.immediate(existsSync()); |
| 96 } |
| 97 |
| 98 bool existsSync() => FileSystemEntity.isLinkSync(path); |
| 99 |
| 100 Future<Link> create(String target) { |
| 101 // TODO(whesse): Replace with asynchronous version. |
| 102 return new Future.of(() { |
| 103 createSync(target); |
| 104 return this; |
| 105 }); |
| 106 } |
| 107 |
| 108 void createSync(String target) { |
| 109 if (Platform.operatingSystem == 'windows') { |
| 110 target = _makeWindowsLinkTarget(target); |
| 111 } |
| 112 var result = _File._createLink(path, target); |
| 113 if (result is Error) { |
| 114 throw new LinkIOException("Error in Link.createSync", result); |
| 115 } |
| 116 } |
| 117 |
| 118 // Put target into the form "\??\C:\my\target\dir". |
| 119 String _makeWindowsLinkTarget(String target) { |
| 120 if (target.startsWith('\\??\\')) { |
| 121 return target; |
| 122 } |
| 123 if (!(target.length > 3 && target[1] == ':' && target[2] == '\\')) { |
| 124 target = new File(target).fullPathSync(); |
| 125 } |
| 126 if (target.length > 3 && target[1] == ':' && target[2] == '\\') { |
| 127 target = '\\??\\$target'; |
| 128 } else { |
| 129 throw new ArgumentError( |
| 130 'Target $target of Link.create on Windows cannot be converted' + |
| 131 ' to start with a drive letter. Unexpected error.'); |
| 132 } |
| 133 return target; |
| 134 } |
| 135 |
| 136 void updateSync(String target, {bool linkRelative: false }) { |
| 137 // TODO(whesse): Replace with atomic update, where supported by platform. |
| 138 deleteSync(); |
| 139 createSync(target); |
| 140 } |
| 141 |
| 142 Future<Link> delete() { |
| 143 return new File(path).delete().then((_) => this); |
| 144 } |
| 145 |
| 146 void deleteSync() { |
| 147 new File(path).deleteSync(); |
| 148 } |
| 149 } |
| 150 |
| 151 |
| 152 class LinkIOException implements Exception { |
| 153 const LinkIOException([String this.message = "", |
| 154 String this.path = "", |
| 155 OSError this.osError = null]); |
| 156 String toString() { |
| 157 StringBuffer sb = new StringBuffer(); |
| 158 sb.write("LinkIOException"); |
| 159 if (!message.isEmpty) { |
| 160 sb.write(": $message"); |
| 161 if (path != null) { |
| 162 sb.write(", path = $path"); |
| 163 } |
| 164 if (osError != null) { |
| 165 sb.write(" ($osError)"); |
| 166 } |
| 167 } else if (osError != null) { |
| 168 sb.write(": $osError"); |
| 169 if (path != null) { |
| 170 sb.write(", path = $path"); |
| 171 } |
| 172 } |
| 173 return sb.toString(); |
| 174 } |
| 175 final String message; |
| 176 final String path; |
| 177 final OSError osError; |
| 178 } |
OLD | NEW |