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

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

Issue 12691002: dart:io | Add Link class, as sibling to File and Directory. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Implement Link.CreateSync, use it in file_system_links_test. 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
(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.
nweiz 2013/03/08 20:26:41 Explain what [target] is relative to if [linkRelat
Bill Hesse 2013/03/11 09:25:59 Done.
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.
nweiz 2013/03/08 20:26:41 Is it possible to create actual symlinks on Window
Bill Hesse 2013/03/11 09:25:59 Eventually, we want to provide access to all types
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 * On the Windows platform, this will only work with directories, and the
58 * target directory must exist. The link will be created as a Junction.
59 */
60 void createSync(String target, {bool linkRelative: false });
61
62 /**
63 * Updates a link to point to a different target.
64 * Returns a [:Future<Link>:] that completes with
65 * the link when it has been created. If the link does not exist,
66 * the future completes with an exception.
67 *
68 * If [linkRelative] is true, the target argument should be a relative path,
69 * and the link will interpret the target as a path relative to the link's
70 * directory.
71 *
72 * On the Windows platform, this will only work with directories, and the
73 * target directory must exist.
74 */
75 Future<Link> update(String target, {bool linkRelative: false });
76
77 /**
78 * Synchronously update the link. Calling [updateSync] on a non-existing link
79 * will throw an exception.
80 *
81 * If [linkRelative] is true, the target argument should be a relative path,
82 * and the link will interpret the target as a path relative to the link's
83 * directory.
84 *
85 * On the Windows platform, this will only work with directories, and the
86 * target directory must exist.
87 */
88 void updateSync(String target, {bool linkRelative: false });
89
90 /**
91 * Delete the link. Returns a [:Future<Link>:] that completes with
92 * the link when it has been deleted. This does not delete, or otherwise
93 * affect, the target of the link.
94 */
95 Future<Link> delete();
96
97 /**
98 * Synchronously delete the link. This does not delete, or otherwise
99 * affect, the target of the link.
100 */
101 void deleteSync();
102
103 /**
104 * Get a [Directory] object for the directory containing this
105 * link. This is the directory containing the link, not the directory
106 * containing the target of the link. Returns a [:Future<Directory>:]
107 * that completes with the directory.
108 */
109 Future<Directory> directory();
110
111 /**
112 * Synchronously get a [Directory] object for the directory containing
113 * this link. This is the directory containing the link, not the directory
114 * containing the target of the link.
115 */
116 Directory directorySync();
117
118 /**
119 * Get the target of the link. Returns a future that completes with
120 * the path to the target.
121 *
122 * If [linkRelative] is true and the link target is a path relative to
123 * the link's directory, the future will complete with that relative path.
124 * Otherwise, the future completes with an error.
nweiz 2013/03/08 20:26:41 Again, explain what happens if the target is relat
Bill Hesse 2013/03/11 09:25:59 This was intended as an initial implementation, wi
125 */
126 Future<String> target({bool linkRelative: false });
127
128 /**
129 * Synchronously get the target of the link. Returns the path to the target.
130 *
131 * If [linkRelative] is true and the link target is a path relative to
132 * the link's directory, that relative path will be returned. Otherwise,
133 * an exception is thrown.
134 */
135 String targetSync({bool linkRelative: false });
136
137 /**
138 * Get the canonical full path corresponding to the link path.
139 * Returns a [:Future<String>:] that completes with the path.
140 */
141 Future<String> fullPath();
142
143 /**
144 * Synchronously get the canonical full path corresponding to the link path.
145 */
146 String fullPathSync();
147 }
148
149
150 class _Link extends FileSystemEntity {
151 final String path;
152
153 _Link(String this.path);
154
155 _Link.fromPath(Path inputPath) : path = inputPath.toNativePath();
156
157 Future<bool> exists() {
158 // TODO(whesse): Replace with asynchronous version.
159 return new Future.of(existsSync());
160 }
161
162 bool existsSync() => FileSystemEntity.isLinkSync(path);
163
164 Future<Link> create(String target, {bool linkRelative: false }) {
165 // TODO(whesse): Replace with asynchronous version.
166 return new Future.of(() {
167 createSync(target, linkRelative: linkRelative);
168 return this;
169 });
170 }
171
172 void createSync(String target, {bool linkRelative: false }) {
173 if (!new Path(target).isAbsolute && !linkRelative) {
174 throw new UnimplementedError(
175 "Link.create with relative path must be linkRelative");
nweiz 2013/03/08 20:26:41 This is not user-friendly behavior. This puts a lo
Bill Hesse 2013/03/11 09:25:59 This CL was intended as a partial, initial impleme
176 }
177 var result = _File._createLink(path, target);
178 if (result is OSError) {
179 throw new LinkIOException("Error in Link.createSync", result);
180 }
181 }
182
183 Future<Link> update(String target, {bool linkRelative: false }) {
184 delete().then((link) {
nweiz 2013/03/08 20:26:41 return
185 link.create(target, linkRelative: linkRelative);
186 });
nweiz 2013/03/08 20:26:41 It worries me that this isn't atomic. It could lea
Bill Hesse 2013/03/11 09:25:59 This was just a temporary implementation, but you
187 }
188
189 void updateSync(String target, {bool linkRelative: false }) {
190 deleteSync();
191 createSync(target, linkRelative: linkRelative);
192 }
193
194 Future<Link> delete() {
195 return new File(path).delete().then((_) => this);
196 }
197
198 void deleteSync() {
199 new File(path).deleteSync();
200 }
201 }
nweiz 2013/03/08 20:26:41 It would be nice to have a [follow] method that re
Bill Hesse 2013/03/11 09:25:59 Do you mean "transitively follow links, until a no
nweiz 2013/03/11 20:26:32 No, I meant just once, although a transitive versi
202
203
204 class LinkIOException implements Exception {
205 const LinkIOException([String this.message = "",
206 String this.path = "",
207 OSError this.osError = null]);
208 String toString() {
209 StringBuffer sb = new StringBuffer();
210 sb.write("LinkIOException");
211 if (!message.isEmpty) {
212 sb.write(": $message");
213 if (path != null) {
214 sb.write(", path = $path");
215 }
216 if (osError != null) {
217 sb.write(" ($osError)");
218 }
219 } else if (osError != null) {
220 sb.write(": $osError");
221 if (path != null) {
222 sb.write(", path = $path");
223 }
224 }
225 return sb.toString();
226 }
227 final String message;
228 final String path;
229 final OSError osError;
230 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698