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)))
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
162 (second-to-last-dot (and last-dot
163 (position #\. namestr :start (1+ start)
164 :end last-dot :from-end t)))
166 ;; If there is a second-to-last dot, check to see whether there is
167 ;; a valid version after the last dot.
168 (when second-to-last-dot
169 (cond ((and (= (+ last-dot 2) end)
170 (char= (schar namestr (1+ last-dot)) #\*))
171 (setf version :wild))
172 ((and (< (1+ last-dot) end)
173 (do ((index (1+ last-dot) (1+ index)))
175 (unless (char<= #\0 (schar namestr index) #\9)
178 (parse-integer namestr :start (1+ last-dot) :end end)))
180 (setf second-to-last-dot nil))))
181 (cond (second-to-last-dot
182 (values (maybe-make-pattern namestr start second-to-last-dot)
183 (maybe-make-pattern namestr
184 (1+ second-to-last-dot)
188 (values (maybe-make-pattern namestr start last-dot)
189 (maybe-make-pattern namestr (1+ last-dot) end)
192 (values (maybe-make-pattern namestr start end)
196 (/show0 "filesys.lisp 200")
198 ;;; Take a string and return a list of cons cells that mark the char
199 ;;; separated subseq. The first value is true if absolute directories
201 (defun split-at-slashes (namestr start end)
202 (declare (type simple-base-string namestr)
203 (type index start end))
204 (let ((absolute (and (/= start end)
205 (char= (schar namestr start) #\/))))
208 ;; Next, split the remainder into slash-separated chunks.
211 (let ((slash (position #\/ namestr :start start :end end)))
212 (pieces (cons start (or slash end)))
215 (setf start (1+ slash))))
216 (values absolute (pieces)))))
218 (defun parse-unix-namestring (namestr start end)
219 (declare (type simple-base-string namestr)
220 (type index start end))
221 (multiple-value-bind (absolute pieces) (split-at-slashes namestr start end)
222 (multiple-value-bind (name type version)
223 (let* ((tail (car (last pieces)))
224 (tail-start (car tail))
225 (tail-end (cdr tail)))
226 (unless (= tail-start tail-end)
227 (setf pieces (butlast pieces))
228 (extract-name-type-and-version namestr tail-start tail-end)))
231 (let ((position (position-if (lambda (char)
232 (or (char= char (code-char 0))
236 (error 'namestring-parse-error
237 :complaint "can't embed #\\Nul or #\\/ in Unix namestring"
240 ;; Now we have everything we want. So return it.
241 (values nil ; no host for Unix namestrings
242 nil ; no device for Unix namestrings
244 (dolist (piece pieces)
245 (let ((piece-start (car piece))
246 (piece-end (cdr piece)))
247 (unless (= piece-start piece-end)
248 (cond ((string= namestr ".."
252 ((string= namestr "**"
255 (dirs :wild-inferiors))
257 (dirs (maybe-make-pattern namestr
261 (cons :absolute (dirs)))
263 (cons :relative (dirs)))
270 (/show0 "filesys.lisp 300")
272 (defun unparse-unix-host (pathname)
273 (declare (type pathname pathname)
275 ;; this host designator needs to be recognized as a physical host in
276 ;; PARSE-NAMESTRING. Until sbcl-0.7.3.x, we had "Unix" here, but
277 ;; that's a valid Logical Hostname, so that's a bad choice. -- CSR,
281 (defun unparse-unix-piece (thing)
285 (let* ((srclen (length thing))
288 (case (schar thing i)
291 (let ((result (make-string dstlen))
293 (dotimes (src srclen)
294 (let ((char (schar thing src)))
297 (setf (schar result dst) #\\)
299 (setf (schar result dst) char)
304 (dolist (piece (pattern-pieces thing))
318 (strings (cdr piece))
321 (error "invalid pattern piece: ~S" piece))))))
326 (defun unparse-unix-directory-list (directory)
327 (declare (type list directory))
330 (ecase (pop directory)
336 (dolist (dir directory)
341 (error ":BACK cannot be represented in namestrings."))
342 ((member :wild-inferiors)
344 ((or simple-string pattern)
345 (pieces (unparse-unix-piece dir))
348 (error "invalid directory component: ~S" dir)))))
349 (apply #'concatenate 'simple-string (pieces))))
351 (defun unparse-unix-directory (pathname)
352 (declare (type pathname pathname))
353 (unparse-unix-directory-list (%pathname-directory pathname)))
355 (defun unparse-unix-file (pathname)
356 (declare (type pathname pathname))
358 (let* ((name (%pathname-name pathname))
359 (type (%pathname-type pathname))
360 (type-supplied (not (or (null type) (eq type :unspecific)))))
361 ;; Note: by ANSI 19.3.1.1.5, we ignore the version slot when
362 ;; translating logical pathnames to a filesystem without
363 ;; versions (like Unix).
365 (strings (unparse-unix-piece name)))
368 (error "cannot specify the type without a file: ~S" pathname))
370 (strings (unparse-unix-piece type))))
371 (apply #'concatenate 'simple-string (strings))))
373 (/show0 "filesys.lisp 406")
375 (defun unparse-unix-namestring (pathname)
376 (declare (type pathname pathname))
377 (concatenate 'simple-string
378 (unparse-unix-directory pathname)
379 (unparse-unix-file pathname)))
381 (defun unparse-unix-enough (pathname defaults)
382 (declare (type pathname pathname defaults))
384 (error "~S cannot be represented relative to ~S."
387 (let* ((pathname-directory (%pathname-directory pathname))
388 (defaults-directory (%pathname-directory defaults))
389 (prefix-len (length defaults-directory))
391 (cond ((and (> prefix-len 1)
392 (>= (length pathname-directory) prefix-len)
393 (compare-component (subseq pathname-directory
396 ;; Pathname starts with a prefix of default. So
397 ;; just use a relative directory from then on out.
398 (cons :relative (nthcdr prefix-len pathname-directory)))
399 ((eq (car pathname-directory) :absolute)
400 ;; We are an absolute pathname, so we can just use it.
403 ;; We are a relative directory. So we lose.
405 (strings (unparse-unix-directory-list result-directory)))
406 (let* ((pathname-version (%pathname-version pathname))
407 (version-needed (and pathname-version
408 (not (eq pathname-version :newest))))
409 (pathname-type (%pathname-type pathname))
410 (type-needed (or version-needed
412 (not (eq pathname-type :unspecific)))))
413 (pathname-name (%pathname-name pathname))
414 (name-needed (or type-needed
416 (not (compare-component pathname-name
420 (unless pathname-name (lose))
421 (strings (unparse-unix-piece pathname-name)))
423 (when (or (null pathname-type) (eq pathname-type :unspecific))
426 (strings (unparse-unix-piece pathname-type)))
428 (typecase pathname-version
432 (strings (format nil ".~D" pathname-version)))
435 (apply #'concatenate 'simple-string (strings)))))
437 ;;;; wildcard matching stuff
439 ;;; Return a list of all the Lispy filenames (not including e.g. the
440 ;;; Unix magic "." and "..") in the directory named by DIRECTORY-NAME.
441 (defun directory-lispy-filenames (directory-name)
442 (with-alien ((adlf (* c-string)
443 (alien-funcall (extern-alien
444 "alloc_directory_lispy_filenames"
445 (function (* c-string) c-string))
447 (if (null-alien adlf)
448 (error 'simple-file-error
449 :pathname directory-name
450 :format-control "~@<couldn't read directory ~S: ~2I~_~A~:>"
451 :format-arguments (list directory-name (strerror)))
453 (c-strings->string-list adlf)
454 (alien-funcall (extern-alien "free_directory_lispy_filenames"
455 (function void (* c-string)))
458 (/show0 "filesys.lisp 498")
460 (defmacro !enumerate-matches ((var pathname &optional result
461 &key (verify-existence t)
465 (%enumerate-matches (pathname ,pathname)
468 (lambda (,var) ,@body))
471 (/show0 "filesys.lisp 500")
473 ;;; Call FUNCTION on matches.
474 (defun %enumerate-matches (pathname verify-existence follow-links function)
475 (/noshow0 "entering %ENUMERATE-MATCHES")
476 (when (pathname-type pathname)
477 (unless (pathname-name pathname)
478 (error "cannot supply a type without a name:~% ~S" pathname)))
479 (when (and (integerp (pathname-version pathname))
480 (member (pathname-type pathname) '(nil :unspecific)))
481 (error "cannot supply a version without a type:~% ~S" pathname))
482 (let ((directory (pathname-directory pathname)))
483 (/noshow0 "computed DIRECTORY")
485 (ecase (first directory)
487 (/noshow0 "absolute directory")
488 (%enumerate-directories "/" (rest directory) pathname
489 verify-existence follow-links
492 (/noshow0 "relative directory")
493 (%enumerate-directories "" (rest directory) pathname
494 verify-existence follow-links
496 (%enumerate-files "" pathname verify-existence function))))
498 ;;; Call FUNCTION on directories.
499 (defun %enumerate-directories (head tail pathname verify-existence
500 follow-links nodes function)
501 (declare (simple-string head))
502 (macrolet ((unix-xstat (name)
504 (sb!unix:unix-stat ,name)
505 (sb!unix:unix-lstat ,name)))
506 (with-directory-node-noted ((head) &body body)
507 `(multiple-value-bind (res dev ino mode)
509 (when (and res (eql (logand mode sb!unix:s-ifmt)
511 (let ((nodes (cons (cons dev ino) nodes)))
513 (with-directory-node-removed ((head) &body body)
514 `(multiple-value-bind (res dev ino mode)
516 (when (and res (eql (logand mode sb!unix:s-ifmt)
518 (let ((nodes (remove (cons dev ino) nodes :test #'equal)))
521 (let ((piece (car tail)))
524 (let ((head (concatenate 'string head piece)))
525 (with-directory-node-noted (head)
526 (%enumerate-directories (concatenate 'string head "/")
528 verify-existence follow-links
530 ((member :wild-inferiors)
531 (%enumerate-directories head (rest tail) pathname
532 verify-existence follow-links
534 (dolist (name (ignore-errors (directory-lispy-filenames head)))
535 (let ((subdir (concatenate 'string head name)))
536 (multiple-value-bind (res dev ino mode)
538 (declare (type (or fixnum null) mode))
539 (when (and res (eql (logand mode sb!unix:s-ifmt)
541 (unless (dolist (dir nodes nil)
542 (when (and (eql (car dir) dev)
545 (let ((nodes (cons (cons dev ino) nodes))
546 (subdir (concatenate 'string subdir "/")))
547 (%enumerate-directories subdir tail pathname
548 verify-existence follow-links
549 nodes function))))))))
550 ((or pattern (member :wild))
551 (dolist (name (directory-lispy-filenames head))
552 (when (or (eq piece :wild) (pattern-matches piece name))
553 (let ((subdir (concatenate 'string head name)))
554 (multiple-value-bind (res dev ino mode)
556 (declare (type (or fixnum null) mode))
558 (eql (logand mode sb!unix:s-ifmt)
560 (let ((nodes (cons (cons dev ino) nodes))
561 (subdir (concatenate 'string subdir "/")))
562 (%enumerate-directories subdir (rest tail) pathname
563 verify-existence follow-links
564 nodes function))))))))
566 (with-directory-node-removed (head)
567 (let ((head (concatenate 'string head "..")))
568 (with-directory-node-noted (head)
569 (%enumerate-directories (concatenate 'string head "/")
571 verify-existence follow-links
572 nodes function)))))))
573 (%enumerate-files head pathname verify-existence function))))
575 ;;; Call FUNCTION on files.
576 (defun %enumerate-files (directory pathname verify-existence function)
577 (declare (simple-string directory))
578 (/noshow0 "entering %ENUMERATE-FILES")
579 (let ((name (%pathname-name pathname))
580 (type (%pathname-type pathname))
581 (version (%pathname-version pathname)))
582 (/noshow0 "computed NAME, TYPE, and VERSION")
583 (cond ((member name '(nil :unspecific))
584 (/noshow0 "UNSPECIFIC, more or less")
585 (when (or (not verify-existence)
586 (sb!unix:unix-file-kind directory))
587 (funcall function directory)))
588 ((or (pattern-p name)
592 (/noshow0 "WILD, more or less")
593 ;; I IGNORE-ERRORS here just because the original CMU CL
594 ;; code did. I think the intent is that it's not an error
595 ;; to request matches to a wild pattern when no matches
596 ;; exist, but I haven't tried to figure out whether
597 ;; everything is kosher. (E.g. what if we try to match a
598 ;; wildcard but we don't have permission to read one of the
599 ;; relevant directories?) -- WHN 2001-04-17
600 (dolist (complete-filename (ignore-errors
601 (directory-lispy-filenames directory)))
603 (file-name file-type file-version)
604 (let ((*ignore-wildcards* t))
605 (extract-name-type-and-version
606 complete-filename 0 (length complete-filename)))
607 (when (and (components-match file-name name)
608 (components-match file-type type)
609 (components-match file-version version))
613 complete-filename))))))
615 (/noshow0 "default case")
616 (let ((file (concatenate 'string directory name)))
617 (/noshow "computed basic FILE")
618 (unless (or (null type) (eq type :unspecific))
619 (/noshow0 "tweaking FILE for more-or-less-:UNSPECIFIC case")
620 (setf file (concatenate 'string file "." type)))
621 (unless (member version '(nil :newest :wild))
622 (/noshow0 "tweaking FILE for more-or-less-:WILD case")
623 (setf file (concatenate 'string file "."
624 (quick-integer-to-string version))))
625 (/noshow0 "finished possibly tweaking FILE")
626 (when (or (not verify-existence)
627 (sb!unix:unix-file-kind file t))
628 (/noshow0 "calling FUNCTION on FILE")
629 (funcall function file)))))))
631 (/noshow0 "filesys.lisp 603")
633 ;;; FIXME: Why do we need this?
634 (defun quick-integer-to-string (n)
635 (declare (type integer n))
636 (cond ((not (fixnump n))
637 (write-to-string n :base 10 :radix nil))
641 (concatenate 'simple-string "-"
642 (the simple-string (quick-integer-to-string (- n)))))
644 (do* ((len (1+ (truncate (integer-length n) 3)))
645 (res (make-string len))
651 (replace res res :start2 i :end2 len)
652 (shrink-vector res (- len i)))
653 (declare (simple-string res)
655 (multiple-value-setq (q r) (truncate q 10))
656 (setf (schar res i) (schar "0123456789" r))))))
660 (defun empty-relative-pathname-spec-p (x)
663 (or (equal (pathname-directory x) '(:relative))
664 ;; KLUDGE: I'm not sure this second check should really
665 ;; have to be here. But on sbcl-0.6.12.7,
666 ;; (PATHNAME-DIRECTORY (PATHNAME "")) is NIL, and
667 ;; (PATHNAME "") seems to act like an empty relative
668 ;; pathname, so in order to work with that, I test
669 ;; for NIL here. -- WHN 2001-05-18
670 (null (pathname-directory x)))
671 (null (pathname-name x))
672 (null (pathname-type x)))
673 ;; (The ANSI definition of "pathname specifier" has
674 ;; other cases, but none of them seem to admit the possibility
675 ;; of being empty and relative.)
678 ;;; Convert PATHNAME into a string that can be used with UNIX system
679 ;;; calls, or return NIL if no match is found. Wild-cards are expanded.
680 ;;; FIXME this should signal file-error if the pathname is wild, whether
681 ;;; or not it turns out to have only one match. Fix post 0.7.2
682 (defun unix-namestring (pathname-spec &optional (for-input t))
683 (let* ((namestring (physicalize-pathname (merge-pathnames pathname-spec)))
684 (matches nil)) ; an accumulator for actual matches
685 (when (wild-pathname-p namestring)
686 (error 'simple-file-error
688 :format-control "bad place for a wild pathname"))
689 (!enumerate-matches (match namestring nil :verify-existence for-input)
690 (push match matches))
691 (case (length matches)
694 (t (bug "!ENUMERATE-MATCHES returned more than one match on a non-wild pathname")))))
696 ;;;; TRUENAME and PROBE-FILE
698 ;;; This is only trivially different from PROBE-FILE, which is silly
700 (defun truename (pathname)
702 "Return the pathname for the actual file described by PATHNAME.
703 An error of type FILE-ERROR is signalled if no such file exists,
704 or the pathname is wild.
706 Under Unix, the TRUENAME of a broken symlink is considered to be
707 the name of the broken symlink itself."
708 (let ((result (probe-file pathname)))
710 (error 'simple-file-error
712 :format-control "The file ~S does not exist."
713 :format-arguments (list (namestring pathname))))
716 ;;; If PATHNAME exists, return its truename, otherwise NIL.
717 (defun probe-file (pathname)
719 "Return a pathname which is the truename of the file if it exists, or NIL
720 otherwise. An error of type FILE-ERROR is signaled if pathname is wild."
721 (let* ((defaulted-pathname (merge-pathnames
723 (sane-default-pathname-defaults)))
724 (namestring (unix-namestring defaulted-pathname t)))
725 (when (and namestring (sb!unix:unix-file-kind namestring t))
726 (let ((trueishname (sb!unix:unix-resolve-links namestring)))
728 (let ((*ignore-wildcards* t))
729 (pathname (sb!unix:unix-simplify-pathname trueishname))))))))
731 ;;;; miscellaneous other operations
733 (/show0 "filesys.lisp 700")
735 (defun rename-file (file new-name)
737 "Rename FILE to have the specified NEW-NAME. If FILE is a stream open to a
738 file, then the associated file is renamed."
739 (let* ((original (truename file))
740 (original-namestring (unix-namestring original t))
741 (new-name (merge-pathnames new-name original))
742 (new-namestring (unix-namestring new-name nil)))
743 (unless new-namestring
744 (error 'simple-file-error
746 :format-control "~S can't be created."
747 :format-arguments (list new-name)))
748 (multiple-value-bind (res error)
749 (sb!unix:unix-rename original-namestring new-namestring)
751 (error 'simple-file-error
753 :format-control "~@<couldn't rename ~2I~_~A ~I~_to ~2I~_~A: ~
755 :format-arguments (list original new-name (strerror error))))
757 (file-name file new-namestring))
758 (values new-name original (truename new-name)))))
760 (defun delete-file (file)
762 "Delete the specified FILE."
763 (let ((namestring (unix-namestring file t)))
765 (close file :abort t))
767 (error 'simple-file-error
769 :format-control "~S doesn't exist."
770 :format-arguments (list file)))
771 (multiple-value-bind (res err) (sb!unix:unix-unlink namestring)
773 (simple-file-perror "couldn't delete ~A" namestring err))))
776 ;;; (This is an ANSI Common Lisp function.)
777 (defun user-homedir-pathname (&optional host)
778 "Return the home directory of the user as a pathname."
779 (declare (ignore host))
780 (pathname (sb!unix:uid-homedir (sb!unix:unix-getuid))))
782 (defun file-write-date (file)
784 "Return file's creation date, or NIL if it doesn't exist.
785 An error of type file-error is signaled if file is a wild pathname"
786 (let ((name (unix-namestring file t)))
789 (res dev ino mode nlink uid gid rdev size atime mtime)
790 (sb!unix:unix-stat name)
791 (declare (ignore dev ino mode nlink uid gid rdev size atime))
793 (+ unix-to-universal-time mtime))))))
795 (defun file-author (file)
797 "Return the file author as a string, or NIL if the author cannot be
798 determined. Signal an error of type FILE-ERROR if FILE doesn't exist,
799 or FILE is a wild pathname."
800 (let ((name (unix-namestring (pathname file) t)))
802 (error 'simple-file-error
804 :format-control "~S doesn't exist."
805 :format-arguments (list file)))
806 (multiple-value-bind (winp dev ino mode nlink uid)
807 (sb!unix:unix-stat name)
808 (declare (ignore dev ino mode nlink))
809 (and winp (sb!unix:uid-username uid)))))
813 (/show0 "filesys.lisp 800")
815 (defun directory (pathname &key)
817 "Return a list of PATHNAMEs, each the TRUENAME of a file that matched the
818 given pathname. Note that the interaction between this ANSI-specified
819 TRUENAMEing and the semantics of the Unix filesystem (symbolic links..)
820 means this function can sometimes return files which don't have the same
821 directory as PATHNAME."
822 (let (;; We create one entry in this hash table for each truename,
823 ;; as an asymptotically efficient way of removing duplicates
824 ;; (which can arise when e.g. multiple symlinks map to the
826 (truenames (make-hash-table :test #'equal))
827 (merged-pathname (merge-pathnames pathname
828 *default-pathname-defaults*)))
829 (!enumerate-matches (match merged-pathname)
830 (let* ((*ignore-wildcards* t)
831 (truename (truename (if (eq (sb!unix:unix-file-kind match)
833 (concatenate 'string match "/")
835 (setf (gethash (namestring truename) truenames)
838 ;; Sorting isn't required by the ANSI spec, but sorting
839 ;; into some canonical order seems good just on the
840 ;; grounds that the implementation should have repeatable
841 ;; behavior when possible.
842 (sort (loop for name being each hash-key in truenames
843 using (hash-value truename)
844 collect (cons name truename))
848 (/show0 "filesys.lisp 899")
850 ;;; predicate to order pathnames by; goes by name
851 (defun pathname-order (x y)
852 (let ((xn (%pathname-name x))
853 (yn (%pathname-name y)))
855 (let ((res (string-lessp xn yn)))
856 (cond ((not res) nil)
857 ((= res (length (the simple-string xn))) t)
858 ((= res (length (the simple-string yn))) nil)
862 (defun ensure-directories-exist (pathspec &key verbose (mode #o777))
864 "Test whether the directories containing the specified file
865 actually exist, and attempt to create them if they do not.
866 The MODE argument is a CMUCL/SBCL-specific extension to control
867 the Unix permission bits."
868 (let ((pathname (physicalize-pathname (pathname pathspec)))
870 (when (wild-pathname-p pathname)
871 (error 'simple-file-error
872 :format-control "bad place for a wild pathname"
874 (let ((dir (pathname-directory pathname)))
875 (loop for i from 1 upto (length dir)
876 do (let ((newpath (make-pathname
877 :host (pathname-host pathname)
878 :device (pathname-device pathname)
879 :directory (subseq dir 0 i))))
880 (unless (probe-file newpath)
881 (let ((namestring (namestring newpath)))
883 (format *standard-output*
884 "~&creating directory: ~A~%"
886 (sb!unix:unix-mkdir namestring mode)
887 (unless (probe-file namestring)
888 (error 'simple-file-error
890 :format-control "can't create directory ~A"
891 :format-arguments (list namestring)))
892 (setf created-p t)))))
893 (values pathname created-p))))
895 (/show0 "filesys.lisp 1000")