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 }); | |
Søren Gjesse
2013/03/12 08:41:49
I think we agreed on getting rid of linkRelative a
Bill Hesse
2013/03/13 16:17:38
Done.
| |
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 * Updates a link to point to a different target. | |
67 * Returns a [:Future<Link>:] that completes with | |
68 * the link when it has been created. If the link does not exist, | |
69 * the future completes with an exception. | |
70 * | |
71 * If [linkRelative] is true, the target argument should be a relative path, | |
72 * and the link will interpret the target as a path relative to the link's | |
73 * directory. | |
74 * | |
75 * On the Windows platform, this will only work with directories, and the | |
76 * target directory must exist. | |
77 */ | |
78 Future<Link> update(String target, {bool linkRelative: false }); | |
Søren Gjesse
2013/03/12 08:41:49
Please remove all the methods not implemented, and
Bill Hesse
2013/03/13 16:17:38
Done.
| |
79 | |
80 /** | |
81 * Synchronously update the link. Calling [updateSync] on a non-existing link | |
82 * will throw an exception. | |
83 * | |
84 * If [linkRelative] is true, the target argument should be a relative path, | |
85 * and the link will interpret the target as a path relative to the link's | |
86 * directory. | |
87 * | |
88 * On the Windows platform, this will only work with directories, and the | |
89 * target directory must exist. | |
90 */ | |
91 void updateSync(String target, {bool linkRelative: false }); | |
92 | |
93 /** | |
94 * Delete the link. Returns a [:Future<Link>:] that completes with | |
95 * the link when it has been deleted. This does not delete, or otherwise | |
96 * affect, the target of the link. | |
97 */ | |
98 Future<Link> delete(); | |
99 | |
100 /** | |
101 * Synchronously delete the link. This does not delete, or otherwise | |
102 * affect, the target of the link. | |
103 */ | |
104 void deleteSync(); | |
105 | |
106 /** | |
107 * Get a [Directory] object for the directory containing this | |
108 * link. This is the directory containing the link, not the directory | |
109 * containing the target of the link. Returns a [:Future<Directory>:] | |
110 * that completes with the directory. | |
111 */ | |
112 Future<Directory> directory(); | |
113 | |
114 /** | |
115 * Synchronously get a [Directory] object for the directory containing | |
116 * this link. This is the directory containing the link, not the directory | |
117 * containing the target of the link. | |
118 */ | |
119 Directory directorySync(); | |
120 | |
121 /** | |
122 * Get the target of the link. Returns a future that completes with | |
123 * the path to the target. | |
124 * | |
125 * If [linkRelative] is true and the link target is a path relative to | |
126 * the link's directory, the future will complete with that relative path. | |
127 * Otherwise, the future completes with an error. | |
128 * | |
129 * If [linkRelative] is false, the future completes with an absolute path | |
130 * to the target. | |
131 */ | |
132 Future<String> target({bool linkRelative: false }); | |
133 | |
134 /** | |
135 * Synchronously get the target of the link. Returns the path to the target. | |
136 * | |
137 * If [linkRelative] is true and the link target is a path relative to | |
138 * the link's directory, that relative path will be returned. Otherwise, | |
139 * an exception is thrown. | |
140 */ | |
141 String targetSync({bool linkRelative: false }); | |
142 | |
143 /** | |
144 * Get the canonical full path corresponding to the link path. | |
145 * Returns a [:Future<String>:] that completes with the path. | |
146 */ | |
147 Future<String> fullPath(); | |
148 | |
149 /** | |
150 * Synchronously get the canonical full path corresponding to the link path. | |
151 */ | |
152 String fullPathSync(); | |
153 } | |
154 | |
155 | |
156 class _Link extends FileSystemEntity implements Link { | |
157 final String path; | |
158 | |
159 _Link(String this.path); | |
160 | |
161 _Link.fromPath(Path inputPath) : path = inputPath.toNativePath(); | |
162 | |
163 Future<bool> exists() { | |
164 // TODO(whesse): Replace with asynchronous version. | |
165 return new Future.immediate(existsSync()); | |
166 } | |
167 | |
168 bool existsSync() => FileSystemEntity.isLinkSync(path); | |
169 | |
170 Future<Link> create(String target, {bool linkRelative: false }) { | |
171 // TODO(whesse): Replace with asynchronous version. | |
172 return new Future.of(() { | |
173 createSync(target, linkRelative: linkRelative); | |
174 return this; | |
175 }); | |
176 } | |
177 | |
178 void createSync(String target, {bool linkRelative: false }) { | |
179 if (!new Path(target).isAbsolute && !linkRelative) { | |
180 throw new UnimplementedError( | |
181 "Link.create with relative path must be linkRelative"); | |
182 } | |
183 var result = _File._createLink(path, target); | |
184 if (result is OSError) { | |
185 throw new LinkIOException("Error in Link.createSync", result); | |
186 } | |
187 } | |
188 | |
189 Future<Link> update(String target, {bool linkRelative: false }) { | |
190 throw new UnimplementedError( | |
191 'Asynchronous, atomic Link.update not yet implemented'); | |
192 } | |
193 | |
194 void updateSync(String target, {bool linkRelative: false }) { | |
195 deleteSync(); | |
196 createSync(target, linkRelative: linkRelative); | |
197 } | |
198 | |
199 Future<Link> delete() { | |
200 return new File(path).delete().then((_) => this); | |
201 } | |
202 | |
203 void deleteSync() { | |
204 new File(path).deleteSync(); | |
205 } | |
206 | |
207 Future<Directory> directory() { | |
208 throw new UnimplementedError('Link.directory not yet implemented'); | |
209 } | |
210 | |
211 Directory directorySync() { | |
212 throw new UnimplementedError('Link.directorySync not yet implemented'); | |
213 } | |
214 | |
215 Future<String> target({bool linkRelative: false }) { | |
216 throw new UnimplementedError('Link.target not yet implemented'); | |
217 } | |
218 | |
219 String targetSync({bool linkRelative: false }) { | |
220 throw new UnimplementedError('Link.targetSync not yet implemented'); | |
221 } | |
222 | |
223 Future<String> fullPath() { | |
224 throw new UnimplementedError('Link.fullPath not yet implemented'); | |
225 } | |
226 | |
227 String fullPathSync() { | |
228 throw new UnimplementedError('Link.fullPathSync not yet implemented'); | |
229 } | |
230 } | |
231 | |
232 | |
233 class LinkIOException implements Exception { | |
234 const LinkIOException([String this.message = "", | |
235 String this.path = "", | |
236 OSError this.osError = null]); | |
237 String toString() { | |
238 StringBuffer sb = new StringBuffer(); | |
239 sb.write("LinkIOException"); | |
240 if (!message.isEmpty) { | |
241 sb.write(": $message"); | |
242 if (path != null) { | |
243 sb.write(", path = $path"); | |
244 } | |
245 if (osError != null) { | |
246 sb.write(" ($osError)"); | |
247 } | |
248 } else if (osError != null) { | |
249 sb.write(": $osError"); | |
250 if (path != null) { | |
251 sb.write(", path = $path"); | |
252 } | |
253 } | |
254 return sb.toString(); | |
255 } | |
256 final String message; | |
257 final String path; | |
258 final OSError osError; | |
259 } | |
OLD | NEW |