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 * If [linkRelative] is true, the target argument should be a relative path, |
| 41 * and the link will interpret the target as a path relative to the link's |
| 42 * directory. |
| 43 * |
| 44 * On the Windows platform, this will only work with directories, and the |
| 45 * target directory must exist. The link will be created as a Junction. |
| 46 */ |
| 47 Future<Link> create(String target, {bool linkRelative: false }); |
| 48 |
| 49 /** |
| 50 * Synchronously create the link. Calling [createSync] on an existing link |
| 51 * will throw an exception. |
| 52 * |
| 53 * If [linkRelative] is true, the target argument should be a relative path, |
| 54 * and the link will interpret the target as a path relative to the link's |
| 55 * directory. |
| 56 * |
| 57 * If [linkRelative] is false, the target argument will be turned into an |
| 58 * absolute path, and the target will be that absolute path. |
| 59 * |
| 60 * On the Windows platform, this will only work with directories, and the |
| 61 * target directory must exist. The link will be created as a Junction. |
| 62 */ |
| 63 void createSync(String target, {bool linkRelative: false }); |
| 64 |
| 65 /** |
| 66 * Synchronously update the link. Calling [updateSync] on a non-existing link |
| 67 * will throw an exception. |
| 68 * |
| 69 * If [linkRelative] is true, the target argument should be a relative path, |
| 70 * and the link will interpret the target as a path relative to the link's |
| 71 * directory. |
| 72 * |
| 73 * On the Windows platform, this will only work with directories, and the |
| 74 * target directory must exist. |
| 75 */ |
| 76 void updateSync(String target, {bool linkRelative: false }); |
| 77 |
| 78 /** |
| 79 * Delete the link. Returns a [:Future<Link>:] that completes with |
| 80 * the link when it has been deleted. This does not delete, or otherwise |
| 81 * affect, the target of the link. |
| 82 */ |
| 83 Future<Link> delete(); |
| 84 |
| 85 /** |
| 86 * Synchronously delete the link. This does not delete, or otherwise |
| 87 * affect, the target of the link. |
| 88 */ |
| 89 void deleteSync(); |
| 90 } |
| 91 |
| 92 |
| 93 class _Link extends FileSystemEntity implements Link { |
| 94 final String path; |
| 95 |
| 96 _Link(String this.path); |
| 97 |
| 98 _Link.fromPath(Path inputPath) : path = inputPath.toNativePath(); |
| 99 |
| 100 Future<bool> exists() { |
| 101 // TODO(whesse): Replace with asynchronous version. |
| 102 return new Future.immediate(existsSync()); |
| 103 } |
| 104 |
| 105 bool existsSync() => FileSystemEntity.isLinkSync(path); |
| 106 |
| 107 Future<Link> create(String target) { |
| 108 // TODO(whesse): Replace with asynchronous version. |
| 109 return new Future.of(() { |
| 110 createSync(target); |
| 111 return this; |
| 112 }); |
| 113 } |
| 114 |
| 115 void createSync(String target) { |
| 116 if (Platform.operatingSystem == 'windows') { |
| 117 target = _makeWindowsLinkTarget(target); |
| 118 } |
| 119 var result = _File._createLink(path, target); |
| 120 if (result is Error) { |
| 121 throw new LinkIOException("Error in Link.createSync", result); |
| 122 } |
| 123 } |
| 124 |
| 125 // Put target into the form "\??\C:\my\target\dir". |
| 126 String _makeWindowsLinkTarget(String target) { |
| 127 if (!target.startsWith('\\??\\')) { |
| 128 return target; |
| 129 } |
| 130 if (!(target.length > 3 && target[1] == ':' && target[2] == '\\')) { |
| 131 target = new File(target).fullPathSync(); |
| 132 } |
| 133 if (target.length > 3 && target[1] == ':' && target[2] == '\\') |
| 134 target = '\\??\\$target'; |
| 135 } else { |
| 136 throw new ArgumentError( |
| 137 'Target $target of Link.create on Windows cannot be converted' + |
| 138 ' to start with a drive letter. Unexpected error.'); |
| 139 } |
| 140 return target; |
| 141 } |
| 142 |
| 143 void updateSync(String target, {bool linkRelative: false }) { |
| 144 // TODO(whesse): Replace with atomic update, where supported by platform. |
| 145 deleteSync(); |
| 146 createSync(target, linkRelative: linkRelative); |
| 147 } |
| 148 |
| 149 Future<Link> delete() { |
| 150 return new File(path).delete().then((_) => this); |
| 151 } |
| 152 |
| 153 void deleteSync() { |
| 154 new File(path).deleteSync(); |
| 155 } |
| 156 } |
| 157 |
| 158 |
| 159 class LinkIOException implements Exception { |
| 160 const LinkIOException([String this.message = "", |
| 161 String this.path = "", |
| 162 OSError this.osError = null]); |
| 163 String toString() { |
| 164 StringBuffer sb = new StringBuffer(); |
| 165 sb.write("LinkIOException"); |
| 166 if (!message.isEmpty) { |
| 167 sb.write(": $message"); |
| 168 if (path != null) { |
| 169 sb.write(", path = $path"); |
| 170 } |
| 171 if (osError != null) { |
| 172 sb.write(" ($osError)"); |
| 173 } |
| 174 } else if (osError != null) { |
| 175 sb.write(": $osError"); |
| 176 if (path != null) { |
| 177 sb.write(", path = $path"); |
| 178 } |
| 179 } |
| 180 return sb.toString(); |
| 181 } |
| 182 final String message; |
| 183 final String path; |
| 184 final OSError osError; |
| 185 } |
OLD | NEW |