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")
17 ;;;; Unix pathname host support
19 ;;; Unix namestrings have the following format:
21 ;;; namestring := [ directory ] [ file [ type [ version ]]]
22 ;;; directory := [ "/" | search-list ] { file "/" }*
23 ;;; search-list := [^:/]*:
25 ;;; type := "." [^/.]*
26 ;;; version := "." ([0-9]+ | "*")
28 ;;; FIXME: Search lists are no longer supported.
30 ;;; Note: this grammar is ambiguous. The string foo.bar.5 can be
31 ;;; parsed as either just the file specified or as specifying the
32 ;;; file, type, and version. Therefore, we use the following rules
33 ;;; when confronted with an ambiguous file.type.version string:
35 ;;; - If the first character is a dot, it's part of the file. It is not
36 ;;; considered a dot in the following rules.
38 ;;; - If there is only one dot, it separates the file and the type.
40 ;;; - If there are multiple dots and the stuff following the last dot
41 ;;; is a valid version, then that is the version and the stuff between
42 ;;; the second to last dot and the last dot is the type.
44 ;;; Wildcard characters:
46 ;;; If the directory, file, type components contain any of the
47 ;;; following characters, it is considered part of a wildcard pattern
48 ;;; and has the following meaning.
50 ;;; ? - matches any character
51 ;;; * - matches any zero or more characters.
52 ;;; [abc] - matches any of a, b, or c.
53 ;;; {str1,str2,...,strn} - matches any of str1, str2, ..., or strn.
55 ;;; Any of these special characters can be preceded by a backslash to
56 ;;; cause it to be treated as a regular character.
57 (defun remove-backslashes (namestr start end)
59 "Remove any occurrences of #\\ from the string because we've already
60 checked for whatever they may have protected."
61 (declare (type simple-base-string namestr)
62 (type index start end))
63 (let* ((result (make-string (- end start)))
66 (do ((src start (1+ src)))
69 (setf (schar result dst) (schar namestr src))
73 (let ((char (schar namestr src)))
74 (cond ((char= char #\\)
77 (setf (schar result dst) char)
80 (error 'namestring-parse-error
81 :complaint "backslash in a bad place"
84 (shrink-vector result dst)))
86 (defvar *ignore-wildcards* nil)
88 (/show0 "filesys.lisp 86")
90 (defun maybe-make-pattern (namestr start end)
91 (declare (type simple-base-string namestr)
92 (type index start end))
93 (if *ignore-wildcards*
94 (subseq namestr start end)
98 (last-regular-char nil)
100 (flet ((flush-pending-regulars ()
101 (when last-regular-char
102 (pattern (if any-quotes
103 (remove-backslashes namestr
106 (subseq namestr last-regular-char index)))
107 (setf any-quotes nil)
108 (setf last-regular-char nil))))
112 (let ((char (schar namestr index)))
119 (unless last-regular-char
120 (setf last-regular-char index))
123 (flush-pending-regulars)
124 (pattern :single-char-wild)
127 (flush-pending-regulars)
128 (pattern :multi-char-wild)
131 (flush-pending-regulars)
133 (position #\] namestr :start index :end end)))
134 (unless close-bracket
135 (error 'namestring-parse-error
136 :complaint "#\\[ with no corresponding #\\]"
139 (pattern (list :character-set
143 (setf index (1+ close-bracket))))
145 (unless last-regular-char
146 (setf last-regular-char index))
148 (flush-pending-regulars)))
149 (cond ((null (pattern))
151 ((null (cdr (pattern)))
152 (let ((piece (first (pattern))))
154 ((member :multi-char-wild) :wild)
155 (simple-string piece)
157 (make-pattern (pattern))))))
159 (make-pattern (pattern)))))))
161 (/show0 "filesys.lisp 160")
163 (defun extract-name-type-and-version (namestr start end)
164 (declare (type simple-base-string namestr)
165 (type index start end))
166 (let* ((last-dot (position #\. namestr :start (1+ start) :end end
168 (second-to-last-dot (and last-dot
169 (position #\. namestr :start (1+ start)
170 :end last-dot :from-end t)))
172 ;; If there is a second-to-last dot, check to see whether there is a valid
173 ;; version after the last dot.
174 (when second-to-last-dot
175 (cond ((and (= (+ last-dot 2) end)
176 (char= (schar namestr (1+ last-dot)) #\*))
177 (setf version :wild))
178 ((and (< (1+ last-dot) end)
179 (do ((index (1+ last-dot) (1+ index)))
181 (unless (char<= #\0 (schar namestr index) #\9)
184 (parse-integer namestr :start (1+ last-dot) :end end)))
186 (setf second-to-last-dot nil))))
187 (cond (second-to-last-dot
188 (values (maybe-make-pattern namestr start second-to-last-dot)
189 (maybe-make-pattern namestr
190 (1+ second-to-last-dot)
194 (values (maybe-make-pattern namestr start last-dot)
195 (maybe-make-pattern namestr (1+ last-dot) end)
198 (values (maybe-make-pattern namestr start end)
202 (/show0 "filesys.lisp 200")
204 ;;; Take a string and return a list of cons cells that mark the char
205 ;;; separated subseq. The first value t if absolute directories location.
206 (defun split-at-slashes (namestr start end)
207 (declare (type simple-base-string namestr)
208 (type index start end))
209 (let ((absolute (and (/= start end)
210 (char= (schar namestr start) #\/))))
213 ;; Next, split the remainder into slash-separated chunks.
216 (let ((slash (position #\/ namestr :start start :end end)))
217 (pieces (cons start (or slash end)))
220 (setf start (1+ slash))))
221 (values absolute (pieces)))))
223 (defun maybe-extract-search-list (namestr start end)
224 (declare (type simple-base-string namestr)
225 (type index start end))
227 (do ((index start (1+ index)))
232 (case (schar namestr index)
236 (return (values (remove-backslashes namestr start index)
239 (defun parse-unix-namestring (namestr start end)
240 (declare (type simple-base-string namestr)
241 (type index start end))
242 (multiple-value-bind (absolute pieces) (split-at-slashes namestr start end)
243 (let ((search-list (if absolute
245 (let ((first (car pieces)))
246 (multiple-value-bind (search-list new-start)
247 (maybe-extract-search-list namestr
252 (setf (car first) new-start))
254 (multiple-value-bind (name type version)
255 (let* ((tail (car (last pieces)))
256 (tail-start (car tail))
257 (tail-end (cdr tail)))
258 (unless (= tail-start tail-end)
259 (setf pieces (butlast pieces))
260 (extract-name-type-and-version namestr tail-start tail-end)))
261 ;; PVE: make sure there are no illegal characters in
262 ;; the name, illegal being (code-char 0) and #\/
264 (when (and (stringp name)
265 (find-if #'(lambda (x) (or (char= x (code-char 0))
268 (error 'parse-error))
270 ;; Now we have everything we want. So return it.
271 (values nil ; no host for unix namestrings.
272 nil ; no devices for unix namestrings.
275 (dirs (intern-search-list search-list)))
276 (dolist (piece pieces)
277 (let ((piece-start (car piece))
278 (piece-end (cdr piece)))
279 (unless (= piece-start piece-end)
280 (cond ((string= namestr ".." :start1 piece-start
283 ((string= namestr "**" :start1 piece-start
285 (dirs :wild-inferiors))
287 (dirs (maybe-make-pattern namestr
291 (cons :absolute (dirs)))
293 (cons :relative (dirs)))
300 (/show0 "filesys.lisp 300")
302 (defun unparse-unix-host (pathname)
303 (declare (type pathname pathname)
307 (defun unparse-unix-piece (thing)
311 (let* ((srclen (length thing))
314 (case (schar thing i)
317 (let ((result (make-string dstlen))
319 (dotimes (src srclen)
320 (let ((char (schar thing src)))
323 (setf (schar result dst) #\\)
325 (setf (schar result dst) char)
330 (dolist (piece (pattern-pieces thing))
344 (strings (cdr piece))
347 (error "invalid pattern piece: ~S" piece))))))
352 (defun unparse-unix-directory-list (directory)
353 (declare (type list directory))
356 (ecase (pop directory)
358 (cond ((search-list-p (car directory))
359 (pieces (search-list-name (pop directory)))
366 (dolist (dir directory)
371 (error ":BACK cannot be represented in namestrings."))
372 ((member :wild-inferiors)
374 ((or simple-string pattern)
375 (pieces (unparse-unix-piece dir))
378 (error "invalid directory component: ~S" dir)))))
379 (apply #'concatenate 'simple-string (pieces))))
381 (defun unparse-unix-directory (pathname)
382 (declare (type pathname pathname))
383 (unparse-unix-directory-list (%pathname-directory pathname)))
385 (defun unparse-unix-file (pathname)
386 (declare (type pathname pathname))
388 (let* ((name (%pathname-name pathname))
389 (type (%pathname-type pathname))
390 (type-supplied (not (or (null type) (eq type :unspecific))))
391 (version (%pathname-version pathname))
392 (version-supplied (not (or (null version) (eq version :newest)))))
394 (strings (unparse-unix-piece name)))
397 (error "cannot specify the type without a file: ~S" pathname))
399 (strings (unparse-unix-piece type)))
400 (when version-supplied
401 (unless type-supplied
402 (error "cannot specify the version without a type: ~S" pathname))
403 (strings (if (eq version :wild)
405 (format nil ".~D" version)))))
406 (apply #'concatenate 'simple-string (strings))))
408 (/show0 "filesys.lisp 406")
410 (defun unparse-unix-namestring (pathname)
411 (declare (type pathname pathname))
412 (concatenate 'simple-string
413 (unparse-unix-directory pathname)
414 (unparse-unix-file pathname)))
416 (defun unparse-unix-enough (pathname defaults)
417 (declare (type pathname pathname defaults))
419 (error "~S cannot be represented relative to ~S."
422 (let* ((pathname-directory (%pathname-directory pathname))
423 (defaults-directory (%pathname-directory defaults))
424 (prefix-len (length defaults-directory))
426 (cond ((and (> prefix-len 1)
427 (>= (length pathname-directory) prefix-len)
428 (compare-component (subseq pathname-directory
431 ;; Pathname starts with a prefix of default. So
432 ;; just use a relative directory from then on out.
433 (cons :relative (nthcdr prefix-len pathname-directory)))
434 ((eq (car pathname-directory) :absolute)
435 ;; We are an absolute pathname, so we can just use it.
438 ;; We are a relative directory. So we lose.
440 (strings (unparse-unix-directory-list result-dir)))
441 (let* ((pathname-version (%pathname-version pathname))
442 (version-needed (and pathname-version
443 (not (eq pathname-version :newest))))
444 (pathname-type (%pathname-type pathname))
445 (type-needed (or version-needed
447 (not (eq pathname-type :unspecific)))))
448 (pathname-name (%pathname-name pathname))
449 (name-needed (or type-needed
451 (not (compare-component pathname-name
455 (unless pathname-name (lose))
456 (strings (unparse-unix-piece pathname-name)))
458 (when (or (null pathname-type) (eq pathname-type :unspecific))
461 (strings (unparse-unix-piece pathname-type)))
463 (typecase pathname-version
467 (strings (format nil ".~D" pathname-version)))
470 (apply #'concatenate 'simple-string (strings)))))
472 (/show0 "filesys.lisp 471")
474 (def!struct (unix-host
475 (:make-load-form-fun make-unix-host-load-form)
477 (parse #'parse-unix-namestring)
478 (unparse #'unparse-unix-namestring)
479 (unparse-host #'unparse-unix-host)
480 (unparse-directory #'unparse-unix-directory)
481 (unparse-file #'unparse-unix-file)
482 (unparse-enough #'unparse-unix-enough)
483 (customary-case :lower))))
485 (/show0 "filesys.lisp 486")
487 (defvar *unix-host* (make-unix-host))
489 (/show0 "filesys.lisp 488")
491 (defun make-unix-host-load-form (host)
492 (declare (ignore host))
495 ;;;; wildcard matching stuff
497 (/show0 "filesys.lisp 498")
499 ;;; FIXME: could maybe be EVAL-WHEN (COMPILE EVAL)
500 (defmacro enumerate-matches ((var pathname &optional result
501 &key (verify-existence t))
503 (let ((body-name (gensym)))
505 (flet ((,body-name (,var)
507 (%enumerate-matches (pathname ,pathname)
512 (/show0 "filesys.lisp 500")
514 (defun %enumerate-matches (pathname verify-existence function)
515 (/show0 "entering %ENUMERATE-MATCHES")
516 (when (pathname-type pathname)
517 (unless (pathname-name pathname)
518 (error "cannot supply a type without a name:~% ~S" pathname)))
519 (when (and (integerp (pathname-version pathname))
520 (member (pathname-type pathname) '(nil :unspecific)))
521 (error "cannot supply a version without a type:~% ~S" pathname))
522 (let ((directory (pathname-directory pathname)))
523 (/show0 "computed DIRECTORY")
525 (ecase (car directory)
527 (/show0 "absolute directory")
528 (%enumerate-directories "/" (cdr directory) pathname
529 verify-existence function))
531 (/show0 "relative directory")
532 (%enumerate-directories "" (cdr directory) pathname
533 verify-existence function)))
534 (%enumerate-files "" pathname verify-existence function))))
536 (defun %enumerate-directories (head tail pathname verify-existence function)
537 (declare (simple-string head))
539 (let ((piece (car tail)))
542 (%enumerate-directories (concatenate 'string head piece "/")
543 (cdr tail) pathname verify-existence
545 ((or pattern (member :wild :wild-inferiors))
546 (let ((dir (sb!unix:open-dir head)))
550 (let ((name (sb!unix:read-dir dir)))
554 ((string= name ".."))
555 ((pattern-matches piece name)
556 (let ((subdir (concatenate 'string
558 (when (eq (sb!unix:unix-file-kind subdir)
560 (%enumerate-directories
561 subdir (cdr tail) pathname verify-existence
563 (sb!unix:close-dir dir)))))
565 (%enumerate-directories (concatenate 'string head "../")
566 (cdr tail) pathname verify-existence
568 (%enumerate-files head pathname verify-existence function)))
570 ;;; REMOVEME after finding bug.
571 #!+sb-show (defvar *show-directory*)
572 #!+sb-show (defvar *show-name*)
574 (defun %enumerate-files (directory pathname verify-existence function)
575 (declare (simple-string directory))
576 (/show0 "entering %ENUMERATE-FILES")
577 (let ((name (%pathname-name pathname))
578 (type (%pathname-type pathname))
579 (version (%pathname-version pathname)))
580 (/show0 "computed NAME, TYPE, and VERSION")
581 (cond ((member name '(nil :unspecific))
582 (/show0 "UNSPECIFIC, more or less")
583 (when (or (not verify-existence)
584 (sb!unix:unix-file-kind directory))
585 (funcall function directory)))
586 ((or (pattern-p name)
590 (/show0 "WILD, more or less")
591 (let ((dir (sb!unix:open-dir directory)))
595 (/show0 "at head of LOOP")
596 (let ((file (sb!unix:read-dir dir)))
598 (unless (or (string= file ".")
601 (file-name file-type file-version)
602 (let ((*ignore-wildcards* t))
603 (extract-name-type-and-version
604 file 0 (length file)))
605 (when (and (components-match file-name name)
606 (components-match file-type type)
607 (components-match file-version
614 (sb!unix:close-dir dir)))))
616 (/show0 "default case")
618 ;; Put DIRECTORY and NAME somewhere we can find them even when
619 ;; things are too screwed up for the debugger.
621 (setf *show-directory* directory
624 (let ((file (concatenate 'string directory name)))
625 (/show0 "computed basic FILE=..")
626 #!+sb-show (%primitive print file)
627 (unless (or (null type) (eq type :unspecific))
628 (/show0 "tweaking FILE for more-or-less-:UNSPECIFIC case")
629 (setf file (concatenate 'string file "." type)))
630 (unless (member version '(nil :newest :wild))
631 (/show0 "tweaking FILE for more-or-less-:WILD case")
632 (setf file (concatenate 'string file "."
633 (quick-integer-to-string version))))
634 (/show0 "finished possibly tweaking FILE=..")
635 #!+sb-show (%primitive print file)
636 (when (or (not verify-existence)
637 (sb!unix:unix-file-kind file t))
638 (/show0 "calling FUNCTION on FILE")
639 (funcall function file)))))))
641 (/show0 "filesys.lisp 603")
643 ;;; FIXME: Why do we need this?
644 (defun quick-integer-to-string (n)
645 (declare (type integer n))
646 (cond ((not (fixnump n))
647 (write-to-string n :base 10 :radix nil))
651 (concatenate 'simple-string "-"
652 (the simple-string (quick-integer-to-string (- n)))))
654 (do* ((len (1+ (truncate (integer-length n) 3)))
655 (res (make-string len))
661 (replace res res :start2 i :end2 len)
662 (shrink-vector res (- len i)))
663 (declare (simple-string res)
665 (multiple-value-setq (q r) (truncate q 10))
666 (setf (schar res i) (schar "0123456789" r))))))
670 (defun unix-namestring (pathname &optional (for-input t) executable-only)
672 "Convert PATHNAME into a string that can be used with UNIX system calls.
673 Search-lists and wild-cards are expanded."
674 ;; toy@rtp.ericsson.se: Let unix-namestring also handle logical
676 ;; FIXME: What does this ^ mean? A bug? A remark on a change already made?
677 (/show0 "entering UNIX-NAMESTRING")
678 (let ((path (let ((lpn (pathname pathname)))
679 (if (typep lpn 'logical-pathname)
680 (namestring (translate-logical-pathname lpn))
682 (/show0 "PATH computed, enumerating search list")
683 (enumerate-search-list
686 (/show0 "collecting NAMES")
687 (enumerate-matches (name pathname nil :verify-existence for-input)
688 (when (or (not executable-only)
689 (and (eq (sb!unix:unix-file-kind name)
691 (sb!unix:unix-access name
694 (/show0 "NAMES collected")
695 (let ((names (names)))
697 (/show0 "NAMES is true.")
699 (/show0 "Alas! CDR NAMES")
700 (error 'simple-file-error
701 :format-control "~S is ambiguous:~{~% ~A~}"
702 :format-arguments (list pathname names)))
703 (/show0 "returning from UNIX-NAMESTRING")
704 (return (car names))))))))
706 ;;;; TRUENAME and PROBE-FILE
708 ;;; Another silly file function trivially different from another function.
709 (defun truename (pathname)
711 "Return the pathname for the actual file described by the pathname
712 An error of type file-error is signalled if no such file exists,
713 or the pathname is wild."
714 (if (wild-pathname-p pathname)
715 (error 'simple-file-error
716 :format-control "bad place for a wild pathname"
718 (let ((result (probe-file pathname)))
720 (error 'simple-file-error
722 :format-control "The file ~S does not exist."
723 :format-arguments (list (namestring pathname))))
726 ;;; If PATHNAME exists, return its truename, otherwise NIL.
727 (defun probe-file (pathname)
729 "Return a pathname which is the truename of the file if it exists, NIL
730 otherwise. An error of type file-error is signaled if pathname is wild."
731 (/show0 "entering PROBE-FILE")
732 (if (wild-pathname-p pathname)
733 (error 'simple-file-error
735 :format-control "bad place for a wild pathname")
736 (let ((namestring (unix-namestring pathname t)))
737 (/show0 "NAMESTRING computed")
738 (when (and namestring (sb!unix:unix-file-kind namestring))
739 (/show0 "NAMESTRING is promising.")
740 (let ((truename (sb!unix:unix-resolve-links
741 (sb!unix:unix-maybe-prepend-current-directory
743 (/show0 "TRUENAME computed")
745 (/show0 "TRUENAME is true.")
746 (let ((*ignore-wildcards* t))
747 (pathname (sb!unix:unix-simplify-pathname truename)))))))))
749 ;;;; miscellaneous other operations
751 (/show0 "filesys.lisp 700")
753 (defun rename-file (file new-name)
755 "Rename File to have the specified New-Name. If file is a stream open to a
756 file, then the associated file is renamed."
757 (let* ((original (truename file))
758 (original-namestring (unix-namestring original t))
759 (new-name (merge-pathnames new-name original))
760 (new-namestring (unix-namestring new-name nil)))
761 (unless new-namestring
762 (error 'simple-file-error
764 :format-control "~S can't be created."
765 :format-arguments (list new-name)))
766 (multiple-value-bind (res error)
767 (sb!unix:unix-rename original-namestring new-namestring)
769 (error 'simple-file-error
771 :format-control "failed to rename ~A to ~A: ~A"
772 :format-arguments (list original new-name
773 (sb!unix:get-unix-error-msg error))))
775 (file-name file new-namestring))
776 (values new-name original (truename new-name)))))
778 (defun delete-file (file)
780 "Delete the specified file."
781 (let ((namestring (unix-namestring file t)))
783 (close file :abort t))
785 (error 'simple-file-error
787 :format-control "~S doesn't exist."
788 :format-arguments (list file)))
790 (multiple-value-bind (res err) (sb!unix:unix-unlink namestring)
792 (error 'simple-file-error
794 :format-control "could not delete ~A: ~A"
795 :format-arguments (list namestring
796 (sb!unix:get-unix-error-msg err))))))
799 ;;; Return Home:, which is set up for us at initialization time.
800 (defun user-homedir-pathname (&optional host)
802 "Returns the home directory of the logged in user as a pathname.
803 This is obtained from the logical name \"home:\"."
804 (declare (ignore host))
805 ;; Note: CMU CL did #P"home:" here instead of using a call to
806 ;; PATHNAME. Delaying construction of the pathname until we're
807 ;; running in a target Lisp lets us avoid figuring out how to dump
808 ;; cross-compilation host Lisp PATHNAME objects into a target Lisp
809 ;; object file. It also might have a small positive effect on
810 ;; efficiency, in that we don't allocate a PATHNAME we don't need,
811 ;; but it it could also have a larger negative effect. Hopefully
812 ;; it'll be OK. -- WHN 19990714
815 (defun file-write-date (file)
817 "Return file's creation date, or NIL if it doesn't exist.
818 An error of type file-error is signaled if file is a wild pathname"
819 (if (wild-pathname-p file)
820 ;; FIXME: This idiom appears many times in this file. Perhaps it
821 ;; should turn into (CANNOT-BE-WILD-PATHNAME FILE). (C-B-W-P
822 ;; should be a macro, not a function, so that the error message
823 ;; is reported as coming from e.g. FILE-WRITE-DATE instead of
824 ;; from CANNOT-BE-WILD-PATHNAME itself.)
825 (error 'simple-file-error
827 :format-control "bad place for a wild pathname")
828 (let ((name (unix-namestring file t)))
831 (res dev ino mode nlink uid gid rdev size atime mtime)
832 (sb!unix:unix-stat name)
833 (declare (ignore dev ino mode nlink uid gid rdev size atime))
835 (+ unix-to-universal-time mtime)))))))
837 (defun file-author (file)
839 "Returns the file author as a string, or nil if the author cannot be
840 determined. Signals an error of type file-error if file doesn't exist,
841 or file is a wild pathname."
842 (if (wild-pathname-p file)
843 (error 'simple-file-error
845 "bad place for a wild pathname")
846 (let ((name (unix-namestring (pathname file) t)))
848 (error 'simple-file-error
850 :format-control "~S doesn't exist."
851 :format-arguments (list file)))
852 (multiple-value-bind (winp dev ino mode nlink uid)
853 (sb!unix:unix-stat name)
854 (declare (ignore dev ino mode nlink))
855 (if winp (lookup-login-name uid))))))
859 (/show0 "filesys.lisp 800")
861 (defun directory (pathname &key (all t) (check-for-subdirs t)
864 "Returns a list of pathnames, one for each file that matches the given
865 pathname. Supplying :ALL as nil causes this to ignore Unix dot files. This
866 never includes Unix dot and dot-dot in the result. If :FOLLOW-LINKS is NIL,
867 then symblolic links in the result are not expanded. This is not the
868 default because TRUENAME does follow links, and the result pathnames are
869 defined to be the TRUENAME of the pathname (the truename of a link may well
870 be in another directory.)"
872 (enumerate-search-list
873 (pathname (merge-pathnames pathname
874 (make-pathname :name :wild
877 (enumerate-matches (name pathname)
879 (let ((slash (position #\/ name :from-end t)))
881 (= (1+ slash) (length name))
882 (char/= (schar name (1+ slash)) #\.))))
883 (push name results))))
884 (let ((*ignore-wildcards* t))
885 (mapcar #'(lambda (name)
886 (let ((name (if (and check-for-subdirs
887 (eq (sb!unix:unix-file-kind name)
889 (concatenate 'string name "/")
891 (if follow-links (truename name) (pathname name))))
892 (sort (delete-duplicates results :test #'string=) #'string<)))))
894 ;;;; translating Unix uid's
896 ;;;; FIXME: should probably move into unix.lisp
898 (defvar *uid-hash-table* (make-hash-table)
900 "hash table for keeping track of uid's and login names")
902 (/show0 "filesys.lisp 844")
904 ;;; LOOKUP-LOGIN-NAME translates a user id into a login name. Previous
905 ;;; lookups are cached in a hash table since groveling the passwd(s)
906 ;;; files is somewhat expensive. The table may hold NIL for id's that
907 ;;; cannot be looked up since this keeps the files from having to be
908 ;;; searched in their entirety each time this id is translated.
909 (defun lookup-login-name (uid)
910 (multiple-value-bind (login-name foundp) (gethash uid *uid-hash-table*)
913 (setf (gethash uid *uid-hash-table*)
914 (get-group-or-user-name :user uid)))))
916 ;;; GET-GROUP-OR-USER-NAME first tries "/etc/passwd" ("/etc/group")
917 ;;; since it is a much smaller file, contains all the local id's, and
918 ;;; most uses probably involve id's on machines one would login into.
919 ;;; Then if necessary, we look in "/etc/passwds" ("/etc/groups") which
920 ;;; is really long and has to be fetched over the net.
922 ;;; FIXME: Now that we no longer have lookup-group-name, we no longer need
923 ;;; the GROUP-OR-USER argument.
924 (defun get-group-or-user-name (group-or-user id)
926 "Returns the simple-string user or group name of the user whose uid or gid
927 is id, or NIL if no such user or group exists. Group-or-user is either
929 (let ((id-string (let ((*print-base* 10)) (prin1-to-string id))))
930 (declare (simple-string id-string))
931 (multiple-value-bind (file1 file2)
933 (:group (values "/etc/group" "/etc/groups"))
934 (:user (values "/etc/passwd" "/etc/passwd")))
935 (or (get-group-or-user-name-aux id-string file1)
936 (get-group-or-user-name-aux id-string file2)))))
938 ;;; FIXME: Isn't there now a POSIX routine to parse the passwd file?
939 ;;; getpwent? getpwuid?
940 (defun get-group-or-user-name-aux (id-string passwd-file)
941 (with-open-file (stream passwd-file)
943 (let ((entry (read-line stream nil)))
944 (unless entry (return nil))
945 (let ((name-end (position #\: (the simple-string entry)
948 (let ((id-start (position #\: (the simple-string entry)
949 :start (1+ name-end) :test #'char=)))
952 (let ((id-end (position #\: (the simple-string entry)
953 :start id-start :test #'char=)))
955 (string= id-string entry
956 :start2 id-start :end2 id-end))
957 (return (subseq entry 0 name-end))))))))))))
959 (/show0 "filesys.lisp 899")
961 ;;; Predicate to order pathnames by. Goes by name.
962 (defun pathname-order (x y)
963 (let ((xn (%pathname-name x))
964 (yn (%pathname-name y)))
966 (let ((res (string-lessp xn yn)))
967 (cond ((not res) nil)
968 ((= res (length (the simple-string xn))) t)
969 ((= res (length (the simple-string yn))) nil)
973 (defun default-directory ()
975 "Returns the pathname for the default directory. This is the place where
976 a file will be written if no directory is specified. This may be changed
978 (multiple-value-bind (gr dir-or-error) (sb!unix:unix-current-directory)
980 (let ((*ignore-wildcards* t))
981 (pathname (concatenate 'simple-string dir-or-error "/")))
982 (error dir-or-error))))
984 (defun %set-default-directory (new-val)
985 (let ((namestring (unix-namestring new-val t)))
987 (error "~S doesn't exist." new-val))
988 (multiple-value-bind (gr error) (sb!unix:unix-chdir namestring)
990 (setf (search-list "default:") (default-directory))
991 (error (sb!unix:get-unix-error-msg error))))
994 (/show0 "filesys.lisp 934")
996 (defun !filesys-cold-init ()
997 (/show0 "entering !FILESYS-COLD-INIT")
998 (setf *default-pathname-defaults*
999 (%make-pathname *unix-host* nil nil nil nil :newest))
1000 (setf (search-list "default:") (default-directory))
1001 (/show0 "leaving !FILESYS-COLD-INIT")
1004 (defun ensure-directories-exist (pathspec &key verbose (mode #o777))
1006 "Tests whether the directories containing the specified file
1007 actually exist, and attempts to create them if they do not.
1008 Portable programs should avoid using the :MODE keyword argument."
1009 (let* ((pathname (pathname pathspec))
1010 (pathname (if (typep pathname 'logical-pathname)
1011 (translate-logical-pathname pathname)
1014 (when (wild-pathname-p pathname)
1015 (error 'simple-file-error
1016 :format-control "bad place for a wild pathname"
1017 :pathname pathspec))
1018 (enumerate-search-list (pathname pathname)
1019 (let ((dir (pathname-directory pathname)))
1020 (loop for i from 1 upto (length dir)
1021 do (let ((newpath (make-pathname
1022 :host (pathname-host pathname)
1023 :device (pathname-device pathname)
1024 :directory (subseq dir 0 i))))
1025 (unless (probe-file newpath)
1026 (let ((namestring (namestring newpath)))
1028 (format *standard-output*
1029 "~&creating directory: ~A~%"
1031 (sb!unix:unix-mkdir namestring mode)
1032 (unless (probe-file namestring)
1033 (error 'simple-file-error
1035 :format-control "can't create directory ~A"
1036 :format-arguments (list namestring)))
1037 (setf created-p t)))))
1038 ;; Only the first path in a search-list is considered.
1039 (return (values pathname created-p))))))
1041 (/show0 "filesys.lisp 1000")