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 ;;; the thing before a colon in a logical path
219 (def!struct (logical-hostname (:make-load-form-fun
221 (values `(make-logical-hostname
222 ,(logical-hostname-name x))
225 (:constructor make-logical-hostname (name)))
226 (name (missing-arg) :type simple-string))
228 (defun maybe-extract-logical-hostname (namestr start end)
229 (declare (type simple-base-string namestr)
230 (type index start end))
232 (do ((index start (1+ index)))
237 (case (schar namestr index)
241 (return (values (make-logical-hostname
242 (remove-backslashes namestr start index))
245 (defun parse-unix-namestring (namestr start end)
246 (declare (type simple-base-string namestr)
247 (type index start end))
248 (multiple-value-bind (absolute pieces) (split-at-slashes namestr start end)
249 (let ((logical-hostname
252 (let ((first (car pieces)))
253 (multiple-value-bind (logical-hostname new-start)
254 (maybe-extract-logical-hostname namestr
257 (when logical-hostname
259 (setf (car first) new-start))
260 logical-hostname)))))
261 (declare (type (or null logical-hostname) logical-hostname))
262 (multiple-value-bind (name type version)
263 (let* ((tail (car (last pieces)))
264 (tail-start (car tail))
265 (tail-end (cdr tail)))
266 (unless (= tail-start tail-end)
267 (setf pieces (butlast pieces))
268 (extract-name-type-and-version namestr tail-start tail-end)))
271 (let ((position (position-if (lambda (char)
272 (or (char= char (code-char 0))
276 (error 'namestring-parse-error
277 :complaint "can't embed #\\Nul or #\\/ in Unix namestring"
281 ;; Now we have everything we want. So return it.
282 (values nil ; no host for Unix namestrings
283 nil ; no device for Unix namestrings
285 (when logical-hostname
286 (dirs logical-hostname))
287 (dolist (piece pieces)
288 (let ((piece-start (car piece))
289 (piece-end (cdr piece)))
290 (unless (= piece-start piece-end)
291 (cond ((string= namestr ".."
295 ((string= namestr "**"
298 (dirs :wild-inferiors))
300 (dirs (maybe-make-pattern namestr
304 (cons :absolute (dirs)))
306 (cons :relative (dirs)))
313 (/show0 "filesys.lisp 300")
315 (defun unparse-unix-host (pathname)
316 (declare (type pathname pathname)
320 (defun unparse-unix-piece (thing)
324 (let* ((srclen (length thing))
327 (case (schar thing i)
330 (let ((result (make-string dstlen))
332 (dotimes (src srclen)
333 (let ((char (schar thing src)))
336 (setf (schar result dst) #\\)
338 (setf (schar result dst) char)
343 (dolist (piece (pattern-pieces thing))
357 (strings (cdr piece))
360 (error "invalid pattern piece: ~S" piece))))))
365 (defun unparse-unix-directory-list (directory)
366 (declare (type list directory))
369 (ecase (pop directory)
371 (cond ((logical-hostname-p (car directory))
372 ;; FIXME: The old CMU CL "search list" extension is
373 ;; gone, but the old machinery is still being used
374 ;; clumsily here and elsewhere, to represent anything
375 ;; which belongs before a colon prefix in the ANSI
376 ;; pathname machinery. This should be cleaned up,
377 ;; using simpler machinery with more mnemonic names.
378 (pieces (logical-hostname-name (pop directory)))
385 (dolist (dir directory)
390 (error ":BACK cannot be represented in namestrings."))
391 ((member :wild-inferiors)
393 ((or simple-string pattern)
394 (pieces (unparse-unix-piece dir))
397 (error "invalid directory component: ~S" dir)))))
398 (apply #'concatenate 'simple-string (pieces))))
400 (defun unparse-unix-directory (pathname)
401 (declare (type pathname pathname))
402 (unparse-unix-directory-list (%pathname-directory pathname)))
404 (defun unparse-unix-file (pathname)
405 (declare (type pathname pathname))
407 (let* ((name (%pathname-name pathname))
408 (type (%pathname-type pathname))
409 (type-supplied (not (or (null type) (eq type :unspecific)))))
410 ;; Note: by ANSI 19.3.1.1.5, we ignore the version slot when
411 ;; translating logical pathnames to a filesystem without
412 ;; versions (like Unix).
414 (strings (unparse-unix-piece name)))
417 (error "cannot specify the type without a file: ~S" pathname))
419 (strings (unparse-unix-piece type))))
420 (apply #'concatenate 'simple-string (strings))))
422 (/show0 "filesys.lisp 406")
424 (defun unparse-unix-namestring (pathname)
425 (declare (type pathname pathname))
426 (concatenate 'simple-string
427 (unparse-unix-directory pathname)
428 (unparse-unix-file pathname)))
430 (defun unparse-unix-enough (pathname defaults)
431 (declare (type pathname pathname defaults))
433 (error "~S cannot be represented relative to ~S."
436 (let* ((pathname-directory (%pathname-directory pathname))
437 (defaults-directory (%pathname-directory defaults))
438 (prefix-len (length defaults-directory))
440 (cond ((and (> prefix-len 1)
441 (>= (length pathname-directory) prefix-len)
442 (compare-component (subseq pathname-directory
445 ;; Pathname starts with a prefix of default. So
446 ;; just use a relative directory from then on out.
447 (cons :relative (nthcdr prefix-len pathname-directory)))
448 ((eq (car pathname-directory) :absolute)
449 ;; We are an absolute pathname, so we can just use it.
452 ;; We are a relative directory. So we lose.
454 (strings (unparse-unix-directory-list result-directory)))
455 (let* ((pathname-version (%pathname-version pathname))
456 (version-needed (and pathname-version
457 (not (eq pathname-version :newest))))
458 (pathname-type (%pathname-type pathname))
459 (type-needed (or version-needed
461 (not (eq pathname-type :unspecific)))))
462 (pathname-name (%pathname-name pathname))
463 (name-needed (or type-needed
465 (not (compare-component pathname-name
469 (unless pathname-name (lose))
470 (strings (unparse-unix-piece pathname-name)))
472 (when (or (null pathname-type) (eq pathname-type :unspecific))
475 (strings (unparse-unix-piece pathname-type)))
477 (typecase pathname-version
481 (strings (format nil ".~D" pathname-version)))
484 (apply #'concatenate 'simple-string (strings)))))
486 ;;;; wildcard matching stuff
488 ;;; Return a list of all the Lispy filenames (not including e.g. the
489 ;;; Unix magic "." and "..") in the directory named by DIRECTORY-NAME.
490 (defun directory-lispy-filenames (directory-name)
491 (with-alien ((adlf (* c-string)
492 (alien-funcall (extern-alien
493 "alloc_directory_lispy_filenames"
494 (function (* c-string) c-string))
496 (if (null-alien adlf)
497 (error 'simple-file-error
498 :pathname directory-name
499 :format-control "~@<couldn't read directory ~S: ~2I~_~A~:>"
500 :format-arguments (list directory-name (strerror)))
502 (c-strings->string-list adlf)
503 (alien-funcall (extern-alien "free_directory_lispy_filenames"
504 (function void (* c-string)))
507 (/show0 "filesys.lisp 498")
509 (defmacro !enumerate-matches ((var pathname &optional result
510 &key (verify-existence t)
514 (%enumerate-matches (pathname ,pathname)
517 (lambda (,var) ,@body))
520 (/show0 "filesys.lisp 500")
522 ;;; Call FUNCTION on matches.
523 (defun %enumerate-matches (pathname verify-existence follow-links function)
524 (/noshow0 "entering %ENUMERATE-MATCHES")
525 (when (pathname-type pathname)
526 (unless (pathname-name pathname)
527 (error "cannot supply a type without a name:~% ~S" pathname)))
528 (when (and (integerp (pathname-version pathname))
529 (member (pathname-type pathname) '(nil :unspecific)))
530 (error "cannot supply a version without a type:~% ~S" pathname))
531 (let ((directory (pathname-directory pathname)))
532 (/noshow0 "computed DIRECTORY")
534 (ecase (first directory)
536 (/noshow0 "absolute directory")
537 (%enumerate-directories "/" (rest directory) pathname
538 verify-existence follow-links
541 (/noshow0 "relative directory")
542 (%enumerate-directories "" (rest directory) pathname
543 verify-existence follow-links
545 (%enumerate-files "" pathname verify-existence function))))
547 ;;; Call FUNCTION on directories.
548 (defun %enumerate-directories (head tail pathname verify-existence
549 follow-links nodes function)
550 (declare (simple-string head))
551 (macrolet ((unix-xstat (name)
553 (sb!unix:unix-stat ,name)
554 (sb!unix:unix-lstat ,name)))
555 (with-directory-node-noted ((head) &body body)
556 `(multiple-value-bind (res dev ino mode)
558 (when (and res (eql (logand mode sb!unix:s-ifmt)
560 (let ((nodes (cons (cons dev ino) nodes)))
563 (let ((piece (car tail)))
566 (let ((head (concatenate 'string head piece)))
567 (with-directory-node-noted (head)
568 (%enumerate-directories (concatenate 'string head "/")
570 verify-existence follow-links
572 ((member :wild-inferiors)
573 (%enumerate-directories head (rest tail) pathname
574 verify-existence follow-links
576 (dolist (name (ignore-errors (directory-lispy-filenames head)))
577 (let ((subdir (concatenate 'string head name)))
578 (multiple-value-bind (res dev ino mode)
580 (declare (type (or fixnum null) mode))
581 (when (and res (eql (logand mode sb!unix:s-ifmt)
583 (unless (dolist (dir nodes nil)
584 (when (and (eql (car dir) dev)
587 (let ((nodes (cons (cons dev ino) nodes))
588 (subdir (concatenate 'string subdir "/")))
589 (%enumerate-directories subdir tail pathname
590 verify-existence follow-links
591 nodes function))))))))
592 ((or pattern (member :wild))
593 (dolist (name (directory-lispy-filenames head))
594 (when (or (eq piece :wild) (pattern-matches piece name))
595 (let ((subdir (concatenate 'string head name)))
596 (multiple-value-bind (res dev ino mode)
598 (declare (type (or fixnum null) mode))
600 (eql (logand mode sb!unix:s-ifmt)
602 (let ((nodes (cons (cons dev ino) nodes))
603 (subdir (concatenate 'string subdir "/")))
604 (%enumerate-directories subdir (rest tail) pathname
605 verify-existence follow-links
606 nodes function))))))))
608 (let ((head (concatenate 'string head "..")))
609 (with-directory-node-noted (head)
610 (%enumerate-directories (concatenate 'string head "/")
612 verify-existence follow-links
614 (%enumerate-files head pathname verify-existence function))))
616 ;;; Call FUNCTION on files.
617 (defun %enumerate-files (directory pathname verify-existence function)
618 (declare (simple-string directory))
619 (/noshow0 "entering %ENUMERATE-FILES")
620 (let ((name (%pathname-name pathname))
621 (type (%pathname-type pathname))
622 (version (%pathname-version pathname)))
623 (/noshow0 "computed NAME, TYPE, and VERSION")
624 (cond ((member name '(nil :unspecific))
625 (/noshow0 "UNSPECIFIC, more or less")
626 (when (or (not verify-existence)
627 (sb!unix:unix-file-kind directory))
628 (funcall function directory)))
629 ((or (pattern-p name)
633 (/noshow0 "WILD, more or less")
634 ;; I IGNORE-ERRORS here just because the original CMU CL
635 ;; code did. I think the intent is that it's not an error
636 ;; to request matches to a wild pattern when no matches
637 ;; exist, but I haven't tried to figure out whether
638 ;; everything is kosher. (E.g. what if we try to match a
639 ;; wildcard but we don't have permission to read one of the
640 ;; relevant directories?) -- WHN 2001-04-17
641 (dolist (complete-filename (ignore-errors
642 (directory-lispy-filenames directory)))
644 (file-name file-type file-version)
645 (let ((*ignore-wildcards* t))
646 (extract-name-type-and-version
647 complete-filename 0 (length complete-filename)))
648 (when (and (components-match file-name name)
649 (components-match file-type type)
650 (components-match file-version version))
654 complete-filename))))))
656 (/noshow0 "default case")
657 (let ((file (concatenate 'string directory name)))
658 (/noshow "computed basic FILE")
659 (unless (or (null type) (eq type :unspecific))
660 (/noshow0 "tweaking FILE for more-or-less-:UNSPECIFIC case")
661 (setf file (concatenate 'string file "." type)))
662 (unless (member version '(nil :newest :wild))
663 (/noshow0 "tweaking FILE for more-or-less-:WILD case")
664 (setf file (concatenate 'string file "."
665 (quick-integer-to-string version))))
666 (/noshow0 "finished possibly tweaking FILE")
667 (when (or (not verify-existence)
668 (sb!unix:unix-file-kind file t))
669 (/noshow0 "calling FUNCTION on FILE")
670 (funcall function file)))))))
672 (/noshow0 "filesys.lisp 603")
674 ;;; FIXME: Why do we need this?
675 (defun quick-integer-to-string (n)
676 (declare (type integer n))
677 (cond ((not (fixnump n))
678 (write-to-string n :base 10 :radix nil))
682 (concatenate 'simple-string "-"
683 (the simple-string (quick-integer-to-string (- n)))))
685 (do* ((len (1+ (truncate (integer-length n) 3)))
686 (res (make-string len))
692 (replace res res :start2 i :end2 len)
693 (shrink-vector res (- len i)))
694 (declare (simple-string res)
696 (multiple-value-setq (q r) (truncate q 10))
697 (setf (schar res i) (schar "0123456789" r))))))
701 (defun empty-relative-pathname-spec-p (x)
704 (or (equal (pathname-directory x) '(:relative))
705 ;; KLUDGE: I'm not sure this second check should really
706 ;; have to be here. But on sbcl-0.6.12.7,
707 ;; (PATHNAME-DIRECTORY (PATHNAME "")) is NIL, and
708 ;; (PATHNAME "") seems to act like an empty relative
709 ;; pathname, so in order to work with that, I test
710 ;; for NIL here. -- WHN 2001-05-18
711 (null (pathname-directory x)))
712 (null (pathname-name x))
713 (null (pathname-type x)))
714 ;; (The ANSI definition of "pathname specifier" has
715 ;; other cases, but none of them seem to admit the possibility
716 ;; of being empty and relative.)
719 ;;; Convert PATHNAME into a string that can be used with UNIX system
720 ;;; calls, or return NIL if no match is found. Wild-cards are expanded.
721 (defun unix-namestring (pathname-spec &optional (for-input t))
722 ;; The ordinary rules of converting Lispy paths to Unix paths break
723 ;; down for the current working directory, which Lisp thinks of as
724 ;; "" (more or less, and modulo ANSI's *DEFAULT-PATHNAME-DEFAULTS*,
725 ;; which unfortunately SBCL, as of sbcl-0.6.12.8, basically ignores)
726 ;; and Unix thinks of as ".". Since we're at the interface between
727 ;; Unix system calls and things like ENSURE-DIRECTORIES-EXIST which
728 ;; think the Lisp way, we perform the conversion.
730 ;; (FIXME: The *right* way to deal with this special case is to
731 ;; merge PATHNAME-SPEC with *DEFAULT-PATHNAME-DEFAULTS* here, after
732 ;; which it's not a relative pathname any more so the special case
733 ;; is no longer an issue. But until *DEFAULT-PATHNAME-DEFAULTS*
734 ;; works, we use this hack.)
735 (if (empty-relative-pathname-spec-p pathname-spec)
737 ;; Otherwise, the ordinary rules apply.
738 (let* ((namestring (physicalize-pathname (pathname pathname-spec)))
739 (matches nil)) ; an accumulator for actual matches
740 (!enumerate-matches (match namestring nil :verify-existence for-input)
741 (push match matches))
742 (case (length matches)
745 (t (error 'simple-file-error
746 :format-control "~S is ambiguous:~{~% ~A~}"
747 :format-arguments (list pathname-spec matches)))))))
749 ;;;; TRUENAME and PROBE-FILE
751 ;;; This is only trivially different from PROBE-FILE, which is silly
753 (defun truename (pathname)
755 "Return the pathname for the actual file described by PATHNAME.
756 An error of type FILE-ERROR is signalled if no such file exists,
757 or the pathname is wild.
759 Under Unix, the TRUENAME of a broken symlink is considered to be
760 the name of the broken symlink itself."
761 (if (wild-pathname-p pathname)
762 (error 'simple-file-error
763 :format-control "can't use a wild pathname here"
765 (let ((result (probe-file pathname)))
767 (error 'simple-file-error
769 :format-control "The file ~S does not exist."
770 :format-arguments (list (namestring pathname))))
773 ;;; If PATHNAME exists, return its truename, otherwise NIL.
774 (defun probe-file (pathname)
776 "Return a pathname which is the truename of the file if it exists, or NIL
777 otherwise. An error of type FILE-ERROR is signaled if pathname is wild."
778 (when (wild-pathname-p pathname)
779 (error 'simple-file-error
781 :format-control "can't use a wild pathname here"))
782 (let* ((defaulted-pathname (merge-pathnames
784 (sane-default-pathname-defaults)))
785 (namestring (unix-namestring defaulted-pathname t)))
786 (when (and namestring (sb!unix:unix-file-kind namestring t))
787 (let ((trueishname (sb!unix:unix-resolve-links namestring)))
789 (let ((*ignore-wildcards* t))
790 (pathname (sb!unix:unix-simplify-pathname trueishname))))))))
792 ;;;; miscellaneous other operations
794 (/show0 "filesys.lisp 700")
796 (defun rename-file (file new-name)
798 "Rename FILE to have the specified NEW-NAME. If FILE is a stream open to a
799 file, then the associated file is renamed."
800 (let* ((original (truename file))
801 (original-namestring (unix-namestring original t))
802 (new-name (merge-pathnames new-name original))
803 (new-namestring (unix-namestring new-name nil)))
804 (unless new-namestring
805 (error 'simple-file-error
807 :format-control "~S can't be created."
808 :format-arguments (list new-name)))
809 (multiple-value-bind (res error)
810 (sb!unix:unix-rename original-namestring new-namestring)
812 (error 'simple-file-error
814 :format-control "~@<couldn't rename ~2I~_~A ~I~_to ~2I~_~A: ~
816 :format-arguments (list original new-name (strerror error))))
818 (file-name file new-namestring))
819 (values new-name original (truename new-name)))))
821 (defun delete-file (file)
823 "Delete the specified FILE."
824 (let ((namestring (unix-namestring file t)))
826 (close file :abort t))
828 (error 'simple-file-error
830 :format-control "~S doesn't exist."
831 :format-arguments (list file)))
832 (multiple-value-bind (res err) (sb!unix:unix-unlink namestring)
834 (simple-file-perror "couldn't delete ~A" namestring err))))
837 ;;; (This is an ANSI Common Lisp function.)
839 ;;; This is obtained from the logical name \"home:\", which is set
840 ;;; up for us at initialization time.
841 (defun user-homedir-pathname (&optional host)
842 "Return the home directory of the user as a pathname."
843 (declare (ignore host))
844 ;; Note: CMU CL did #P"home:" here instead of using a call to
845 ;; PATHNAME. Delaying construction of the pathname until we're
846 ;; running in a target Lisp lets us avoid figuring out how to dump
847 ;; cross-compilation host Lisp PATHNAME objects into a target Lisp
848 ;; object file. It also might have a small positive effect on
849 ;; efficiency, in that we don't allocate a PATHNAME we don't need,
850 ;; but it it could also have a larger negative effect. Hopefully
851 ;; it'll be OK. -- WHN 19990714
854 (defun file-write-date (file)
856 "Return file's creation date, or NIL if it doesn't exist.
857 An error of type file-error is signaled if file is a wild pathname"
858 (if (wild-pathname-p file)
859 ;; FIXME: This idiom appears many times in this file. Perhaps it
860 ;; should turn into (CANNOT-BE-WILD-PATHNAME FILE). (C-B-W-P
861 ;; should be a macro, not a function, so that the error message
862 ;; is reported as coming from e.g. FILE-WRITE-DATE instead of
863 ;; from CANNOT-BE-WILD-PATHNAME itself.)
864 (error 'simple-file-error
866 :format-control "bad place for a wild pathname")
867 (let ((name (unix-namestring file t)))
870 (res dev ino mode nlink uid gid rdev size atime mtime)
871 (sb!unix:unix-stat name)
872 (declare (ignore dev ino mode nlink uid gid rdev size atime))
874 (+ unix-to-universal-time mtime)))))))
876 (defun file-author (file)
878 "Return the file author as a string, or NIL if the author cannot be
879 determined. Signal an error of type FILE-ERROR if FILE doesn't exist,
880 or FILE is a wild pathname."
881 (if (wild-pathname-p file)
882 (error 'simple-file-error
884 "bad place for a wild pathname")
885 (let ((name (unix-namestring (pathname file) t)))
887 (error 'simple-file-error
889 :format-control "~S doesn't exist."
890 :format-arguments (list file)))
891 (multiple-value-bind (winp dev ino mode nlink uid)
892 (sb!unix:unix-stat name)
893 (declare (ignore dev ino mode nlink))
894 (and winp (sb!unix:uid-username uid))))))
898 (/show0 "filesys.lisp 800")
900 (defun directory (pathname &key)
902 "Return a list of PATHNAMEs, each the TRUENAME of a file that matched the
903 given pathname. Note that the interaction between this ANSI-specified
904 TRUENAMEing and the semantics of the Unix filesystem (symbolic links..)
905 means this function can sometimes return files which don't have the same
906 directory as PATHNAME."
907 (let (;; We create one entry in this hash table for each truename,
908 ;; as an asymptotically fast way of removing duplicates (which
909 ;; can arise when e.g. multiple symlinks map to the same
911 (truenames (make-hash-table :test #'equal))
912 ;; FIXME: not really right, as per bug 139
913 (merged-pathname (merge-pathnames pathname
914 (make-pathname :name :wild
917 (!enumerate-matches (match merged-pathname)
918 (let ((*ignore-wildcards* t)
919 (truename (truename (if (eq (sb!unix:unix-file-kind match)
921 (concatenate 'string match "/")
923 (setf (gethash (namestring truename) truenames)
926 ;; Sorting isn't required by the ANSI spec, but sorting
927 ;; into some canonical order seems good just on the
928 ;; grounds that the implementation should have repeatable
929 ;; behavior when possible.
930 (sort (loop for name being each hash-key in truenames
931 using (hash-value truename)
932 collect (cons name truename))
936 (/show0 "filesys.lisp 899")
938 ;;; predicate to order pathnames by; goes by name
939 (defun pathname-order (x y)
940 (let ((xn (%pathname-name x))
941 (yn (%pathname-name y)))
943 (let ((res (string-lessp xn yn)))
944 (cond ((not res) nil)
945 ((= res (length (the simple-string xn))) t)
946 ((= res (length (the simple-string yn))) nil)
950 (defun ensure-directories-exist (pathspec &key verbose (mode #o777))
952 "Test whether the directories containing the specified file
953 actually exist, and attempt to create them if they do not.
954 The MODE argument is a CMUCL/SBCL-specific extension to control
955 the Unix permission bits."
956 (let ((pathname (physicalize-pathname (pathname pathspec)))
958 (when (wild-pathname-p pathname)
959 (error 'simple-file-error
960 :format-control "bad place for a wild pathname"
962 (let ((dir (pathname-directory pathname)))
963 (loop for i from 1 upto (length dir)
964 do (let ((newpath (make-pathname
965 :host (pathname-host pathname)
966 :device (pathname-device pathname)
967 :directory (subseq dir 0 i))))
968 (unless (probe-file newpath)
969 (let ((namestring (namestring newpath)))
971 (format *standard-output*
972 "~&creating directory: ~A~%"
974 (sb!unix:unix-mkdir namestring mode)
975 (unless (probe-file namestring)
976 (error 'simple-file-error
978 :format-control "can't create directory ~A"
979 :format-arguments (list namestring)))
980 (setf created-p t)))))
981 (values pathname created-p))))
983 (/show0 "filesys.lisp 1000")