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

Contents of /src/code/filesys.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.93 - (show annotations)
Fri Sep 30 15:44:19 2005 UTC (8 years, 6 months ago) by rtoy
Branch: MAIN
Changes since 1.92: +14 -4 lines
parse-unix-namestring:
o Don't allow a namestring to be parsed as a search-list if a logical
  host with the same host name already exists.  Allowing this causes
  confusing in printing such pathnames because, when read, the logical
  host takes precedence.

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

  ViewVC Help
Powered by ViewVC 1.1.5