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

Contents of /src/code/filesys.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.95 - (show annotations)
Fri Oct 21 18:10:11 2005 UTC (8 years, 6 months ago) by rtoy
Branch: MAIN
Changes since 1.94: +1 -2 lines
CMUCL cannot readably print a pathname with just a version component,
so signal an error when we try to generate the the namestring.
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.95 2005/10/21 18:10:11 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 version (not name))
500 ;; We don't want version without a name, because when we try
501 ;; to read #p".~*~" back, the name is "", not NIL.
502 (error "Cannot specify a version without a file: ~S" pathname))
503 (when version-supplied
504 (strings (if (eq version :wild)
505 (if logical-p ".*" ".~*~")
506 (format nil (if logical-p ".~A" ".~~~D~~")
507 version)))))
508 (and (strings) (apply #'concatenate 'simple-string (strings)))))
509
510 (defun unparse-unix-namestring (pathname)
511 (declare (type pathname pathname))
512 (concatenate 'simple-string
513 (unparse-unix-directory pathname)
514 (unparse-unix-file pathname)))
515
516 (defun unparse-unix-enough (pathname defaults)
517 (declare (type pathname pathname defaults))
518 (flet ((lose ()
519 (error "~S cannot be represented relative to ~S"
520 pathname defaults)))
521 ;; Only the first path in a search-list is considered.
522 (enumerate-search-list (pathname pathname)
523 (enumerate-search-list (defaults defaults)
524 (collect ((strings))
525 (let* ((pathname-directory (%pathname-directory pathname))
526 (defaults-directory (%pathname-directory defaults))
527 (prefix-len (length defaults-directory))
528 (result-dir
529 (cond ((null pathname-directory)
530 ;; No directory, so relative to default. But
531 ;; if we're relative to default, NIL is as
532 ;; good as '(:relative) and it results in a
533 ;; shorter namestring.
534 #+nil (list :relative)
535 nil)
536 ((and (>= prefix-len 1)
537 (>= (length pathname-directory) prefix-len)
538 (compare-component (subseq pathname-directory
539 0 prefix-len)
540 defaults-directory))
541 ;; Pathname starts with a prefix of default,
542 ;; which also means both are either :relative
543 ;; or :absolute directories. So just use a
544 ;; relative directory from then on out.
545 (let ((dir-tail (nthcdr prefix-len pathname-directory)))
546 ;; If both directories are identical, don't
547 ;; return just :relative. Returning NIL
548 ;; results in a shorter string.
549 (if dir-tail
550 (cons :relative dir-tail)
551 nil)))
552 ((and (eq (car pathname-directory) :relative)
553 (not (eq (car defaults-directory) :absolute)))
554 ;; Can't represent a relative directory
555 ;; relative to an absolute directory. But
556 ;; there's no problem if both are relative;
557 ;; we just return our path.
558 pathname-directory)
559 ((eq (car pathname-directory) :absolute)
560 ;; We are an absolute pathname, so we can just use it.
561 pathname-directory)
562 (t
563 ;; We are a relative directory. So we lose.
564 (lose)))))
565 (strings (unparse-unix-directory-list result-dir)))
566 (let* ((pathname-version (%pathname-version pathname))
567 (version-needed (and pathname-version
568 (not (eq pathname-version :newest))))
569 (pathname-type (%pathname-type pathname))
570 (type-needed (or version-needed
571 (and pathname-type
572 (not (eq pathname-type :unspecific)))))
573 (pathname-name (%pathname-name pathname))
574 (name-needed (or type-needed
575 (and pathname-name
576 (not (compare-component pathname-name
577 (%pathname-name
578 defaults)))))))
579 (when name-needed
580 (unless pathname-name (lose))
581 (strings (unparse-unix-piece pathname-name)))
582 (when type-needed
583 (when (or (null pathname-type) (eq pathname-type :unspecific))
584 (lose))
585 (strings ".")
586 (strings (unparse-unix-piece pathname-type)))
587 (when version-needed
588 (typecase pathname-version
589 ((member :wild)
590 (strings ".~*~"))
591 (integer
592 (strings (format nil ".~~~D~~" pathname-version)))
593 (t
594 (lose)))))
595 (return-from unparse-unix-enough (apply #'concatenate 'simple-string (strings))))))))
596
597
598 (defstruct (unix-host
599 (:include host
600 (:parse #'parse-unix-namestring)
601 (:unparse #'unparse-unix-namestring)
602 (:unparse-host #'unparse-unix-host)
603 (:unparse-directory #'unparse-unix-directory)
604 (:unparse-file #'unparse-unix-file)
605 (:unparse-enough #'unparse-unix-enough)
606 (:customary-case :lower))
607 (:make-load-form-fun make-unix-host-load-form))
608 )
609
610 (defvar *unix-host* (make-unix-host))
611
612 (defun make-unix-host-load-form (host)
613 (declare (ignore host))
614 '*unix-host*)
615
616
617 ;;;; Wildcard matching stuff.
618
619 (defmacro enumerate-matches ((var pathname &optional result
620 &key (verify-existance t) (follow-links t))
621 &body body)
622 (let ((body-name (gensym)))
623 `(block nil
624 (flet ((,body-name (,var)
625 ,@body))
626 (%enumerate-matches (pathname ,pathname)
627 ,verify-existance ,follow-links
628 #',body-name)
629 ,result))))
630
631 (defun %enumerate-matches (pathname verify-existance follow-links function)
632 (when (pathname-type pathname)
633 (unless (pathname-name pathname)
634 (error "Cannot supply a type without a name:~% ~S" pathname)))
635 (let ((directory (pathname-directory pathname)))
636 (if directory
637 (ecase (car directory)
638 (:absolute
639 (%enumerate-directories "/" (cdr directory) pathname
640 verify-existance follow-links
641 nil function))
642 (:relative
643 (%enumerate-directories "" (cdr directory) pathname
644 verify-existance follow-links
645 nil function)))
646 (%enumerate-files "" pathname verify-existance function))))
647
648 ;;; %enumerate-directories -- Internal
649 ;;;
650 ;;; The directory node and device numbers are maintained for the current path
651 ;;; during the search for the detection of path loops upon :wild-inferiors.
652 ;;;
653 (defun %enumerate-directories (head tail pathname verify-existance
654 follow-links nodes function)
655 (declare (simple-string head))
656 (macrolet ((unix-xstat (name)
657 `(if follow-links
658 (unix:unix-stat ,name)
659 (unix:unix-lstat ,name)))
660 (with-directory-node-noted ((head) &body body)
661 `(multiple-value-bind (res dev ino mode)
662 (unix-xstat ,head)
663 (when (and res (eql (logand mode unix:s-ifmt) unix:s-ifdir))
664 (let ((nodes (cons (cons dev ino) nodes)))
665 ,@body))))
666 (do-directory-entries ((name directory) &body body)
667 `(let ((dir (unix:open-dir ,directory)))
668 (when dir
669 (unwind-protect
670 (loop
671 (let ((,name (unix:read-dir dir)))
672 (cond ((null ,name)
673 (return))
674 ((string= ,name "."))
675 ((string= ,name ".."))
676 (t
677 ,@body))))
678 (unix:close-dir dir))))))
679 (if tail
680 (let ((piece (car tail)))
681 (etypecase piece
682 (simple-string
683 (let ((head (concatenate 'string head piece)))
684 (with-directory-node-noted (head)
685 (%enumerate-directories (concatenate 'string head "/")
686 (cdr tail) pathname
687 verify-existance follow-links
688 nodes function))))
689 ((member :wild-inferiors)
690 (%enumerate-directories head (rest tail) pathname
691 verify-existance follow-links
692 nodes function)
693 (do-directory-entries (name head)
694 (let ((subdir (concatenate 'string head name)))
695 (multiple-value-bind (res dev ino mode)
696 (unix-xstat subdir)
697 (declare (type (or fixnum null) mode))
698 (when (and res (eql (logand mode unix:s-ifmt) unix:s-ifdir))
699 (unless (dolist (dir nodes nil)
700 (when (and (eql (car dir) dev)
701 (eql (cdr dir) ino))
702 (return t)))
703 (let ((nodes (cons (cons dev ino) nodes))
704 (subdir (concatenate 'string subdir "/")))
705 (%enumerate-directories subdir tail pathname
706 verify-existance follow-links
707 nodes function))))))))
708 ((or pattern (member :wild))
709 (do-directory-entries (name head)
710 (when (or (eq piece :wild) (pattern-matches piece name))
711 (let ((subdir (concatenate 'string head name)))
712 (multiple-value-bind (res dev ino mode)
713 (unix-xstat subdir)
714 (declare (type (or fixnum null) mode))
715 (when (and res
716 (eql (logand mode unix:s-ifmt) unix:s-ifdir))
717 (let ((nodes (cons (cons dev ino) nodes))
718 (subdir (concatenate 'string subdir "/")))
719 (%enumerate-directories subdir (rest tail) pathname
720 verify-existance follow-links
721 nodes function))))))))
722 ((member :up)
723 (let ((head (concatenate 'string head "..")))
724 (with-directory-node-noted (head)
725 (%enumerate-directories (concatenate 'string head "/")
726 (rest tail) pathname
727 verify-existance follow-links
728 nodes function))))))
729 (%enumerate-files head pathname verify-existance function))))
730
731 (defun %enumerate-files (directory pathname verify-existance function)
732 (declare (simple-string directory))
733 (let ((name (%pathname-name pathname))
734 (type (%pathname-type pathname))
735 (version (%pathname-version pathname)))
736 (cond ((member name '(nil :unspecific))
737 (when (or (not verify-existance)
738 (unix:unix-file-kind directory))
739 (funcall function directory)))
740 ((or (pattern-p name)
741 (pattern-p type)
742 (eq name :wild)
743 (eq type :wild)
744 (eq version :wild))
745 (let ((dir (unix:open-dir directory)))
746 (when dir
747 (unwind-protect
748 (loop
749 (let ((file (unix:read-dir dir)))
750 (if file
751 (unless (or (string= file ".")
752 (string= file ".."))
753 (multiple-value-bind
754 (file-name file-type file-version)
755 (let ((*ignore-wildcards* t))
756 (extract-name-type-and-version
757 file 0 (length file)))
758 ;; Match also happens if the file has
759 ;; no explicit version and we're asking
760 ;; for version :NEWEST, since that's
761 ;; what no version means.
762 (when (and (components-match file-name name)
763 (components-match file-type type)
764 (or (components-match file-version
765 version)
766 (and (eq file-version nil)
767 (eq version :newest))))
768 (funcall function
769 (concatenate 'string
770 directory
771 file)))))
772 (return))))
773 (unix:close-dir dir)))))
774 (t
775 (let ((file (concatenate 'string directory name)))
776 (unless (or (null type) (eq type :unspecific))
777 (setf file (concatenate 'string file "." type)))
778 (unless (member version '(nil :newest :wild :unspecific))
779 (setf file (concatenate 'string file ".~"
780 (quick-integer-to-string version)
781 "~")))
782 (when (or (not verify-existance)
783 (unix:unix-file-kind file t))
784 (funcall function file)))))))
785
786 (defun quick-integer-to-string (n)
787 (declare (type integer n))
788 (cond ((not (fixnump n))
789 (write-to-string n :base 10 :radix nil))
790 ((zerop n) "0")
791 ((eql n 1) "1")
792 ((minusp n)
793 (concatenate 'simple-string "-"
794 (the simple-string (quick-integer-to-string (- n)))))
795 (t
796 (do* ((len (1+ (truncate (integer-length n) 3)))
797 (res (make-string len))
798 (i (1- len) (1- i))
799 (q n)
800 (r 0))
801 ((zerop q)
802 (incf i)
803 (replace res res :start2 i :end2 len)
804 (shrink-vector res (- len i)))
805 (declare (simple-string res)
806 (fixnum len i r q))
807 (multiple-value-setq (q r) (truncate q 10))
808 (setf (schar res i) (schar "0123456789" r))))))
809
810
811 ;;;; UNIX-NAMESTRING -- public
812 ;;;
813 (defun unix-namestring (pathname &optional (for-input t) executable-only)
814 "Convert PATHNAME into a string that can be used with UNIX system calls.
815 Search-lists and wild-cards are expanded. If optional argument
816 FOR-INPUT is true and PATHNAME doesn't exist, NIL is returned.
817 If optional argument EXECUTABLE-ONLY is true, NIL is returned
818 unless an executable version of PATHNAME exists."
819 ;; toy@rtp.ericsson.se: Let unix-namestring also handle logical
820 ;; pathnames too.
821 (let ((path (let ((lpn (pathname pathname)))
822 (if (logical-pathname-p lpn)
823 (namestring (translate-logical-pathname lpn))
824 pathname))))
825 (enumerate-search-list
826 (pathname path)
827 (collect ((names))
828 (enumerate-matches (name pathname nil :verify-existance for-input
829 :follow-links t)
830 (when (or (not executable-only)
831 (and (eq (unix:unix-file-kind name) :file)
832 (unix:unix-access name unix:x_ok)))
833 (names name)))
834 (let ((names (names)))
835 (when names
836 (when (cdr names)
837 (error 'simple-file-error
838 :format-control "~S is ambiguous:~{~% ~A~}"
839 :format-arguments (list pathname names)))
840 (return (car names))))))))
841
842
843 ;;;; TRUENAME and PROBE-FILE.
844
845 ;;; Truename -- Public
846 ;;;
847 ;;; Another silly file function trivially different from another function.
848 ;;;
849 (defun truename (pathname)
850 "Return the pathname for the actual file described by the pathname
851 An error of type file-error is signalled if no such file exists,
852 or the pathname is wild."
853 (if (wild-pathname-p pathname)
854 (error 'simple-file-error
855 :format-control "Bad place for a wild pathname."
856 :pathname pathname)
857 (let ((result (probe-file pathname)))
858 (unless result
859 (error 'simple-file-error
860 :pathname pathname
861 :format-control "The file ~S does not exist."
862 :format-arguments (list (namestring pathname))))
863 result)))
864
865 ;;; Probe-File -- Public
866 ;;;
867 ;;; If PATHNAME exists, return its truename, otherwise NIL.
868 ;;;
869 (defun probe-file (pathname)
870 "Return a pathname which is the truename of the file if it exists, NIL
871 otherwise. An error of type file-error is signalled if pathname is wild."
872 (if (wild-pathname-p pathname)
873 (error 'simple-file-error
874 :pathname pathname
875 :format-control "Bad place for a wild pathname.")
876 (let ((namestring (unix-namestring (merge-pathnames pathname) t)))
877 (when (and namestring (unix:unix-file-kind namestring))
878 (let ((truename (unix:unix-resolve-links
879 (unix:unix-maybe-prepend-current-directory
880 namestring))))
881 (when truename
882 (let ((*ignore-wildcards* t))
883 (pathname (unix:unix-simplify-pathname truename)))))))))
884
885
886 ;;;; Other random operations.
887
888 ;;; Rename-File -- Public
889 ;;;
890 (defun rename-file (file new-name)
891 "Rename File to have the specified New-Name. If file is a stream open to a
892 file, then the associated file is renamed."
893 (let* ((original (truename file))
894 (original-namestring (unix-namestring original t))
895 (new-name (merge-pathnames new-name original))
896 (new-namestring (unix-namestring new-name nil)))
897 (unless new-namestring
898 (error 'simple-file-error
899 :pathname new-name
900 :format-control "~S can't be created."
901 :format-arguments (list new-name)))
902 (multiple-value-bind (res error)
903 (unix:unix-rename original-namestring
904 new-namestring)
905 (unless res
906 (error 'simple-file-error
907 :pathname new-name
908 :format-control "Failed to rename ~A to ~A: ~A"
909 :format-arguments (list original new-name
910 (unix:get-unix-error-msg error))))
911 (when (streamp file)
912 (file-name file new-namestring))
913 (values new-name original (truename new-name)))))
914
915 ;;; Delete-File -- Public
916 ;;;
917 ;;; Delete the file, Man.
918 ;;;
919 (defun delete-file (file)
920 "Delete the specified file."
921 (let ((namestring (unix-namestring file t)))
922 (when (streamp file)
923 ;; Close the file, but don't try to revert or anything. We want
924 ;; to delete it, man!
925 (close file))
926 (unless namestring
927 (error 'simple-file-error
928 :pathname file
929 :format-control "~S doesn't exist."
930 :format-arguments (list file)))
931
932 (multiple-value-bind (res err) (unix:unix-unlink namestring)
933 (unless res
934 (error 'simple-file-error
935 :pathname namestring
936 :format-control "Could not delete ~A: ~A."
937 :format-arguments (list namestring
938 (unix:get-unix-error-msg err))))))
939 t)
940
941 ;;; Purge-Backup-Files -- Public
942 ;;;
943 ;;; Purge old file versions
944 ;;;
945 (defun purge-backup-files (pathname &optional (keep 0))
946 "Delete old versions of files matching the given Pathname,
947 optionally keeping some of the most recent old versions."
948 (declare (type (or pathname string stream) pathname)
949 (type (integer 0 *) keep))
950 (let ((hash (make-hash-table :test 'equal)))
951 (enumerate-search-list
952 (path (make-pathname :version :wild :defaults pathname))
953 (clrhash hash)
954 (enumerate-matches (name path nil :follow-links nil)
955 (let ((dot (position #\. name :from-end t))
956 (len (length name)))
957 (when (and dot
958 (> len (+ dot 3))
959 (char= (char name (1+ dot)) #\~)
960 (char= (char name (1- len)) #\~)
961 (eq (unix:unix-file-kind name) :file))
962 (multiple-value-bind (version next)
963 (parse-integer name :start (+ dot 2) :end (1- len)
964 :junk-allowed t)
965 (when (and version (= next (1- len)))
966 (push (cons version name)
967 (gethash (subseq name 0 dot) hash '())))))))
968 (maphash (lambda (key value)
969 (declare (ignore key))
970 (mapc #'unix:unix-unlink
971 (mapcar #'cdr (nthcdr keep
972 (sort value #'> :key #'car)))))
973 hash))))
974
975
976 ;;; User-Homedir-Pathname -- Public
977 ;;;
978 ;;; Return Home:, which is set up for us at initialization time.
979 ;;;
980 (defun user-homedir-pathname (&optional host)
981 "Returns the home directory of the logged in user as a pathname.
982 This is obtained from the logical name \"home:\"."
983 (declare (ignore host))
984 #p"home:")
985
986 ;;; File-Write-Date -- Public
987 ;;;
988 (defun file-write-date (file)
989 "Return file's creation date, or NIL if it doesn't exist.
990 An error of type file-error is signalled if file is a wild pathname"
991 (if (wild-pathname-p file)
992 (error 'simple-file-error
993 :pathname file
994 :format-control "Bad place for a wild pathname.")
995 (let ((name (unix-namestring file t)))
996 (when name
997 (multiple-value-bind
998 (res dev ino mode nlink uid gid rdev size atime mtime)
999 (unix:unix-stat name)
1000 (declare (ignore dev ino mode nlink uid gid rdev size atime))
1001 (when res
1002 (+ unix-to-universal-time mtime)))))))
1003
1004 ;;; File-Author -- Public
1005 ;;;
1006 (defun file-author (file)
1007 "Returns the file author as a string, or nil if the author cannot be
1008 determined. Signals an error of type file-error if file doesn't exist,
1009 or file is a wild pathname."
1010 (if (wild-pathname-p file)
1011 (error 'simple-file-error
1012 :pathname file
1013 :format-control "Bad place for a wild pathname.")
1014 (let ((name (unix-namestring (pathname file) t)))
1015 (unless name
1016 (error 'simple-file-error
1017 :pathname file
1018 :format-control "~S doesn't exist."
1019 :format-arguments (list file)))
1020 (multiple-value-bind (winp dev ino mode nlink uid)
1021 (unix:unix-stat name)
1022 (declare (ignore dev ino mode nlink))
1023 (when winp
1024 (let ((user-info (unix:unix-getpwuid uid)))
1025 (when user-info
1026 (unix:user-info-name user-info))))))))
1027
1028
1029 ;;;; DIRECTORY.
1030
1031 ;;; DIRECTORY -- public.
1032 ;;;
1033 (defun directory (pathname &key (all t) (check-for-subdirs t)
1034 (truenamep t) (follow-links t))
1035 "Returns a list of pathnames, one for each file that matches the given
1036 pathname. Supplying :ALL as nil causes this to ignore Unix dot files. This
1037 never includes Unix dot and dot-dot in the result. If :TRUENAMEP is NIL,
1038 then symbolic links in the result are not expanded, which is not the
1039 default because TRUENAME does follow links and the result pathnames are
1040 defined to be the TRUENAME of the pathname (the truename of a link may well
1041 be in another directory). If FOLLOW-LINKS is NIL then symbolic links are
1042 not followed."
1043 (let ((results nil))
1044 (enumerate-search-list
1045 (pathname (merge-pathnames pathname
1046 (make-pathname :name :wild
1047 :type :wild
1048 :version :wild
1049 :defaults *default-pathname-defaults*)
1050 :wild))
1051 (enumerate-matches (name pathname nil :follow-links follow-links)
1052 (when (or all
1053 (let ((slash (position #\/ name :from-end t)))
1054 (or (null slash)
1055 (= (1+ slash) (length name))
1056 (char/= (schar name (1+ slash)) #\.))))
1057 (push name results))))
1058 (let ((*ignore-wildcards* t))
1059 (mapcar #'(lambda (name)
1060 (let ((name (if (and check-for-subdirs
1061 (eq (unix:unix-file-kind name)
1062 :directory))
1063 (concatenate 'string name "/")
1064 name)))
1065 (if truenamep (truename name) (pathname name))))
1066 (sort (delete-duplicates results :test #'string=) #'string<)))))
1067
1068
1069 ;;;; Printing directories.
1070
1071 ;;; PRINT-DIRECTORY is exported from the EXTENSIONS package.
1072 ;;;
1073 (defun print-directory (pathname &optional stream &key all verbose return-list)
1074 "Like Directory, but prints a terse, multi-column directory listing
1075 instead of returning a list of pathnames. When :all is supplied and
1076 non-nil, then Unix dot files are included too (as ls -a). When :verbose
1077 is supplied and non-nil, then a long listing of miscellaneous
1078 information is output one file per line."
1079 (let ((*standard-output* (out-synonym-of stream))
1080 (pathname pathname))
1081 (if verbose
1082 (print-directory-verbose pathname all return-list)
1083 (print-directory-formatted pathname all return-list))))
1084
1085 (defun print-directory-verbose (pathname all return-list)
1086 (let ((contents (directory pathname :all all :check-for-subdirs nil
1087 :truenamep nil))
1088 (result nil))
1089 (format t "Directory of ~A:~%" (namestring pathname))
1090 (dolist (file contents)
1091 (let* ((namestring (unix-namestring file))
1092 (tail (subseq namestring
1093 (1+ (or (position #\/ namestring
1094 :from-end t
1095 :test #'char=)
1096 -1)))))
1097 (multiple-value-bind
1098 (reslt dev-or-err ino mode nlink uid gid rdev size atime mtime)
1099 (unix:unix-stat namestring)
1100 (declare (ignore ino gid rdev atime)
1101 (fixnum uid mode))
1102 (cond (reslt
1103 ;;
1104 ;; Print characters for file modes.
1105 (macrolet ((frob (bit name &optional sbit sname negate)
1106 `(if ,(if negate
1107 `(not (logbitp ,bit mode))
1108 `(logbitp ,bit mode))
1109 ,(if sbit
1110 `(if (logbitp ,sbit mode)
1111 (write-char ,sname)
1112 (write-char ,name))
1113 `(write-char ,name))
1114 (write-char #\-))))
1115 (frob 15 #\d nil nil t)
1116 (frob 8 #\r)
1117 (frob 7 #\w)
1118 (frob 6 #\x 11 #\s)
1119 (frob 5 #\r)
1120 (frob 4 #\w)
1121 (frob 3 #\x 10 #\s)
1122 (frob 2 #\r)
1123 (frob 1 #\w)
1124 (frob 0 #\x))
1125 ;;
1126 ;; Print the rest.
1127 (multiple-value-bind (sec min hour date month year)
1128 (get-decoded-time)
1129 (declare (ignore sec min hour date month))
1130 (format t "~2D ~8A ~8D ~12A ~A~@[/~]~%"
1131 nlink
1132 (let ((user-info (unix:unix-getpwuid uid)))
1133 (if user-info (unix:user-info-name user-info) uid))
1134 size
1135 (decode-universal-time-for-files mtime year)
1136 tail
1137 (= (logand mode unix:s-ifmt) unix:s-ifdir))))
1138 (t (format t "Couldn't stat ~A -- ~A.~%"
1139 tail
1140 (unix:get-unix-error-msg dev-or-err))))
1141 (when return-list
1142 (push (if (= (logand mode unix:s-ifmt) unix:s-ifdir)
1143 (pathname (concatenate 'string namestring "/"))
1144 file)
1145 result)))))
1146 (nreverse result)))
1147
1148 (defun decode-universal-time-for-files (time current-year)
1149 (multiple-value-bind (sec min hour day month year)
1150 (decode-universal-time (+ time unix-to-universal-time))
1151 (declare (ignore sec))
1152 (format nil "~A ~2,' D ~:[ ~D~;~*~2,'0D:~2,'0D~]"
1153 (svref '#("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug"
1154 "Sep" "Oct" "Nov" "Dec")
1155 (1- month))
1156 day (= current-year year) year hour min)))
1157
1158 (defun print-directory-formatted (pathname all return-list)
1159 (let ((width (or (line-length *standard-output*) 80))
1160 (names ())
1161 (cnt 0)
1162 (max-len 0)
1163 (result (directory pathname :all all :truenamep nil)))
1164 (declare (list names) (fixnum max-len cnt))
1165 ;;
1166 ;; Get the data.
1167 (dolist (file result)
1168 (let* ((name (unix-namestring file))
1169 (length (length name))
1170 (end (if (and (plusp length)
1171 (char= (schar name (1- length)) #\/))
1172 (1- length)
1173 length))
1174 (slash-name (subseq name
1175 (1+ (or (position #\/ name
1176 :from-end t
1177 :end end
1178 :test #'char=)
1179 -1))))
1180 (len (length slash-name)))
1181 (declare (simple-string slash-name)
1182 (fixnum len))
1183 (if (> len max-len) (setq max-len len))
1184 (incf cnt)
1185 (push slash-name names)))
1186 (setq names (nreverse names))
1187 ;;
1188 ;; Do the output.
1189 (let* ((col-width (1+ max-len))
1190 (cols (max (truncate width col-width) 1))
1191 (lines (ceiling cnt cols)))
1192 (declare (fixnum cols lines))
1193 (format t "Directory of ~A:~%" (namestring pathname))
1194 (dotimes (i lines)
1195 (declare (fixnum i))
1196 (dotimes (j cols)
1197 (declare (fixnum j))
1198 (let ((name (nth (+ i (the fixnum (* j lines))) names)))
1199 (when name
1200 (write-string name)
1201 (unless (eql j (1- cols))
1202 (dotimes (i (- col-width (length (the simple-string name))))
1203 (write-char #\space))))))
1204 (terpri)))
1205 (when return-list
1206 result)))
1207
1208
1209 ;;;; File completion.
1210
1211 ;;; COMPLETE-FILE -- Public
1212 ;;;
1213 (defun complete-file (pathname &key (defaults *default-pathname-defaults*)
1214 ignore-types)
1215 (let ((files (directory (complete-file-directory-arg pathname defaults)
1216 :check-for-subdirs nil
1217 :truenamep nil)))
1218 (cond ((null files)
1219 (values nil nil))
1220 ((null (cdr files))
1221 (values (merge-pathnames (file-namestring (car files))
1222 pathname)
1223 t))
1224 (t
1225 (let ((good-files
1226 (delete-if #'(lambda (pathname)
1227 (and (simple-string-p
1228 (pathname-type pathname))
1229 (member (pathname-type pathname)
1230 ignore-types
1231 :test #'string=)))
1232 files)))
1233 (cond ((null good-files))
1234 ((null (cdr good-files))
1235 (return-from complete-file
1236 (values (merge-pathnames (file-namestring
1237 (car good-files))
1238 pathname)
1239 t)))
1240 (t
1241 (setf files good-files)))
1242 (let ((common (file-namestring (car files))))
1243 (dolist (file (cdr files))
1244 (let ((name (file-namestring file)))
1245 (dotimes (i (min (length common) (length name))
1246 (when (< (length name) (length common))
1247 (setf common name)))
1248 (unless (char= (schar common i) (schar name i))
1249 (setf common (subseq common 0 i))
1250 (return)))))
1251 (values (merge-pathnames common pathname)
1252 nil)))))))
1253
1254 ;;; COMPLETE-FILE-DIRECTORY-ARG -- Internal.
1255 ;;;
1256 (defun complete-file-directory-arg (pathname defaults)
1257 (let* ((pathname (merge-pathnames pathname (directory-namestring defaults)))
1258 (type (pathname-type pathname)))
1259 (flet ((append-multi-char-wild (thing)
1260 (etypecase thing
1261 (null :wild)
1262 (pattern
1263 (make-pattern (append (pattern-pieces thing)
1264 (list :multi-char-wild))))
1265 (simple-string
1266 (make-pattern (list thing :multi-char-wild))))))
1267 (if (or (null type) (eq type :unspecific))
1268 ;; There is no type.
1269 (make-pathname :defaults pathname
1270 :name (append-multi-char-wild (pathname-name pathname))
1271 :type :wild)
1272 ;; There already is a type, so just extend it.
1273 (make-pathname :defaults pathname
1274 :name (pathname-name pathname)
1275 :type (append-multi-char-wild (pathname-type pathname)))))))
1276
1277 ;;; Ambiguous-Files -- Public
1278 ;;;
1279 (defun ambiguous-files (pathname
1280 &optional (defaults *default-pathname-defaults*))
1281 "Return a list of all files which are possible completions of Pathname.
1282 We look in the directory specified by Defaults as well as looking down
1283 the search list."
1284 (directory (complete-file-directory-arg pathname defaults)
1285 :truenamep nil
1286 :check-for-subdirs nil))
1287
1288
1289
1290 ;;; File-writable -- exported from extensions.
1291 ;;;
1292 ;;; Determines whether the single argument (which should be a pathname)
1293 ;;; can be written by the current task.
1294 ;;;
1295 (defun file-writable (name)
1296 "File-writable accepts a pathname and returns T if the current
1297 process can write it, and NIL otherwise."
1298 (let ((name (unix-namestring name nil)))
1299 (cond ((null name)
1300 nil)
1301 ((unix:unix-file-kind name)
1302 (values (unix:unix-access name unix:w_ok)))
1303 (t
1304 (values
1305 (unix:unix-access (subseq name
1306 0
1307 (or (position #\/ name :from-end t)
1308 0))
1309 (logior unix:w_ok unix:x_ok)))))))
1310
1311
1312 ;;; Pathname-Order -- Internal
1313 ;;;
1314 ;;; Predicate to order pathnames by. Goes by name.
1315 ;;;
1316 (defun pathname-order (x y)
1317 (let ((xn (%pathname-name x))
1318 (yn (%pathname-name y)))
1319 (if (and xn yn)
1320 (let ((res (string-lessp xn yn)))
1321 (cond ((not res) nil)
1322 ((= res (length (the simple-string xn))) t)
1323 ((= res (length (the simple-string yn))) nil)
1324 (t t)))
1325 xn)))
1326
1327
1328 ;;; Default-Directory -- Public
1329 ;;;
1330 (defun default-directory ()
1331 "Returns the pathname for the default directory. This is the place where
1332 a file will be written if no directory is specified. This may be changed
1333 with setf."
1334 (multiple-value-bind (gr dir-or-error)
1335 (unix:unix-current-directory)
1336 (if gr
1337 (let ((*ignore-wildcards* t))
1338 (values
1339 (parse-namestring (concatenate 'simple-string dir-or-error "/")
1340 *unix-host*)))
1341 (error dir-or-error))))
1342
1343 ;;; %Set-Default-Directory -- Internal
1344 ;;;
1345 (defun %set-default-directory (new-val)
1346 (let ((namestring (unix-namestring new-val t)))
1347 (unless namestring
1348 (error 'simple-file-error
1349 :format-control "~S doesn't exist."
1350 :format-arguments (list new-val)))
1351 (multiple-value-bind (gr error)
1352 (unix:unix-chdir namestring)
1353 (if gr
1354 (setf (search-list "default:") (default-directory))
1355 (error (unix:get-unix-error-msg error))))
1356 new-val))
1357 ;;;
1358 (defsetf default-directory %set-default-directory)
1359
1360 (defun filesys-init ()
1361 ;; Use :unspecific so we don't create file versions whenever merging
1362 ;; happens. If the user wants that, let him change
1363 ;; *default-pathname-defaults* appropriately.
1364 (setf *default-pathname-defaults*
1365 (%make-pathname *unix-host* nil nil nil nil :unspecific))
1366 (setf (search-list "default:") (default-directory))
1367 nil)
1368
1369 ;;; Ensure-Directories-Exist -- Public
1370 ;;;
1371 (defun ensure-directories-exist (pathspec &key verbose (mode #o777))
1372 "Tests whether the directories containing the specified file
1373 actually exist, and attempts to create them if they do not.
1374 Portable programs should avoid using the :MODE keyword argument."
1375 (let* ((pathname (pathname pathspec))
1376 (pathname (if (logical-pathname-p pathname)
1377 (translate-logical-pathname pathname)
1378 pathname))
1379 (created-p nil))
1380 (when (wild-pathname-p pathname)
1381 (error 'simple-file-error
1382 :format-control "Bad place for a wild pathname."
1383 :pathname pathspec))
1384 (enumerate-search-list (pathname pathname)
1385 (let ((dir (pathname-directory pathname)))
1386 (loop for i from 1 upto (length dir)
1387 do (let ((newpath (make-pathname
1388 :host (pathname-host pathname)
1389 :device (pathname-device pathname)
1390 :directory (subseq dir 0 i))))
1391 (tagbody
1392 retry
1393 (restart-case
1394 (unless (probe-file newpath)
1395 (let ((namestring (namestring newpath)))
1396 (when verbose
1397 (format *standard-output* "~&Creating directory: ~A~%"
1398 namestring))
1399 (unix:unix-mkdir namestring mode)
1400 (unless (probe-file namestring)
1401 (error 'simple-file-error
1402 :pathname pathspec
1403 :format-control "Can't create directory ~A."
1404 :format-arguments (list namestring)))
1405 (setf created-p t)))
1406 (retry () :report "Try to create the directory again"
1407 (go retry))))))
1408 ;; Only the first path in a search-list is considered.
1409 (return (values pathname created-p))))))

  ViewVC Help
Powered by ViewVC 1.1.5