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

Contents of /src/code/filesys.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5