/[cmucl]/src/code/filesys.lisp
ViewVC logotype

Contents of /src/code/filesys.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.106 - (show annotations)
Thu Jun 19 21:28:51 2008 UTC (5 years, 9 months ago) by rtoy
Branch: MAIN
CVS Tags: snapshot-2008-08, snapshot-2008-09, sse2-packed-2008-11-12, snapshot-2008-07, sse2-base, sse2-packed-base, release-19f-pre1, snapshot-2008-12, snapshot-2008-11, label-2009-03-16, release-19f-base, merge-sse2-packed, merge-with-19f, RELEASE_19f, snapshot-2008-10, label-2009-03-25, sse2-checkpoint-2008-10-01, sse2-merge-with-2008-11, sse2-merge-with-2008-10, snapshot-2009-02, snapshot-2009-01, snapshot-2009-05, snapshot-2009-04
Branch point for: RELEASE-19F-BRANCH, sse2-packed-branch, sse2-branch
Changes since 1.105: +36 -26 lines
Make DIRECTORY work faster when there are a large number of files.
Basically, don't use delete-duplicates but do it ourselves since the
list is already sorted.

From Lynn Quam, cmucl-imp, 2008/06/05, slightly modified.

code/filesys.lisp:
o New DIRECTORY function

general-info/release-19f.txt:
o Update
1 ;;; -*- Log: code.log; Package: Lisp -*-
2 ;;; **********************************************************************
3 ;;; This code was written as part of the CMU Common Lisp project at
4 ;;; Carnegie Mellon University, and has been placed in the public domain.
5 ;;; If you want to use this code or any part of CMU Common Lisp, please contact
6 ;;; Scott Fahlman or slisp-group@cs.cmu.edu.
7 ;;;
8 (ext:file-comment
9 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/filesys.lisp,v 1.106 2008/06/19 21:28:51 rtoy Exp $")
10 ;;;
11 ;;; **********************************************************************
12 ;;;
13 ;;; File system interface functions. This file is pretty UNIX specific.
14 ;;;
15 ;;; Written by William Lott
16 ;;;
17 ;;; **********************************************************************
18
19 (in-package "LISP")
20
21 (export '(truename probe-file user-homedir-pathname directory
22 rename-file delete-file file-write-date file-author))
23
24 (use-package "EXTENSIONS")
25
26 (in-package "EXTENSIONS")
27 (export '(print-directory complete-file ambiguous-files default-directory
28 purge-backup-files file-writable unix-namestring))
29 (in-package "LISP")
30
31
32 ;;;; Unix pathname host support.
33
34 ;;; Unix namestrings have the following format:
35 ;;;
36 ;;; namestring := [ directory ] [ file [ type [ version ]]]
37 ;;; directory := [ "/" | search-list ] { file "/" }*
38 ;;; search-list := [^:/]*:
39 ;;; file := [^/]*
40 ;;; type := "." [^/.]*
41 ;;; version := ".*" | ".~" ([1-9]+[0-9]* | "*") "~"
42 ;;;
43 ;;; Note: this grammar is ambiguous. The string foo.bar.~5~ can be parsed
44 ;;; as either just the file specified or as specifying the file, type, and
45 ;;; version. Therefore, we use the following rules when confronted with
46 ;;; an ambiguous file.type.version string:
47 ;;;
48 ;;; - If the first character is a dot, it's part of the file. It is not
49 ;;; considered a dot in the following rules.
50 ;;;
51 ;;; - If there is only one dot, it separates the file and the type.
52 ;;;
53 ;;; - If there are multiple dots and the stuff following the last dot
54 ;;; is a valid version, then that is the version and the stuff between
55 ;;; the second to last dot and the last dot is the type.
56 ;;;
57 ;;; Wildcard characters:
58 ;;;
59 ;;; If the directory, file, type components contain any of the following
60 ;;; characters, it is considered part of a wildcard pattern and has the
61 ;;; following meaning.
62 ;;;
63 ;;; ? - matches any character
64 ;;; * - matches any zero or more characters.
65 ;;; [abc] - matches any of a, b, or c.
66 ;;; {str1,str2,...,strn} - matches any of str1, str2, ..., or strn.
67 ;;;
68 ;;; Any of these special characters can be preceded by a backslash to
69 ;;; cause it to be treated as a regular character.
70 ;;;
71
72 (defun remove-backslashes (namestr start end)
73 "Remove any occurrences of \\ from the string because we've already
74 checked for whatever may have been backslashed."
75 (declare (type simple-base-string namestr)
76 (type index start end))
77 (let* ((result (make-string (- end start)))
78 (dst 0)
79 (quoted nil))
80 (do ((src start (1+ src)))
81 ((= src end))
82 (cond (quoted
83 (setf (schar result dst) (schar namestr src))
84 (setf quoted nil)
85 (incf dst))
86 (t
87 (let ((char (schar namestr src)))
88 (cond ((char= char #\\)
89 (setq quoted t))
90 (t
91 (setf (schar result dst) char)
92 (incf dst)))))))
93 (when quoted
94 (error 'namestring-parse-error
95 :complaint "Backslash in bad place."
96 :namestring namestr
97 :offset (1- end)))
98 (shrink-vector result dst)))
99
100 (defvar *ignore-wildcards* nil
101 "If non-NIL, Unix shell-style wildcards are ignored when parsing
102 pathname namestrings. They are also ignored when computing
103 namestrings for pathname objects. Thus, *, ?, etc. are not
104 wildcards when parsing a namestring, and are not escaped when
105 printing pathnames.")
106
107 (defun maybe-make-pattern (namestr start end)
108 (declare (type simple-base-string namestr)
109 (type index start end))
110 (if *ignore-wildcards*
111 (subseq namestr start end)
112 (collect ((pattern))
113 (let ((quoted nil)
114 (any-quotes nil)
115 (last-regular-char nil)
116 (index start))
117 (flet ((flush-pending-regulars ()
118 (when last-regular-char
119 (pattern (if any-quotes
120 (remove-backslashes namestr
121 last-regular-char
122 index)
123 (subseq namestr last-regular-char index)))
124 (setf any-quotes nil)
125 (setf last-regular-char nil))))
126 (loop
127 (when (>= index end)
128 (return))
129 (let ((char (schar namestr index)))
130 (cond (quoted
131 (incf index)
132 (setf quoted nil))
133 ((char= char #\\)
134 (setf quoted t)
135 (setf any-quotes t)
136 (unless last-regular-char
137 (setf last-regular-char index))
138 (incf index))
139 ((char= char #\?)
140 (flush-pending-regulars)
141 (pattern :single-char-wild)
142 (incf index))
143 ((char= char #\*)
144 (flush-pending-regulars)
145 (pattern :multi-char-wild)
146 (incf index))
147 ((char= char #\[)
148 (flush-pending-regulars)
149 (let ((close-bracket
150 (position #\] namestr :start index :end end)))
151 (unless close-bracket
152 (error 'namestring-parse-error
153 :complaint "``['' with no corresponding ``]''"
154 :namestring namestr
155 :offset index))
156 (pattern (list :character-set
157 (subseq namestr
158 (1+ index)
159 close-bracket)))
160 (setf index (1+ close-bracket))))
161 (t
162 (unless last-regular-char
163 (setf last-regular-char index))
164 (incf index)))))
165 (flush-pending-regulars)))
166 (cond ((null (pattern))
167 "")
168 ((null (cdr (pattern)))
169 (let ((piece (first (pattern))))
170 (typecase piece
171 ((member :multi-char-wild) :wild)
172 (simple-string piece)
173 (t
174 (make-pattern (pattern))))))
175 (t
176 (make-pattern (pattern)))))))
177
178 ;;; extract-name-type-and-version -- Internal.
179 ;;;
180 (defun extract-name-type-and-version (namestr start end)
181 (declare (type simple-base-string namestr)
182 (type index start end))
183 (labels
184 ((explicit-version (namestr start end)
185 ;; Look for something like "~*~" at the end of the
186 ;; namestring, where * can be #\* or some digits. This
187 ;; denotes a version.
188 ;;(format t "explicit-version ~S ~A ~A~%" namestr start end)
189 (cond ((or (< (- end start) 4)
190 (and (char/= (schar namestr (1- end)) #\~)
191 (char/= (schar namestr (1- end)) #\*)))
192 ;; No explicit version given, so return NIL to
193 ;; indicate we don't want file versions, unless
194 ;; requested in other ways.
195 ;;(format t "case 1: ~A ~A~%" nil end)
196 (values nil end))
197 ((and (not *ignore-wildcards*)
198 (char= (schar namestr (- end 2)) #\*)
199 (char= (schar namestr (- end 3)) #\~)
200 (char= (schar namestr (- end 4)) #\.))
201 ;; Found "~*~", so it's a wild version
202 ;;(format t "case 2: ~A ~A~%" :wild (- end 4))
203 (values :wild (- end 4)))
204 (t
205 ;; Look for a version number. Start at the end, just
206 ;; before the ~ and keep looking for digits. If the
207 ;; first non-digit is ~, and the leading character is
208 ;; a non-zero digit, we have a version number, so get
209 ;; it. If not, we didn't find a version number, so we
210 ;; call it :newest
211 (do ((i (- end 2) (1- i)))
212 ((< i (+ start 1))
213 ;;(format t "case 3: ~A ~A~%" :newest end)
214 (values :newest end))
215 (let ((char (schar namestr i)))
216 (when (eql char #\~)
217 (return (if (char= (schar namestr (1- i)) #\.)
218 (if (char= (schar namestr (1+ i)) #\0)
219 (values nil end)
220 (values (parse-integer namestr :start (1+ i)
221 :end (1- end))
222 (1- i)))
223 (values :newest end))))
224 (unless (char<= #\0 char #\9)
225 ;; It's not a digit. Give up, and say the
226 ;; version is NIL.
227 ;;(format t "case 3 return: ~A ~A~%" nil end)
228 (return (values nil end))))))))
229 (any-version (namestr start end)
230 ;; process end of string looking for a version candidate.
231 (multiple-value-bind (version where)
232 (explicit-version namestr start end)
233 (cond ((not (eq version :newest))
234 (values version where))
235 ((and (not *ignore-wildcards*)
236 (>= (- end 2) start)
237 (char= (schar namestr (- end 1)) #\*)
238 (char= (schar namestr (- end 2)) #\.)
239 (find #\. namestr
240 :start (min (1+ start) (- end 2))
241 :end (- end 2)))
242 (values :wild (- end 2)))
243 (t (values version where)))))
244 (any-type (namestr start end)
245 ;; Process end of string looking for a type. A leading "."
246 ;; is part of the name.
247 (let ((where (position #\. namestr
248 :start (min (1+ start) end)
249 :end end :from-end t)))
250 (when where
251 (values where end))))
252 (any-name (namestr start end)
253 (declare (ignore namestr))
254 (values start end)))
255 (multiple-value-bind (version vstart)
256 (any-version namestr start end)
257 (multiple-value-bind (tstart tend)
258 (any-type namestr start vstart)
259 (multiple-value-bind (nstart nend)
260 (any-name namestr start (or tstart vstart))
261 (values
262 (maybe-make-pattern namestr nstart nend)
263 (and tstart (maybe-make-pattern namestr (1+ tstart) tend))
264 version))))))
265
266 ;;; Take a string and return a list of cons cells that mark the char
267 ;;; separated subseq. The first value t if absolute directories location.
268 ;;;
269 (defun split-at-slashes (namestr start end)
270 (declare (type simple-base-string namestr)
271 (type index start end))
272 (let ((absolute (and (/= start end)
273 (char= (schar namestr start) #\/))))
274 (when absolute
275 (incf start))
276 ;; Next, split the remainder into slash separated chunks.
277 (collect ((pieces))
278 (loop
279 (let ((slash (position #\/ namestr :start start :end end)))
280 (pieces (cons start (or slash end)))
281 (unless slash
282 (return))
283 (setf start (1+ slash))))
284 (values absolute (pieces)))))
285
286 (defun maybe-extract-search-list (namestr start end)
287 (declare (type simple-base-string namestr)
288 (type index start end))
289 (let ((quoted nil))
290 (do ((index start (1+ index)))
291 ((= index end)
292 (values nil start))
293 (if quoted
294 (setf quoted nil)
295 (case (schar namestr index)
296 (#\\
297 (setf quoted t))
298 (#\:
299 (return (values (remove-backslashes namestr start index)
300 (1+ index)))))))))
301
302 (defun parse-unix-namestring (namestr start end)
303 (declare (type simple-base-string namestr)
304 (type index start end))
305 (multiple-value-bind
306 (absolute pieces)
307 (split-at-slashes namestr start end)
308 (let ((search-list
309 (if absolute
310 nil
311 (let ((first (car pieces)))
312 (multiple-value-bind
313 (search-list new-start)
314 (maybe-extract-search-list namestr
315 (car first) (cdr first))
316 (when search-list
317 ;; Lose if this search-list is already defined as
318 ;; a logical host. Since the syntax for
319 ;; search-lists and logical pathnames are the
320 ;; same, we can't allow the creation of one when
321 ;; the other is defined.
322 (when (find-logical-host search-list nil)
323 (error "~A already names a logical host" search-list))
324 (setf absolute t)
325 (setf (car first) new-start))
326 search-list)))))
327 (multiple-value-bind (name type version)
328 (let* ((tail (car (last pieces)))
329 (tail-start (car tail))
330 (tail-end (cdr tail)))
331 (unless (= tail-start tail-end)
332 (setf pieces (butlast pieces))
333 (cond ((string= namestr ".." :start1 tail-start :end1 tail-end)
334 ;; ".." is a directory. Add this piece to the
335 ;; list of pieces, and make the name/type/version
336 ;; nil.
337 (setf pieces (append pieces (list (cons tail-start tail-end))))
338 (values nil nil nil))
339 ((string= namestr "." :start1 tail-start :end1 tail-end)
340 ;; "." is a directory as well.
341 (setf pieces (append pieces (list (cons tail-start tail-end))))
342 (values nil nil nil))
343 ((not (find-if-not #'(lambda (c)
344 (char= c #\.))
345 namestr :start tail-start :end tail-end))
346 ;; Got a bunch of dots. Make it a file of the
347 ;; same name, and type the empty string.
348 (values (subseq namestr tail-start (1- tail-end)) "" nil))
349 (t
350 (extract-name-type-and-version namestr tail-start tail-end)))))
351 ;; PVE: Make sure there are no illegal characters in the name
352 ;; such as #\Null and #\/.
353 (when (and (stringp name)
354 (find-if #'(lambda (x)
355 (or (char= x #\Null) (char= x #\/)))
356 name))
357 (error 'parse-error))
358 ;; Now we have everything we want. So return it.
359 (values nil ; no host for unix namestrings.
360 nil ; no devices for unix namestrings.
361 (collect ((dirs))
362 (when search-list
363 (dirs (intern-search-list search-list)))
364 (dolist (piece pieces)
365 (let ((piece-start (car piece))
366 (piece-end (cdr piece)))
367 (unless (= piece-start piece-end)
368 (cond ((string= namestr ".." :start1 piece-start
369 :end1 piece-end)
370 (dirs :up))
371 ((string= namestr "**" :start1 piece-start
372 :end1 piece-end)
373 (dirs :wild-inferiors))
374 (t
375 (dirs (maybe-make-pattern namestr
376 piece-start
377 piece-end)))))))
378 (cond (absolute
379 (cons :absolute (dirs)))
380 ((dirs)
381 ;; "." in a :relative directory is the same
382 ;; as if it weren't there, so remove them.
383 (cons :relative (delete "." (dirs) :test #'equal)))
384 (t
385 ;; If there is no directory and the name is
386 ;; "." and the type is NIL, we really got
387 ;; directory ".", so make it so.
388 (if (and (equal name ".")
389 (null type))
390 (list :relative)
391 nil))))
392 ;; A file with name "." and type NIL can't be the name
393 ;; of file on Unix because it's a directory. This was
394 ;; handled above, so we can just set the name to nil.
395 (if (and (equal name ".")
396 (null type))
397 nil
398 name)
399 type
400 version)))))
401
402 (defun unparse-unix-host (pathname)
403 (declare (type pathname pathname)
404 (ignore pathname))
405 ;; this host designator needs to be recognized as a physical host in
406 ;; PARSE-NAMESTRING. Until sbcl-0.7.3.x, we had "Unix" here, but
407 ;; that's a valid Logical Hostname, so that's a bad choice. -- CSR,
408 "")
409
410 (defun unparse-unix-piece (thing)
411 (etypecase thing
412 ((member :wild) "*")
413 ((member :unspecific)
414 ;; CLHS 19.2.2.2.3.1 says "That is, both nil and :unspecific
415 ;; cause the component not to appear in the namestring."
416 "")
417 (simple-string
418 (if *ignore-wildcards*
419 thing
420 (let* ((srclen (length thing))
421 (dstlen srclen))
422 (dotimes (i srclen)
423 (case (schar thing i)
424 ((#\* #\? #\[)
425 (incf dstlen))))
426 (let ((result (make-string dstlen))
427 (dst 0))
428 (dotimes (src srclen)
429 (let ((char (schar thing src)))
430 (case char
431 ((#\* #\? #\[)
432 (setf (schar result dst) #\\)
433 (incf dst)))
434 (setf (schar result dst) char)
435 (incf dst)))
436 result))))
437 (pattern
438 (collect ((strings))
439 (dolist (piece (pattern-pieces thing))
440 (etypecase piece
441 (simple-string
442 (strings piece))
443 (symbol
444 (ecase piece
445 (:multi-char-wild
446 (strings "*"))
447 (:single-char-wild
448 (strings "?"))))
449 (cons
450 (case (car piece)
451 (:character-set
452 (strings "[")
453 (strings (second piece))
454 (strings "]"))
455 (t
456 (error "Invalid pattern piece: ~S" piece))))))
457 (apply #'concatenate
458 'simple-string
459 (strings))))))
460
461 (defun unparse-unix-directory-list (directory)
462 (declare (type list directory))
463 (collect ((pieces))
464 (when directory
465 (ecase (pop directory)
466 (:absolute
467 (cond ((search-list-p (car directory))
468 (pieces (search-list-name (pop directory)))
469 (pieces ":"))
470 (t
471 (pieces "/"))))
472 (:relative
473 ;; Nothing special, except if we were given '(:relative).
474 (unless directory
475 (pieces "./"))
476 ))
477 (dolist (dir directory)
478 (typecase dir
479 ((member :up)
480 (pieces "../"))
481 ((member :back)
482 (error ":BACK cannot be represented in namestrings."))
483 ((member :wild-inferiors)
484 (pieces "**/"))
485 ((or simple-string pattern (eql :wild))
486 (pieces (unparse-unix-piece dir))
487 (pieces "/"))
488 (t
489 (error "Invalid directory component: ~S" dir)))))
490 (apply #'concatenate 'simple-string (pieces))))
491
492 (defun unparse-unix-directory (pathname)
493 (declare (type pathname pathname))
494 (unparse-unix-directory-list (%pathname-directory pathname)))
495
496 (defun unparse-unix-file (pathname)
497 (declare (type pathname pathname))
498 (collect ((strings))
499 (let* ((name (%pathname-name pathname))
500 (type (%pathname-type pathname))
501 (type-supplied (not (or (null type) (eq type :unspecific))))
502 (logical-p (logical-pathname-p pathname))
503 (version (%pathname-version pathname))
504 ;; Preserve version :newest for logical pathnames.
505 (version-supplied (not (or (null version)
506 (member version (if logical-p
507 '(:unspecific)
508 '(:newest
509 :unspecific)))))))
510 (when name
511 (when (stringp name)
512 (when (find #\/ name)
513 (error "Cannot specify a directory separator in a pathname name: ~S" name))
514 (when (and (not type-supplied)
515 (find #\. name :start 1))
516 ;; A single leading dot is ok.
517 (error "Cannot specify a dot in a pathname name without a pathname type: ~S" name))
518 (when (or (and (string= ".." name)
519 (not type-supplied))
520 (and (string= "." name)
521 (not type-supplied)))
522 ;; Can't have a name of ".." or "." without a type.
523 (error "Invalid value for a pathname name: ~S" name)))
524 (strings (unparse-unix-piece name)))
525 (when type-supplied
526 (unless name
527 (error "Cannot specify the type without a file: ~S" pathname))
528 (when (stringp type)
529 (when (find #\/ type)
530 (error "Cannot specify a directory separator in a pathname type: ~S" type))
531 (when (find #\. type)
532 (error "Cannot specify a dot in a pathname type: ~S" type)))
533 (strings ".")
534 (strings (unparse-unix-piece type)))
535 (when (and (not (member version '(nil :newest :unspecific)))
536 (not name))
537 ;; We don't want version without a name, because when we try
538 ;; to read #p".~*~" back, the name is "", not NIL.
539 (error "Cannot specify a version without a file: ~S" pathname))
540 (when version-supplied
541 (strings (if (eq version :wild)
542 (if logical-p ".*" ".~*~")
543 (format nil (if logical-p ".~A" ".~~~D~~")
544 version)))))
545 (and (strings) (apply #'concatenate 'simple-string (strings)))))
546
547 (defun unparse-unix-namestring (pathname)
548 (declare (type pathname pathname))
549 (concatenate 'simple-string
550 (unparse-unix-directory pathname)
551 (unparse-unix-file pathname)))
552
553 (defun unparse-unix-enough (pathname defaults)
554 (declare (type pathname pathname defaults))
555 (flet ((lose ()
556 (error "~S cannot be represented relative to ~S"
557 pathname defaults)))
558 ;; Only the first path in a search-list is considered.
559 (enumerate-search-list (pathname pathname)
560 (enumerate-search-list (defaults defaults)
561 (collect ((strings))
562 (let* ((pathname-directory (%pathname-directory pathname))
563 (defaults-directory (%pathname-directory defaults))
564 (prefix-len (length defaults-directory))
565 (result-dir
566 (cond ((null pathname-directory)
567 ;; No directory, so relative to default. But
568 ;; if we're relative to default, NIL is as
569 ;; good as '(:relative) and it results in a
570 ;; shorter namestring.
571 #+nil (list :relative)
572 nil)
573 ((and (>= prefix-len 1)
574 (>= (length pathname-directory) prefix-len)
575 (compare-component (subseq pathname-directory
576 0 prefix-len)
577 defaults-directory))
578 ;; Pathname starts with a prefix of default,
579 ;; which also means both are either :relative
580 ;; or :absolute directories. So just use a
581 ;; relative directory from then on out.
582 (let ((dir-tail (nthcdr prefix-len pathname-directory)))
583 ;; If both directories are identical, don't
584 ;; return just :relative. Returning NIL
585 ;; results in a shorter string.
586 (if dir-tail
587 (cons :relative dir-tail)
588 nil)))
589 ((and (eq (car pathname-directory) :relative)
590 (not (eq (car defaults-directory) :absolute)))
591 ;; Can't represent a relative directory
592 ;; relative to an absolute directory. But
593 ;; there's no problem if both are relative;
594 ;; we just return our path.
595 pathname-directory)
596 ((eq (car pathname-directory) :absolute)
597 ;; We are an absolute pathname, so we can just use it.
598 pathname-directory)
599 (t
600 ;; We are a relative directory. So we lose.
601 (lose)))))
602 (strings (unparse-unix-directory-list result-dir)))
603 (let* ((pathname-version (%pathname-version pathname))
604 (version-needed (and pathname-version
605 (not (eq pathname-version :newest))))
606 (pathname-type (%pathname-type pathname))
607 (type-needed (or version-needed
608 (and pathname-type
609 (not (eq pathname-type :unspecific)))))
610 (pathname-name (%pathname-name pathname))
611 (name-needed (or type-needed
612 (and pathname-name
613 (not (compare-component pathname-name
614 (%pathname-name
615 defaults)))))))
616 (when name-needed
617 (unless pathname-name (lose))
618 (strings (unparse-unix-piece pathname-name)))
619 (when type-needed
620 (when (or (null pathname-type) (eq pathname-type :unspecific))
621 (lose))
622 (strings ".")
623 (strings (unparse-unix-piece pathname-type)))
624 (when version-needed
625 (typecase pathname-version
626 ((member :wild)
627 (strings ".~*~"))
628 (integer
629 (strings (format nil ".~~~D~~" pathname-version)))
630 (t
631 (lose)))))
632 (return-from unparse-unix-enough (apply #'concatenate 'simple-string (strings))))))))
633
634
635 (defstruct (unix-host
636 (:include host
637 (:parse #'parse-unix-namestring)
638 (:unparse #'unparse-unix-namestring)
639 (:unparse-host #'unparse-unix-host)
640 (:unparse-directory #'unparse-unix-directory)
641 (:unparse-file #'unparse-unix-file)
642 (:unparse-enough #'unparse-unix-enough)
643 (:customary-case :lower))
644 (:make-load-form-fun make-unix-host-load-form))
645 )
646
647 (defvar *unix-host* (make-unix-host))
648
649 (defun make-unix-host-load-form (host)
650 (declare (ignore host))
651 '*unix-host*)
652
653
654 ;;;; Wildcard matching stuff.
655
656 (defmacro enumerate-matches ((var pathname &optional result
657 &key (verify-existance t) (follow-links t))
658 &body body)
659 (let ((body-name (gensym)))
660 `(block nil
661 (flet ((,body-name (,var)
662 ,@body))
663 (%enumerate-matches (pathname ,pathname)
664 ,verify-existance ,follow-links
665 #',body-name)
666 ,result))))
667
668 (defun %enumerate-matches (pathname verify-existance follow-links function)
669 (when (pathname-type pathname)
670 (unless (pathname-name pathname)
671 (error "Cannot supply a type without a name:~% ~S" pathname)))
672 (let ((directory (pathname-directory pathname)))
673 (if directory
674 (ecase (car directory)
675 (:absolute
676 (%enumerate-directories "/" (cdr directory) pathname
677 verify-existance follow-links
678 nil function))
679 (:relative
680 (%enumerate-directories "" (cdr directory) pathname
681 verify-existance follow-links
682 nil function)))
683 (%enumerate-files "" pathname verify-existance function))))
684
685 ;;; %enumerate-directories -- Internal
686 ;;;
687 ;;; The directory node and device numbers are maintained for the current path
688 ;;; during the search for the detection of path loops upon :wild-inferiors.
689 ;;;
690 (defun %enumerate-directories (head tail pathname verify-existance
691 follow-links nodes function)
692 (declare (simple-string head))
693 (macrolet ((unix-xstat (name)
694 `(if follow-links
695 (unix:unix-stat ,name)
696 (unix:unix-lstat ,name)))
697 (with-directory-node-noted ((head) &body body)
698 `(multiple-value-bind (res dev ino mode)
699 (unix-xstat ,head)
700 (when (and res (eql (logand mode unix:s-ifmt) unix:s-ifdir))
701 (let ((nodes (cons (cons dev ino) nodes)))
702 ,@body))))
703 (do-directory-entries ((name directory) &body body)
704 `(let ((dir (unix:open-dir ,directory)))
705 (when dir
706 (unwind-protect
707 (loop
708 (let ((,name (unix:read-dir dir)))
709 (cond ((null ,name)
710 (return))
711 ((string= ,name "."))
712 ((string= ,name ".."))
713 (t
714 ,@body))))
715 (unix:close-dir dir))))))
716 (if tail
717 (let ((piece (car tail)))
718 (etypecase piece
719 (simple-string
720 (let ((head (concatenate 'string head piece)))
721 (with-directory-node-noted (head)
722 (%enumerate-directories (concatenate 'string head "/")
723 (cdr tail) pathname
724 verify-existance follow-links
725 nodes function))))
726 ((member :wild-inferiors)
727 (%enumerate-directories head (rest tail) pathname
728 verify-existance follow-links
729 nodes function)
730 (do-directory-entries (name head)
731 (let ((subdir (concatenate 'string head name)))
732 (multiple-value-bind (res dev ino mode)
733 (unix-xstat subdir)
734 (declare (type (or fixnum null) mode))
735 (when (and res (eql (logand mode unix:s-ifmt) unix:s-ifdir))
736 (unless (dolist (dir nodes nil)
737 (when (and (eql (car dir) dev)
738 (eql (cdr dir) ino))
739 (return t)))
740 (let ((nodes (cons (cons dev ino) nodes))
741 (subdir (concatenate 'string subdir "/")))
742 (%enumerate-directories subdir tail pathname
743 verify-existance follow-links
744 nodes function))))))))
745 ((or pattern (member :wild))
746 (do-directory-entries (name head)
747 (when (or (eq piece :wild) (pattern-matches piece name))
748 (let ((subdir (concatenate 'string head name)))
749 (multiple-value-bind (res dev ino mode)
750 (unix-xstat subdir)
751 (declare (type (or fixnum null) mode))
752 (when (and res
753 (eql (logand mode unix:s-ifmt) unix:s-ifdir))
754 (let ((nodes (cons (cons dev ino) nodes))
755 (subdir (concatenate 'string subdir "/")))
756 (%enumerate-directories subdir (rest tail) pathname
757 verify-existance follow-links
758 nodes function))))))))
759 ((member :up)
760 (let ((head (concatenate 'string head "..")))
761 (with-directory-node-noted (head)
762 (%enumerate-directories (concatenate 'string head "/")
763 (rest tail) pathname
764 verify-existance follow-links
765 nodes function))))))
766 (%enumerate-files head pathname verify-existance function))))
767
768 (defun %enumerate-files (directory pathname verify-existance function)
769 (declare (simple-string directory))
770 (let ((name (%pathname-name pathname))
771 (type (%pathname-type pathname))
772 (version (%pathname-version pathname)))
773 (cond ((member name '(nil :unspecific))
774 (when (or (not verify-existance)
775 (unix:unix-file-kind directory))
776 (funcall function directory)))
777 ((or (pattern-p name)
778 (pattern-p type)
779 (eq name :wild)
780 (eq type :wild)
781 (eq version :wild))
782 (let ((dir (unix:open-dir directory)))
783 (when dir
784 (unwind-protect
785 (loop
786 (let ((file (unix:read-dir dir)))
787 (if file
788 (unless (or (string= file ".")
789 (string= file ".."))
790 (multiple-value-bind
791 (file-name file-type file-version)
792 (let ((*ignore-wildcards* t))
793 (extract-name-type-and-version
794 file 0 (length file)))
795 ;; Match also happens if the file has
796 ;; no explicit version and we're asking
797 ;; for version :NEWEST, since that's
798 ;; what no version means.
799 (when (and (components-match file-name name)
800 (components-match file-type type)
801 (or (components-match file-version
802 version)
803 (and (eq file-version nil)
804 (eq version :newest))))
805 (funcall function
806 (concatenate 'string
807 directory
808 file)))))
809 (return))))
810 (unix:close-dir dir)))))
811 (t
812 (let ((file (concatenate 'string directory name)))
813 (unless (or (null type) (eq type :unspecific))
814 (setf file (concatenate 'string file "." type)))
815 (unless (member version '(nil :newest :wild :unspecific))
816 (setf file (concatenate 'string file ".~"
817 (quick-integer-to-string version)
818 "~")))
819 (when (or (not verify-existance)
820 (unix:unix-file-kind file t))
821 (funcall function file)))))))
822
823 (defun quick-integer-to-string (n)
824 (declare (type integer n))
825 (cond ((not (fixnump n))
826 (write-to-string n :base 10 :radix nil))
827 ((zerop n) "0")
828 ((eql n 1) "1")
829 ((minusp n)
830 (concatenate 'simple-string "-"
831 (the simple-string (quick-integer-to-string (- n)))))
832 (t
833 (do* ((len (1+ (truncate (integer-length n) 3)))
834 (res (make-string len))
835 (i (1- len) (1- i))
836 (q n)
837 (r 0))
838 ((zerop q)
839 (incf i)
840 (replace res res :start2 i :end2 len)
841 (shrink-vector res (- len i)))
842 (declare (simple-string res)
843 (fixnum len i r q))
844 (multiple-value-setq (q r) (truncate q 10))
845 (setf (schar res i) (schar "0123456789" r))))))
846
847
848 ;;;; UNIX-NAMESTRING -- public
849 ;;;
850 (defun unix-namestring (pathname &optional (for-input t) executable-only)
851 "Convert PATHNAME into a string that can be used with UNIX system calls.
852 Search-lists and wild-cards are expanded. If optional argument
853 FOR-INPUT is true and PATHNAME doesn't exist, NIL is returned.
854 If optional argument EXECUTABLE-ONLY is true, NIL is returned
855 unless an executable version of PATHNAME exists."
856 ;; toy@rtp.ericsson.se: Let unix-namestring also handle logical
857 ;; pathnames too.
858 (let ((path (let ((lpn (pathname pathname)))
859 (if (logical-pathname-p lpn)
860 (namestring (translate-logical-pathname lpn))
861 pathname))))
862 (enumerate-search-list
863 (pathname path)
864 (collect ((names))
865 (enumerate-matches (name pathname nil :verify-existance for-input
866 :follow-links t)
867 (when (or (not executable-only)
868 (and (eq (unix:unix-file-kind name) :file)
869 (unix:unix-access name unix:x_ok)))
870 (names name)))
871 (let ((names (names)))
872 (when names
873 (when (cdr names)
874 (error 'simple-file-error
875 :format-control "~S is ambiguous:~{~% ~A~}"
876 :format-arguments (list pathname names)))
877 (return (car names))))))))
878
879
880 ;;;; TRUENAME and PROBE-FILE.
881
882 ;;; Truename -- Public
883 ;;;
884 ;;; Another silly file function trivially different from another function.
885 ;;;
886 (defun truename (pathname)
887 "Return the pathname for the actual file described by the pathname
888 An error of type file-error is signalled if no such file exists,
889 or the pathname is wild."
890 (if (wild-pathname-p pathname)
891 (error 'simple-file-error
892 :format-control "Bad place for a wild pathname."
893 :pathname pathname)
894 (let ((result (probe-file pathname)))
895 (unless result
896 (error 'simple-file-error
897 :pathname pathname
898 :format-control "The file ~S does not exist."
899 :format-arguments (list (namestring pathname))))
900 result)))
901
902 ;;; Probe-File -- Public
903 ;;;
904 ;;; If PATHNAME exists, return its truename, otherwise NIL.
905 ;;;
906 (defun probe-file (pathname)
907 "Return a pathname which is the truename of the file if it exists, NIL
908 otherwise. An error of type file-error is signalled if pathname is wild."
909 (if (wild-pathname-p pathname)
910 (error 'simple-file-error
911 :pathname pathname
912 :format-control "Bad place for a wild pathname.")
913 (let ((namestring (unix-namestring (merge-pathnames pathname) t)))
914 (when (and namestring (unix:unix-file-kind namestring))
915 (let ((truename (unix:unix-resolve-links
916 (unix:unix-maybe-prepend-current-directory
917 namestring))))
918 (when truename
919 (let ((*ignore-wildcards* t))
920 (pathname (unix:unix-simplify-pathname truename)))))))))
921
922
923 ;;;; Other random operations.
924
925 ;;; Rename-File -- Public
926 ;;;
927 (defun rename-file (file new-name)
928 "Rename File to have the specified New-Name. If file is a stream open to a
929 file, then the associated file is renamed."
930 (let* ((original (truename file))
931 (original-namestring (unix-namestring original t))
932 (new-name (merge-pathnames new-name file))
933 (new-namestring (unix-namestring new-name nil)))
934 (unless new-namestring
935 (error 'simple-file-error
936 :pathname new-name
937 :format-control "~S can't be created."
938 :format-arguments (list new-name)))
939 (multiple-value-bind (res error)
940 (unix:unix-rename original-namestring
941 new-namestring)
942 (unless res
943 (error 'simple-file-error
944 :pathname new-name
945 :format-control "Failed to rename ~A to ~A: ~A"
946 :format-arguments (list original new-name
947 (unix:get-unix-error-msg error))))
948 (when (streamp file)
949 (file-name file new-namestring))
950 (values new-name original (truename new-name)))))
951
952 ;;; Delete-File -- Public
953 ;;;
954 ;;; Delete the file, Man.
955 ;;;
956 (defun delete-file (file)
957 "Delete the specified file."
958 (let ((namestring (unix-namestring file t)))
959 (when (streamp file)
960 ;; Close the file, but don't try to revert or anything. We want
961 ;; to delete it, man!
962 (close file))
963 (unless namestring
964 (error 'simple-file-error
965 :pathname file
966 :format-control "~S doesn't exist."
967 :format-arguments (list file)))
968
969 (multiple-value-bind (res err) (unix:unix-unlink namestring)
970 (unless res
971 (error 'simple-file-error
972 :pathname namestring
973 :format-control "Could not delete ~A: ~A."
974 :format-arguments (list namestring
975 (unix:get-unix-error-msg err))))))
976 t)
977
978 ;;; Purge-Backup-Files -- Public
979 ;;;
980 ;;; Purge old file versions
981 ;;;
982 (defun purge-backup-files (pathname &optional (keep 0))
983 "Delete old versions of files matching the given Pathname,
984 optionally keeping some of the most recent old versions."
985 (declare (type (or pathname string stream) pathname)
986 (type (integer 0 *) keep))
987 (let ((hash (make-hash-table :test 'equal)))
988 (enumerate-search-list
989 (path (make-pathname :version :wild :defaults pathname))
990 (clrhash hash)
991 (enumerate-matches (name path nil :follow-links nil)
992 (let ((dot (position #\. name :from-end t))
993 (len (length name)))
994 (when (and dot
995 (> len (+ dot 3))
996 (char= (char name (1+ dot)) #\~)
997 (char= (char name (1- len)) #\~)
998 (eq (unix:unix-file-kind name) :file))
999 (multiple-value-bind (version next)
1000 (parse-integer name :start (+ dot 2) :end (1- len)
1001 :junk-allowed t)
1002 (when (and version (= next (1- len)))
1003 (push (cons version name)
1004 (gethash (subseq name 0 dot) hash '())))))))
1005 (maphash (lambda (key value)
1006 (declare (ignore key))
1007 (mapc #'unix:unix-unlink
1008 (mapcar #'cdr (nthcdr keep
1009 (sort value #'> :key #'car)))))
1010 hash))))
1011
1012
1013 ;;; User-Homedir-Pathname -- Public
1014 ;;;
1015 ;;; Return Home:, which is set up for us at initialization time.
1016 ;;;
1017 (defun user-homedir-pathname (&optional host)
1018 "Returns the home directory of the logged in user as a pathname.
1019 This is obtained from the logical name \"home:\"."
1020 (declare (ignore host))
1021 #p"home:")
1022
1023 ;;; File-Write-Date -- Public
1024 ;;;
1025 (defun file-write-date (file)
1026 "Return file's creation date, or NIL if it doesn't exist.
1027 An error of type file-error is signalled if file is a wild pathname"
1028 (if (wild-pathname-p file)
1029 (error 'simple-file-error
1030 :pathname file
1031 :format-control "Bad place for a wild pathname.")
1032 (let ((name (unix-namestring file t)))
1033 (when name
1034 (multiple-value-bind
1035 (res dev ino mode nlink uid gid rdev size atime mtime)
1036 (unix:unix-stat name)
1037 (declare (ignore dev ino mode nlink uid gid rdev size atime))
1038 (when res
1039 (+ unix-to-universal-time mtime)))))))
1040
1041 ;;; File-Author -- Public
1042 ;;;
1043 (defun file-author (file)
1044 "Returns the file author as a string, or nil if the author cannot be
1045 determined. Signals an error of type file-error if file doesn't exist,
1046 or file is a wild pathname."
1047 (if (wild-pathname-p file)
1048 (error 'simple-file-error
1049 :pathname file
1050 :format-control "Bad place for a wild pathname.")
1051 (let ((name (unix-namestring (pathname file) t)))
1052 (unless name
1053 (error 'simple-file-error
1054 :pathname file
1055 :format-control "~S doesn't exist."
1056 :format-arguments (list file)))
1057 (multiple-value-bind (winp dev ino mode nlink uid)
1058 (unix:unix-stat name)
1059 (declare (ignore dev ino mode nlink))
1060 (when winp
1061 (let ((user-info (unix:unix-getpwuid uid)))
1062 (when user-info
1063 (unix:user-info-name user-info))))))))
1064
1065
1066 ;;;; DIRECTORY.
1067
1068 ;;; DIRECTORY -- public.
1069 ;;;
1070 (defun directory (pathname &key (all t) (check-for-subdirs t)
1071 (truenamep t) (follow-links t))
1072 "Returns a list of pathnames, one for each file that matches the given
1073 pathname. Supplying :ALL as nil causes this to ignore Unix dot files. This
1074 never includes Unix dot and dot-dot in the result. If :TRUENAMEP is NIL,
1075 then symbolic links in the result are not expanded, which is not the
1076 default because TRUENAME does follow links and the result pathnames are
1077 defined to be the TRUENAME of the pathname (the truename of a link may well
1078 be in another directory). If FOLLOW-LINKS is NIL then symbolic links are
1079 not followed."
1080 (flet ((ordered-strings-remove-duplicates (list)
1081 (let ((results '())
1082 (prev nil))
1083 (dolist (elem list)
1084 (when (or (null prev)
1085 (not (string= elem prev)))
1086 (push elem results))
1087 (setf prev elem))
1088 (nreverse results))))
1089 (let ((results nil))
1090 (enumerate-search-list
1091 (pathname (merge-pathnames pathname
1092 (make-pathname :name :wild
1093 :type :wild
1094 :version :wild
1095 :defaults *default-pathname-defaults*)
1096 :wild))
1097 (enumerate-matches (name pathname nil :follow-links follow-links)
1098 (when (or all
1099 (let ((slash (position #\/ name :from-end t)))
1100 (or (null slash)
1101 (= (1+ slash) (length name))
1102 (char/= (schar name (1+ slash)) #\.))))
1103 (push name results))))
1104 (let ((*ignore-wildcards* t))
1105 (mapcar #'(lambda (name)
1106 (let ((name (if (and check-for-subdirs
1107 (eq (unix:unix-file-kind name)
1108 :directory))
1109 (concatenate 'string name "/")
1110 name)))
1111 (if truenamep (truename name) (pathname name))))
1112 (ordered-strings-remove-duplicates (sort results #'string<)))))))
1113
1114
1115
1116 ;;;; Printing directories.
1117
1118 ;;; PRINT-DIRECTORY is exported from the EXTENSIONS package.
1119 ;;;
1120 (defun print-directory (pathname &optional stream &key all verbose return-list)
1121 "Like Directory, but prints a terse, multi-column directory listing
1122 instead of returning a list of pathnames. When :all is supplied and
1123 non-nil, then Unix dot files are included too (as ls -a). When :verbose
1124 is supplied and non-nil, then a long listing of miscellaneous
1125 information is output one file per line."
1126 (let ((*standard-output* (out-synonym-of stream))
1127 (pathname pathname))
1128 (if verbose
1129 (print-directory-verbose pathname all return-list)
1130 (print-directory-formatted pathname all return-list))))
1131
1132 (defun print-directory-verbose (pathname all return-list)
1133 (let ((contents (directory pathname :all all :check-for-subdirs nil
1134 :truenamep nil))
1135 (result nil))
1136 (format t "Directory of ~A:~%" (namestring pathname))
1137 (dolist (file contents)
1138 (let* ((namestring (unix-namestring file))
1139 (tail (subseq namestring
1140 (1+ (or (position #\/ namestring
1141 :from-end t
1142 :test #'char=)
1143 -1)))))
1144 (multiple-value-bind
1145 (reslt dev-or-err ino mode nlink uid gid rdev size atime mtime)
1146 (unix:unix-stat namestring)
1147 (declare (ignore ino gid rdev atime)
1148 (fixnum uid mode))
1149 (cond (reslt
1150 ;;
1151 ;; Print characters for file modes.
1152 (macrolet ((frob (bit name &optional sbit sname negate)
1153 `(if ,(if negate
1154 `(not (logbitp ,bit mode))
1155 `(logbitp ,bit mode))
1156 ,(if sbit
1157 `(if (logbitp ,sbit mode)
1158 (write-char ,sname)
1159 (write-char ,name))
1160 `(write-char ,name))
1161 (write-char #\-))))
1162 (frob 15 #\d nil nil t)
1163 (frob 8 #\r)
1164 (frob 7 #\w)
1165 (frob 6 #\x 11 #\s)
1166 (frob 5 #\r)
1167 (frob 4 #\w)
1168 (frob 3 #\x 10 #\s)
1169 (frob 2 #\r)
1170 (frob 1 #\w)
1171 (frob 0 #\x))
1172 ;;
1173 ;; Print the rest.
1174 (multiple-value-bind (sec min hour date month year)
1175 (get-decoded-time)
1176 (declare (ignore sec min hour date month))
1177 (format t "~2D ~8A ~8D ~12A ~A~@[/~]~%"
1178 nlink
1179 (let ((user-info (unix:unix-getpwuid uid)))
1180 (if user-info (unix:user-info-name user-info) uid))
1181 size
1182 (decode-universal-time-for-files mtime year)
1183 tail
1184 (= (logand mode unix:s-ifmt) unix:s-ifdir))))
1185 (t (format t "Couldn't stat ~A -- ~A.~%"
1186 tail
1187 (unix:get-unix-error-msg dev-or-err))))
1188 (when return-list
1189 (push (if (= (logand mode unix:s-ifmt) unix:s-ifdir)
1190 (pathname (concatenate 'string namestring "/"))
1191 file)
1192 result)))))
1193 (nreverse result)))
1194
1195 (defun decode-universal-time-for-files (time current-year)
1196 (multiple-value-bind (sec min hour day month year)
1197 (decode-universal-time (+ time unix-to-universal-time))
1198 (declare (ignore sec))
1199 (format nil "~A ~2,' D ~:[ ~D~;~*~2,'0D:~2,'0D~]"
1200 (svref '#("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug"
1201 "Sep" "Oct" "Nov" "Dec")
1202 (1- month))
1203 day (= current-year year) year hour min)))
1204
1205 (defun print-directory-formatted (pathname all return-list)
1206 (let ((width (or (line-length *standard-output*) 80))
1207 (names ())
1208 (cnt 0)
1209 (max-len 0)
1210 (result (directory pathname :all all :truenamep nil)))
1211 (declare (list names) (fixnum max-len cnt))
1212 ;;
1213 ;; Get the data.
1214 (dolist (file result)
1215 (let* ((name (unix-namestring file))
1216 (length (length name))
1217 (end (if (and (plusp length)
1218 (char= (schar name (1- length)) #\/))
1219 (1- length)
1220 length))
1221 (slash-name (subseq name
1222 (1+ (or (position #\/ name
1223 :from-end t
1224 :end end
1225 :test #'char=)
1226 -1))))
1227 (len (length slash-name)))
1228 (declare (simple-string slash-name)
1229 (fixnum len))
1230 (if (> len max-len) (setq max-len len))
1231 (incf cnt)
1232 (push slash-name names)))
1233 (setq names (nreverse names))
1234 ;;
1235 ;; Do the output.
1236 (let* ((col-width (1+ max-len))
1237 (cols (max (truncate width col-width) 1))
1238 (lines (ceiling cnt cols)))
1239 (declare (fixnum cols lines))
1240 (format t "Directory of ~A:~%" (namestring pathname))
1241 (dotimes (i lines)
1242 (declare (fixnum i))
1243 (dotimes (j cols)
1244 (declare (fixnum j))
1245 (let ((name (nth (+ i (the fixnum (* j lines))) names)))
1246 (when name
1247 (write-string name)
1248 (unless (eql j (1- cols))
1249 (dotimes (i (- col-width (length (the simple-string name))))
1250 (write-char #\space))))))
1251 (terpri)))
1252 (when return-list
1253 result)))
1254
1255
1256 ;;;; File completion.
1257
1258 ;;; COMPLETE-FILE -- Public
1259 ;;;
1260 (defun complete-file (pathname &key (defaults *default-pathname-defaults*)
1261 ignore-types)
1262 (let ((files (directory (complete-file-directory-arg pathname defaults)
1263 :check-for-subdirs nil
1264 :truenamep nil)))
1265 (cond ((null files)
1266 (values nil nil))
1267 ((null (cdr files))
1268 (values (merge-pathnames (file-namestring (car files))
1269 pathname)
1270 t))
1271 (t
1272 (let ((good-files
1273 (delete-if #'(lambda (pathname)
1274 (and (simple-string-p
1275 (pathname-type pathname))
1276 (member (pathname-type pathname)
1277 ignore-types
1278 :test #'string=)))
1279 files)))
1280 (cond ((null good-files))
1281 ((null (cdr good-files))
1282 (return-from complete-file
1283 (values (merge-pathnames (file-namestring
1284 (car good-files))
1285 pathname)
1286 t)))
1287 (t
1288 (setf files good-files)))
1289 (let ((common (file-namestring (car files))))
1290 (dolist (file (cdr files))
1291 (let ((name (file-namestring file)))
1292 (dotimes (i (min (length common) (length name))
1293 (when (< (length name) (length common))
1294 (setf common name)))
1295 (unless (char= (schar common i) (schar name i))
1296 (setf common (subseq common 0 i))
1297 (return)))))
1298 (values (merge-pathnames common pathname)
1299 nil)))))))
1300
1301 ;;; COMPLETE-FILE-DIRECTORY-ARG -- Internal.
1302 ;;;
1303 (defun complete-file-directory-arg (pathname defaults)
1304 (let* ((pathname (merge-pathnames pathname (directory-namestring defaults)))
1305 (type (pathname-type pathname)))
1306 (flet ((append-multi-char-wild (thing)
1307 (etypecase thing
1308 (null :wild)
1309 (pattern
1310 (make-pattern (append (pattern-pieces thing)
1311 (list :multi-char-wild))))
1312 (simple-string
1313 (make-pattern (list thing :multi-char-wild))))))
1314 (if (or (null type) (eq type :unspecific))
1315 ;; There is no type.
1316 (make-pathname :defaults pathname
1317 :name (append-multi-char-wild (pathname-name pathname))
1318 :type :wild)
1319 ;; There already is a type, so just extend it.
1320 (make-pathname :defaults pathname
1321 :name (pathname-name pathname)
1322 :type (append-multi-char-wild (pathname-type pathname)))))))
1323
1324 ;;; Ambiguous-Files -- Public
1325 ;;;
1326 (defun ambiguous-files (pathname
1327 &optional (defaults *default-pathname-defaults*))
1328 "Return a list of all files which are possible completions of Pathname.
1329 We look in the directory specified by Defaults as well as looking down
1330 the search list."
1331 (directory (complete-file-directory-arg pathname defaults)
1332 :truenamep nil
1333 :check-for-subdirs nil))
1334
1335
1336
1337 ;;; File-writable -- exported from extensions.
1338 ;;;
1339 ;;; Determines whether the single argument (which should be a pathname)
1340 ;;; can be written by the current task.
1341 ;;;
1342 (defun file-writable (name)
1343 "File-writable accepts a pathname and returns T if the current
1344 process can write it, and NIL otherwise."
1345 (let ((name (unix-namestring name nil)))
1346 (cond ((null name)
1347 nil)
1348 ((unix:unix-file-kind name)
1349 (values (unix:unix-access name unix:w_ok)))
1350 (t
1351 (values
1352 (unix:unix-access (subseq name
1353 0
1354 (or (position #\/ name :from-end t)
1355 0))
1356 (logior unix:w_ok unix:x_ok)))))))
1357
1358
1359 ;;; Pathname-Order -- Internal
1360 ;;;
1361 ;;; Predicate to order pathnames by. Goes by name.
1362 ;;;
1363 (defun pathname-order (x y)
1364 (let ((xn (%pathname-name x))
1365 (yn (%pathname-name y)))
1366 (if (and xn yn)
1367 (let ((res (string-lessp xn yn)))
1368 (cond ((not res) nil)
1369 ((= res (length (the simple-string xn))) t)
1370 ((= res (length (the simple-string yn))) nil)
1371 (t t)))
1372 xn)))
1373
1374
1375 ;;; Default-Directory -- Public
1376 ;;;
1377 #+ (and)
1378 (defun default-directory ()
1379 "Returns the pathname for the default directory. This is the place where
1380 a file will be written if no directory is specified. This may be changed
1381 with setf."
1382 (multiple-value-bind (gr dir-or-error)
1383 (unix:unix-current-directory)
1384 (if gr
1385 (let ((*ignore-wildcards* t))
1386 (values
1387 (parse-namestring (concatenate 'simple-string dir-or-error "/")
1388 *unix-host*)))
1389 (error dir-or-error))))
1390
1391 ;;;
1392 ;;; XXXX This code was modified by me (fmg) to avoid calling
1393 ;;; concatenate. The reason for this is that there have been
1394 ;;; intermittent instabilities (segv on startup) when the function
1395 ;;; environment-init (in save.lisp) is called. Apparently the type
1396 ;;; system is not completely sorted out at the time this function is
1397 ;;; first called. As a result, strange, not completely reproducable
1398 ;;; things happen, related to something in the state of the
1399 ;;; environment (e.g. the paths or the user environment variables or
1400 ;;; something). These errors occur in the course of calling
1401 ;;; default-directory and the backtrace indicates they occur in the
1402 ;;; context of the type system. Since I haven't been able to figure
1403 ;;; out why they happen, I decided to punt.
1404 ;;;
1405 ;;; Hopefully someone will really fix the problem someday.
1406 ;;;
1407 ;;; Seems like maybe it's fixed by changes made by Ray Toy to avoid heap corruption.
1408 #- (and)
1409 (defun default-directory ()
1410 "Returns the pathname for the default directory. This is the place where
1411 a file will be written if no directory is specified. This may be changed
1412 with setf."
1413 (multiple-value-bind (gr dir-or-error)
1414 (unix:unix-current-directory)
1415 (if gr
1416 (let ((*ignore-wildcards* t)
1417 (string (make-string (1+ (length dir-or-error)) :initial-element #\/)))
1418 (values
1419 (parse-namestring (replace string dir-or-error) *unix-host*)))
1420 (error dir-or-error))))
1421
1422
1423
1424
1425 ;;; %Set-Default-Directory -- Internal
1426 ;;;
1427 (defun %set-default-directory (new-val)
1428 (let ((namestring (unix-namestring new-val t)))
1429 (unless namestring
1430 (error 'simple-file-error
1431 :format-control "~S doesn't exist."
1432 :format-arguments (list new-val)))
1433 (multiple-value-bind (gr error)
1434 (unix:unix-chdir namestring)
1435 (if gr
1436 (setf (search-list "default:") (default-directory))
1437 (error (unix:get-unix-error-msg error))))
1438 new-val))
1439 ;;;
1440 (defsetf default-directory %set-default-directory)
1441
1442 (defun filesys-init ()
1443 ;; Use :unspecific so we don't create file versions whenever merging
1444 ;; happens. If the user wants that, let him change
1445 ;; *default-pathname-defaults* appropriately.
1446 (setf *default-pathname-defaults*
1447 (%make-pathname *unix-host* nil nil nil nil :unspecific))
1448 (setf (search-list "default:") (default-directory))
1449 nil)
1450
1451 ;;; Ensure-Directories-Exist -- Public
1452 ;;;
1453 (defun ensure-directories-exist (pathspec &key verbose (mode #o777))
1454 "Tests whether the directories containing the specified file
1455 actually exist, and attempts to create them if they do not.
1456 Portable programs should avoid using the :MODE keyword argument."
1457 (let* ((pathname (pathname pathspec))
1458 (pathname (if (logical-pathname-p pathname)
1459 (translate-logical-pathname pathname)
1460 pathname))
1461 (created-p nil))
1462 (when (wild-pathname-p pathname)
1463 (error 'simple-file-error
1464 :format-control "Bad place for a wild pathname."
1465 :pathname pathspec))
1466 (enumerate-search-list (pathname pathname)
1467 (let ((dir (pathname-directory pathname)))
1468 (loop for i from 1 upto (length dir)
1469 do (let ((newpath (make-pathname
1470 :host (pathname-host pathname)
1471 :device (pathname-device pathname)
1472 :directory (subseq dir 0 i))))
1473 (tagbody
1474 retry
1475 (restart-case
1476 (unless (probe-file newpath)
1477 (let ((namestring (namestring newpath)))
1478 (when verbose
1479 (format *standard-output* "~&Creating directory: ~A~%"
1480 namestring))
1481 (unix:unix-mkdir namestring mode)
1482 (unless (probe-file namestring)
1483 (error 'simple-file-error
1484 :pathname pathspec
1485 :format-control "Can't create directory ~A."
1486 :format-arguments (list namestring)))
1487 (setf created-p t)))
1488 (retry () :report "Try to create the directory again"
1489 (go retry))))))
1490 ;; Only the first path in a search-list is considered.
1491 (return (values pathname created-p))))))

  ViewVC Help
Powered by ViewVC 1.1.5