1 | /* |
---|
2 | * Pathname.java |
---|
3 | * |
---|
4 | * Copyright (C) 2003-2007 Peter Graves |
---|
5 | * $Id: Pathname.java 15680 2023-04-01 14:36:04Z mevenson $ |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License |
---|
9 | * as published by the Free Software Foundation; either version 2 |
---|
10 | * of the License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
20 | * |
---|
21 | * As a special exception, the copyright holders of this library give you |
---|
22 | * permission to link this library with independent modules to produce an |
---|
23 | * executable, regardless of the license terms of these independent |
---|
24 | * modules, and to copy and distribute the resulting executable under |
---|
25 | * terms of your choice, provided that you also meet, for each linked |
---|
26 | * independent module, the terms and conditions of the license of that |
---|
27 | * module. An independent module is a module which is not derived from |
---|
28 | * or based on this library. If you modify this library, you may extend |
---|
29 | * this exception to your version of the library, but you are not |
---|
30 | * obligated to do so. If you do not wish to do so, delete this |
---|
31 | * exception statement from your version. |
---|
32 | */ |
---|
33 | package org.armedbear.lisp; |
---|
34 | |
---|
35 | import static org.armedbear.lisp.Lisp.*; |
---|
36 | |
---|
37 | import java.io.*; |
---|
38 | import java.net.MalformedURLException; |
---|
39 | import java.net.URI; |
---|
40 | import java.net.URISyntaxException; |
---|
41 | import java.net.URL; |
---|
42 | import java.text.MessageFormat; |
---|
43 | import java.util.Enumeration; |
---|
44 | import java.util.StringTokenizer; |
---|
45 | import java.util.zip.ZipEntry; |
---|
46 | import java.util.zip.ZipFile; |
---|
47 | import java.util.zip.ZipInputStream; |
---|
48 | |
---|
49 | public class Pathname extends LispObject |
---|
50 | implements Serializable |
---|
51 | { |
---|
52 | protected static Pathname create() { |
---|
53 | return new Pathname(); |
---|
54 | } |
---|
55 | |
---|
56 | public static Pathname create(Pathname p) { |
---|
57 | if (p instanceof JarPathname) { |
---|
58 | return JarPathname.create((JarPathname)p); |
---|
59 | } else if (p instanceof URLPathname) { |
---|
60 | return URLPathname.create((URLPathname)p); |
---|
61 | } else if (p instanceof LogicalPathname) { |
---|
62 | return LogicalPathname.create((LogicalPathname)p); |
---|
63 | } else { |
---|
64 | return new Pathname((Pathname)p); |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | public static Pathname create(String s) { |
---|
69 | // TODO distinguish between logical hosts and schemes for URLs |
---|
70 | // which we can meaningfully parse. |
---|
71 | |
---|
72 | if (s.startsWith(JarPathname.JAR_URI_PREFIX)) { |
---|
73 | return JarPathname.create(s); |
---|
74 | } |
---|
75 | if (isValidURL(s)) { |
---|
76 | return URLPathname.create(s); |
---|
77 | } else { |
---|
78 | if (LogicalPathname.isValidLogicalPathname(s)) { |
---|
79 | return LogicalPathname.create(s); |
---|
80 | } |
---|
81 | } |
---|
82 | Pathname result = Pathname.init(s); |
---|
83 | |
---|
84 | return result; |
---|
85 | } |
---|
86 | |
---|
87 | public static Pathname create(String s, String host) { |
---|
88 | return LogicalPathname.create(s, host); |
---|
89 | } |
---|
90 | |
---|
91 | protected LispObject host = NIL; |
---|
92 | public LispObject getHost() { |
---|
93 | return host; |
---|
94 | } |
---|
95 | public Pathname setHost(LispObject host) { |
---|
96 | this.host = host; |
---|
97 | return this; |
---|
98 | } |
---|
99 | |
---|
100 | protected LispObject device = NIL; |
---|
101 | public final LispObject getDevice() { |
---|
102 | return device; |
---|
103 | } |
---|
104 | public Pathname setDevice(LispObject device) { |
---|
105 | this.device = device; |
---|
106 | return this; |
---|
107 | } |
---|
108 | |
---|
109 | protected LispObject directory = NIL; |
---|
110 | public LispObject getDirectory() { |
---|
111 | return directory; |
---|
112 | } |
---|
113 | public Pathname setDirectory(LispObject directory) { |
---|
114 | this.directory = directory; |
---|
115 | return this; |
---|
116 | } |
---|
117 | |
---|
118 | protected LispObject name = NIL; |
---|
119 | public LispObject getName() { |
---|
120 | return name; |
---|
121 | } |
---|
122 | public Pathname setName(LispObject name) { |
---|
123 | this.name = name; |
---|
124 | return this; |
---|
125 | } |
---|
126 | |
---|
127 | /** A string, NIL, :WILD or :UNSPECIFIC. */ |
---|
128 | protected LispObject type = NIL; |
---|
129 | public LispObject getType() { |
---|
130 | return type; |
---|
131 | } |
---|
132 | public Pathname setType(LispObject type) { |
---|
133 | this.type = type; |
---|
134 | return this; |
---|
135 | } |
---|
136 | |
---|
137 | /** A positive integer, or NIL, :WILD, :UNSPECIFIC, or :NEWEST. */ |
---|
138 | protected LispObject version = NIL; |
---|
139 | public LispObject getVersion() { |
---|
140 | return version; |
---|
141 | } |
---|
142 | |
---|
143 | public Pathname setVersion(LispObject version) { |
---|
144 | this.version = version; |
---|
145 | return this; |
---|
146 | } |
---|
147 | |
---|
148 | /** |
---|
149 | * The path component separator used by internally generated |
---|
150 | * path namestrings. |
---|
151 | */ |
---|
152 | public final static char directoryDelimiter = '/'; |
---|
153 | |
---|
154 | |
---|
155 | // If we don't declare the no-arg constructor protected, then |
---|
156 | // inheriting classes cannot invoke their constructors !?! |
---|
157 | protected Pathname() {} |
---|
158 | |
---|
159 | private Pathname(Pathname p) { |
---|
160 | /** Copy constructor which shares no structure with the original. */ |
---|
161 | copyFrom(p); |
---|
162 | } |
---|
163 | |
---|
164 | /** |
---|
165 | * Coerces type between descendents of Pathname types by copying structure |
---|
166 | */ |
---|
167 | static public LispObject ncoerce(Pathname orig, Pathname dest) { |
---|
168 | return dest.copyFrom(orig); |
---|
169 | } |
---|
170 | |
---|
171 | /** |
---|
172 | * Create a deep copy of all the information referenced by p |
---|
173 | */ |
---|
174 | Pathname copyFrom(Pathname p) { |
---|
175 | if (p.host != NIL) { |
---|
176 | LispObject pHost = p.getHost(); |
---|
177 | if (pHost instanceof SimpleString) { |
---|
178 | setHost(new SimpleString(pHost.getStringValue())); |
---|
179 | } else if (pHost instanceof Symbol) { |
---|
180 | setHost(pHost); |
---|
181 | } else if (pHost instanceof Cons) { |
---|
182 | LispObject newHost = NIL; |
---|
183 | LispObject components = pHost.reverse(); |
---|
184 | while (!components.car().equals(NIL)) { |
---|
185 | LispObject copy = components.car(); // TODO actually make a copy? |
---|
186 | newHost = newHost.push(copy); |
---|
187 | components = components.cdr(); |
---|
188 | } |
---|
189 | setHost(newHost); |
---|
190 | } else { |
---|
191 | simple_error("Failed to copy host in pathname ~a", p); |
---|
192 | } |
---|
193 | } |
---|
194 | if (p.device != NIL) { |
---|
195 | if (p.device instanceof SimpleString) { |
---|
196 | device = new SimpleString(((SimpleString)p.getDevice()).getStringValue()); |
---|
197 | } else if (p.getDevice() instanceof Cons) { |
---|
198 | LispObject jars = p.getDevice(); |
---|
199 | setDevice(NIL); |
---|
200 | URLPathname root = null; |
---|
201 | Pathname rootPathname = (Pathname) jars.car(); |
---|
202 | if (rootPathname instanceof URLPathname) { |
---|
203 | root = URLPathname.create((URLPathname)rootPathname); |
---|
204 | } else { |
---|
205 | root = URLPathname.create((Pathname)rootPathname); |
---|
206 | } |
---|
207 | device = device.push(root); |
---|
208 | jars = jars.cdr(); |
---|
209 | while (jars.car() != NIL) { |
---|
210 | Pathname jar |
---|
211 | = (Pathname) Pathname.create(((Pathname)jars.car()).getNamestring()); |
---|
212 | device = device.push(jar); |
---|
213 | jars = jars.cdr(); |
---|
214 | } |
---|
215 | device.nreverse(); |
---|
216 | } else if (p.device instanceof Symbol) { // When is this the case? |
---|
217 | device = p.device; |
---|
218 | } else { |
---|
219 | simple_error("Failed to copy device in pathname ~a", p); |
---|
220 | } |
---|
221 | } |
---|
222 | if (p.directory != NIL) { |
---|
223 | if (p.directory instanceof Cons) { |
---|
224 | directory = NIL; |
---|
225 | for (LispObject list = p.directory; list != NIL; list = list.cdr()) { |
---|
226 | LispObject o = list.car(); |
---|
227 | if (o instanceof Symbol) { |
---|
228 | directory = directory.push(o); |
---|
229 | } else if (o instanceof SimpleString) { |
---|
230 | directory = directory.push(new SimpleString(((SimpleString)o).getStringValue())); |
---|
231 | } else { |
---|
232 | Debug.assertTrue(false); |
---|
233 | } |
---|
234 | } |
---|
235 | directory.nreverse(); |
---|
236 | } else { |
---|
237 | simple_error("Failed to copy directory in pathname ~a", p); |
---|
238 | } |
---|
239 | } |
---|
240 | if (p.name != NIL) { |
---|
241 | if (p.name instanceof SimpleString) { |
---|
242 | name = new SimpleString(((SimpleString)p.getName()).getStringValue()); |
---|
243 | } else if (p.name instanceof Symbol) { |
---|
244 | name = p.name; |
---|
245 | } else { |
---|
246 | simple_error("Failed to copy name in pathname ~a", p); |
---|
247 | } |
---|
248 | } |
---|
249 | if (p.type != NIL) { |
---|
250 | if (p.type instanceof SimpleString) { |
---|
251 | type = new SimpleString(((SimpleString)p.getType()).getStringValue()); |
---|
252 | } else if (p.type instanceof Symbol) { |
---|
253 | type = p.type; |
---|
254 | } else { |
---|
255 | simple_error("Failed to copy type in pathname ~a", p); |
---|
256 | } |
---|
257 | } |
---|
258 | if (p.version != NIL) { |
---|
259 | if (p.version instanceof Symbol) { |
---|
260 | version = p.version; |
---|
261 | } else if (p.version instanceof LispInteger) { |
---|
262 | version = p.version; |
---|
263 | } else { |
---|
264 | simple_error("Failed to copy version in pathname ~a", p); |
---|
265 | } |
---|
266 | } |
---|
267 | return this; |
---|
268 | } |
---|
269 | |
---|
270 | public static boolean isSupportedProtocol(String protocol) { |
---|
271 | // There is no programmatic way to know what protocols will |
---|
272 | // sucessfully construct a URL, so we check for well known ones... |
---|
273 | if ("jar".equals(protocol) |
---|
274 | || "file".equals(protocol)) |
---|
275 | // || "http".equals(protocol)) XXX remove this as an optimization |
---|
276 | { |
---|
277 | return true; |
---|
278 | } |
---|
279 | // ... and try the entire constructor with some hopefully |
---|
280 | // reasonable parameters for everything else. |
---|
281 | try { |
---|
282 | new URL(protocol, "example.org", "foo"); |
---|
283 | return true; |
---|
284 | } catch (MalformedURLException e) { |
---|
285 | return false; |
---|
286 | } |
---|
287 | } |
---|
288 | |
---|
289 | private static final Pathname init(String s) { |
---|
290 | Pathname result = new Pathname(); |
---|
291 | if (s == null) { |
---|
292 | return (Pathname)parse_error("Refusing to create a PATHNAME for the null reference."); |
---|
293 | } |
---|
294 | if (s.equals(".") || s.equals("./") |
---|
295 | || (Utilities.isPlatformWindows && s.equals(".\\"))) { |
---|
296 | result.setDirectory(new Cons(Keyword.RELATIVE)); |
---|
297 | return result; |
---|
298 | } |
---|
299 | if (s.startsWith("./")) |
---|
300 | { s = s.substring(2); } |
---|
301 | if (s.equals("..") || s.equals("../")) { |
---|
302 | result.setDirectory(list(Keyword.RELATIVE, Keyword.UP)); |
---|
303 | return result; |
---|
304 | } |
---|
305 | // UNC Windows shares |
---|
306 | if (Utilities.isPlatformWindows) { |
---|
307 | if (s.startsWith("\\\\") || s.startsWith("//")) { |
---|
308 | // UNC path support |
---|
309 | int shareIndex; |
---|
310 | int dirIndex; |
---|
311 | // match \\<server>\<share>\[directories-and-files] |
---|
312 | if (s.startsWith("\\\\")) { |
---|
313 | shareIndex = s.indexOf('\\', 2); |
---|
314 | dirIndex = s.indexOf('\\', shareIndex + 1); |
---|
315 | // match //<server>/<share>/[directories-and-files] |
---|
316 | } else { |
---|
317 | shareIndex = s.indexOf('/', 2); |
---|
318 | dirIndex = s.indexOf('/', shareIndex + 1); |
---|
319 | } |
---|
320 | if (shareIndex == -1 || dirIndex == -1) { |
---|
321 | return (Pathname)parse_error("Unsupported UNC path format: \"" + s + '"'); |
---|
322 | } |
---|
323 | |
---|
324 | result |
---|
325 | .setHost(new SimpleString(s.substring(2, shareIndex))) |
---|
326 | .setDevice(new SimpleString(s.substring(shareIndex + 1, dirIndex))); |
---|
327 | |
---|
328 | Pathname p = (Pathname)Pathname.create(s.substring(dirIndex)); |
---|
329 | result |
---|
330 | .setDirectory(p.getDirectory()) |
---|
331 | .setName(p.getName()) |
---|
332 | .setType(p.getType()) |
---|
333 | .setVersion(p.getVersion()); |
---|
334 | return result; |
---|
335 | } |
---|
336 | } |
---|
337 | |
---|
338 | // A JAR file |
---|
339 | if (s.startsWith(JarPathname.JAR_URI_PREFIX) |
---|
340 | && s.endsWith(JarPathname.JAR_URI_SUFFIX)) { |
---|
341 | return (JarPathname)JarPathname.create(s); |
---|
342 | } |
---|
343 | |
---|
344 | // An entry in a JAR file |
---|
345 | final int separatorIndex = s.lastIndexOf(JarPathname.JAR_URI_SUFFIX); |
---|
346 | if (separatorIndex > 0 && s.startsWith(JarPathname.JAR_URI_PREFIX)) { |
---|
347 | return (JarPathname)JarPathname.create(s); |
---|
348 | } |
---|
349 | |
---|
350 | // A URL (anything with a scheme that is not a logical |
---|
351 | // pathname, and not a JAR file or an entry in a JAR file) |
---|
352 | if (isValidURL(s)) { |
---|
353 | return (URLPathname)URLPathname.create(s); |
---|
354 | } |
---|
355 | |
---|
356 | // Normalize path separators to forward slashes |
---|
357 | if (Utilities.isPlatformWindows) { |
---|
358 | if (s.contains("\\")) { |
---|
359 | s = s.replace("\\", "/"); |
---|
360 | } |
---|
361 | } |
---|
362 | |
---|
363 | // Expand user home directories |
---|
364 | if (Utilities.isPlatformUnix) { |
---|
365 | if (s.equals("~")) { |
---|
366 | s = System.getProperty("user.home").concat("/"); |
---|
367 | } else if (s.startsWith("~/")) { |
---|
368 | s = System.getProperty("user.home").concat(s.substring(1)); |
---|
369 | } |
---|
370 | } |
---|
371 | |
---|
372 | // possible MSDOS device |
---|
373 | if (Utilities.isPlatformWindows) { |
---|
374 | if (s.length() >= 2 && s.charAt(1) == ':') { |
---|
375 | result.setDevice(new SimpleString(s.charAt(0))); |
---|
376 | s = s.substring(2); |
---|
377 | } |
---|
378 | } |
---|
379 | |
---|
380 | String d = null; |
---|
381 | // Find last file separator char. |
---|
382 | for (int i = s.length(); i-- > 0;) { |
---|
383 | if (s.charAt(i) == '/') { |
---|
384 | d = s.substring(0, i + 1); |
---|
385 | s = s.substring(i + 1); |
---|
386 | break; |
---|
387 | } |
---|
388 | } |
---|
389 | |
---|
390 | if (d != null) { |
---|
391 | if (s.equals("..")) { |
---|
392 | d = d.concat(s); |
---|
393 | s = ""; |
---|
394 | } |
---|
395 | result.setDirectory(parseDirectory(d)); |
---|
396 | } |
---|
397 | |
---|
398 | int index = s.lastIndexOf('.'); |
---|
399 | String name = null; |
---|
400 | String type = null; |
---|
401 | if (index > 0) { |
---|
402 | name = s.substring(0, index); |
---|
403 | type = s.substring(index + 1); |
---|
404 | } else if (s.length() > 0) { |
---|
405 | name = s; |
---|
406 | } |
---|
407 | if (name != null) { |
---|
408 | if (name.equals("*")) { |
---|
409 | result.setName(Keyword.WILD); |
---|
410 | } else { |
---|
411 | result.setName(new SimpleString(name)); |
---|
412 | } |
---|
413 | } |
---|
414 | if (type != null) { |
---|
415 | if (type.equals("*")) { |
---|
416 | result.setType(Keyword.WILD); |
---|
417 | } else { |
---|
418 | result.setType(new SimpleString(type)); |
---|
419 | } |
---|
420 | } |
---|
421 | return result; |
---|
422 | } |
---|
423 | |
---|
424 | private static final LispObject parseDirectory(String d) { |
---|
425 | if (d.equals("/") || (Utilities.isPlatformWindows && d.equals("\\"))) { |
---|
426 | return new Cons(Keyword.ABSOLUTE); |
---|
427 | } |
---|
428 | LispObject result; |
---|
429 | if (d.startsWith("/") || (Utilities.isPlatformWindows && d.startsWith("\\"))) { |
---|
430 | result = new Cons(Keyword.ABSOLUTE); |
---|
431 | } else { |
---|
432 | result = new Cons(Keyword.RELATIVE); |
---|
433 | } |
---|
434 | StringTokenizer st = new StringTokenizer(d, "/\\"); |
---|
435 | while (st.hasMoreTokens()) { |
---|
436 | String token = st.nextToken(); |
---|
437 | LispObject obj; |
---|
438 | if (token.equals("*")) { |
---|
439 | obj = Keyword.WILD; |
---|
440 | } else if (token.equals("**")) { |
---|
441 | obj = Keyword.WILD_INFERIORS; |
---|
442 | } else if (token.equals("..")) { |
---|
443 | if (result.car() instanceof AbstractString) { |
---|
444 | result = result.cdr(); |
---|
445 | continue; |
---|
446 | } |
---|
447 | obj = Keyword.UP; |
---|
448 | } else { |
---|
449 | obj = new SimpleString(token); |
---|
450 | } |
---|
451 | result = new Cons(obj, result); |
---|
452 | } |
---|
453 | return result.nreverse(); |
---|
454 | } |
---|
455 | |
---|
456 | @Override |
---|
457 | public LispObject getParts() { |
---|
458 | LispObject parts |
---|
459 | = list(new Cons("HOST", getHost()), |
---|
460 | new Cons("DEVICE", getDevice()), |
---|
461 | new Cons("DIRECTORY", getDirectory()), |
---|
462 | new Cons("NAME", getName()), |
---|
463 | new Cons("TYPE", getType()), |
---|
464 | new Cons("VERSION", getVersion())); |
---|
465 | return parts; |
---|
466 | } |
---|
467 | |
---|
468 | @Override |
---|
469 | public LispObject typeOf() { |
---|
470 | if (isJar()) { |
---|
471 | return Symbol.JAR_PATHNAME; |
---|
472 | } |
---|
473 | if (isURL()) { |
---|
474 | return Symbol.URL_PATHNAME; |
---|
475 | } |
---|
476 | return Symbol.PATHNAME; |
---|
477 | } |
---|
478 | |
---|
479 | @Override |
---|
480 | public LispObject classOf() { |
---|
481 | if (isJar()) { |
---|
482 | return BuiltInClass.JAR_PATHNAME; |
---|
483 | } |
---|
484 | if (isURL()) { |
---|
485 | return BuiltInClass.URL_PATHNAME; |
---|
486 | } |
---|
487 | return BuiltInClass.PATHNAME; |
---|
488 | } |
---|
489 | |
---|
490 | @Override |
---|
491 | public LispObject typep(LispObject type) { |
---|
492 | if (type == Symbol.PATHNAME) { |
---|
493 | return T; |
---|
494 | } |
---|
495 | if (type == Symbol.JAR_PATHNAME && isJar()) { |
---|
496 | return T; |
---|
497 | } |
---|
498 | if (type == Symbol.URL_PATHNAME && isURL()) { |
---|
499 | return T; |
---|
500 | } |
---|
501 | if (type == BuiltInClass.PATHNAME) { |
---|
502 | return T; |
---|
503 | } |
---|
504 | if (type == BuiltInClass.JAR_PATHNAME && isJar()) { |
---|
505 | return T; |
---|
506 | } |
---|
507 | if (type == BuiltInClass.URL_PATHNAME && isURL()) { |
---|
508 | return T; |
---|
509 | } |
---|
510 | return super.typep(type); |
---|
511 | } |
---|
512 | |
---|
513 | public String getNamestring() { |
---|
514 | if (getDirectory() instanceof AbstractString) { |
---|
515 | Debug.assertTrue(false); |
---|
516 | } |
---|
517 | StringBuilder sb = new StringBuilder(); |
---|
518 | // "If a pathname is converted to a namestring, the symbols NIL and |
---|
519 | // :UNSPECIFIC cause the field to be treated as if it were empty. That |
---|
520 | // is, both NIL and :UNSPECIFIC cause the component not to appear in |
---|
521 | // the namestring." 19.2.2.2.3.1 |
---|
522 | if (getHost() != NIL) { |
---|
523 | Debug.assertTrue(getHost() instanceof AbstractString |
---|
524 | || isURL()); |
---|
525 | if (isURL()) { |
---|
526 | LispObject scheme = Symbol.GETF.execute(getHost(), URLPathname.SCHEME, NIL); |
---|
527 | LispObject authority = Symbol.GETF.execute(getHost(), URLPathname.AUTHORITY, NIL); |
---|
528 | Debug.assertTrue(scheme != NIL); |
---|
529 | sb.append(scheme.getStringValue()); |
---|
530 | sb.append(":"); |
---|
531 | if (authority != NIL) { |
---|
532 | sb.append("//"); |
---|
533 | sb.append(authority.getStringValue()); |
---|
534 | } |
---|
535 | } else if (this instanceof LogicalPathname) { |
---|
536 | sb.append(getHost().getStringValue()); |
---|
537 | sb.append(':'); |
---|
538 | } else { |
---|
539 | // A UNC path |
---|
540 | sb.append("//").append(getHost().getStringValue()).append("/"); |
---|
541 | } |
---|
542 | } |
---|
543 | |
---|
544 | if (getDevice().equals(NIL) |
---|
545 | || getDevice().equals(Keyword.UNSPECIFIC)) { |
---|
546 | // nothing emitted for device |
---|
547 | } else if (getDevice() instanceof AbstractString) { |
---|
548 | sb.append(getDevice().getStringValue()); |
---|
549 | if (this instanceof LogicalPathname |
---|
550 | || getHost() == NIL) { |
---|
551 | sb.append(':'); // non-UNC paths |
---|
552 | } |
---|
553 | } else { |
---|
554 | simple_error("Transitional error in pathname: should be a JAR-PATHNAME", this); |
---|
555 | } |
---|
556 | |
---|
557 | String directoryNamestring = getDirectoryNamestring(); |
---|
558 | sb.append(directoryNamestring); |
---|
559 | |
---|
560 | if (getName() instanceof AbstractString) { |
---|
561 | String n = getName().getStringValue(); |
---|
562 | if (n.indexOf('/') >= 0) { |
---|
563 | return null; |
---|
564 | } |
---|
565 | sb.append(n); |
---|
566 | } else if (getName() == Keyword.WILD) { |
---|
567 | sb.append('*'); |
---|
568 | } |
---|
569 | |
---|
570 | if (getType() != NIL && getType() != Keyword.UNSPECIFIC) { |
---|
571 | sb.append('.'); |
---|
572 | if (getType() instanceof AbstractString) { |
---|
573 | String t = getType().getStringValue(); |
---|
574 | // Allow Windows shortcuts to include TYPE |
---|
575 | if (!(t.endsWith(".lnk") && Utilities.isPlatformWindows)) { |
---|
576 | if (t.indexOf('.') >= 0) { |
---|
577 | return null; |
---|
578 | } |
---|
579 | } |
---|
580 | sb.append(t); |
---|
581 | } else if (getType() == Keyword.WILD) { |
---|
582 | sb.append('*'); |
---|
583 | } else { |
---|
584 | Debug.assertTrue(false); |
---|
585 | } |
---|
586 | } |
---|
587 | |
---|
588 | if (this instanceof LogicalPathname) { |
---|
589 | if (getVersion().integerp()) { |
---|
590 | sb.append('.'); |
---|
591 | int base = Fixnum.getValue(Symbol.PRINT_BASE.symbolValue()); |
---|
592 | if (getVersion() instanceof Fixnum) { |
---|
593 | sb.append(Integer.toString(((Fixnum) getVersion()).value, base).toUpperCase()); |
---|
594 | } else if (getVersion() instanceof Bignum) { |
---|
595 | sb.append(((Bignum) getVersion()).value.toString(base).toUpperCase()); |
---|
596 | } |
---|
597 | } else if (getVersion() == Keyword.WILD) { |
---|
598 | sb.append(".*"); |
---|
599 | } else if (getVersion() == Keyword.NEWEST) { |
---|
600 | sb.append(".NEWEST"); |
---|
601 | } |
---|
602 | } |
---|
603 | return sb.toString(); |
---|
604 | } |
---|
605 | |
---|
606 | protected String getDirectoryNamestring() { |
---|
607 | validateDirectory(true); |
---|
608 | StringBuilder sb = new StringBuilder(); |
---|
609 | // "If a pathname is converted to a namestring, the symbols NIL and |
---|
610 | // :UNSPECIFIC cause the field to be treated as if it were empty. That |
---|
611 | // is, both NIL and :UNSPECIFIC cause the component not to appear in |
---|
612 | // the namestring." 19.2.2.2.3.1 |
---|
613 | if (getDirectory() != NIL && getDirectory() != Keyword.UNSPECIFIC) { |
---|
614 | LispObject temp = getDirectory(); |
---|
615 | LispObject part = temp.car(); |
---|
616 | temp = temp.cdr(); |
---|
617 | if (part == Keyword.ABSOLUTE) { |
---|
618 | sb.append(directoryDelimiter); |
---|
619 | } else if (part == Keyword.RELATIVE) { |
---|
620 | if (temp == NIL) { |
---|
621 | // #p"./" |
---|
622 | sb.append('.'); |
---|
623 | sb.append(directoryDelimiter); |
---|
624 | } |
---|
625 | // else: Nothing to do. |
---|
626 | } else { |
---|
627 | error(new FileError("Unsupported directory component " |
---|
628 | + part.printObject() + ".", |
---|
629 | this)); |
---|
630 | } |
---|
631 | while (temp != NIL) { |
---|
632 | part = temp.car(); |
---|
633 | if (part instanceof AbstractString) { |
---|
634 | sb.append(part.getStringValue()); |
---|
635 | } else if (part == Keyword.WILD) { |
---|
636 | sb.append('*'); |
---|
637 | } else if (part == Keyword.WILD_INFERIORS) { |
---|
638 | sb.append("**"); |
---|
639 | } else if (part == Keyword.UP) { |
---|
640 | sb.append(".."); |
---|
641 | } |
---|
642 | sb.append(directoryDelimiter); |
---|
643 | temp = temp.cdr(); |
---|
644 | } |
---|
645 | } |
---|
646 | return sb.toString(); |
---|
647 | } |
---|
648 | |
---|
649 | |
---|
650 | @Override |
---|
651 | public boolean equal(LispObject obj) { |
---|
652 | if (this == obj) { |
---|
653 | return true; |
---|
654 | } |
---|
655 | if (obj instanceof Pathname) { |
---|
656 | Pathname p = (Pathname) obj; |
---|
657 | if (Utilities.isPlatformWindows) { |
---|
658 | if (!host.equalp(p.host)) { |
---|
659 | return false; |
---|
660 | } |
---|
661 | if (!device.equalp(p.device)) { |
---|
662 | return false; |
---|
663 | } |
---|
664 | if (!directory.equalp(p.directory)) { |
---|
665 | return false; |
---|
666 | } |
---|
667 | if (!name.equalp(p.name)) { |
---|
668 | return false; |
---|
669 | } |
---|
670 | if (!type.equalp(p.type)) { |
---|
671 | return false; |
---|
672 | } |
---|
673 | // Ignore version component. |
---|
674 | //if (!version.equalp(p.version)) |
---|
675 | // return false; |
---|
676 | } else { |
---|
677 | // Unix. |
---|
678 | if (!host.equal(p.host)) { |
---|
679 | return false; |
---|
680 | } |
---|
681 | if (!device.equal(p.device)) { |
---|
682 | return false; |
---|
683 | } |
---|
684 | if (!directory.equal(p.directory)) { |
---|
685 | return false; |
---|
686 | } |
---|
687 | if (!name.equal(p.name)) { |
---|
688 | return false; |
---|
689 | } |
---|
690 | if (!type.equal(p.type)) { |
---|
691 | return false; |
---|
692 | } |
---|
693 | // Ignore version component. |
---|
694 | //if (!version.equal(p.version)) |
---|
695 | // return false; |
---|
696 | } |
---|
697 | return true; |
---|
698 | } |
---|
699 | return false; |
---|
700 | } |
---|
701 | |
---|
702 | @Override |
---|
703 | public boolean equalp(LispObject obj) { |
---|
704 | return equal(obj); |
---|
705 | } |
---|
706 | |
---|
707 | public boolean equals(Object o) { |
---|
708 | if (!(this.getClass().isAssignableFrom(o.getClass()))) { |
---|
709 | return super.equals(o); |
---|
710 | } |
---|
711 | return equal((Pathname)o); |
---|
712 | } |
---|
713 | |
---|
714 | public int hashCode() { |
---|
715 | return sxhash(); |
---|
716 | } |
---|
717 | |
---|
718 | @Override |
---|
719 | public int sxhash() { |
---|
720 | return ((getHost().sxhash() |
---|
721 | ^ getDevice().sxhash() |
---|
722 | ^ getDirectory().sxhash() |
---|
723 | ^ getName().sxhash() |
---|
724 | ^ getType().sxhash()) & 0x7fffffff); |
---|
725 | } |
---|
726 | |
---|
727 | @Override |
---|
728 | public String printObject() { |
---|
729 | final LispThread thread = LispThread.currentThread(); |
---|
730 | final boolean printReadably = (Symbol.PRINT_READABLY.symbolValue(thread) != NIL); |
---|
731 | final boolean printEscape = (Symbol.PRINT_ESCAPE.symbolValue(thread) != NIL); |
---|
732 | boolean useNamestring; |
---|
733 | String s = null; |
---|
734 | s = getNamestring(); |
---|
735 | if (s != null) { |
---|
736 | useNamestring = true; |
---|
737 | if (printReadably) { |
---|
738 | // We have a namestring. Check for pathname components that |
---|
739 | // can't be read from the namestring. |
---|
740 | if ((getHost() != NIL && !isURL()) |
---|
741 | || getVersion() != NIL) |
---|
742 | { |
---|
743 | useNamestring = false; |
---|
744 | } else if (getName() instanceof AbstractString) { |
---|
745 | String n = getName().getStringValue(); |
---|
746 | if (n.equals(".") || n.equals("..")) { |
---|
747 | useNamestring = false; |
---|
748 | // ??? File.separatorChar is platform dependent. Does this help on Windows? |
---|
749 | } else if (n.indexOf(File.separatorChar) >= 0) { |
---|
750 | useNamestring = false; |
---|
751 | } |
---|
752 | } |
---|
753 | } |
---|
754 | } else { |
---|
755 | useNamestring = false; |
---|
756 | } |
---|
757 | StringBuilder sb = new StringBuilder(); |
---|
758 | |
---|
759 | if (useNamestring) { |
---|
760 | if (printReadably || printEscape) { |
---|
761 | sb.append("#P\""); |
---|
762 | } |
---|
763 | final int limit = s.length(); |
---|
764 | for (int i = 0; i < limit; i++) { |
---|
765 | char c = s.charAt(i); |
---|
766 | if (printReadably || printEscape) { |
---|
767 | if (c == '\"' || c == '\\') { |
---|
768 | sb.append('\\'); |
---|
769 | } |
---|
770 | } |
---|
771 | sb.append(c); |
---|
772 | } |
---|
773 | if (printReadably || printEscape) { |
---|
774 | sb.append('"'); |
---|
775 | } |
---|
776 | return sb.toString(); |
---|
777 | } |
---|
778 | |
---|
779 | sb.append("PATHNAME (with no namestring) "); |
---|
780 | if (getHost() != NIL) { |
---|
781 | sb.append(":HOST ") |
---|
782 | .append(getHost().printObject()) |
---|
783 | .append(" "); |
---|
784 | } |
---|
785 | if (getDevice() != NIL) { |
---|
786 | sb.append(":DEVICE ") |
---|
787 | .append(getDevice().printObject()) |
---|
788 | .append(" "); |
---|
789 | } |
---|
790 | if (getDirectory() != NIL) { |
---|
791 | sb.append(":DIRECTORY ") |
---|
792 | .append(getDirectory().printObject()) |
---|
793 | .append(" "); |
---|
794 | } |
---|
795 | if (getName() != NIL) { |
---|
796 | sb.append(":NAME ") |
---|
797 | .append(getName().printObject()) |
---|
798 | .append(" "); |
---|
799 | } |
---|
800 | if (getType() != NIL) { |
---|
801 | sb.append(":TYPE ") |
---|
802 | .append(getType().printObject()) |
---|
803 | .append(" "); |
---|
804 | } |
---|
805 | if (getVersion() != NIL) { |
---|
806 | sb.append(":VERSION ") |
---|
807 | .append(getVersion().printObject()) |
---|
808 | .append(" "); |
---|
809 | } |
---|
810 | if (sb.charAt(sb.length() - 1) == ' ') { |
---|
811 | sb.setLength(sb.length() - 1); |
---|
812 | } |
---|
813 | |
---|
814 | return unreadableString(sb.toString()); |
---|
815 | } |
---|
816 | |
---|
817 | public static Pathname parseNamestring(String s) { |
---|
818 | return (Pathname)Pathname.create(s); |
---|
819 | } |
---|
820 | |
---|
821 | public static boolean isValidURL(String s) { |
---|
822 | // On Windows, the scheme "[A-Z]:.*" is ambiguous; reject as urls |
---|
823 | // This special case reduced exceptions while compiling Maxima by 90%+ |
---|
824 | if (Utilities.isPlatformWindows && s.length() >= 2 && s.charAt(1) == ':') { |
---|
825 | char c = s.charAt(0); |
---|
826 | if (('A' <= s.charAt(0) && s.charAt(0) <= 'Z') |
---|
827 | || ('a' <= s.charAt(0) && s.charAt(0) <= 'z')) |
---|
828 | return false; |
---|
829 | } |
---|
830 | |
---|
831 | if (s.indexOf(':') == -1) // no schema separator; can't be valid |
---|
832 | return false; |
---|
833 | |
---|
834 | try { |
---|
835 | URL url = new URL(s); |
---|
836 | } catch (MalformedURLException e) { |
---|
837 | return false; |
---|
838 | } |
---|
839 | return true; |
---|
840 | } |
---|
841 | |
---|
842 | public static LispObject parseNamestring(AbstractString namestring) { |
---|
843 | // Check for a logical pathname host. |
---|
844 | String s = namestring.getStringValue(); |
---|
845 | if (!isValidURL(s)) { |
---|
846 | String h = LogicalPathname.getHostString(s); |
---|
847 | if (h != null |
---|
848 | && LogicalPathname.TRANSLATIONS.get(new SimpleString(h)) != null) { |
---|
849 | // A defined logical pathname host. |
---|
850 | return LogicalPathname.create(h, s.substring(s.indexOf(':') + 1)); |
---|
851 | } |
---|
852 | } |
---|
853 | return Pathname.create(s); |
---|
854 | } |
---|
855 | |
---|
856 | public static LogicalPathname parseNamestring(AbstractString namestring, |
---|
857 | AbstractString host) |
---|
858 | { |
---|
859 | String s = namestring.getStringValue(); |
---|
860 | |
---|
861 | // Look for a logical pathname host in the namestring. |
---|
862 | String h = LogicalPathname.getHostString(s); |
---|
863 | if (h != null) { |
---|
864 | if (!h.equals(host.getStringValue())) { |
---|
865 | error(new LispError("Host in " + s |
---|
866 | + " does not match requested host " |
---|
867 | + host.getStringValue())); |
---|
868 | // Not reached. |
---|
869 | return null; |
---|
870 | } |
---|
871 | // Remove host prefix from namestring. |
---|
872 | s = s.substring(s.indexOf(':') + 1); |
---|
873 | } |
---|
874 | if (LogicalPathname.TRANSLATIONS.get(host) != null) { |
---|
875 | // A defined logical pathname host. |
---|
876 | return LogicalPathname.create(host.getStringValue(), s); |
---|
877 | } |
---|
878 | error(new LispError(host.princToString() + " is not defined as a logical pathname host.")); |
---|
879 | // Not reached. |
---|
880 | return null; |
---|
881 | } |
---|
882 | |
---|
883 | static final void checkCaseArgument(LispObject arg) { |
---|
884 | if (arg != Keyword.COMMON && arg != Keyword.LOCAL) { |
---|
885 | type_error(arg, list(Symbol.MEMBER, |
---|
886 | Keyword.COMMON, Keyword.LOCAL)); |
---|
887 | } |
---|
888 | } |
---|
889 | |
---|
890 | private static final Primitive _PATHNAME_HOST = new pf_pathname_host(); |
---|
891 | @DocString(name="%pathname-host") |
---|
892 | private static class pf_pathname_host extends Primitive { |
---|
893 | pf_pathname_host() { |
---|
894 | super("%pathname-host", PACKAGE_SYS, false); |
---|
895 | } |
---|
896 | @Override |
---|
897 | public LispObject execute(LispObject first, LispObject second) { |
---|
898 | checkCaseArgument(second); // FIXME Why is this ignored? |
---|
899 | return coerceToPathname(first).getHost(); |
---|
900 | } |
---|
901 | } |
---|
902 | private static final Primitive _PATHNAME_DEVICE = new pf_pathname_device(); |
---|
903 | @DocString(name="%pathname-device") |
---|
904 | private static class pf_pathname_device extends Primitive { |
---|
905 | pf_pathname_device() { |
---|
906 | super("%pathname-device", PACKAGE_SYS, false); |
---|
907 | } |
---|
908 | @Override |
---|
909 | public LispObject execute(LispObject first, LispObject second) { |
---|
910 | checkCaseArgument(second); // FIXME Why is this ignored? |
---|
911 | return coerceToPathname(first).getDevice(); |
---|
912 | } |
---|
913 | } |
---|
914 | private static final Primitive _PATHNAME_DIRECTORY = new pf_pathname_directory(); |
---|
915 | @DocString(name="%pathname-directory") |
---|
916 | private static class pf_pathname_directory extends Primitive { |
---|
917 | pf_pathname_directory() { |
---|
918 | super("%pathname-directory", PACKAGE_SYS, false); |
---|
919 | } |
---|
920 | @Override |
---|
921 | public LispObject execute(LispObject first, LispObject second) { |
---|
922 | checkCaseArgument(second); // FIXME Why is this ignored? |
---|
923 | return coerceToPathname(first).getDirectory(); |
---|
924 | } |
---|
925 | } |
---|
926 | private static final Primitive _PATHNAME_NAME = new pf_pathname_name(); |
---|
927 | @DocString(name="%pathname-name") |
---|
928 | private static class pf_pathname_name extends Primitive { |
---|
929 | pf_pathname_name() { |
---|
930 | super ("%pathname-name", PACKAGE_SYS, false); |
---|
931 | } |
---|
932 | @Override |
---|
933 | public LispObject execute(LispObject first, LispObject second) { |
---|
934 | checkCaseArgument(second); // FIXME Why is this ignored? |
---|
935 | return coerceToPathname(first).getName(); |
---|
936 | } |
---|
937 | } |
---|
938 | private static final Primitive _PATHNAME_TYPE = new pf_pathname_type(); |
---|
939 | @DocString(name="%pathname-type") |
---|
940 | private static class pf_pathname_type extends Primitive { |
---|
941 | pf_pathname_type() { |
---|
942 | super("%pathname-type", PACKAGE_SYS, false); |
---|
943 | } |
---|
944 | @Override |
---|
945 | public LispObject execute(LispObject first, LispObject second) { |
---|
946 | checkCaseArgument(second); // FIXME Why is this ignored? |
---|
947 | return coerceToPathname(first).getType(); |
---|
948 | } |
---|
949 | } |
---|
950 | |
---|
951 | private static final Primitive PATHNAME_VERSION = new pf_pathname_version(); |
---|
952 | @DocString(name="pathname-version", |
---|
953 | args="pathname", |
---|
954 | returns="version", |
---|
955 | doc="Return the version component of PATHNAME.") |
---|
956 | private static class pf_pathname_version extends Primitive { |
---|
957 | pf_pathname_version() { |
---|
958 | super("pathname-version", "pathname"); |
---|
959 | } |
---|
960 | @Override |
---|
961 | public LispObject execute(LispObject arg) { |
---|
962 | return coerceToPathname(arg).getVersion(); |
---|
963 | } |
---|
964 | } |
---|
965 | private static final Primitive NAMESTRING = new pf_namestring(); |
---|
966 | @DocString(name="namestring", |
---|
967 | args="pathname", |
---|
968 | returns="namestring", |
---|
969 | doc="Returns the NAMESTRING of PATHNAME if it has one.\n" |
---|
970 | + "\n" |
---|
971 | + "If PATHNAME is of type url-pathname or jar-pathname the NAMESTRING is encoded\n" |
---|
972 | + "according to the uri percent escape rules.\n" |
---|
973 | + "\n" |
---|
974 | + "Signals an error if PATHNAME lacks a printable NAMESTRING representation.\n") |
---|
975 | private static class pf_namestring extends Primitive { |
---|
976 | pf_namestring() { |
---|
977 | super("namestring", "pathname"); |
---|
978 | } |
---|
979 | @Override |
---|
980 | public LispObject execute(LispObject arg) { |
---|
981 | Pathname pathname = coerceToPathname(arg); |
---|
982 | String namestring = pathname.getNamestring(); |
---|
983 | if (namestring == null) { |
---|
984 | error(new SimpleError("Pathname has no namestring: " |
---|
985 | + pathname.princToString())); |
---|
986 | } |
---|
987 | return new SimpleString(namestring); |
---|
988 | } |
---|
989 | } |
---|
990 | |
---|
991 | private static final Primitive DIRECTORY_NAMESTRING = new pf_directory_namestring(); |
---|
992 | // TODO clarify uri encoding rules in implementation, then document |
---|
993 | @DocString(name="directory-namestring", |
---|
994 | args="pathname", |
---|
995 | returns="namestring", |
---|
996 | doc="Returns the NAMESTRING of directory porition of PATHNAME if it has one.") |
---|
997 | private static class pf_directory_namestring extends Primitive { |
---|
998 | pf_directory_namestring() { |
---|
999 | super("directory-namestring", "pathname"); |
---|
1000 | } |
---|
1001 | @Override |
---|
1002 | public LispObject execute(LispObject arg) { |
---|
1003 | return new SimpleString(coerceToPathname(arg).getDirectoryNamestring()); |
---|
1004 | } |
---|
1005 | } |
---|
1006 | private static final Primitive PATHNAME = new pf_pathname(); |
---|
1007 | @DocString(name="pathname", |
---|
1008 | args="pathspec", |
---|
1009 | returns="pathname", |
---|
1010 | doc="Returns the PATHNAME denoted by PATHSPEC.") |
---|
1011 | private static class pf_pathname extends Primitive { |
---|
1012 | pf_pathname() { |
---|
1013 | super("pathname", "pathspec"); |
---|
1014 | } |
---|
1015 | @Override |
---|
1016 | public LispObject execute(LispObject arg) { |
---|
1017 | return coerceToPathname(arg); |
---|
1018 | } |
---|
1019 | } |
---|
1020 | private static final Primitive _PARSE_NAMESTRING = new pf_parse_namestring(); |
---|
1021 | @DocString(name="%parse-namestring", |
---|
1022 | args="namestring host default-pathname", |
---|
1023 | returns="pathname, position") |
---|
1024 | private static class pf_parse_namestring extends Primitive { |
---|
1025 | pf_parse_namestring() { |
---|
1026 | super("%parse-namestring", PACKAGE_SYS, false, |
---|
1027 | "namestring host default-pathname"); |
---|
1028 | } |
---|
1029 | @Override |
---|
1030 | public LispObject execute(LispObject first, LispObject second, LispObject third) { |
---|
1031 | final LispThread thread = LispThread.currentThread(); |
---|
1032 | final AbstractString namestring = checkString(first); |
---|
1033 | // The HOST parameter must be a string or NIL. |
---|
1034 | if (second == NIL) { |
---|
1035 | // "If HOST is NIL, DEFAULT-PATHNAME is a logical pathname, and |
---|
1036 | // THING is a syntactically valid logical pathname namestring |
---|
1037 | // without an explicit host, then it is parsed as a logical |
---|
1038 | // pathname namestring on the host that is the host component |
---|
1039 | // of DEFAULT-PATHNAME." |
---|
1040 | third = coerceToPathname(third); |
---|
1041 | if (third instanceof LogicalPathname) { |
---|
1042 | second = ((LogicalPathname) third).getHost(); |
---|
1043 | } else { |
---|
1044 | return thread.setValues(parseNamestring(namestring), |
---|
1045 | namestring.LENGTH()); |
---|
1046 | } |
---|
1047 | } |
---|
1048 | Debug.assertTrue(second != NIL); |
---|
1049 | final AbstractString host = checkString(second); |
---|
1050 | return thread.setValues(parseNamestring(namestring, host), |
---|
1051 | namestring.LENGTH()); |
---|
1052 | } |
---|
1053 | } |
---|
1054 | private static final Primitive MAKE_PATHNAME = new pf_make_pathname(); |
---|
1055 | @DocString(name="make-pathname", |
---|
1056 | args="&key host device directory name type version defaults case", |
---|
1057 | returns="pathname", |
---|
1058 | doc="Constructs and returns a pathname from the supplied keyword arguments.") |
---|
1059 | private static class pf_make_pathname extends Primitive { |
---|
1060 | pf_make_pathname() { |
---|
1061 | super("make-pathname", |
---|
1062 | "&key host device directory name type version defaults case"); |
---|
1063 | } |
---|
1064 | @Override |
---|
1065 | public LispObject execute(LispObject[] args) { |
---|
1066 | LispObject result = _makePathname(args); |
---|
1067 | return result; |
---|
1068 | } |
---|
1069 | } |
---|
1070 | |
---|
1071 | // Used by the #p reader. |
---|
1072 | public static final Pathname makePathname(LispObject args) { |
---|
1073 | return (Pathname) _makePathname(args.copyToArray()); |
---|
1074 | } |
---|
1075 | |
---|
1076 | public static final Pathname makePathname(File file) { |
---|
1077 | String namestring = null; |
---|
1078 | try { |
---|
1079 | namestring = file.getCanonicalPath(); |
---|
1080 | } catch (IOException e) { |
---|
1081 | Debug.trace("Failed to make a Pathname from " |
---|
1082 | + "." + file + "'"); |
---|
1083 | return null; |
---|
1084 | } |
---|
1085 | return (Pathname)Pathname.create(namestring); |
---|
1086 | } |
---|
1087 | |
---|
1088 | static final LispObject _makePathname(LispObject[] args) { |
---|
1089 | if (args.length % 2 != 0) { |
---|
1090 | program_error("Odd number of keyword arguments."); |
---|
1091 | } |
---|
1092 | LispObject host = NIL; |
---|
1093 | LispObject device = NIL; |
---|
1094 | LispObject directory = NIL; |
---|
1095 | LispObject name = NIL; |
---|
1096 | LispObject type = NIL; |
---|
1097 | LispObject version = NIL; |
---|
1098 | Pathname defaults = null; |
---|
1099 | boolean hostSupplied = false; |
---|
1100 | boolean deviceSupplied = false; |
---|
1101 | boolean nameSupplied = false; |
---|
1102 | boolean typeSupplied = false; |
---|
1103 | boolean directorySupplied = false; |
---|
1104 | boolean versionSupplied = false; |
---|
1105 | for (int i = 0; i < args.length; i += 2) { |
---|
1106 | LispObject key = args[i]; |
---|
1107 | LispObject value = args[i + 1]; |
---|
1108 | if (key == Keyword.HOST) { |
---|
1109 | host = value; |
---|
1110 | hostSupplied = true; |
---|
1111 | } else if (key == Keyword.DEVICE) { |
---|
1112 | device = value; |
---|
1113 | deviceSupplied = true; |
---|
1114 | if (!(value instanceof AbstractString |
---|
1115 | || value.equals(Keyword.UNSPECIFIC) |
---|
1116 | || value.equals(NIL) |
---|
1117 | || value instanceof Cons)) { |
---|
1118 | return type_error("DEVICE is not a string, :UNSPECIFIC, NIL, or a list.", |
---|
1119 | value, |
---|
1120 | list(Symbol.OR, |
---|
1121 | Symbol.STRING, Keyword.UNSPECIFIC, NIL, Symbol.CONS)); |
---|
1122 | } |
---|
1123 | } else if (key == Keyword.DIRECTORY) { |
---|
1124 | directorySupplied = true; |
---|
1125 | if (value instanceof AbstractString) { |
---|
1126 | directory = list(Keyword.ABSOLUTE, value); |
---|
1127 | } else if (value == Keyword.WILD) { |
---|
1128 | directory = list(Keyword.ABSOLUTE, Keyword.WILD); |
---|
1129 | } else { |
---|
1130 | // a valid pathname directory is a string, a list of |
---|
1131 | // strings, nil, :wild, :unspecific |
---|
1132 | // |
---|
1133 | // ??? would be nice to (deftype pathname-arg () |
---|
1134 | // '(or (member :wild :unspecific) string (and cons |
---|
1135 | // ,(mapcar ... Is this possible? |
---|
1136 | if ((value instanceof Cons |
---|
1137 | // XXX check that the elements of a list are themselves valid |
---|
1138 | || value == Keyword.UNSPECIFIC |
---|
1139 | || value.equals(NIL))) { |
---|
1140 | directory = value; |
---|
1141 | } else { |
---|
1142 | return |
---|
1143 | type_error("DIRECTORY argument not a string, list of strings, nil, :WILD, or :UNSPECIFIC.", |
---|
1144 | value, |
---|
1145 | list(Symbol.OR, |
---|
1146 | NIL, Symbol.STRING, Symbol.CONS, Keyword.WILD, Keyword.UNSPECIFIC)); |
---|
1147 | } |
---|
1148 | } |
---|
1149 | } else if (key == Keyword.NAME) { |
---|
1150 | name = value; |
---|
1151 | nameSupplied = true; |
---|
1152 | } else if (key == Keyword.TYPE) { |
---|
1153 | type = value; |
---|
1154 | typeSupplied = true; |
---|
1155 | } else if (key == Keyword.VERSION) { |
---|
1156 | version = value; |
---|
1157 | versionSupplied = true; |
---|
1158 | } else if (key == Keyword.DEFAULTS) { |
---|
1159 | defaults = coerceToPathname(value); |
---|
1160 | } else if (key == Keyword.CASE) { |
---|
1161 | // Ignored. |
---|
1162 | } |
---|
1163 | } |
---|
1164 | if (defaults != null) { |
---|
1165 | if (!hostSupplied) { |
---|
1166 | host = defaults.getHost(); |
---|
1167 | } |
---|
1168 | if (!directorySupplied) { |
---|
1169 | directory = defaults.getDirectory(); |
---|
1170 | } |
---|
1171 | if (!deviceSupplied) { |
---|
1172 | device = defaults.getDevice(); |
---|
1173 | } |
---|
1174 | if (!nameSupplied) { |
---|
1175 | name = defaults.getName(); |
---|
1176 | } |
---|
1177 | if (!typeSupplied) { |
---|
1178 | type = defaults.getType(); |
---|
1179 | } |
---|
1180 | if (!versionSupplied) { |
---|
1181 | version = defaults.getVersion(); |
---|
1182 | } |
---|
1183 | } |
---|
1184 | Pathname p; // Pathname is always created in following |
---|
1185 | // resolution for values of HOST |
---|
1186 | LispObject logicalHost = NIL; |
---|
1187 | if (host != NIL) { |
---|
1188 | if (host instanceof AbstractString) { |
---|
1189 | logicalHost = LogicalPathname.canonicalizeStringComponent((AbstractString) host); |
---|
1190 | } |
---|
1191 | if (LogicalPathname.TRANSLATIONS.get(logicalHost) == null) { |
---|
1192 | // Not a defined logical pathname host -- A UNC path |
---|
1193 | //warning(new LispError(host.printObject() + " is not defined as a logical pathname host.")); |
---|
1194 | p = Pathname.create(); |
---|
1195 | p.setHost(host); |
---|
1196 | } else { |
---|
1197 | p = LogicalPathname.create(); |
---|
1198 | p.setHost(logicalHost); |
---|
1199 | } |
---|
1200 | if (!Utilities.isPlatformWindows) { |
---|
1201 | p.setDevice(Keyword.UNSPECIFIC); |
---|
1202 | } |
---|
1203 | } else { |
---|
1204 | p = Pathname.create(); |
---|
1205 | } |
---|
1206 | |
---|
1207 | if (device != NIL) { |
---|
1208 | if (p instanceof LogicalPathname) { |
---|
1209 | // "The device component of a logical pathname is always :UNSPECIFIC." |
---|
1210 | if (device != Keyword.UNSPECIFIC) { |
---|
1211 | return type_error("The device component of a logical pathname must be :UNSPECIFIC.", |
---|
1212 | p.getDevice(), Keyword.UNSPECIFIC); |
---|
1213 | } |
---|
1214 | } else { |
---|
1215 | if (device instanceof Cons) { |
---|
1216 | LispObject normalizedDevice = NIL; |
---|
1217 | if (device.car() instanceof SimpleString) { |
---|
1218 | String rootNamestring = device.car().getStringValue(); |
---|
1219 | URLPathname root = new URLPathname(); |
---|
1220 | if (!isValidURL(rootNamestring)) { |
---|
1221 | Pathname rootPathname = Pathname.create(rootNamestring); |
---|
1222 | root = URLPathname.createFromFile(rootPathname); |
---|
1223 | } else { |
---|
1224 | root = URLPathname.create(rootNamestring); |
---|
1225 | } |
---|
1226 | normalizedDevice = normalizedDevice.push(root); |
---|
1227 | } else { |
---|
1228 | normalizedDevice = normalizedDevice.push(device.car()); |
---|
1229 | } |
---|
1230 | LispObject o = device.cdr(); |
---|
1231 | while (!o.car().equals(NIL)) { |
---|
1232 | Pathname next = coerceToPathname(o.car()); |
---|
1233 | normalizedDevice = normalizedDevice.push(next); |
---|
1234 | o = o.cdr(); |
---|
1235 | } |
---|
1236 | normalizedDevice = normalizedDevice.nreverse(); |
---|
1237 | p.setDevice(normalizedDevice); |
---|
1238 | } else { |
---|
1239 | p.setDevice(device); |
---|
1240 | } |
---|
1241 | } |
---|
1242 | } |
---|
1243 | if (directory != NIL) { |
---|
1244 | if (p instanceof LogicalPathname) { |
---|
1245 | if (directory.listp()) { |
---|
1246 | LispObject d = NIL; |
---|
1247 | while (directory != NIL) { |
---|
1248 | LispObject component = directory.car(); |
---|
1249 | if (component instanceof AbstractString) { |
---|
1250 | d = d.push(LogicalPathname.canonicalizeStringComponent((AbstractString) component)); |
---|
1251 | } else { |
---|
1252 | d = d.push(component); |
---|
1253 | } |
---|
1254 | directory = directory.cdr(); |
---|
1255 | } |
---|
1256 | p.setDirectory(d.nreverse()); |
---|
1257 | } else if (directory == Keyword.WILD || directory == Keyword.WILD_INFERIORS) { |
---|
1258 | p.setDirectory(directory); |
---|
1259 | } else { |
---|
1260 | error(new LispError("Invalid directory component for logical pathname: " + directory.princToString())); |
---|
1261 | } |
---|
1262 | } else { |
---|
1263 | p.setDirectory(directory); |
---|
1264 | } |
---|
1265 | } |
---|
1266 | if (name != NIL) { |
---|
1267 | if (p instanceof LogicalPathname && name instanceof AbstractString) { |
---|
1268 | p.setName(LogicalPathname.canonicalizeStringComponent((AbstractString) name)); |
---|
1269 | } else if (name instanceof AbstractString) { |
---|
1270 | p.setName(validateStringComponent((AbstractString) name)); |
---|
1271 | } else { |
---|
1272 | p.setName(name); |
---|
1273 | } |
---|
1274 | } |
---|
1275 | if (type != NIL) { |
---|
1276 | if (p instanceof LogicalPathname && type instanceof AbstractString) { |
---|
1277 | p.setType(LogicalPathname.canonicalizeStringComponent((AbstractString) type)); |
---|
1278 | } else { |
---|
1279 | p.setType(type); |
---|
1280 | } |
---|
1281 | } |
---|
1282 | |
---|
1283 | p.setVersion(version); |
---|
1284 | p.validateDirectory(true); |
---|
1285 | |
---|
1286 | // Possibly downcast type to JarPathname |
---|
1287 | if (p.getDevice() instanceof Cons) { |
---|
1288 | JarPathname result = new JarPathname(); |
---|
1289 | result.copyFrom(p); |
---|
1290 | Pathname root = (Pathname)result.getDevice().car(); |
---|
1291 | URLPathname rootDevice = null; |
---|
1292 | if (root instanceof URLPathname) { |
---|
1293 | rootDevice = URLPathname.create((URLPathname)root); |
---|
1294 | } else { |
---|
1295 | rootDevice = URLPathname.create(root); |
---|
1296 | } |
---|
1297 | result.setDevice(new Cons(rootDevice, result.getDevice().cdr())); |
---|
1298 | |
---|
1299 | if (result.getDirectory().equals(NIL) |
---|
1300 | && (!result.getName().equals(NIL) |
---|
1301 | || !result.getType().equals(NIL))) { |
---|
1302 | result.setDirectory(NIL.push(Keyword.ABSOLUTE)); |
---|
1303 | } |
---|
1304 | |
---|
1305 | // sanity check that the pathname has been constructed correctly |
---|
1306 | result.validateComponents(); |
---|
1307 | return result; |
---|
1308 | } |
---|
1309 | |
---|
1310 | // Possibly downcast to URLPathname |
---|
1311 | if (p.isURL()) { |
---|
1312 | URLPathname result = new URLPathname(); |
---|
1313 | result.copyFrom(p); |
---|
1314 | |
---|
1315 | return result; |
---|
1316 | } |
---|
1317 | |
---|
1318 | return p; |
---|
1319 | } |
---|
1320 | |
---|
1321 | |
---|
1322 | |
---|
1323 | private static final AbstractString validateStringComponent(AbstractString s) { |
---|
1324 | final int limit = s.length(); |
---|
1325 | for (int i = 0; i < limit; i++) { |
---|
1326 | char c = s.charAt(i); |
---|
1327 | // XXX '\\' should be illegal in all Pathnames at this point? |
---|
1328 | if (c == '/' || c == '\\' && Utilities.isPlatformWindows) { |
---|
1329 | error(new LispError("Invalid character #\\" + c |
---|
1330 | + " in pathname component \"" + s |
---|
1331 | + '"')); |
---|
1332 | // Not reached. |
---|
1333 | return null; |
---|
1334 | } |
---|
1335 | } |
---|
1336 | return s; |
---|
1337 | } |
---|
1338 | |
---|
1339 | private final boolean validateDirectory(boolean signalError) { |
---|
1340 | LispObject temp = getDirectory(); |
---|
1341 | if (temp == Keyword.UNSPECIFIC) { |
---|
1342 | return true; |
---|
1343 | } |
---|
1344 | while (temp != NIL) { |
---|
1345 | LispObject first = temp.car(); |
---|
1346 | temp = temp.cdr(); |
---|
1347 | if (first == Keyword.ABSOLUTE || first == Keyword.WILD_INFERIORS) { |
---|
1348 | LispObject second = temp.car(); |
---|
1349 | if (second == Keyword.UP || second == Keyword.BACK) { |
---|
1350 | if (signalError) { |
---|
1351 | StringBuilder sb = new StringBuilder(); |
---|
1352 | sb.append(first.printObject()); |
---|
1353 | sb.append(" may not be followed immediately by "); |
---|
1354 | sb.append(second.printObject()); |
---|
1355 | sb.append('.'); |
---|
1356 | error(new FileError(sb.toString(), this)); |
---|
1357 | } |
---|
1358 | return false; |
---|
1359 | } |
---|
1360 | } else if (first != Keyword.RELATIVE |
---|
1361 | && first != Keyword.WILD |
---|
1362 | && first != Keyword.UP |
---|
1363 | && first != Keyword.BACK |
---|
1364 | && !(first instanceof AbstractString)) { |
---|
1365 | if (signalError) { |
---|
1366 | error(new FileError("Unsupported directory component " + first.princToString() + ".", |
---|
1367 | this)); |
---|
1368 | } |
---|
1369 | return false; |
---|
1370 | } |
---|
1371 | } |
---|
1372 | return true; |
---|
1373 | } |
---|
1374 | private static final Primitive PATHNAMEP = new pf_pathnamep(); |
---|
1375 | @DocString(name="pathnamep", |
---|
1376 | args="object", |
---|
1377 | returns="generalized-boolean", |
---|
1378 | doc="Returns true if OBJECT is of type pathname; otherwise, returns false.") |
---|
1379 | private static class pf_pathnamep extends Primitive { |
---|
1380 | pf_pathnamep() { |
---|
1381 | super("pathnamep", "object"); |
---|
1382 | } |
---|
1383 | @Override |
---|
1384 | public LispObject execute(LispObject arg) { |
---|
1385 | return arg instanceof Pathname ? T : NIL; |
---|
1386 | } |
---|
1387 | } |
---|
1388 | private static final Primitive LOGICAL_PATHNAME_P = new pf_logical_pathname_p(); |
---|
1389 | @DocString(name="logical-pathname-p", |
---|
1390 | args="object", |
---|
1391 | returns="generalized-boolean", |
---|
1392 | |
---|
1393 | doc="Returns true if OBJECT is of type logical-pathname; otherwise, returns false.") |
---|
1394 | private static class pf_logical_pathname_p extends Primitive { |
---|
1395 | pf_logical_pathname_p() { |
---|
1396 | super("logical-pathname-p", PACKAGE_SYS, true, "object"); |
---|
1397 | } |
---|
1398 | @Override |
---|
1399 | public LispObject execute(LispObject arg) { |
---|
1400 | return arg instanceof LogicalPathname ? T : NIL; |
---|
1401 | } |
---|
1402 | } |
---|
1403 | |
---|
1404 | private static final Primitive USER_HOMEDIR_PATHNAME = new pf_user_homedir_pathname(); |
---|
1405 | @DocString(name="user-homedir-pathname", |
---|
1406 | args="&optional host", |
---|
1407 | returns="pathname", |
---|
1408 | doc="Determines the pathname that corresponds to the user's home directory.\n" |
---|
1409 | + "The value returned is obtained from the JVM system propoerty 'user.home'.\n" |
---|
1410 | + "If HOST is specified, returns NIL.") |
---|
1411 | private static class pf_user_homedir_pathname extends Primitive { |
---|
1412 | pf_user_homedir_pathname() { |
---|
1413 | super("user-homedir-pathname", "&optional host"); |
---|
1414 | } |
---|
1415 | @Override |
---|
1416 | public LispObject execute(LispObject[] args) { |
---|
1417 | switch (args.length) { |
---|
1418 | case 0: { |
---|
1419 | String s = System.getProperty("user.home"); |
---|
1420 | if (!s.endsWith(File.separator)) { |
---|
1421 | s = s.concat(File.separator); |
---|
1422 | } |
---|
1423 | return Pathname.create(s); |
---|
1424 | } |
---|
1425 | case 1: |
---|
1426 | return NIL; |
---|
1427 | default: |
---|
1428 | return error(new WrongNumberOfArgumentsException(this, 0, 1)); |
---|
1429 | } |
---|
1430 | } |
---|
1431 | } |
---|
1432 | |
---|
1433 | private static final Primitive LIST_DIRECTORY = new pf_list_directory(); |
---|
1434 | @DocString(name="list-directory", |
---|
1435 | args="directory &optional (resolve-symlinks nil)", |
---|
1436 | returns="pathnames", |
---|
1437 | doc="Lists the contents of DIRECTORY, optionally resolving symbolic links.") |
---|
1438 | private static class pf_list_directory extends Primitive { |
---|
1439 | pf_list_directory() { |
---|
1440 | super("list-directory", PACKAGE_SYS, true, "directory &optional (resolve-symlinks t)"); |
---|
1441 | } |
---|
1442 | @Override |
---|
1443 | public LispObject execute(LispObject arg) { |
---|
1444 | return execute(arg, T); |
---|
1445 | } |
---|
1446 | @Override |
---|
1447 | public LispObject execute(LispObject arg, LispObject resolveSymlinks) { |
---|
1448 | Pathname pathname = coerceToPathname(arg); |
---|
1449 | if (pathname instanceof LogicalPathname) { |
---|
1450 | pathname = LogicalPathname.translateLogicalPathname((LogicalPathname) pathname); |
---|
1451 | } |
---|
1452 | |
---|
1453 | LispObject result = NIL; |
---|
1454 | if (pathname.isJar()) { |
---|
1455 | return JarPathname.listDirectory((JarPathname)pathname); |
---|
1456 | } |
---|
1457 | |
---|
1458 | File f = pathname.getFile(); |
---|
1459 | if (f.isDirectory()) { |
---|
1460 | try { |
---|
1461 | File[] files = f.listFiles(); |
---|
1462 | if (files == null) { |
---|
1463 | return error(new FileError("Unable to list directory " |
---|
1464 | + pathname.princToString() + ".", |
---|
1465 | pathname)); |
---|
1466 | } |
---|
1467 | for (int i = files.length; i-- > 0;) { |
---|
1468 | File file = files[i]; |
---|
1469 | String path; |
---|
1470 | if (resolveSymlinks == NIL) { |
---|
1471 | path = file.getAbsolutePath(); |
---|
1472 | } else { |
---|
1473 | path = file.getCanonicalPath(); |
---|
1474 | } |
---|
1475 | if (file.isDirectory() |
---|
1476 | && !path.endsWith("/")) { |
---|
1477 | path += "/"; |
---|
1478 | } |
---|
1479 | Pathname p; |
---|
1480 | p = (Pathname)Pathname.create(path); |
---|
1481 | result = new Cons(p, result); |
---|
1482 | } |
---|
1483 | } catch (IOException e) { |
---|
1484 | return error(new FileError("Unable to list directory " |
---|
1485 | + pathname.princToString() + ".", |
---|
1486 | pathname)); |
---|
1487 | } catch (SecurityException e) { |
---|
1488 | return error(new FileError("Unable to list directory: " + e, pathname)); |
---|
1489 | } |
---|
1490 | } |
---|
1491 | return result; |
---|
1492 | } |
---|
1493 | }; |
---|
1494 | |
---|
1495 | public boolean isAbsolute() { |
---|
1496 | if (!directory.equals(NIL) || !(directory == null)) { |
---|
1497 | if (getDirectory() instanceof Cons) { |
---|
1498 | if (((Cons)getDirectory()).car().equals(Keyword.ABSOLUTE)) { |
---|
1499 | return true; |
---|
1500 | } |
---|
1501 | } |
---|
1502 | } |
---|
1503 | return false; |
---|
1504 | } |
---|
1505 | |
---|
1506 | // FIXME This should be named JAR-PATHNAME-P |
---|
1507 | @DocString(name="pathname-jar-p", |
---|
1508 | args="pathname", |
---|
1509 | returns="generalized-boolean", |
---|
1510 | doc="Predicate functionfor whether PATHNAME references a jar.") |
---|
1511 | private static final Primitive PATHNAME_JAR_P = new pf_pathname_jar_p(); |
---|
1512 | private static class pf_pathname_jar_p extends Primitive { |
---|
1513 | pf_pathname_jar_p() { |
---|
1514 | super("pathname-jar-p", PACKAGE_EXT, true); |
---|
1515 | } |
---|
1516 | @Override |
---|
1517 | public LispObject execute(LispObject arg) { |
---|
1518 | if (arg instanceof Pathname) { |
---|
1519 | Pathname p = coerceToPathname(arg); |
---|
1520 | return p.isJar() ? T : NIL; |
---|
1521 | } else { |
---|
1522 | return NIL; |
---|
1523 | } |
---|
1524 | } |
---|
1525 | } |
---|
1526 | |
---|
1527 | public boolean isJar() { |
---|
1528 | return (getDevice() instanceof Cons); |
---|
1529 | } |
---|
1530 | |
---|
1531 | /// FIXME should be named URL-PATHNAME-P |
---|
1532 | @DocString(name="pathname-url-p", |
---|
1533 | args="pathname", |
---|
1534 | returns="generalized-boolean", |
---|
1535 | doc="Predicate function for whether PATHNAME references a jaurl.") |
---|
1536 | private static final Primitive PATHNAME_URL_P = new pf_pathname_url_p(); |
---|
1537 | private static class pf_pathname_url_p extends Primitive { |
---|
1538 | pf_pathname_url_p() { |
---|
1539 | super("pathname-url-p", PACKAGE_EXT, true, "pathname", |
---|
1540 | "Predicate for whether PATHNAME references a URL."); |
---|
1541 | } |
---|
1542 | @Override |
---|
1543 | public LispObject execute(LispObject arg) { |
---|
1544 | if (arg instanceof Pathname) { |
---|
1545 | Pathname p = coerceToPathname(arg); |
---|
1546 | return p.isURL() ? T : NIL; |
---|
1547 | } else { |
---|
1548 | return NIL; |
---|
1549 | } |
---|
1550 | } |
---|
1551 | } |
---|
1552 | |
---|
1553 | public boolean isURL() { |
---|
1554 | return (getHost() instanceof Cons); |
---|
1555 | } |
---|
1556 | |
---|
1557 | public boolean isWild() { |
---|
1558 | if (getHost() == Keyword.WILD || getHost() == Keyword.WILD_INFERIORS) { |
---|
1559 | return true; |
---|
1560 | } |
---|
1561 | if (getDevice() == Keyword.WILD || getDevice() == Keyword.WILD_INFERIORS) { |
---|
1562 | return true; |
---|
1563 | } |
---|
1564 | if (getDirectory() instanceof Cons) { |
---|
1565 | if (memq(Keyword.WILD, getDirectory())) { |
---|
1566 | return true; |
---|
1567 | } |
---|
1568 | if (memq(Keyword.WILD_INFERIORS, getDirectory())) { |
---|
1569 | return true; |
---|
1570 | } |
---|
1571 | Cons d = (Cons) getDirectory(); |
---|
1572 | while (true) { |
---|
1573 | if (d.car() instanceof AbstractString) { |
---|
1574 | String s = d.car().printObject(); |
---|
1575 | if (s.contains("*")) { |
---|
1576 | return true; |
---|
1577 | } |
---|
1578 | } |
---|
1579 | if (d.cdr() == NIL || ! (d.cdr() instanceof Cons)) { |
---|
1580 | break; |
---|
1581 | } |
---|
1582 | d = (Cons)d.cdr(); |
---|
1583 | } |
---|
1584 | } |
---|
1585 | if (getName() == Keyword.WILD || getName() == Keyword.WILD_INFERIORS) { |
---|
1586 | return true; |
---|
1587 | } |
---|
1588 | if (getName() instanceof AbstractString) { |
---|
1589 | if (getName().printObject().contains("*")) { |
---|
1590 | return true; |
---|
1591 | } |
---|
1592 | } |
---|
1593 | if (getType() == Keyword.WILD || getType() == Keyword.WILD_INFERIORS) { |
---|
1594 | return true; |
---|
1595 | } |
---|
1596 | if (getType() instanceof AbstractString) { |
---|
1597 | if (getType().printObject().contains("*")) { |
---|
1598 | return true; |
---|
1599 | } |
---|
1600 | } |
---|
1601 | if (getVersion() == Keyword.WILD || getVersion() == Keyword.WILD_INFERIORS) { |
---|
1602 | return true; |
---|
1603 | } |
---|
1604 | return false; |
---|
1605 | } |
---|
1606 | |
---|
1607 | private static final Primitive _WILD_PATHNAME_P = new pf_wild_pathname_p(); |
---|
1608 | @DocString(name="%wild-pathname-p", |
---|
1609 | args="pathname keyword", |
---|
1610 | returns="generalized-boolean", |
---|
1611 | doc="Predicate for determing whether PATHNAME contains wild components.\n" |
---|
1612 | + "KEYWORD, if non-nil, should be one of :directory, :host, :device,\n" |
---|
1613 | + ":name, :type, or :version indicating that only the specified component\n" |
---|
1614 | + "should be checked for wildness.") |
---|
1615 | static final class pf_wild_pathname_p extends Primitive { |
---|
1616 | pf_wild_pathname_p() { |
---|
1617 | super("%wild-pathname-p", PACKAGE_SYS, true); |
---|
1618 | } |
---|
1619 | @Override |
---|
1620 | public LispObject execute(LispObject first, LispObject second) { |
---|
1621 | Pathname pathname = coerceToPathname(first); |
---|
1622 | if (second == NIL) { |
---|
1623 | return pathname.isWild() ? T : NIL; |
---|
1624 | } |
---|
1625 | if (second == Keyword.DIRECTORY) { |
---|
1626 | if (pathname.getDirectory() instanceof Cons) { |
---|
1627 | if (memq(Keyword.WILD, pathname.getDirectory())) { |
---|
1628 | return T; |
---|
1629 | } |
---|
1630 | if (memq(Keyword.WILD_INFERIORS, pathname.getDirectory())) { |
---|
1631 | return T; |
---|
1632 | } |
---|
1633 | } |
---|
1634 | return NIL; |
---|
1635 | } |
---|
1636 | LispObject value; |
---|
1637 | if (second == Keyword.HOST) { |
---|
1638 | value = pathname.getHost(); |
---|
1639 | } else if (second == Keyword.DEVICE) { |
---|
1640 | value = pathname.getDevice(); |
---|
1641 | } else if (second == Keyword.NAME) { |
---|
1642 | value = pathname.getName(); |
---|
1643 | } else if (second == Keyword.TYPE) { |
---|
1644 | value = pathname.getType(); |
---|
1645 | } else if (second == Keyword.VERSION) { |
---|
1646 | value = pathname.getVersion(); |
---|
1647 | } else { |
---|
1648 | return program_error("Unrecognized keyword " |
---|
1649 | + second.princToString() + "."); |
---|
1650 | } |
---|
1651 | if (value == Keyword.WILD || value == Keyword.WILD_INFERIORS) { |
---|
1652 | return T; |
---|
1653 | } else { |
---|
1654 | return NIL; |
---|
1655 | } |
---|
1656 | } |
---|
1657 | } |
---|
1658 | |
---|
1659 | static final Primitive MERGE_PATHNAMES = new pf_merge_pathnames(); |
---|
1660 | @DocString(name="merge-pathnames", |
---|
1661 | args="pathname &optional default-pathname default-version", |
---|
1662 | returns="pathname", |
---|
1663 | doc="Constructs a pathname from PATHNAME by filling in any unsupplied components\n" |
---|
1664 | + "with the corresponding values from DEFAULT-PATHNAME and DEFAULT-VERSION.") |
---|
1665 | static final class pf_merge_pathnames extends Primitive { |
---|
1666 | pf_merge_pathnames() { |
---|
1667 | super(Symbol.MERGE_PATHNAMES, "pathname &optional default-pathname default-version"); |
---|
1668 | } |
---|
1669 | @Override |
---|
1670 | public LispObject execute(LispObject arg) { |
---|
1671 | Pathname pathname = coerceToPathname(arg); |
---|
1672 | Pathname defaultPathname |
---|
1673 | = coerceToPathname(Symbol.DEFAULT_PATHNAME_DEFAULTS.symbolValue()); |
---|
1674 | LispObject defaultVersion = Keyword.NEWEST; |
---|
1675 | return mergePathnames(pathname, defaultPathname, defaultVersion); |
---|
1676 | } |
---|
1677 | @Override |
---|
1678 | public LispObject execute(LispObject first, LispObject second) { |
---|
1679 | Pathname pathname = coerceToPathname(first); |
---|
1680 | Pathname defaultPathname = coerceToPathname(second); |
---|
1681 | LispObject defaultVersion = Keyword.NEWEST; |
---|
1682 | return mergePathnames(pathname, defaultPathname, defaultVersion); |
---|
1683 | } |
---|
1684 | @Override |
---|
1685 | public LispObject execute(LispObject first, LispObject second, |
---|
1686 | LispObject third) { |
---|
1687 | Pathname pathname = coerceToPathname(first); |
---|
1688 | Pathname defaultPathname = coerceToPathname(second); |
---|
1689 | LispObject defaultVersion = third; |
---|
1690 | return mergePathnames(pathname, defaultPathname, defaultVersion); |
---|
1691 | } |
---|
1692 | } |
---|
1693 | |
---|
1694 | public static final Pathname mergePathnames(Pathname pathname, Pathname defaultPathname) { |
---|
1695 | return mergePathnames(pathname, defaultPathname, Keyword.NEWEST); |
---|
1696 | } |
---|
1697 | |
---|
1698 | public static final Pathname mergePathnames(final Pathname pathname, |
---|
1699 | final Pathname defaultPathname, |
---|
1700 | final LispObject defaultVersion) { |
---|
1701 | Pathname result; |
---|
1702 | Pathname p = Pathname.create(pathname); |
---|
1703 | Pathname d; |
---|
1704 | |
---|
1705 | if (pathname instanceof LogicalPathname) { |
---|
1706 | result = LogicalPathname.create(); |
---|
1707 | d = Pathname.create(defaultPathname); |
---|
1708 | } else { |
---|
1709 | if (pathname instanceof JarPathname |
---|
1710 | // If the defaults contain a JAR-PATHNAME, and the pathname |
---|
1711 | // to be be merged is not a JAR-PATHNAME, does not have a |
---|
1712 | // specified DEVICE or a specified HOST and has a NIL or |
---|
1713 | // relative directory then the result will be a JAR-PATHNAME. |
---|
1714 | || (defaultPathname instanceof JarPathname |
---|
1715 | && !(pathname instanceof JarPathname) |
---|
1716 | && pathname.getHost().equals(NIL) |
---|
1717 | && pathname.getDevice().equals(NIL) |
---|
1718 | && (pathname.getDirectory().equals(NIL) |
---|
1719 | || pathname.getDirectory().car().equals(Keyword.RELATIVE)))) { |
---|
1720 | result = JarPathname.create(); |
---|
1721 | } else if (pathname instanceof URLPathname) { |
---|
1722 | result = URLPathname.create(); |
---|
1723 | } else { |
---|
1724 | result = Pathname.create(); |
---|
1725 | } |
---|
1726 | |
---|
1727 | if (defaultPathname instanceof LogicalPathname) { |
---|
1728 | d = LogicalPathname.translateLogicalPathname((LogicalPathname) defaultPathname); |
---|
1729 | } else { |
---|
1730 | if (defaultPathname instanceof JarPathname) { |
---|
1731 | d = JarPathname.create((JarPathname)defaultPathname); |
---|
1732 | } else if (defaultPathname instanceof URLPathname) { |
---|
1733 | d = URLPathname.create(defaultPathname); |
---|
1734 | } else { |
---|
1735 | d = Pathname.create(defaultPathname); |
---|
1736 | } |
---|
1737 | } |
---|
1738 | } |
---|
1739 | |
---|
1740 | if (pathname.getHost().equals(NIL)) { |
---|
1741 | result.setHost(d.getHost()); |
---|
1742 | } else { |
---|
1743 | result.setHost(p.getHost()); |
---|
1744 | } |
---|
1745 | |
---|
1746 | if (!pathname.getDevice().equals(NIL)) { |
---|
1747 | if (!Utilities.isPlatformWindows) { |
---|
1748 | result.setDevice(p.getDevice()); |
---|
1749 | } else { |
---|
1750 | if (d instanceof JarPathname |
---|
1751 | && p instanceof JarPathname) { |
---|
1752 | result.setDevice(d.getDevice()); |
---|
1753 | } else { |
---|
1754 | result.setDevice(p.getDevice()); |
---|
1755 | } |
---|
1756 | } |
---|
1757 | } else { |
---|
1758 | // If the defaults contain a JAR-PATHNAME, and the pathname |
---|
1759 | // to be be merged is not a JAR-PATHNAME, does not have a |
---|
1760 | // specified DEVICE, a specified HOST, and doesn't contain a |
---|
1761 | // relative DIRECTORY, then on non-MSDOG, set its device to |
---|
1762 | // :UNSPECIFIC. |
---|
1763 | if ((d instanceof JarPathname) |
---|
1764 | && !(result instanceof JarPathname)) { |
---|
1765 | if (!Utilities.isPlatformWindows) { |
---|
1766 | result.setDevice(Keyword.UNSPECIFIC); |
---|
1767 | } else { |
---|
1768 | result.setDevice(d.getDevice()); |
---|
1769 | } |
---|
1770 | } else { |
---|
1771 | if (p.isLocalFile()) { |
---|
1772 | result.setDevice(d.getDevice()); |
---|
1773 | } else { |
---|
1774 | result.setDevice(p.getDevice()); |
---|
1775 | } |
---|
1776 | } |
---|
1777 | } |
---|
1778 | |
---|
1779 | if (pathname.isJar()) { |
---|
1780 | result.setDirectory(p.getDirectory()); |
---|
1781 | } else { |
---|
1782 | result.setDirectory(mergeDirectories(p.getDirectory(), d.getDirectory())); |
---|
1783 | // Directories are always absolute in a JarPathname |
---|
1784 | if (result instanceof JarPathname) { |
---|
1785 | LispObject directories = result.getDirectory(); |
---|
1786 | if ((!directories.car().equals(NIL)) |
---|
1787 | && directories.car().equals(Keyword.RELATIVE)) { |
---|
1788 | directories = directories.cdr().push(Keyword.ABSOLUTE); |
---|
1789 | result.setDirectory(directories); |
---|
1790 | } |
---|
1791 | } |
---|
1792 | } |
---|
1793 | |
---|
1794 | if (pathname.getName() != NIL) { |
---|
1795 | result.setName(p.getName()); |
---|
1796 | } else { |
---|
1797 | result.setName(d.getName()); |
---|
1798 | } |
---|
1799 | if (pathname.getType() != NIL) { |
---|
1800 | result.setType(p.getType()); |
---|
1801 | } else { |
---|
1802 | result.setType(d.getType()); |
---|
1803 | } |
---|
1804 | |
---|
1805 | // JAR-PATHNAME directories are always absolute |
---|
1806 | if ((result instanceof JarPathname) |
---|
1807 | && (!result.getName().equals(NIL) |
---|
1808 | || !result.getType().equals(NIL)) |
---|
1809 | && result.getDirectory().equals(NIL)) { |
---|
1810 | result.setDirectory(NIL.push(Keyword.ABSOLUTE)); |
---|
1811 | } |
---|
1812 | |
---|
1813 | // CLtLv2 MERGE-PATHNAMES |
---|
1814 | |
---|
1815 | // "[T]he missing components in the given pathname are filled |
---|
1816 | // in from the defaults pathname, except that if no version is |
---|
1817 | // specified the default version is used." |
---|
1818 | |
---|
1819 | // "The merging rules for the version are more complicated and |
---|
1820 | // depend on whether the pathname specifies a name. If the |
---|
1821 | // pathname doesn't specify a name, then the version, if not |
---|
1822 | // provided, will come from the defaults, just like the other |
---|
1823 | // components. However, if the pathname does specify a name, |
---|
1824 | // then the version is not affected by the defaults. The |
---|
1825 | // reason is that the version ``belongs to'' some other file |
---|
1826 | // name and is unlikely to have anything to do with the new |
---|
1827 | // one. Finally, if this process leaves the |
---|
1828 | // version missing, the default version is used." |
---|
1829 | if (p.getVersion() != NIL) { |
---|
1830 | result.setVersion(p.getVersion()); |
---|
1831 | } else if (p.getName() == NIL) { |
---|
1832 | if (defaultPathname.getVersion() == NIL) { |
---|
1833 | result.setVersion(defaultVersion); |
---|
1834 | } else { |
---|
1835 | result.setVersion(defaultPathname.getVersion()); |
---|
1836 | } |
---|
1837 | } else if (defaultVersion == NIL) { |
---|
1838 | result.setVersion(p.getVersion()); |
---|
1839 | } |
---|
1840 | if (result.getVersion() == NIL) { |
---|
1841 | result.setVersion(defaultVersion); |
---|
1842 | } |
---|
1843 | |
---|
1844 | if (pathname instanceof LogicalPathname) { |
---|
1845 | // When we're returning a logical |
---|
1846 | result.setDevice(Keyword.UNSPECIFIC); |
---|
1847 | if (result.getDirectory().listp()) { |
---|
1848 | LispObject original = result.getDirectory(); |
---|
1849 | LispObject canonical = NIL; |
---|
1850 | while (original != NIL) { |
---|
1851 | LispObject component = original.car(); |
---|
1852 | if (component instanceof AbstractString) { |
---|
1853 | component = LogicalPathname.canonicalizeStringComponent((AbstractString) component); |
---|
1854 | } |
---|
1855 | canonical = canonical.push(component); |
---|
1856 | original = original.cdr(); |
---|
1857 | } |
---|
1858 | result.setDirectory(canonical.nreverse()); |
---|
1859 | } |
---|
1860 | if (result.getName() instanceof AbstractString) { |
---|
1861 | result.setName(LogicalPathname.canonicalizeStringComponent((AbstractString) result.getName())); |
---|
1862 | } |
---|
1863 | if (result.getType() instanceof AbstractString) { |
---|
1864 | result.setType(LogicalPathname.canonicalizeStringComponent((AbstractString) result.getType())); |
---|
1865 | } |
---|
1866 | } |
---|
1867 | // Downcast to URLPathname if resolving a URLPathname |
---|
1868 | if (result instanceof Pathname |
---|
1869 | && URLPathname.hasExplicitFile(result)) { |
---|
1870 | URLPathname downcastResult = new URLPathname(); |
---|
1871 | downcastResult.copyFrom(result); |
---|
1872 | result = downcastResult; |
---|
1873 | } |
---|
1874 | |
---|
1875 | return result; |
---|
1876 | } |
---|
1877 | |
---|
1878 | private static final LispObject mergeDirectories(LispObject dir, |
---|
1879 | LispObject defaultDir) { |
---|
1880 | if (dir == NIL) { |
---|
1881 | return defaultDir; |
---|
1882 | } |
---|
1883 | if (dir.car() == Keyword.RELATIVE && defaultDir != NIL) { |
---|
1884 | LispObject result = NIL; |
---|
1885 | while (defaultDir != NIL) { |
---|
1886 | result = new Cons(defaultDir.car(), result); |
---|
1887 | defaultDir = defaultDir.cdr(); |
---|
1888 | } |
---|
1889 | dir = dir.cdr(); // Skip :RELATIVE. |
---|
1890 | while (dir != NIL) { |
---|
1891 | result = new Cons(dir.car(), result); |
---|
1892 | dir = dir.cdr(); |
---|
1893 | } |
---|
1894 | LispObject[] array = result.copyToArray(); |
---|
1895 | for (int i = 0; i < array.length - 1; i++) { |
---|
1896 | if (array[i] == Keyword.BACK) { |
---|
1897 | if (array[i + 1] instanceof AbstractString || array[i + 1] == Keyword.WILD) { |
---|
1898 | array[i] = null; |
---|
1899 | array[i + 1] = null; |
---|
1900 | } |
---|
1901 | } |
---|
1902 | } |
---|
1903 | result = NIL; |
---|
1904 | for (int i = 0; i < array.length; i++) { |
---|
1905 | if (array[i] != null) { |
---|
1906 | result = new Cons(array[i], result); |
---|
1907 | } |
---|
1908 | } |
---|
1909 | return result; |
---|
1910 | } |
---|
1911 | return dir; |
---|
1912 | } |
---|
1913 | |
---|
1914 | public static LispObject truename(Pathname pathname) { |
---|
1915 | return truename(pathname, false); |
---|
1916 | } |
---|
1917 | |
---|
1918 | public static LispObject truename(LispObject arg) { |
---|
1919 | return truename(arg, false); |
---|
1920 | } |
---|
1921 | |
---|
1922 | public static LispObject truename(LispObject arg, boolean errorIfDoesNotExist) { |
---|
1923 | final Pathname pathname = coerceToPathname(arg); |
---|
1924 | return truename(pathname, errorIfDoesNotExist); |
---|
1925 | } |
---|
1926 | |
---|
1927 | /** @return The canonical TRUENAME as a Pathname if the pathname |
---|
1928 | * exists, otherwise returns NIL or possibly a subtype of |
---|
1929 | * LispError if there are logical problems with the input. |
---|
1930 | */ |
---|
1931 | public static LispObject truename(Pathname pathname, |
---|
1932 | boolean errorIfDoesNotExist) { |
---|
1933 | if (pathname == null || pathname.equals(NIL)) { |
---|
1934 | return doTruenameExit(pathname, errorIfDoesNotExist); |
---|
1935 | } |
---|
1936 | if (pathname instanceof LogicalPathname) { |
---|
1937 | pathname = LogicalPathname.translateLogicalPathname((LogicalPathname) pathname); |
---|
1938 | } |
---|
1939 | if (pathname.isWild()) { |
---|
1940 | return error(new FileError("Fundamentally unable to find a truename for any wild pathname.", |
---|
1941 | pathname)); |
---|
1942 | } |
---|
1943 | Pathname result |
---|
1944 | = (Pathname)mergePathnames(pathname, |
---|
1945 | coerceToPathname(Symbol.DEFAULT_PATHNAME_DEFAULTS.symbolValue()), |
---|
1946 | NIL); |
---|
1947 | final File file = result.getFile(); |
---|
1948 | if (file != null |
---|
1949 | && file.exists()) { |
---|
1950 | if (file.isDirectory()) { |
---|
1951 | result = Pathname.getDirectoryPathname(file); |
---|
1952 | } else { |
---|
1953 | try { |
---|
1954 | result = (Pathname)Pathname.create(file.getCanonicalPath()); |
---|
1955 | } catch (IOException e) { |
---|
1956 | return error(new FileError(e.getMessage(), pathname)); |
---|
1957 | } |
---|
1958 | } |
---|
1959 | if (Utilities.isPlatformUnix) { |
---|
1960 | result.setDevice(Keyword.UNSPECIFIC); |
---|
1961 | } |
---|
1962 | return result; |
---|
1963 | } |
---|
1964 | return doTruenameExit(pathname, errorIfDoesNotExist); |
---|
1965 | } |
---|
1966 | |
---|
1967 | static LispObject doTruenameExit(Pathname pathname, boolean errorIfDoesNotExist) { |
---|
1968 | if (errorIfDoesNotExist) { |
---|
1969 | StringBuilder sb = new StringBuilder("The file "); |
---|
1970 | sb.append(pathname.princToString()); |
---|
1971 | sb.append(" does not exist."); |
---|
1972 | return error(new FileError(sb.toString(), pathname)); |
---|
1973 | } |
---|
1974 | return NIL; |
---|
1975 | } |
---|
1976 | |
---|
1977 | public static final Primitive GET_INPUT_STREAM = new pf_get_input_stream(); |
---|
1978 | @DocString(name="get-input-stream", |
---|
1979 | args="pathname", |
---|
1980 | doc="Returns a java.io.InputStream for resource denoted by PATHNAME.") |
---|
1981 | private static final class pf_get_input_stream extends Primitive { |
---|
1982 | pf_get_input_stream() { |
---|
1983 | super(Symbol.GET_INPUT_STREAM, "pathname"); |
---|
1984 | } |
---|
1985 | @Override |
---|
1986 | public LispObject execute(LispObject pathname) { |
---|
1987 | Pathname p = (Pathname) coerceToPathname(pathname); |
---|
1988 | return new JavaObject(p.getInputStream()); |
---|
1989 | } |
---|
1990 | }; |
---|
1991 | |
---|
1992 | public InputStream getInputStream() { |
---|
1993 | InputStream result = null; |
---|
1994 | File file = getFile(); |
---|
1995 | try { |
---|
1996 | result = new FileInputStream(file); |
---|
1997 | } catch (IOException e) { |
---|
1998 | simple_error("Failed to get InputStream from ~a because ~a", this, e); |
---|
1999 | } |
---|
2000 | return result; |
---|
2001 | } |
---|
2002 | |
---|
2003 | /** @return Time in milliseconds since the UNIX epoch at which the |
---|
2004 | * resource was last modified, or 0 if the time is unknown. |
---|
2005 | */ |
---|
2006 | public long getLastModified() { |
---|
2007 | File f = getFile(); |
---|
2008 | return f.lastModified(); |
---|
2009 | } |
---|
2010 | |
---|
2011 | private static final Primitive MKDIR = new pf_mkdir(); |
---|
2012 | @DocString(name="mkdir", |
---|
2013 | args="pathname", |
---|
2014 | returns="generalized-boolean", |
---|
2015 | doc="Attempts to create directory at PATHNAME returning the success or failure.") |
---|
2016 | private static class pf_mkdir extends Primitive { |
---|
2017 | pf_mkdir() { |
---|
2018 | super("mkdir", PACKAGE_SYS, false, "pathname"); |
---|
2019 | } |
---|
2020 | |
---|
2021 | @Override |
---|
2022 | public LispObject execute(LispObject arg) { |
---|
2023 | final Pathname pathname = coerceToPathname(arg); |
---|
2024 | if (pathname.isWild()) { |
---|
2025 | error(new FileError("Bad place for a wild pathname.", pathname)); |
---|
2026 | } |
---|
2027 | Pathname defaultedPathname |
---|
2028 | = (Pathname)mergePathnames(pathname, |
---|
2029 | coerceToPathname(Symbol.DEFAULT_PATHNAME_DEFAULTS.symbolValue()), |
---|
2030 | NIL); |
---|
2031 | if (defaultedPathname.isURL() || defaultedPathname.isJar()) { |
---|
2032 | return new FileError("Cannot mkdir with a " |
---|
2033 | + (defaultedPathname.isURL() ? "URL" : "jar") |
---|
2034 | + " Pathname.", |
---|
2035 | defaultedPathname); |
---|
2036 | } |
---|
2037 | |
---|
2038 | File file = defaultedPathname.getFile(); |
---|
2039 | return file.mkdir() ? T : NIL; |
---|
2040 | } |
---|
2041 | } |
---|
2042 | |
---|
2043 | private static final Primitive RENAME_FILE = new pf_rename_file(); |
---|
2044 | @DocString(name="rename-file", |
---|
2045 | args="filespec new-name", |
---|
2046 | returns="defaulted-new-name, old-truename, new-truename", |
---|
2047 | doc = "Modifies the file system in such a way that the file indicated by FILESPEC is renamed to DEFAULTED-NEW-NAME.\n" |
---|
2048 | + "\n" |
---|
2049 | + "Returns three values if successful. The primary value, DEFAULTED-NEW-NAME, is \n" |
---|
2050 | + "the resulting name which is composed of NEW-NAME with any missing components filled in by \n" |
---|
2051 | + "performing a merge-pathnames operation using filespec as the defaults. The secondary \n" |
---|
2052 | + "value, OLD-TRUENAME, is the truename of the file before it was renamed. The tertiary \n" |
---|
2053 | + "value, NEW-TRUENAME, is the truename of the file after it was renamed.\n") |
---|
2054 | private static class pf_rename_file extends Primitive { |
---|
2055 | pf_rename_file() { |
---|
2056 | super("rename-file", "filespec new-name"); |
---|
2057 | } |
---|
2058 | @Override |
---|
2059 | public LispObject execute(LispObject first, LispObject second) { |
---|
2060 | Pathname oldPathname = coerceToPathname(first); |
---|
2061 | Pathname oldTruename = (Pathname) Symbol.TRUENAME.execute(oldPathname); |
---|
2062 | Pathname newName = coerceToPathname(second); |
---|
2063 | if (newName.isWild()) { |
---|
2064 | error(new FileError("Bad place for a wild pathname.", newName)); |
---|
2065 | } |
---|
2066 | if (oldTruename.isJar()) { |
---|
2067 | error(new FileError("Bad place for a jar pathname.", oldTruename)); |
---|
2068 | } |
---|
2069 | if (newName.isJar()) { |
---|
2070 | error(new FileError("Bad place for a jar pathname.", newName)); |
---|
2071 | } |
---|
2072 | if (oldTruename.isURL()) { |
---|
2073 | error(new FileError("Bad place for a URL pathname.", oldTruename)); |
---|
2074 | } |
---|
2075 | if (newName.isURL()) { |
---|
2076 | error(new FileError("Bad place for a jar pathname.", newName)); |
---|
2077 | } |
---|
2078 | |
---|
2079 | Pathname defaultedNewName = (Pathname)mergePathnames(newName, oldTruename, NIL); |
---|
2080 | |
---|
2081 | File source = oldTruename.getFile(); |
---|
2082 | File destination = null; |
---|
2083 | if (defaultedNewName instanceof LogicalPathname) { |
---|
2084 | destination = LogicalPathname.translateLogicalPathname((LogicalPathname)defaultedNewName) |
---|
2085 | .getFile(); |
---|
2086 | } else { |
---|
2087 | destination = defaultedNewName.getFile(); |
---|
2088 | } |
---|
2089 | if (Utilities.isPlatformWindows) { |
---|
2090 | if (destination.isFile()) { |
---|
2091 | //if (destination.isJar()) { |
---|
2092 | // By default, MSDOG doesn't allow one to remove files that are open, so we need to close |
---|
2093 | // any open jar references |
---|
2094 | // FIXME |
---|
2095 | // ZipCache.remove(destination); |
---|
2096 | // } |
---|
2097 | destination.delete(); |
---|
2098 | } |
---|
2099 | } |
---|
2100 | if (source.renameTo(destination)) { // Success! |
---|
2101 | Pathname newTruename = (Pathname)truename(defaultedNewName, true); |
---|
2102 | return LispThread.currentThread().setValues(defaultedNewName, |
---|
2103 | oldTruename, |
---|
2104 | newTruename); |
---|
2105 | } |
---|
2106 | return error(new FileError("Unable to rename " |
---|
2107 | + oldTruename.princToString() |
---|
2108 | + " to " + newName.princToString() |
---|
2109 | + ".", |
---|
2110 | oldTruename)); |
---|
2111 | } |
---|
2112 | } |
---|
2113 | |
---|
2114 | // TODO clarify uri encoding cases in implementation and document |
---|
2115 | private static final Primitive FILE_NAMESTRING = new pf_file_namestring(); |
---|
2116 | @DocString(name="file-namestring", |
---|
2117 | args="pathname", |
---|
2118 | returns="namestring", |
---|
2119 | doc="Returns just the name, type, and version components of PATHNAME.") |
---|
2120 | private static class pf_file_namestring extends Primitive { |
---|
2121 | pf_file_namestring() { |
---|
2122 | super(Symbol.FILE_NAMESTRING, "pathname"); |
---|
2123 | } |
---|
2124 | @Override |
---|
2125 | public LispObject execute(LispObject arg) { |
---|
2126 | Pathname p = coerceToPathname(arg); |
---|
2127 | StringBuilder sb = new StringBuilder(); |
---|
2128 | if (p.getName() instanceof AbstractString) { |
---|
2129 | sb.append(p.getName().getStringValue()); |
---|
2130 | } else if (p.getName() == Keyword.WILD) { |
---|
2131 | sb.append('*'); |
---|
2132 | } else { |
---|
2133 | return NIL; |
---|
2134 | } |
---|
2135 | if (p.getType() instanceof AbstractString) { |
---|
2136 | sb.append('.'); |
---|
2137 | sb.append(p.getType().getStringValue()); |
---|
2138 | } else if (p.getType() == Keyword.WILD) { |
---|
2139 | sb.append(".*"); |
---|
2140 | } |
---|
2141 | return new SimpleString(sb); |
---|
2142 | } |
---|
2143 | } |
---|
2144 | |
---|
2145 | private static final Primitive HOST_NAMESTRING = new pf_host_namestring(); |
---|
2146 | @DocString(name="host-namestring", |
---|
2147 | args="pathname", |
---|
2148 | returns="namestring", |
---|
2149 | doc="Returns the host name of PATHNAME.") |
---|
2150 | private static class pf_host_namestring extends Primitive { |
---|
2151 | pf_host_namestring() { |
---|
2152 | super("host-namestring", "pathname"); |
---|
2153 | } |
---|
2154 | @Override |
---|
2155 | public LispObject execute(LispObject arg) { |
---|
2156 | return coerceToPathname(arg).getHost(); // XXX URL-PATHNAME |
---|
2157 | } |
---|
2158 | } |
---|
2159 | |
---|
2160 | static { |
---|
2161 | LispObject obj = Symbol.DEFAULT_PATHNAME_DEFAULTS.getSymbolValue(); |
---|
2162 | Symbol.DEFAULT_PATHNAME_DEFAULTS.setSymbolValue(coerceToPathname(obj)); |
---|
2163 | } |
---|
2164 | |
---|
2165 | |
---|
2166 | File getFile() { |
---|
2167 | String namestring = getNamestring(); // XXX UNC pathnames currently have no namestring |
---|
2168 | if (namestring != null) { |
---|
2169 | try { |
---|
2170 | URI uri = new URI(namestring); |
---|
2171 | return new File(uri); |
---|
2172 | } catch (URISyntaxException ex) { |
---|
2173 | return new File(namestring); |
---|
2174 | } catch (IllegalArgumentException e) { |
---|
2175 | return new File(namestring); |
---|
2176 | } |
---|
2177 | } |
---|
2178 | error(new FileError("Pathname has no namestring: " + princToString(), |
---|
2179 | this)); |
---|
2180 | return (File)UNREACHED; |
---|
2181 | } |
---|
2182 | |
---|
2183 | public static Pathname getDirectoryPathname(File file) { |
---|
2184 | try { |
---|
2185 | String namestring = file.getCanonicalPath(); |
---|
2186 | if (namestring != null && namestring.length() > 0) { |
---|
2187 | // ??? do we really want the platform dependent separatorChar? |
---|
2188 | if (namestring.charAt(namestring.length() - 1) != File.separatorChar) { |
---|
2189 | namestring = namestring.concat(File.separator); |
---|
2190 | } |
---|
2191 | } |
---|
2192 | return (Pathname)Pathname.create(namestring); |
---|
2193 | } catch (IOException e) { |
---|
2194 | error(new LispError(e.getMessage())); |
---|
2195 | // Not reached. |
---|
2196 | return null; |
---|
2197 | } |
---|
2198 | } |
---|
2199 | |
---|
2200 | // Whether this pathname represents a file on the filesystem, not |
---|
2201 | // addressed as a JAR-PATHNAME |
---|
2202 | public boolean isLocalFile() { |
---|
2203 | if (getHost().equals(NIL) |
---|
2204 | || Symbol.GETF.execute(getHost(), URLPathname.SCHEME, NIL).equals(URLPathname.FILE)) { |
---|
2205 | return true; |
---|
2206 | } |
---|
2207 | return false; |
---|
2208 | } |
---|
2209 | |
---|
2210 | Pathname getEntryPath() { |
---|
2211 | return Pathname.create(asEntryPath()); |
---|
2212 | } |
---|
2213 | |
---|
2214 | /** @return The representation of the DIRECTORY/NAME/TYPE elements |
---|
2215 | * of pathname suitable for referencing an entry in a Zip/JAR file. |
---|
2216 | * |
---|
2217 | * This representation is always a relative path. |
---|
2218 | */ |
---|
2219 | String asEntryPath() { |
---|
2220 | Pathname p = Pathname.create(); |
---|
2221 | p.setDirectory(getDirectory()) |
---|
2222 | .setName(getName()) |
---|
2223 | .setType(getType()); |
---|
2224 | String path = p.getNamestring(); |
---|
2225 | |
---|
2226 | StringBuilder result = new StringBuilder(); |
---|
2227 | result.append(path); |
---|
2228 | |
---|
2229 | // ZipEntry syntax is always relative |
---|
2230 | if (result.length() > 1 |
---|
2231 | && result.substring(0, 1).equals("/")) { |
---|
2232 | return result.substring(1); |
---|
2233 | } |
---|
2234 | return result.toString(); |
---|
2235 | } |
---|
2236 | |
---|
2237 | boolean isRemote() { |
---|
2238 | if (this instanceof URLPathname) { |
---|
2239 | URLPathname p = (URLPathname) this; |
---|
2240 | LispObject scheme = Symbol.GETF.execute(p.getHost(), URLPathname.SCHEME, NIL); |
---|
2241 | if (scheme.equals(NIL) |
---|
2242 | || p.getHost().getStringValue().equals("file")) { |
---|
2243 | return false; |
---|
2244 | } |
---|
2245 | return true; |
---|
2246 | } else if (this instanceof JarPathname) { |
---|
2247 | Pathname root = (Pathname) ((JarPathname)this).getRootJar(); |
---|
2248 | return root.isRemote(); |
---|
2249 | } else { |
---|
2250 | return false; |
---|
2251 | } |
---|
2252 | } |
---|
2253 | } |
---|