1 ;;;; file system interface functions -- fairly Unix-specific
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!IMPL")
14 ;;;; Unix pathname host support
16 ;;; Unix namestrings have the following format:
18 ;;; namestring := [ directory ] [ file [ type [ version ]]]
19 ;;; directory := [ "/" ] { file "/" }*
21 ;;; type := "." [^/.]*
22 ;;; version := "." ([0-9]+ | "*")
24 ;;; Note: this grammar is ambiguous. The string foo.bar.5 can be
25 ;;; parsed as either just the file specified or as specifying the
26 ;;; file, type, and version. Therefore, we use the following rules
27 ;;; when confronted with an ambiguous file.type.version string:
29 ;;; - If the first character is a dot, it's part of the file. It is not
30 ;;; considered a dot in the following rules.
32 ;;; - If there is only one dot, it separates the file and the type.
34 ;;; - If there are multiple dots and the stuff following the last dot
35 ;;; is a valid version, then that is the version and the stuff between
36 ;;; the second to last dot and the last dot is the type.
38 ;;; Wildcard characters:
40 ;;; If the directory, file, type components contain any of the
41 ;;; following characters, it is considered part of a wildcard pattern
42 ;;; and has the following meaning.
44 ;;; ? - matches any character
45 ;;; * - matches any zero or more characters.
46 ;;; [abc] - matches any of a, b, or c.
47 ;;; {str1,str2,...,strn} - matches any of str1, str2, ..., or strn.
49 ;;; Any of these special characters can be preceded by a backslash to
50 ;;; cause it to be treated as a regular character.
51 (defun remove-backslashes (namestr start end)
53 "Remove any occurrences of #\\ from the string because we've already
54 checked for whatever they may have protected."
55 (declare (type simple-base-string namestr)
56 (type index start end))
57 (let* ((result (make-string (- end start) :element-type 'base-char))
60 (do ((src start (1+ src)))
63 (setf (schar result dst) (schar namestr src))
67 (let ((char (schar namestr src)))
68 (cond ((char= char #\\)
71 (setf (schar result dst) char)
74 (error 'namestring-parse-error
75 :complaint "backslash in a bad place"
78 (shrink-vector result dst)))
80 (defvar *ignore-wildcards* nil)
82 (/show0 "filesys.lisp 86")
84 (defun maybe-make-pattern (namestr start end)
85 (declare (type simple-base-string namestr)
86 (type index start end))
87 (if *ignore-wildcards*
88 (subseq namestr start end)
92 (last-regular-char nil)
94 (flet ((flush-pending-regulars ()
95 (when last-regular-char
96 (pattern (if any-quotes
97 (remove-backslashes namestr
100 (subseq namestr last-regular-char index)))
101 (setf any-quotes nil)
102 (setf last-regular-char nil))))
106 (let ((char (schar namestr index)))
113 (unless last-regular-char
114 (setf last-regular-char index))
117 (flush-pending-regulars)
118 (pattern :single-char-wild)
121 (flush-pending-regulars)
122 (pattern :multi-char-wild)
125 (flush-pending-regulars)
127 (position #\] namestr :start index :end end)))
128 (unless close-bracket
129 (error 'namestring-parse-error
130 :complaint "#\\[ with no corresponding #\\]"
133 (pattern (list :character-set
137 (setf index (1+ close-bracket))))
139 (unless last-regular-char
140 (setf last-regular-char index))
142 (flush-pending-regulars)))
143 (cond ((null (pattern))
145 ((null (cdr (pattern)))
146 (let ((piece (first (pattern))))
148 ((member :multi-char-wild) :wild)
149 (simple-string piece)
151 (make-pattern (pattern))))))
153 (make-pattern (pattern)))))))
155 (/show0 "filesys.lisp 160")
157 (defun extract-name-type-and-version (namestr start end)
158 (declare (type simple-base-string namestr)
159 (type index start end))
160 (let* ((last-dot (position #\. namestr :start (1+ start) :end end
164 (values (maybe-make-pattern namestr start last-dot)
165 (maybe-make-pattern namestr (1+ last-dot) end)
168 (values (maybe-make-pattern namestr start end)
172 (/show0 "filesys.lisp 200")
174 ;;; Take a string and return a list of cons cells that mark the char
175 ;;; separated subseq. The first value is true if absolute directories
177 (defun split-at-slashes (namestr start end)
178 (declare (type simple-base-string namestr)
179 (type index start end))
180 (let ((absolute (and (/= start end)
181 (char= (schar namestr start) #\/))))
184 ;; Next, split the remainder into slash-separated chunks.
187 (let ((slash (position #\/ namestr :start start :end end)))
188 (pieces (cons start (or slash end)))
191 (setf start (1+ slash))))
192 (values absolute (pieces)))))
194 (defun parse-unix-namestring (namestr start end)
195 (declare (type simple-base-string namestr)
196 (type index start end))
197 (multiple-value-bind (absolute pieces) (split-at-slashes namestr start end)
198 (multiple-value-bind (name type version)
199 (let* ((tail (car (last pieces)))
200 (tail-start (car tail))
201 (tail-end (cdr tail)))
202 (unless (= tail-start tail-end)
203 (setf pieces (butlast pieces))
204 (extract-name-type-and-version namestr tail-start tail-end)))
207 (let ((position (position-if (lambda (char)
208 (or (char= char (code-char 0))
212 (error 'namestring-parse-error
213 :complaint "can't embed #\\Nul or #\\/ in Unix namestring"
216 ;; Now we have everything we want. So return it.
217 (values nil ; no host for Unix namestrings
218 nil ; no device for Unix namestrings
220 (dolist (piece pieces)
221 (let ((piece-start (car piece))
222 (piece-end (cdr piece)))
223 (unless (= piece-start piece-end)
224 (cond ((string= namestr ".."
228 ((string= namestr "**"
231 (dirs :wild-inferiors))
233 (dirs (maybe-make-pattern namestr
237 (cons :absolute (dirs)))
239 (cons :relative (dirs)))
246 (/show0 "filesys.lisp 300")
248 (defun unparse-unix-host (pathname)
249 (declare (type pathname pathname)
251 ;; this host designator needs to be recognized as a physical host in
252 ;; PARSE-NAMESTRING. Until sbcl-0.7.3.x, we had "Unix" here, but
253 ;; that's a valid Logical Hostname, so that's a bad choice. -- CSR,
257 (defun unparse-unix-piece (thing)
261 (let* ((srclen (length thing))
264 (case (schar thing i)
267 (let ((result (make-string dstlen))
269 (dotimes (src srclen)
270 (let ((char (schar thing src)))
273 (setf (schar result dst) #\\)
275 (setf (schar result dst) char)
280 (dolist (piece (pattern-pieces thing))
294 (strings (cdr piece))
297 (error "invalid pattern piece: ~S" piece))))))
302 (defun unparse-unix-directory-list (directory)
303 (declare (type list directory))
306 (ecase (pop directory)
312 (dolist (dir directory)
317 (error ":BACK cannot be represented in namestrings."))
318 ((member :wild-inferiors)
320 ((or simple-string pattern)
321 (pieces (unparse-unix-piece dir))
324 (error "invalid directory component: ~S" dir)))))
325 (apply #'concatenate 'simple-string (pieces))))
327 (defun unparse-unix-directory (pathname)
328 (declare (type pathname pathname))
329 (unparse-unix-directory-list (%pathname-directory pathname)))
331 (defun unparse-unix-file (pathname)
332 (declare (type pathname pathname))
334 (let* ((name (%pathname-name pathname))
335 (type (%pathname-type pathname))
336 (type-supplied (not (or (null type) (eq type :unspecific)))))
337 ;; Note: by ANSI 19.3.1.1.5, we ignore the version slot when
338 ;; translating logical pathnames to a filesystem without
339 ;; versions (like Unix).
341 (when (and (null type) (position #\. name :start 1))
342 (error "too many dots in the name: ~S" pathname))
343 (strings (unparse-unix-piece name)))
346 (error "cannot specify the type without a file: ~S" pathname))
347 (when (typep type 'simple-base-string)
348 (when (position #\. type)
349 (error "type component can't have a #\. inside: ~S" pathname)))
351 (strings (unparse-unix-piece type))))
352 (apply #'concatenate 'simple-string (strings))))
354 (/show0 "filesys.lisp 406")
356 (defun unparse-unix-namestring (pathname)
357 (declare (type pathname pathname))
358 (concatenate 'simple-string
359 (unparse-unix-directory pathname)
360 (unparse-unix-file pathname)))
362 (defun unparse-unix-enough (pathname defaults)
363 (declare (type pathname pathname defaults))
365 (error "~S cannot be represented relative to ~S."
368 (let* ((pathname-directory (%pathname-directory pathname))
369 (defaults-directory (%pathname-directory defaults))
370 (prefix-len (length defaults-directory))
372 (cond ((and (> prefix-len 1)
373 (>= (length pathname-directory) prefix-len)
374 (compare-component (subseq pathname-directory
377 ;; Pathname starts with a prefix of default. So
378 ;; just use a relative directory from then on out.
379 (cons :relative (nthcdr prefix-len pathname-directory)))
380 ((eq (car pathname-directory) :absolute)
381 ;; We are an absolute pathname, so we can just use it.
384 ;; We are a relative directory. So we lose.
386 (strings (unparse-unix-directory-list result-directory)))
387 (let* ((pathname-type (%pathname-type pathname))
388 (type-needed (and pathname-type
389 (not (eq pathname-type :unspecific))))
390 (pathname-name (%pathname-name pathname))
391 (name-needed (or type-needed
393 (not (compare-component pathname-name
397 (unless pathname-name (lose))
398 (when (and (null pathname-type)
399 (position #\. pathname-name :start 1))
400 (error "too many dots in the name: ~S" pathname))
401 (strings (unparse-unix-piece pathname-name)))
403 (when (or (null pathname-type) (eq pathname-type :unspecific))
405 (when (typep pathname-type 'simple-base-string)
406 (when (position #\. pathname-type)
407 (error "type component can't have a #\. inside: ~S" pathname)))
409 (strings (unparse-unix-piece pathname-type))))
410 (apply #'concatenate 'simple-string (strings)))))
412 ;;;; wildcard matching stuff
414 ;;; Return a list of all the Lispy filenames (not including e.g. the
415 ;;; Unix magic "." and "..") in the directory named by DIRECTORY-NAME.
416 (defun directory-lispy-filenames (directory-name)
417 (with-alien ((adlf (* c-string)
418 (alien-funcall (extern-alien
419 "alloc_directory_lispy_filenames"
420 (function (* c-string) c-string))
422 (if (null-alien adlf)
423 (error 'simple-file-error
424 :pathname directory-name
425 :format-control "~@<couldn't read directory ~S: ~2I~_~A~:>"
426 :format-arguments (list directory-name (strerror)))
428 (c-strings->string-list adlf)
429 (alien-funcall (extern-alien "free_directory_lispy_filenames"
430 (function void (* c-string)))
433 (/show0 "filesys.lisp 498")
435 (defmacro !enumerate-matches ((var pathname &optional result
436 &key (verify-existence t)
440 (%enumerate-matches (pathname ,pathname)
443 (lambda (,var) ,@body))
446 (/show0 "filesys.lisp 500")
448 ;;; Call FUNCTION on matches.
449 (defun %enumerate-matches (pathname verify-existence follow-links function)
450 (/noshow0 "entering %ENUMERATE-MATCHES")
451 (when (pathname-type pathname)
452 (unless (pathname-name pathname)
453 (error "cannot supply a type without a name:~% ~S" pathname)))
454 (when (and (integerp (pathname-version pathname))
455 (member (pathname-type pathname) '(nil :unspecific)))
456 (error "cannot supply a version without a type:~% ~S" pathname))
457 (let ((directory (pathname-directory pathname)))
458 (/noshow0 "computed DIRECTORY")
460 (ecase (first directory)
462 (/noshow0 "absolute directory")
463 (%enumerate-directories "/" (rest directory) pathname
464 verify-existence follow-links
467 (/noshow0 "relative directory")
468 (%enumerate-directories "" (rest directory) pathname
469 verify-existence follow-links
471 (%enumerate-files "" pathname verify-existence function))))
473 ;;; Call FUNCTION on directories.
474 (defun %enumerate-directories (head tail pathname verify-existence
475 follow-links nodes function)
476 (declare (simple-string head))
477 (macrolet ((unix-xstat (name)
479 (sb!unix:unix-stat ,name)
480 (sb!unix:unix-lstat ,name)))
481 (with-directory-node-noted ((head) &body body)
482 `(multiple-value-bind (res dev ino mode)
484 (when (and res (eql (logand mode sb!unix:s-ifmt)
486 (let ((nodes (cons (cons dev ino) nodes)))
488 (with-directory-node-removed ((head) &body body)
489 `(multiple-value-bind (res dev ino mode)
491 (when (and res (eql (logand mode sb!unix:s-ifmt)
493 (let ((nodes (remove (cons dev ino) nodes :test #'equal)))
496 (let ((piece (car tail)))
499 (let ((head (concatenate 'base-string head piece)))
500 (with-directory-node-noted (head)
501 (%enumerate-directories (concatenate 'base-string head "/")
503 verify-existence follow-links
505 ((member :wild-inferiors)
506 ;; now with extra error case handling from CLHS
507 ;; 19.2.2.4.3 -- CSR, 2004-01-24
508 (when (member (cadr tail) '(:up :back))
509 (error 'simple-file-error
511 :format-control "~@<invalid use of ~S after :WILD-INFERIORS~@:>."
512 :format-arguments (list (cadr tail))))
513 (%enumerate-directories head (rest tail) pathname
514 verify-existence follow-links
516 (dolist (name (ignore-errors (directory-lispy-filenames head)))
517 (let ((subdir (concatenate 'base-string head name)))
518 (multiple-value-bind (res dev ino mode)
520 (declare (type (or fixnum null) mode))
521 (when (and res (eql (logand mode sb!unix:s-ifmt)
523 (unless (dolist (dir nodes nil)
524 (when (and (eql (car dir) dev)
527 (let ((nodes (cons (cons dev ino) nodes))
528 (subdir (concatenate 'base-string subdir "/")))
529 (%enumerate-directories subdir tail pathname
530 verify-existence follow-links
531 nodes function))))))))
532 ((or pattern (member :wild))
533 (dolist (name (directory-lispy-filenames head))
534 (when (or (eq piece :wild) (pattern-matches piece name))
535 (let ((subdir (concatenate 'base-string head name)))
536 (multiple-value-bind (res dev ino mode)
538 (declare (type (or fixnum null) mode))
540 (eql (logand mode sb!unix:s-ifmt)
542 (let ((nodes (cons (cons dev ino) nodes))
543 (subdir (concatenate 'base-string subdir "/")))
544 (%enumerate-directories subdir (rest tail) pathname
545 verify-existence follow-links
546 nodes function))))))))
548 (when (string= head "/")
549 (error 'simple-file-error
551 :format-control "~@<invalid use of :UP after :ABSOLUTE.~@:>"))
552 (with-directory-node-removed (head)
553 (let ((head (concatenate 'base-string head "..")))
554 (with-directory-node-noted (head)
555 (%enumerate-directories (concatenate 'base-string head "/")
557 verify-existence follow-links
560 ;; :WILD-INFERIORS is handled above, so the only case here
561 ;; should be (:ABSOLUTE :BACK)
562 (aver (string= head "/"))
563 (error 'simple-file-error
565 :format-control "~@<invalid use of :BACK after :ABSOLUTE.~@:>"))))
566 (%enumerate-files head pathname verify-existence function))))
568 ;;; Call FUNCTION on files.
569 (defun %enumerate-files (directory pathname verify-existence function)
570 (declare (simple-string directory))
571 (/noshow0 "entering %ENUMERATE-FILES")
572 (let ((name (%pathname-name pathname))
573 (type (%pathname-type pathname))
574 (version (%pathname-version pathname)))
575 (/noshow0 "computed NAME, TYPE, and VERSION")
576 (cond ((member name '(nil :unspecific))
577 (/noshow0 "UNSPECIFIC, more or less")
578 (when (or (not verify-existence)
579 (sb!unix:unix-file-kind directory))
580 (funcall function directory)))
581 ((or (pattern-p name)
585 (/noshow0 "WILD, more or less")
586 ;; I IGNORE-ERRORS here just because the original CMU CL
587 ;; code did. I think the intent is that it's not an error
588 ;; to request matches to a wild pattern when no matches
589 ;; exist, but I haven't tried to figure out whether
590 ;; everything is kosher. (E.g. what if we try to match a
591 ;; wildcard but we don't have permission to read one of the
592 ;; relevant directories?) -- WHN 2001-04-17
593 (dolist (complete-filename (ignore-errors
594 (directory-lispy-filenames directory)))
596 (file-name file-type file-version)
597 (let ((*ignore-wildcards* t))
598 (extract-name-type-and-version
599 complete-filename 0 (length complete-filename)))
600 (when (and (components-match file-name name)
601 (components-match file-type type)
602 (components-match file-version version))
604 (concatenate 'base-string
606 complete-filename))))))
608 (/noshow0 "default case")
609 (let ((file (concatenate 'base-string directory name)))
610 (/noshow "computed basic FILE")
611 (unless (or (null type) (eq type :unspecific))
612 (/noshow0 "tweaking FILE for more-or-less-:UNSPECIFIC case")
613 (setf file (concatenate 'base-string file "." type)))
614 (unless (member version '(nil :newest :wild :unspecific))
615 (/noshow0 "tweaking FILE for more-or-less-:WILD case")
616 (setf file (concatenate 'base-string file "."
617 (quick-integer-to-string version))))
618 (/noshow0 "finished possibly tweaking FILE")
619 (when (or (not verify-existence)
620 (sb!unix:unix-file-kind file t))
621 (/noshow0 "calling FUNCTION on FILE")
622 (funcall function file)))))))
624 (/noshow0 "filesys.lisp 603")
626 ;;; FIXME: Why do we need this?
627 (defun quick-integer-to-string (n)
628 (declare (type integer n))
629 (cond ((not (fixnump n))
630 (write-to-string n :base 10 :radix nil))
634 (concatenate 'simple-base-string "-"
635 (the simple-base-string (quick-integer-to-string (- n)))))
637 (do* ((len (1+ (truncate (integer-length n) 3)))
638 (res (make-string len :element-type 'base-char))
644 (replace res res :start2 i :end2 len)
645 (shrink-vector res (- len i)))
646 (declare (simple-string res)
648 (multiple-value-setq (q r) (truncate q 10))
649 (setf (schar res i) (schar "0123456789" r))))))
653 (defun empty-relative-pathname-spec-p (x)
656 (or (equal (pathname-directory x) '(:relative))
657 ;; KLUDGE: I'm not sure this second check should really
658 ;; have to be here. But on sbcl-0.6.12.7,
659 ;; (PATHNAME-DIRECTORY (PATHNAME "")) is NIL, and
660 ;; (PATHNAME "") seems to act like an empty relative
661 ;; pathname, so in order to work with that, I test
662 ;; for NIL here. -- WHN 2001-05-18
663 (null (pathname-directory x)))
664 (null (pathname-name x))
665 (null (pathname-type x)))
666 ;; (The ANSI definition of "pathname specifier" has
667 ;; other cases, but none of them seem to admit the possibility
668 ;; of being empty and relative.)
671 ;;; Convert PATHNAME into a string that can be used with UNIX system
672 ;;; calls, or return NIL if no match is found. Wild-cards are expanded.
673 (defun unix-namestring (pathname-spec &optional (for-input t))
674 (let* ((namestring (physicalize-pathname (merge-pathnames pathname-spec)))
675 (matches nil)) ; an accumulator for actual matches
676 (when (wild-pathname-p namestring)
677 (error 'simple-file-error
679 :format-control "bad place for a wild pathname"))
680 (!enumerate-matches (match namestring nil :verify-existence for-input)
681 (push match matches))
682 (case (length matches)
685 (t (bug "!ENUMERATE-MATCHES returned more than one match on a non-wild pathname")))))
687 ;;;; TRUENAME and PROBE-FILE
689 ;;; This is only trivially different from PROBE-FILE, which is silly
691 (defun truename (pathname)
693 "Return the pathname for the actual file described by PATHNAME.
694 An error of type FILE-ERROR is signalled if no such file exists,
695 or the pathname is wild.
697 Under Unix, the TRUENAME of a broken symlink is considered to be
698 the name of the broken symlink itself."
699 (let ((result (probe-file pathname)))
701 (error 'simple-file-error
703 :format-control "The file ~S does not exist."
704 :format-arguments (list (namestring pathname))))
707 (defun probe-file (pathname)
709 "Return a pathname which is the truename of the file if it exists, or NIL
710 otherwise. An error of type FILE-ERROR is signaled if pathname is wild."
711 (let* ((defaulted-pathname (merge-pathnames
713 (sane-default-pathname-defaults)))
714 (namestring (unix-namestring defaulted-pathname t)))
715 (when (and namestring (sb!unix:unix-file-kind namestring t))
716 (let ((trueishname (sb!unix:unix-resolve-links namestring)))
718 (let* ((*ignore-wildcards* t)
719 (name (sb!unix:unix-simplify-pathname trueishname)))
720 (if (eq (sb!unix:unix-file-kind name) :directory)
721 (pathname (concatenate 'string name "/"))
722 (pathname name))))))))
724 ;;;; miscellaneous other operations
726 (/show0 "filesys.lisp 700")
728 (defun rename-file (file new-name)
730 "Rename FILE to have the specified NEW-NAME. If FILE is a stream open to a
731 file, then the associated file is renamed."
732 (let* ((original (truename file))
733 (original-namestring (unix-namestring original t))
734 (new-name (merge-pathnames new-name original))
735 (new-namestring (unix-namestring new-name nil)))
736 (unless new-namestring
737 (error 'simple-file-error
739 :format-control "~S can't be created."
740 :format-arguments (list new-name)))
741 (multiple-value-bind (res error)
742 (sb!unix:unix-rename original-namestring new-namestring)
744 (error 'simple-file-error
746 :format-control "~@<couldn't rename ~2I~_~A ~I~_to ~2I~_~A: ~
748 :format-arguments (list original new-name (strerror error))))
750 (file-name file new-name))
751 (values new-name original (truename new-name)))))
753 (defun delete-file (file)
755 "Delete the specified FILE."
756 (let ((namestring (unix-namestring file t)))
758 (close file :abort t))
760 (error 'simple-file-error
762 :format-control "~S doesn't exist."
763 :format-arguments (list file)))
764 (multiple-value-bind (res err) (sb!unix:unix-unlink namestring)
766 (simple-file-perror "couldn't delete ~A" namestring err))))
769 ;;; (This is an ANSI Common Lisp function.)
770 (defun user-homedir-pathname (&optional host)
771 "Return the home directory of the user as a pathname."
772 (declare (ignore host))
773 (pathname (sb!unix:uid-homedir (sb!unix:unix-getuid))))
775 (defun file-write-date (file)
777 "Return file's creation date, or NIL if it doesn't exist.
778 An error of type file-error is signaled if file is a wild pathname"
779 (let ((name (unix-namestring file t)))
782 (res dev ino mode nlink uid gid rdev size atime mtime)
783 (sb!unix:unix-stat name)
784 (declare (ignore dev ino mode nlink uid gid rdev size atime))
786 (+ unix-to-universal-time mtime))))))
788 (defun file-author (file)
790 "Return the file author as a string, or NIL if the author cannot be
791 determined. Signal an error of type FILE-ERROR if FILE doesn't exist,
792 or FILE is a wild pathname."
793 (let ((name (unix-namestring (pathname file) t)))
795 (error 'simple-file-error
797 :format-control "~S doesn't exist."
798 :format-arguments (list file)))
799 (multiple-value-bind (winp dev ino mode nlink uid)
800 (sb!unix:unix-stat name)
801 (declare (ignore dev ino mode nlink))
802 (and winp (sb!unix:uid-username uid)))))
806 (/show0 "filesys.lisp 800")
808 (defun directory (pathname &key)
810 "Return a list of PATHNAMEs, each the TRUENAME of a file that matched the
811 given pathname. Note that the interaction between this ANSI-specified
812 TRUENAMEing and the semantics of the Unix filesystem (symbolic links..)
813 means this function can sometimes return files which don't have the same
814 directory as PATHNAME."
815 (let (;; We create one entry in this hash table for each truename,
816 ;; as an asymptotically efficient way of removing duplicates
817 ;; (which can arise when e.g. multiple symlinks map to the
819 (truenames (make-hash-table :test #'equal))
820 (merged-pathname (merge-pathnames pathname)))
821 (!enumerate-matches (match merged-pathname)
822 (let* ((*ignore-wildcards* t)
823 ;; FIXME: Why not TRUENAME? As reported by Milan Zamazal
824 ;; sbcl-devel 2003-10-05, using TRUENAME causes a race
825 ;; condition whereby removal of a file during the
826 ;; directory operation causes an error. It's not clear
827 ;; what the right thing to do is, though. -- CSR,
829 (truename (probe-file match)))
831 (setf (gethash (namestring truename) truenames)
834 ;; Sorting isn't required by the ANSI spec, but sorting
835 ;; into some canonical order seems good just on the
836 ;; grounds that the implementation should have repeatable
837 ;; behavior when possible.
838 (sort (loop for name being each hash-key in truenames
839 using (hash-value truename)
840 collect (cons name truename))
844 (/show0 "filesys.lisp 899")
846 ;;; predicate to order pathnames by; goes by name
847 (defun pathname-order (x y)
848 (let ((xn (%pathname-name x))
849 (yn (%pathname-name y)))
851 (let ((res (string-lessp xn yn)))
852 (cond ((not res) nil)
853 ((= res (length (the simple-string xn))) t)
854 ((= res (length (the simple-string yn))) nil)
858 (defun ensure-directories-exist (pathspec &key verbose (mode #o777))
860 "Test whether the directories containing the specified file
861 actually exist, and attempt to create them if they do not.
862 The MODE argument is a CMUCL/SBCL-specific extension to control
863 the Unix permission bits."
864 (let ((pathname (physicalize-pathname (pathname pathspec)))
866 (when (wild-pathname-p pathname)
867 (error 'simple-file-error
868 :format-control "bad place for a wild pathname"
870 (let ((dir (pathname-directory pathname)))
871 (loop for i from 1 upto (length dir)
872 do (let ((newpath (make-pathname
873 :host (pathname-host pathname)
874 :device (pathname-device pathname)
875 :directory (subseq dir 0 i))))
876 (unless (probe-file newpath)
877 (let ((namestring (namestring newpath)))
879 (format *standard-output*
880 "~&creating directory: ~A~%"
882 (sb!unix:unix-mkdir namestring mode)
883 (unless (probe-file namestring)
884 (error 'simple-file-error
886 :format-control "can't create directory ~A"
887 :format-arguments (list namestring)))
888 (setf created-p t)))))
889 (values pathname created-p))))
891 (/show0 "filesys.lisp 1000")