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 := [ "/" | search-list ] { file "/" }*
20 ;;; search-list := [^:/]*:
22 ;;; type := "." [^/.]*
23 ;;; version := "." ([0-9]+ | "*")
25 ;;; FIXME: Search lists are no longer supported.
27 ;;; Note: this grammar is ambiguous. The string foo.bar.5 can be
28 ;;; parsed as either just the file specified or as specifying the
29 ;;; file, type, and version. Therefore, we use the following rules
30 ;;; when confronted with an ambiguous file.type.version string:
32 ;;; - If the first character is a dot, it's part of the file. It is not
33 ;;; considered a dot in the following rules.
35 ;;; - If there is only one dot, it separates the file and the type.
37 ;;; - If there are multiple dots and the stuff following the last dot
38 ;;; is a valid version, then that is the version and the stuff between
39 ;;; the second to last dot and the last dot is the type.
41 ;;; Wildcard characters:
43 ;;; If the directory, file, type components contain any of the
44 ;;; following characters, it is considered part of a wildcard pattern
45 ;;; and has the following meaning.
47 ;;; ? - matches any character
48 ;;; * - matches any zero or more characters.
49 ;;; [abc] - matches any of a, b, or c.
50 ;;; {str1,str2,...,strn} - matches any of str1, str2, ..., or strn.
52 ;;; Any of these special characters can be preceded by a backslash to
53 ;;; cause it to be treated as a regular character.
54 (defun remove-backslashes (namestr start end)
56 "Remove any occurrences of #\\ from the string because we've already
57 checked for whatever they may have protected."
58 (declare (type simple-base-string namestr)
59 (type index start end))
60 (let* ((result (make-string (- end start)))
63 (do ((src start (1+ src)))
66 (setf (schar result dst) (schar namestr src))
70 (let ((char (schar namestr src)))
71 (cond ((char= char #\\)
74 (setf (schar result dst) char)
77 (error 'namestring-parse-error
78 :complaint "backslash in a bad place"
81 (shrink-vector result dst)))
83 (defvar *ignore-wildcards* nil)
85 (/show0 "filesys.lisp 86")
87 (defun maybe-make-pattern (namestr start end)
88 (declare (type simple-base-string namestr)
89 (type index start end))
90 (if *ignore-wildcards*
91 (subseq namestr start end)
95 (last-regular-char nil)
97 (flet ((flush-pending-regulars ()
98 (when last-regular-char
99 (pattern (if any-quotes
100 (remove-backslashes namestr
103 (subseq namestr last-regular-char index)))
104 (setf any-quotes nil)
105 (setf last-regular-char nil))))
109 (let ((char (schar namestr index)))
116 (unless last-regular-char
117 (setf last-regular-char index))
120 (flush-pending-regulars)
121 (pattern :single-char-wild)
124 (flush-pending-regulars)
125 (pattern :multi-char-wild)
128 (flush-pending-regulars)
130 (position #\] namestr :start index :end end)))
131 (unless close-bracket
132 (error 'namestring-parse-error
133 :complaint "#\\[ with no corresponding #\\]"
136 (pattern (list :character-set
140 (setf index (1+ close-bracket))))
142 (unless last-regular-char
143 (setf last-regular-char index))
145 (flush-pending-regulars)))
146 (cond ((null (pattern))
148 ((null (cdr (pattern)))
149 (let ((piece (first (pattern))))
151 ((member :multi-char-wild) :wild)
152 (simple-string piece)
154 (make-pattern (pattern))))))
156 (make-pattern (pattern)))))))
158 (/show0 "filesys.lisp 160")
160 (defun extract-name-type-and-version (namestr start end)
161 (declare (type simple-base-string namestr)
162 (type index start end))
163 (let* ((last-dot (position #\. namestr :start (1+ start) :end end
165 (second-to-last-dot (and last-dot
166 (position #\. namestr :start (1+ start)
167 :end last-dot :from-end t)))
169 ;; If there is a second-to-last dot, check to see whether there is
170 ;; a valid version after the last dot.
171 (when second-to-last-dot
172 (cond ((and (= (+ last-dot 2) end)
173 (char= (schar namestr (1+ last-dot)) #\*))
174 (setf version :wild))
175 ((and (< (1+ last-dot) end)
176 (do ((index (1+ last-dot) (1+ index)))
178 (unless (char<= #\0 (schar namestr index) #\9)
181 (parse-integer namestr :start (1+ last-dot) :end end)))
183 (setf second-to-last-dot nil))))
184 (cond (second-to-last-dot
185 (values (maybe-make-pattern namestr start second-to-last-dot)
186 (maybe-make-pattern namestr
187 (1+ second-to-last-dot)
191 (values (maybe-make-pattern namestr start last-dot)
192 (maybe-make-pattern namestr (1+ last-dot) end)
195 (values (maybe-make-pattern namestr start end)
199 (/show0 "filesys.lisp 200")
201 ;;; Take a string and return a list of cons cells that mark the char
202 ;;; separated subseq. The first value is true if absolute directories
204 (defun split-at-slashes (namestr start end)
205 (declare (type simple-base-string namestr)
206 (type index start end))
207 (let ((absolute (and (/= start end)
208 (char= (schar namestr start) #\/))))
211 ;; Next, split the remainder into slash-separated chunks.
214 (let ((slash (position #\/ namestr :start start :end end)))
215 (pieces (cons start (or slash end)))
218 (setf start (1+ slash))))
219 (values absolute (pieces)))))
221 (defun maybe-extract-search-list (namestr start end)
222 (declare (type simple-base-string namestr)
223 (type index start end))
225 (do ((index start (1+ index)))
230 (case (schar namestr index)
234 (return (values (remove-backslashes namestr start index)
237 (defun parse-unix-namestring (namestr start end)
238 (declare (type simple-base-string namestr)
239 (type index start end))
240 (multiple-value-bind (absolute pieces) (split-at-slashes namestr start end)
241 (let ((search-list (if absolute
243 (let ((first (car pieces)))
244 (multiple-value-bind (search-list new-start)
245 (maybe-extract-search-list namestr
250 (setf (car first) new-start))
252 (multiple-value-bind (name type version)
253 (let* ((tail (car (last pieces)))
254 (tail-start (car tail))
255 (tail-end (cdr tail)))
256 (unless (= tail-start tail-end)
257 (setf pieces (butlast pieces))
258 (extract-name-type-and-version namestr tail-start tail-end)))
259 ;; PVE: make sure there are no illegal characters in
260 ;; the name, illegal being (code-char 0) and #\/
262 (when (and (stringp name)
263 (find-if #'(lambda (x) (or (char= x (code-char 0))
266 (error 'parse-error))
268 ;; Now we have everything we want. So return it.
269 (values nil ; no host for unix namestrings.
270 nil ; no devices for unix namestrings.
273 (dirs (intern-search-list search-list)))
274 (dolist (piece pieces)
275 (let ((piece-start (car piece))
276 (piece-end (cdr piece)))
277 (unless (= piece-start piece-end)
278 (cond ((string= namestr ".." :start1 piece-start
281 ((string= namestr "**" :start1 piece-start
283 (dirs :wild-inferiors))
285 (dirs (maybe-make-pattern namestr
289 (cons :absolute (dirs)))
291 (cons :relative (dirs)))
298 (/show0 "filesys.lisp 300")
300 (defun unparse-unix-host (pathname)
301 (declare (type pathname pathname)
305 (defun unparse-unix-piece (thing)
309 (let* ((srclen (length thing))
312 (case (schar thing i)
315 (let ((result (make-string dstlen))
317 (dotimes (src srclen)
318 (let ((char (schar thing src)))
321 (setf (schar result dst) #\\)
323 (setf (schar result dst) char)
328 (dolist (piece (pattern-pieces thing))
342 (strings (cdr piece))
345 (error "invalid pattern piece: ~S" piece))))))
350 (defun unparse-unix-directory-list (directory)
351 (declare (type list directory))
354 (ecase (pop directory)
356 (cond ((search-list-p (car directory))
357 (pieces (search-list-name (pop directory)))
364 (dolist (dir directory)
369 (error ":BACK cannot be represented in namestrings."))
370 ((member :wild-inferiors)
372 ((or simple-string pattern)
373 (pieces (unparse-unix-piece dir))
376 (error "invalid directory component: ~S" dir)))))
377 (apply #'concatenate 'simple-string (pieces))))
379 (defun unparse-unix-directory (pathname)
380 (declare (type pathname pathname))
381 (unparse-unix-directory-list (%pathname-directory pathname)))
383 (defun unparse-unix-file (pathname)
384 (declare (type pathname pathname))
386 (let* ((name (%pathname-name pathname))
387 (type (%pathname-type pathname))
388 (type-supplied (not (or (null type) (eq type :unspecific)))))
389 ;; Note: by ANSI 19.3.1.1.5, we ignore the version slot when
390 ;; translating logical pathnames to a filesystem without
391 ;; versions (like Unix).
393 (strings (unparse-unix-piece name)))
396 (error "cannot specify the type without a file: ~S" pathname))
398 (strings (unparse-unix-piece type))))
399 (apply #'concatenate 'simple-string (strings))))
401 (/show0 "filesys.lisp 406")
403 (defun unparse-unix-namestring (pathname)
404 (declare (type pathname pathname))
405 (concatenate 'simple-string
406 (unparse-unix-directory pathname)
407 (unparse-unix-file pathname)))
409 (defun unparse-unix-enough (pathname defaults)
410 (declare (type pathname pathname defaults))
412 (error "~S cannot be represented relative to ~S."
415 (let* ((pathname-directory (%pathname-directory pathname))
416 (defaults-directory (%pathname-directory defaults))
417 (prefix-len (length defaults-directory))
419 (cond ((and (> prefix-len 1)
420 (>= (length pathname-directory) prefix-len)
421 (compare-component (subseq pathname-directory
424 ;; Pathname starts with a prefix of default. So
425 ;; just use a relative directory from then on out.
426 (cons :relative (nthcdr prefix-len pathname-directory)))
427 ((eq (car pathname-directory) :absolute)
428 ;; We are an absolute pathname, so we can just use it.
431 ;; We are a relative directory. So we lose.
433 (strings (unparse-unix-directory-list result-dir)))
434 (let* ((pathname-version (%pathname-version pathname))
435 (version-needed (and pathname-version
436 (not (eq pathname-version :newest))))
437 (pathname-type (%pathname-type pathname))
438 (type-needed (or version-needed
440 (not (eq pathname-type :unspecific)))))
441 (pathname-name (%pathname-name pathname))
442 (name-needed (or type-needed
444 (not (compare-component pathname-name
448 (unless pathname-name (lose))
449 (strings (unparse-unix-piece pathname-name)))
451 (when (or (null pathname-type) (eq pathname-type :unspecific))
454 (strings (unparse-unix-piece pathname-type)))
456 (typecase pathname-version
460 (strings (format nil ".~D" pathname-version)))
463 (apply #'concatenate 'simple-string (strings)))))
465 (/show0 "filesys.lisp 471")
467 (def!struct (unix-host
468 (:make-load-form-fun make-unix-host-load-form)
470 (parse #'parse-unix-namestring)
471 (unparse #'unparse-unix-namestring)
472 (unparse-host #'unparse-unix-host)
473 (unparse-directory #'unparse-unix-directory)
474 (unparse-file #'unparse-unix-file)
475 (unparse-enough #'unparse-unix-enough)
476 (customary-case :lower))))
478 (/show0 "filesys.lisp 486")
480 (defvar *unix-host* (make-unix-host))
482 (/show0 "filesys.lisp 488")
484 (defun make-unix-host-load-form (host)
485 (declare (ignore host))
488 ;;;; wildcard matching stuff
490 (/show0 "filesys.lisp 498")
492 ;;; FIXME: could maybe be EVAL-WHEN (COMPILE EVAL)
494 (defmacro enumerate-matches ((var pathname &optional result
495 &key (verify-existence t)
498 (let ((body-name (gensym)))
500 (flet ((,body-name (,var)
502 (%enumerate-matches (pathname ,pathname)
508 (/show0 "filesys.lisp 500")
510 (defun %enumerate-matches (pathname verify-existence follow-links function)
511 (/show0 "entering %ENUMERATE-MATCHES")
512 (when (pathname-type pathname)
513 (unless (pathname-name pathname)
514 (error "cannot supply a type without a name:~% ~S" pathname)))
515 (when (and (integerp (pathname-version pathname))
516 (member (pathname-type pathname) '(nil :unspecific)))
517 (error "cannot supply a version without a type:~% ~S" pathname))
518 (let ((directory (pathname-directory pathname)))
519 (/show0 "computed DIRECTORY")
521 (ecase (car directory)
523 (/show0 "absolute directory")
524 (%enumerate-directories "/" (cdr directory) pathname
525 verify-existence follow-links
528 (/show0 "relative directory")
529 (%enumerate-directories "" (cdr directory) pathname
530 verify-existence follow-links
532 (%enumerate-files "" pathname verify-existence function))))
534 (defun %enumerate-directories (head tail pathname verify-existence
535 follow-links nodes function)
536 (declare (simple-string head))
537 (macrolet ((unix-xstat (name)
539 (sb!unix:unix-stat ,name)
540 (sb!unix:unix-lstat ,name)))
541 (with-directory-node-noted ((head) &body body)
542 `(multiple-value-bind (res dev ino mode)
544 (when (and res (eql (logand mode sb!unix:s-ifmt)
546 (let ((nodes (cons (cons dev ino) nodes)))
548 (do-directory-entries ((name directory) &body body)
549 `(let ((dir (sb!unix:open-dir ,directory)))
553 (let ((,name (sb!unix:read-dir dir)))
556 ((string= ,name "."))
557 ((string= ,name ".."))
560 (sb!unix:close-dir dir))))))
562 (let ((piece (car tail)))
565 (let ((head (concatenate 'string head piece)))
566 (with-directory-node-noted (head)
567 (%enumerate-directories (concatenate 'string head "/")
569 verify-existence follow-links
571 ((member :wild-inferiors)
572 (%enumerate-directories head (rest tail) pathname
573 verify-existence follow-links
575 (do-directory-entries (name head)
576 (let ((subdir (concatenate 'string head name)))
577 (multiple-value-bind (res dev ino mode)
579 (declare (type (or fixnum null) mode))
580 (when (and res (eql (logand mode sb!unix:s-ifmt)
582 (unless (dolist (dir nodes nil)
583 (when (and (eql (car dir) dev)
586 (let ((nodes (cons (cons dev ino) nodes))
587 (subdir (concatenate 'string subdir "/")))
588 (%enumerate-directories subdir tail pathname
589 verify-existence follow-links
590 nodes function))))))))
591 ((or pattern (member :wild))
592 (do-directory-entries (name head)
593 (when (or (eq piece :wild) (pattern-matches piece name))
594 (let ((subdir (concatenate 'string head name)))
595 (multiple-value-bind (res dev ino mode)
597 (declare (type (or fixnum null) mode))
599 (eql (logand mode sb!unix:s-ifmt)
601 (let ((nodes (cons (cons dev ino) nodes))
602 (subdir (concatenate 'string subdir "/")))
603 (%enumerate-directories subdir (rest tail) pathname
604 verify-existence follow-links
605 nodes function))))))))
607 (let ((head (concatenate 'string head "..")))
608 (with-directory-node-noted (head)
609 (%enumerate-directories (concatenate 'string head "/")
611 verify-existence follow-links
613 (%enumerate-files head pathname verify-existence function))))
615 (defun %enumerate-files (directory pathname verify-existence function)
616 (declare (simple-string directory))
617 (/show0 "entering %ENUMERATE-FILES")
618 (let ((name (%pathname-name pathname))
619 (type (%pathname-type pathname))
620 (version (%pathname-version pathname)))
621 (/show0 "computed NAME, TYPE, and VERSION")
622 (cond ((member name '(nil :unspecific))
623 (/show0 "UNSPECIFIC, more or less")
624 (when (or (not verify-existence)
625 (sb!unix:unix-file-kind directory))
626 (funcall function directory)))
627 ((or (pattern-p name)
631 (/show0 "WILD, more or less")
632 (let ((dir (sb!unix:open-dir directory)))
636 (/show0 "at head of LOOP")
637 (let ((file (sb!unix:read-dir dir)))
639 (unless (or (string= file ".")
642 (file-name file-type file-version)
643 (let ((*ignore-wildcards* t))
644 (extract-name-type-and-version
645 file 0 (length file)))
646 (when (and (components-match file-name name)
647 (components-match file-type type)
648 (components-match file-version
655 (sb!unix:close-dir dir)))))
657 (/show0 "default case")
658 (let ((file (concatenate 'string directory name)))
659 (/show0 "computed basic FILE=..")
660 (/primitive-print file)
661 (unless (or (null type) (eq type :unspecific))
662 (/show0 "tweaking FILE for more-or-less-:UNSPECIFIC case")
663 (setf file (concatenate 'string file "." type)))
664 (unless (member version '(nil :newest :wild))
665 (/show0 "tweaking FILE for more-or-less-:WILD case")
666 (setf file (concatenate 'string file "."
667 (quick-integer-to-string version))))
668 (/show0 "finished possibly tweaking FILE=..")
669 (/primitive-print file)
670 (when (or (not verify-existence)
671 (sb!unix:unix-file-kind file t))
672 (/show0 "calling FUNCTION on FILE")
673 (funcall function file)))))))
675 (/show0 "filesys.lisp 603")
677 ;;; FIXME: Why do we need this?
678 (defun quick-integer-to-string (n)
679 (declare (type integer n))
680 (cond ((not (fixnump n))
681 (write-to-string n :base 10 :radix nil))
685 (concatenate 'simple-string "-"
686 (the simple-string (quick-integer-to-string (- n)))))
688 (do* ((len (1+ (truncate (integer-length n) 3)))
689 (res (make-string len))
695 (replace res res :start2 i :end2 len)
696 (shrink-vector res (- len i)))
697 (declare (simple-string res)
699 (multiple-value-setq (q r) (truncate q 10))
700 (setf (schar res i) (schar "0123456789" r))))))
704 (defun unix-namestring (pathname &optional (for-input t) executable-only)
706 "Convert PATHNAME into a string that can be used with UNIX system calls.
707 Search-lists and wild-cards are expanded."
708 ;; toy@rtp.ericsson.se: Let unix-namestring also handle logical
710 ;; FIXME: What does this ^ mean? A bug? A remark on a change already made?
711 (let ((path (let ((lpn (pathname pathname)))
712 (if (typep lpn 'logical-pathname)
713 (namestring (translate-logical-pathname lpn))
715 (enumerate-search-list
718 (enumerate-matches (name pathname nil :verify-existence for-input)
719 (when (or (not executable-only)
720 (and (eq (sb!unix:unix-file-kind name)
722 (sb!unix:unix-access name
725 (let ((names (names)))
728 (error 'simple-file-error
729 :format-control "~S is ambiguous:~{~% ~A~}"
730 :format-arguments (list pathname names)))
731 (return (car names))))))))
733 ;;;; TRUENAME and PROBE-FILE
735 ;;; Another silly file function trivially different from another function.
736 (defun truename (pathname)
738 "Return the pathname for the actual file described by the pathname
739 An error of type file-error is signalled if no such file exists,
740 or the pathname is wild."
741 (if (wild-pathname-p pathname)
742 (error 'simple-file-error
743 :format-control "bad place for a wild pathname"
745 (let ((result (probe-file pathname)))
747 (error 'simple-file-error
749 :format-control "The file ~S does not exist."
750 :format-arguments (list (namestring pathname))))
753 ;;; If PATHNAME exists, return its truename, otherwise NIL.
754 (defun probe-file (pathname)
756 "Return a pathname which is the truename of the file if it exists, NIL
757 otherwise. An error of type FILE-ERROR is signaled if pathname is wild."
758 (if (wild-pathname-p pathname)
759 (error 'simple-file-error
761 :format-control "bad place for a wild pathname")
762 (let ((namestring (unix-namestring pathname t)))
763 (when (and namestring (sb!unix:unix-file-kind namestring))
764 (let ((truename (sb!unix:unix-resolve-links
765 (sb!unix:unix-maybe-prepend-current-directory
768 (let ((*ignore-wildcards* t))
769 (pathname (sb!unix:unix-simplify-pathname truename)))))))))
771 ;;;; miscellaneous other operations
773 (/show0 "filesys.lisp 700")
775 (defun rename-file (file new-name)
777 "Rename File to have the specified New-Name. If file is a stream open to a
778 file, then the associated file is renamed."
779 (let* ((original (truename file))
780 (original-namestring (unix-namestring original t))
781 (new-name (merge-pathnames new-name original))
782 (new-namestring (unix-namestring new-name nil)))
783 (unless new-namestring
784 (error 'simple-file-error
786 :format-control "~S can't be created."
787 :format-arguments (list new-name)))
788 (multiple-value-bind (res error)
789 (sb!unix:unix-rename original-namestring new-namestring)
791 (error 'simple-file-error
793 :format-control "failed to rename ~A to ~A: ~A"
794 :format-arguments (list original new-name
795 (sb!unix:get-unix-error-msg error))))
797 (file-name file new-namestring))
798 (values new-name original (truename new-name)))))
800 (defun delete-file (file)
802 "Delete the specified file."
803 (let ((namestring (unix-namestring file t)))
805 (close file :abort t))
807 (error 'simple-file-error
809 :format-control "~S doesn't exist."
810 :format-arguments (list file)))
812 (multiple-value-bind (res err) (sb!unix:unix-unlink namestring)
814 (error 'simple-file-error
816 :format-control "could not delete ~A: ~A"
817 :format-arguments (list namestring
818 (sb!unix:get-unix-error-msg err))))))
821 ;;; Return Home:, which is set up for us at initialization time.
822 (defun user-homedir-pathname (&optional host)
824 "Returns the home directory of the logged in user as a pathname.
825 This is obtained from the logical name \"home:\"."
826 (declare (ignore host))
827 ;; Note: CMU CL did #P"home:" here instead of using a call to
828 ;; PATHNAME. Delaying construction of the pathname until we're
829 ;; running in a target Lisp lets us avoid figuring out how to dump
830 ;; cross-compilation host Lisp PATHNAME objects into a target Lisp
831 ;; object file. It also might have a small positive effect on
832 ;; efficiency, in that we don't allocate a PATHNAME we don't need,
833 ;; but it it could also have a larger negative effect. Hopefully
834 ;; it'll be OK. -- WHN 19990714
837 (defun file-write-date (file)
839 "Return file's creation date, or NIL if it doesn't exist.
840 An error of type file-error is signaled if file is a wild pathname"
841 (if (wild-pathname-p file)
842 ;; FIXME: This idiom appears many times in this file. Perhaps it
843 ;; should turn into (CANNOT-BE-WILD-PATHNAME FILE). (C-B-W-P
844 ;; should be a macro, not a function, so that the error message
845 ;; is reported as coming from e.g. FILE-WRITE-DATE instead of
846 ;; from CANNOT-BE-WILD-PATHNAME itself.)
847 (error 'simple-file-error
849 :format-control "bad place for a wild pathname")
850 (let ((name (unix-namestring file t)))
853 (res dev ino mode nlink uid gid rdev size atime mtime)
854 (sb!unix:unix-stat name)
855 (declare (ignore dev ino mode nlink uid gid rdev size atime))
857 (+ unix-to-universal-time mtime)))))))
859 (defun file-author (file)
861 "Returns the file author as a string, or nil if the author cannot be
862 determined. Signals an error of type file-error if file doesn't exist,
863 or file is a wild pathname."
864 (if (wild-pathname-p file)
865 (error 'simple-file-error
867 "bad place for a wild pathname")
868 (let ((name (unix-namestring (pathname file) t)))
870 (error 'simple-file-error
872 :format-control "~S doesn't exist."
873 :format-arguments (list file)))
874 (multiple-value-bind (winp dev ino mode nlink uid)
875 (sb!unix:unix-stat name)
876 (declare (ignore dev ino mode nlink))
877 (if winp (lookup-login-name uid))))))
881 (/show0 "filesys.lisp 800")
883 (defun directory (pathname &key (all t) (check-for-subdirs t)
886 "Returns a list of pathnames, one for each file that matches the given
887 pathname. Supplying :ALL as NIL causes this to ignore Unix dot files. This
888 never includes Unix dot and dot-dot in the result. If :FOLLOW-LINKS is NIL,
889 then symbolic links in the result are not expanded. This is not the
890 default because TRUENAME does follow links, and the result pathnames are
891 defined to be the TRUENAME of the pathname (the truename of a link may well
892 be in another directory.)"
894 (enumerate-search-list
895 (pathname (merge-pathnames pathname
896 (make-pathname :name :wild
899 (enumerate-matches (name pathname)
901 (let ((slash (position #\/ name :from-end t)))
903 (= (1+ slash) (length name))
904 (char/= (schar name (1+ slash)) #\.))))
905 (push name results))))
906 (let ((*ignore-wildcards* t))
907 (mapcar (lambda (name)
908 (let ((name (if (and check-for-subdirs
909 (eq (sb!unix:unix-file-kind name)
911 (concatenate 'string name "/")
913 (if follow-links (truename name) (pathname name))))
914 (sort (delete-duplicates results :test #'string=) #'string<)))))
916 ;;;; translating Unix uid's
918 ;;;; FIXME: should probably move into unix.lisp
920 (defvar *uid-hash-table* (make-hash-table)
922 "hash table for keeping track of uid's and login names")
924 (/show0 "filesys.lisp 844")
926 ;;; LOOKUP-LOGIN-NAME translates a user id into a login name. Previous
927 ;;; lookups are cached in a hash table since groveling the passwd(s)
928 ;;; files is somewhat expensive. The table may hold NIL for id's that
929 ;;; cannot be looked up since this keeps the files from having to be
930 ;;; searched in their entirety each time this id is translated.
931 (defun lookup-login-name (uid)
932 (multiple-value-bind (login-name foundp) (gethash uid *uid-hash-table*)
935 (setf (gethash uid *uid-hash-table*)
936 (get-group-or-user-name :user uid)))))
938 ;;; GET-GROUP-OR-USER-NAME first tries "/etc/passwd" ("/etc/group")
939 ;;; since it is a much smaller file, contains all the local id's, and
940 ;;; most uses probably involve id's on machines one would login into.
941 ;;; Then if necessary, we look in "/etc/passwds" ("/etc/groups") which
942 ;;; is really long and has to be fetched over the net.
944 ;;; FIXME: Now that we no longer have lookup-group-name, we no longer need
945 ;;; the GROUP-OR-USER argument.
946 (defun get-group-or-user-name (group-or-user id)
948 "Returns the simple-string user or group name of the user whose uid or gid
949 is id, or NIL if no such user or group exists. Group-or-user is either
951 (let ((id-string (let ((*print-base* 10)) (prin1-to-string id))))
952 (declare (simple-string id-string))
953 (multiple-value-bind (file1 file2)
955 (:group (values "/etc/group" "/etc/groups"))
956 (:user (values "/etc/passwd" "/etc/passwd")))
957 (or (get-group-or-user-name-aux id-string file1)
958 (get-group-or-user-name-aux id-string file2)))))
960 ;;; FIXME: Isn't there now a POSIX routine to parse the passwd file?
961 ;;; getpwent? getpwuid?
962 (defun get-group-or-user-name-aux (id-string passwd-file)
963 (with-open-file (stream passwd-file)
965 (let ((entry (read-line stream nil)))
966 (unless entry (return nil))
967 (let ((name-end (position #\: (the simple-string entry)
970 (let ((id-start (position #\: (the simple-string entry)
971 :start (1+ name-end) :test #'char=)))
974 (let ((id-end (position #\: (the simple-string entry)
975 :start id-start :test #'char=)))
977 (string= id-string entry
978 :start2 id-start :end2 id-end))
979 (return (subseq entry 0 name-end))))))))))))
981 (/show0 "filesys.lisp 899")
983 ;;; predicate to order pathnames by; goes by name
984 (defun pathname-order (x y)
985 (let ((xn (%pathname-name x))
986 (yn (%pathname-name y)))
988 (let ((res (string-lessp xn yn)))
989 (cond ((not res) nil)
990 ((= res (length (the simple-string xn))) t)
991 ((= res (length (the simple-string yn))) nil)
995 (defun default-directory ()
997 "Returns the pathname for the default directory. This is the place where
998 a file will be written if no directory is specified. This may be changed
1000 (multiple-value-bind (gr dir-or-error) (sb!unix:unix-current-directory)
1002 (let ((*ignore-wildcards* t))
1003 (pathname (concatenate 'simple-string dir-or-error "/")))
1004 (error dir-or-error))))
1006 (defun %set-default-directory (new-val)
1007 (let ((namestring (unix-namestring new-val t)))
1009 (error "~S doesn't exist." new-val))
1010 (multiple-value-bind (gr error) (sb!unix:unix-chdir namestring)
1012 (setf (search-list "default:") (default-directory))
1013 (error (sb!unix:get-unix-error-msg error))))
1016 (/show0 "filesys.lisp 934")
1018 (/show0 "entering what used to be !FILESYS-COLD-INIT")
1019 (defvar *default-pathname-defaults*
1020 (%make-pathname *unix-host* nil nil nil nil :newest))
1021 (setf (search-list "default:") (default-directory))
1022 (/show0 "leaving what used to be !FILESYS-COLD-INIT")
1024 (defun ensure-directories-exist (pathspec &key verbose (mode #o777))
1026 "Tests whether the directories containing the specified file
1027 actually exist, and attempts to create them if they do not.
1028 Portable programs should avoid using the :MODE argument."
1029 (let* ((pathname (pathname pathspec))
1030 (pathname (if (typep pathname 'logical-pathname)
1031 (translate-logical-pathname pathname)
1034 (when (wild-pathname-p pathname)
1035 (error 'simple-file-error
1036 :format-control "bad place for a wild pathname"
1037 :pathname pathspec))
1038 (enumerate-search-list (pathname pathname)
1039 (let ((dir (pathname-directory pathname)))
1040 (loop for i from 1 upto (length dir)
1041 do (let ((newpath (make-pathname
1042 :host (pathname-host pathname)
1043 :device (pathname-device pathname)
1044 :directory (subseq dir 0 i))))
1045 (unless (probe-file newpath)
1046 (let ((namestring (namestring newpath)))
1048 (format *standard-output*
1049 "~&creating directory: ~A~%"
1051 (sb!unix:unix-mkdir namestring mode)
1052 (unless (probe-file namestring)
1053 (error 'simple-file-error
1055 :format-control "can't create directory ~A"
1056 :format-arguments (list namestring)))
1057 (setf created-p t)))))
1058 ;; Only the first path in a search-list is considered.
1059 (return (values pathname created-p))))))
1061 (/show0 "filesys.lisp 1000")