Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(413)

Side by Side Diff: sdk/lib/io/link.dart

Issue 12812010: dart:io | Add Link.targetSync on all platforms. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Improve copying of target string on Windows. Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 */
11 abstract class Link extends FileSystemEntity { 11 abstract class Link extends FileSystemEntity {
12 /** 12 /**
13 * Create a Link object. 13 * Creates a Link object.
14 */ 14 */
15 factory Link(String path) => new _Link(path); 15 factory Link(String path) => new _Link(path);
16 16
17 /** 17 /**
18 * Create a Link object from a Path object. 18 * Creates a Link object from a Path object.
19 */ 19 */
20 factory Link.fromPath(Path path) => new _Link.fromPath(path); 20 factory Link.fromPath(Path path) => new _Link.fromPath(path);
21 21
22 /** 22 /**
23 * Check if the link exists. The link may exist, even if its target 23 * Checks if the link exists. The link may exist, even if its target
24 * is missing or deleted. 24 * is missing or deleted.
25 * Returns a [:Future<bool>:] that completes when the answer is known. 25 * Returns a [:Future<bool>:] that completes when the answer is known.
26 */ 26 */
27 Future<bool> exists(); 27 Future<bool> exists();
28 28
29 /** 29 /**
30 * Synchronously check 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 * Create 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, the function
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 */ 44 */
45 Future<Link> create(String target); 45 Future<Link> create(String target);
46 46
47 /** 47 /**
48 * Synchronously create the link. Calling [createSync] on an existing link 48 * Synchronously create the link. Calling [createSync] on an existing link
49 * will throw an exception. 49 * will throw an exception.
50 * 50 *
51 * On the Windows platform, this will only work with directories, and the 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. 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 53 * Only absolute links will be created, and relative paths to the target
54 * will be converted to absolute paths. 54 * will be converted to absolute paths.
55 */ 55 */
56 void createSync(String target); 56 void createSync(String target);
57 57
58 /** 58 /**
59 * Synchronously update the link. Calling [updateSync] on a non-existing link 59 * Synchronously updates the link. Calling [updateSync] on a non-existing link
60 * will throw an exception. 60 * will throw an exception.
61 * 61 *
62 * If [linkRelative] is true, the target argument should be a relative path, 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 63 * and the link will interpret the target as a path relative to the link's
64 * directory. 64 * directory.
65 * 65 *
66 * On the Windows platform, this will only work with directories, and the 66 * On the Windows platform, this will only work with directories, and the
67 * target directory must exist. 67 * target directory must exist.
68 */ 68 */
69 void updateSync(String target, {bool linkRelative: false }); 69 void updateSync(String target, {bool linkRelative: false });
70 70
71 /** 71 /**
72 * Delete the link. Returns a [:Future<Link>:] that completes with 72 * Deletes the link. Returns a [:Future<Link>:] that completes with
73 * the link when it has been deleted. This does not delete, or otherwise 73 * the link when it has been deleted. This does not delete, or otherwise
74 * affect, the target of the link. 74 * affect, the target of the link.
75 */ 75 */
76 Future<Link> delete(); 76 Future<Link> delete();
77 77
78 /** 78 /**
79 * Synchronously delete the link. This does not delete, or otherwise 79 * Synchronously deletes the link. This does not delete, or otherwise
80 * affect, the target of the link. 80 * affect, the target of the link.
81 */ 81 */
82 void deleteSync(); 82 void deleteSync();
83
84 /**
85 * Gets the target of the link. Returns a future that completes with
86 * the path to the target.
87 *
88 * If the returned target is a relative path, it is relative to the
89 * directory containing the link.
90 *
91 * If the link does not exist, or is not a link, the future completes with
92 * a LinkIOException.
93 */
94 Future<String> target();
95
96 /**
97 * Synchronously gets the target of the link. Returns the path to the target.
98 *
99 * If the returned target is a relative path, it is relative to the
100 * directory containing the link.
101 *
102 * If the link does not exist, or is not a link, throws a LinkIOException.
103 */
104 String targetSync();
83 } 105 }
84 106
85 107
86 class _Link extends FileSystemEntity implements Link { 108 class _Link extends FileSystemEntity implements Link {
87 final String path; 109 final String path;
88 110
89 _Link(String this.path); 111 _Link(String this.path);
90 112
91 _Link.fromPath(Path inputPath) : path = inputPath.toNativePath(); 113 _Link.fromPath(Path inputPath) : path = inputPath.toNativePath();
92 114
(...skipping 10 matching lines...) Expand all
103 createSync(target); 125 createSync(target);
104 return this; 126 return this;
105 }); 127 });
106 } 128 }
107 129
108 void createSync(String target) { 130 void createSync(String target) {
109 if (Platform.operatingSystem == 'windows') { 131 if (Platform.operatingSystem == 'windows') {
110 target = _makeWindowsLinkTarget(target); 132 target = _makeWindowsLinkTarget(target);
111 } 133 }
112 var result = _File._createLink(path, target); 134 var result = _File._createLink(path, target);
113 if (result is Error) { 135 if (result is OSError) {
114 throw new LinkIOException("Error in Link.createSync", result); 136 throw new LinkIOException("Error in Link.createSync", result);
115 } 137 }
116 } 138 }
117 139
118 // Put target into the form "\??\C:\my\target\dir". 140 // Put target into the form "\??\C:\my\target\dir".
119 String _makeWindowsLinkTarget(String target) { 141 String _makeWindowsLinkTarget(String target) {
120 if (target.startsWith('\\??\\')) { 142 if (target.startsWith('\\??\\')) {
121 return target; 143 return target;
122 } 144 }
123 if (!(target.length > 3 && target[1] == ':' && target[2] == '\\')) { 145 if (!(target.length > 3 && target[1] == ':' && target[2] == '\\')) {
(...skipping 15 matching lines...) Expand all
139 createSync(target); 161 createSync(target);
140 } 162 }
141 163
142 Future<Link> delete() { 164 Future<Link> delete() {
143 return new File(path).delete().then((_) => this); 165 return new File(path).delete().then((_) => this);
144 } 166 }
145 167
146 void deleteSync() { 168 void deleteSync() {
147 new File(path).deleteSync(); 169 new File(path).deleteSync();
148 } 170 }
171
172 Future<String> target() {
173 // TODO(whesse): Replace with asynchronous version.
174 return new Future.of(targetSync);
175 }
176
177 String targetSync() {
178 var result = _File._linkTarget(path);
179 if (result is OSError) {
180 throw new LinkIOException("Error in Link.targetSync", result);
181 }
182 return result;
183 }
149 } 184 }
150 185
151 186
152 class LinkIOException implements Exception { 187 class LinkIOException implements Exception {
153 const LinkIOException([String this.message = "", 188 const LinkIOException([String this.message = "",
154 String this.path = "", 189 String this.path = "",
155 OSError this.osError = null]); 190 OSError this.osError = null]);
156 String toString() { 191 String toString() {
157 StringBuffer sb = new StringBuffer(); 192 StringBuffer sb = new StringBuffer();
158 sb.write("LinkIOException"); 193 sb.write("LinkIOException");
(...skipping 10 matching lines...) Expand all
169 if (path != null) { 204 if (path != null) {
170 sb.write(", path = $path"); 205 sb.write(", path = $path");
171 } 206 }
172 } 207 }
173 return sb.toString(); 208 return sb.toString();
174 } 209 }
175 final String message; 210 final String message;
176 final String path; 211 final String path;
177 final OSError osError; 212 final OSError osError;
178 } 213 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698