1 ;;;; file system interface functions -- fairly Unix-centric, but with
2 ;;;; differences between Unix and Win32 papered over.
4 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
13 (in-package "SB!IMPL")
15 ;;;; Unix pathname host support
17 ;;; FIXME: the below shouldn't really be here, but in documentation
18 ;;; (chapter 19 makes a lot of requirements for documenting
19 ;;; implementation-dependent decisions), but anyway it's probably not
20 ;;; what we currently do.
22 ;;; Unix namestrings have the following format:
24 ;;; namestring := [ directory ] [ file [ type [ version ]]]
25 ;;; directory := [ "/" ] { file "/" }*
27 ;;; type := "." [^/.]*
28 ;;; version := "." ([0-9]+ | "*")
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 ;;; - Otherwise, the last dot separates the file and the type.
40 ;;; Wildcard characters:
42 ;;; If the directory, file, type components contain any of the
43 ;;; following characters, it is considered part of a wildcard pattern
44 ;;; and has the following meaning.
46 ;;; ? - matches any one character
47 ;;; * - matches any zero or more characters.
48 ;;; [abc] - matches any of a, b, or c.
49 ;;; {str1,str2,...,strn} - matches any of str1, str2, ..., or strn.
50 ;;; (FIXME: no it doesn't)
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-string namestr)
59 (type index start end))
60 (let* ((result (make-string (- end start) :element-type 'character))
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-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 (cons :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-string namestr)
162 (type index start end))
163 (let* ((last-dot (position #\. namestr :start (1+ start) :end end
167 (values (maybe-make-pattern namestr start last-dot)
168 (maybe-make-pattern namestr (1+ last-dot) end)
171 (values (maybe-make-pattern namestr start end)
175 (/show0 "filesys.lisp 200")
178 ;;;; wildcard matching stuff
180 ;;; Return a list of all the Lispy filenames (not including e.g. the
181 ;;; Unix magic "." and "..") in the directory named by DIRECTORY-NAME.
182 (defun directory-lispy-filenames (directory-name)
183 (with-alien ((adlf (* c-string)
184 (alien-funcall (extern-alien
185 "alloc_directory_lispy_filenames"
186 (function (* c-string) c-string))
188 (if (null-alien adlf)
189 (error 'simple-file-error
190 :pathname directory-name
191 :format-control "~@<couldn't read directory ~S: ~2I~_~A~:>"
192 :format-arguments (list directory-name (strerror)))
194 (c-strings->string-list adlf)
195 (alien-funcall (extern-alien "free_directory_lispy_filenames"
196 (function void (* c-string)))
199 (/show0 "filesys.lisp 498")
201 (defmacro !enumerate-matches ((var pathname &optional result
202 &key (verify-existence t)
206 (%enumerate-matches (pathname ,pathname)
209 (lambda (,var) ,@body))
212 (/show0 "filesys.lisp 500")
214 ;;; Call FUNCTION on matches.
216 ;;; KLUDGE: this assumes that an absolute pathname is indicated to the
217 ;;; operating system by having a directory separator as the first
218 ;;; character in the directory part. This is true for Win32 pathnames
219 ;;; and for Unix pathnames, but it isn't true for LispM pathnames (and
220 ;;; their bastard offspring, logical pathnames. Also it assumes that
221 ;;; Unix pathnames have an empty or :unspecific device, and that
222 ;;; windows drive letters are the only kinds of non-empty/:UNSPECIFIC
224 (defun %enumerate-matches (pathname verify-existence follow-links function)
225 (/noshow0 "entering %ENUMERATE-MATCHES")
226 (when (pathname-type pathname)
227 (unless (pathname-name pathname)
228 (error "cannot supply a type without a name:~% ~S" pathname)))
229 (when (and (integerp (pathname-version pathname))
230 (member (pathname-type pathname) '(nil :unspecific)))
231 (error "cannot supply a version without a type:~% ~S" pathname))
232 (let ((host (pathname-host pathname))
233 (device (pathname-device pathname))
234 (directory (pathname-directory pathname)))
235 (/noshow0 "computed HOST and DIRECTORY")
236 (let* ((dirstring (if directory
237 (ecase (first directory)
238 (:absolute (host-unparse-directory-separator host))
241 (devstring (if (and device (not (eq device :unspecific)))
242 (concatenate 'simple-string (string device) (string #\:))
244 (headstring (concatenate 'simple-string devstring dirstring)))
246 (%enumerate-directories headstring (rest directory) pathname
247 verify-existence follow-links nil function)
248 (%enumerate-files headstring pathname verify-existence function)))))
250 ;;; Call FUNCTION on directories.
251 (defun %enumerate-directories (head tail pathname verify-existence
252 follow-links nodes function
253 &aux (host (pathname-host pathname)))
254 (declare (simple-string head))
256 (setf follow-links nil)
257 (macrolet ((unix-xstat (name)
259 (sb!unix:unix-stat ,name)
260 (sb!unix:unix-lstat ,name)))
261 (with-directory-node-noted ((head) &body body)
262 `(multiple-value-bind (res dev ino mode)
264 (when (and res (eql (logand mode sb!unix:s-ifmt)
266 (let ((nodes (cons (cons dev ino) nodes)))
268 (with-directory-node-removed ((head) &body body)
269 `(multiple-value-bind (res dev ino mode)
271 (when (and res (eql (logand mode sb!unix:s-ifmt)
273 (let ((nodes (remove (cons dev ino) nodes :test #'equal)))
276 (let ((piece (car tail)))
279 (let ((head (concatenate 'string head piece)))
280 (with-directory-node-noted (head)
281 (%enumerate-directories
282 (concatenate 'string head
283 (host-unparse-directory-separator host))
285 verify-existence follow-links
287 ((member :wild-inferiors)
288 ;; now with extra error case handling from CLHS
289 ;; 19.2.2.4.3 -- CSR, 2004-01-24
290 (when (member (cadr tail) '(:up :back))
291 (error 'simple-file-error
293 :format-control "~@<invalid use of ~S after :WILD-INFERIORS~@:>."
294 :format-arguments (list (cadr tail))))
295 (%enumerate-directories head (rest tail) pathname
296 verify-existence follow-links
298 (dolist (name (directory-lispy-filenames head))
299 (let ((subdir (concatenate 'string head name)))
300 (multiple-value-bind (res dev ino mode)
302 (declare (type (or fixnum null) mode))
303 (when (and res (eql (logand mode sb!unix:s-ifmt)
305 (unless (dolist (dir nodes nil)
306 (when (and (eql (car dir) dev)
311 (let ((nodes (cons (cons dev ino) nodes))
312 (subdir (concatenate 'string subdir (host-unparse-directory-separator host))))
313 (%enumerate-directories subdir tail pathname
314 verify-existence follow-links
315 nodes function))))))))
316 ((or pattern (member :wild))
317 (dolist (name (directory-lispy-filenames head))
318 (when (or (eq piece :wild) (pattern-matches piece name))
319 (let ((subdir (concatenate 'string head name)))
320 (multiple-value-bind (res dev ino mode)
322 (declare (type (or fixnum null) mode))
324 (eql (logand mode sb!unix:s-ifmt)
326 (let ((nodes (cons (cons dev ino) nodes))
327 (subdir (concatenate 'string subdir (host-unparse-directory-separator host))))
328 (%enumerate-directories subdir (rest tail) pathname
329 verify-existence follow-links
330 nodes function))))))))
332 (when (string= head (host-unparse-directory-separator host))
333 (error 'simple-file-error
335 :format-control "~@<invalid use of :UP after :ABSOLUTE.~@:>"))
336 (with-directory-node-removed (head)
337 (let ((head (concatenate 'string head "..")))
338 (with-directory-node-noted (head)
339 (%enumerate-directories (concatenate 'string head (host-unparse-directory-separator host))
341 verify-existence follow-links
344 ;; :WILD-INFERIORS is handled above, so the only case here
345 ;; should be (:ABSOLUTE :BACK)
346 (aver (string= head (host-unparse-directory-separator host)))
347 (error 'simple-file-error
349 :format-control "~@<invalid use of :BACK after :ABSOLUTE.~@:>"))))
350 (%enumerate-files head pathname verify-existence function))))
352 ;;; Call FUNCTION on files.
353 (defun %enumerate-files (directory pathname verify-existence function)
354 (declare (simple-string directory))
355 (/noshow0 "entering %ENUMERATE-FILES")
356 (let ((name (%pathname-name pathname))
357 (type (%pathname-type pathname))
358 (version (%pathname-version pathname)))
359 (/noshow0 "computed NAME, TYPE, and VERSION")
360 (cond ((member name '(nil :unspecific))
361 (/noshow0 "UNSPECIFIC, more or less")
362 (let ((directory (coerce directory 'string)))
363 (when (or (not verify-existence)
364 (sb!unix:unix-file-kind directory))
365 (funcall function directory))))
366 ((or (pattern-p name)
370 (/noshow0 "WILD, more or less")
371 ;; I IGNORE-ERRORS here just because the original CMU CL
372 ;; code did. I think the intent is that it's not an error
373 ;; to request matches to a wild pattern when no matches
374 ;; exist, but I haven't tried to figure out whether
375 ;; everything is kosher. (E.g. what if we try to match a
376 ;; wildcard but we don't have permission to read one of the
377 ;; relevant directories?) -- WHN 2001-04-17
378 (dolist (complete-filename (ignore-errors
379 (directory-lispy-filenames directory)))
381 (file-name file-type file-version)
382 (let ((*ignore-wildcards* t))
383 (extract-name-type-and-version
384 complete-filename 0 (length complete-filename)))
385 (when (and (components-match file-name name)
386 (components-match file-type type)
387 (components-match file-version version))
391 complete-filename))))))
393 (/noshow0 "default case")
394 (let ((file (concatenate 'string directory name)))
395 (/noshow "computed basic FILE")
396 (unless (or (null type) (eq type :unspecific))
397 (/noshow0 "tweaking FILE for more-or-less-:UNSPECIFIC case")
398 (setf file (concatenate 'string file "." type)))
399 (unless (member version '(nil :newest :wild :unspecific))
400 (/noshow0 "tweaking FILE for more-or-less-:WILD case")
401 (setf file (concatenate 'string file "."
402 (quick-integer-to-string version))))
403 (/noshow0 "finished possibly tweaking FILE")
404 (when (or (not verify-existence)
405 (sb!unix:unix-file-kind file t))
406 (/noshow0 "calling FUNCTION on FILE")
407 (funcall function file)))))))
409 (/noshow0 "filesys.lisp 603")
411 ;;; FIXME: Why do we need this?
412 (defun quick-integer-to-string (n)
413 (declare (type integer n))
414 (cond ((not (fixnump n))
415 (write-to-string n :base 10 :radix nil))
419 (concatenate 'simple-base-string "-"
420 (the simple-base-string (quick-integer-to-string (- n)))))
422 (do* ((len (1+ (truncate (integer-length n) 3)))
423 (res (make-string len :element-type 'base-char))
429 (replace res res :start2 i :end2 len)
430 (%shrink-vector res (- len i)))
431 (declare (simple-string res)
433 (multiple-value-setq (q r) (truncate q 10))
434 (setf (schar res i) (schar "0123456789" r))))))
438 (defun empty-relative-pathname-spec-p (x)
441 (or (equal (pathname-directory x) '(:relative))
442 ;; KLUDGE: I'm not sure this second check should really
443 ;; have to be here. But on sbcl-0.6.12.7,
444 ;; (PATHNAME-DIRECTORY (PATHNAME "")) is NIL, and
445 ;; (PATHNAME "") seems to act like an empty relative
446 ;; pathname, so in order to work with that, I test
447 ;; for NIL here. -- WHN 2001-05-18
448 (null (pathname-directory x)))
449 (null (pathname-name x))
450 (null (pathname-type x)))
451 ;; (The ANSI definition of "pathname specifier" has
452 ;; other cases, but none of them seem to admit the possibility
453 ;; of being empty and relative.)
456 ;;; Convert PATHNAME into a string that can be used with UNIX system
457 ;;; calls, or return NIL if no match is found. Wild-cards are expanded.
459 ;;; FIXME: apart from the error checking (for wildness and for
460 ;;; existence) and conversion to physical pathanme, this is redundant
461 ;;; with UNPARSE-NATIVE-UNIX-NAMESTRING; one should probably be
462 ;;; written in terms of the other.
464 ;;; FIXME: actually this (I think) works not just for Unix.
465 (defun unix-namestring (pathname-spec &optional (for-input t))
466 (let* ((namestring (physicalize-pathname (merge-pathnames pathname-spec)))
467 (matches nil)) ; an accumulator for actual matches
468 (when (wild-pathname-p namestring)
469 (error 'simple-file-error
471 :format-control "bad place for a wild pathname"))
472 (!enumerate-matches (match namestring nil :verify-existence for-input)
473 (push match matches))
474 (case (length matches)
477 (t (bug "!ENUMERATE-MATCHES returned more than one match on a non-wild pathname")))))
479 ;;;; TRUENAME and PROBE-FILE
481 ;;; This is only trivially different from PROBE-FILE, which is silly
483 (defun truename (pathname)
485 "Return the pathname for the actual file described by PATHNAME.
486 An error of type FILE-ERROR is signalled if no such file exists, or the
489 Under Unix, the TRUENAME of a broken symlink is considered to be the name of
490 the broken symlink itself."
491 (let ((result (probe-file pathname)))
493 (error 'simple-file-error
495 :format-control "The file ~S does not exist."
496 :format-arguments (list (namestring pathname))))
499 (defun probe-file (pathname)
501 "Return a pathname which is the truename of the file if it exists, or NIL
502 otherwise. An error of type FILE-ERROR is signaled if pathname is wild."
503 (let* ((defaulted-pathname (merge-pathnames
505 (sane-default-pathname-defaults)))
506 (namestring (unix-namestring defaulted-pathname t)))
507 (when (and namestring (sb!unix:unix-file-kind namestring t))
508 (let ((trueishname (sb!unix:unix-resolve-links namestring)))
510 (let* ((*ignore-wildcards* t)
511 (name (simplify-namestring
513 (pathname-host defaulted-pathname))))
514 (if (eq (sb!unix:unix-file-kind name) :directory)
515 ;; FIXME: this might work, but it's ugly.
516 (pathname (concatenate 'string name "/"))
517 (pathname name))))))))
519 ;;;; miscellaneous other operations
521 (/show0 "filesys.lisp 700")
523 (defun rename-file (file new-name)
525 "Rename FILE to have the specified NEW-NAME. If FILE is a stream open to a
526 file, then the associated file is renamed."
527 (let* ((original (truename file))
528 (original-namestring (unix-namestring original t))
529 (new-name (merge-pathnames new-name original))
530 (new-namestring (unix-namestring new-name nil)))
531 (unless new-namestring
532 (error 'simple-file-error
534 :format-control "~S can't be created."
535 :format-arguments (list new-name)))
536 (multiple-value-bind (res error)
537 (sb!unix:unix-rename original-namestring new-namestring)
539 (error 'simple-file-error
541 :format-control "~@<couldn't rename ~2I~_~A ~I~_to ~2I~_~A: ~
543 :format-arguments (list original new-name (strerror error))))
545 (file-name file new-name))
546 (values new-name original (truename new-name)))))
548 (defun delete-file (file)
550 "Delete the specified FILE."
551 (let ((namestring (unix-namestring file t)))
553 (close file :abort t))
555 (error 'simple-file-error
557 :format-control "~S doesn't exist."
558 :format-arguments (list file)))
559 (multiple-value-bind (res err) (sb!unix:unix-unlink namestring)
561 (simple-file-perror "couldn't delete ~A" namestring err))))
564 (defun ensure-trailing-slash (string)
565 (let ((last-char (char string (1- (length string)))))
566 (if (or (eql last-char #\/)
570 (concatenate 'string string "/"))))
572 (defun sbcl-homedir-pathname ()
573 (let ((sbcl-home (posix-getenv "SBCL_HOME")))
574 ;; SBCL_HOME isn't set for :EXECUTABLE T embedded cores
575 (when (and sbcl-home (not (string= sbcl-home "")))
576 (parse-native-namestring
577 (ensure-trailing-slash sbcl-home)))))
579 ;;; (This is an ANSI Common Lisp function.)
580 (defun user-homedir-pathname (&optional host)
582 "Return the home directory of the user as a pathname. If the HOME
583 environment variable has been specified, the directory it designates
584 is returned; otherwise obtains the home directory from the operating
586 (declare (ignore host))
587 (let ((env-home (posix-getenv "HOME")))
588 (parse-native-namestring
589 (ensure-trailing-slash
590 (if (and env-home (not (string= env-home "")))
593 (sb!unix:uid-homedir (sb!unix:unix-getuid))
595 ;; Needs to bypass PARSE-NATIVE-NAMESTRING & ENSURE-TRAILING-SLASH
596 (return-from user-homedir-pathname
597 (sb!win32::get-folder-pathname sb!win32::csidl_profile)))))))
599 (defun file-write-date (file)
601 "Return file's creation date, or NIL if it doesn't exist.
602 An error of type file-error is signaled if file is a wild pathname"
603 (let ((name (unix-namestring file t)))
606 (res dev ino mode nlink uid gid rdev size atime mtime)
607 (sb!unix:unix-stat name)
608 (declare (ignore dev ino mode nlink uid gid rdev size atime))
610 (+ unix-to-universal-time mtime))))))
612 (defun file-author (file)
614 "Return the file author as a string, or NIL if the author cannot be
615 determined. Signal an error of type FILE-ERROR if FILE doesn't exist,
616 or FILE is a wild pathname."
617 (let ((name (unix-namestring (pathname file) t)))
619 (error 'simple-file-error
621 :format-control "~S doesn't exist."
622 :format-arguments (list file)))
623 (multiple-value-bind (winp dev ino mode nlink uid)
624 (sb!unix:unix-stat name)
625 (declare (ignore dev ino mode nlink))
626 (and winp (sb!unix:uid-username uid)))))
630 (/show0 "filesys.lisp 800")
632 ;;; NOTE: There is a fair amount of hair below that is probably not
633 ;;; strictly necessary.
635 ;;; The issue is the following: what does (DIRECTORY "SYS:*;") mean?
636 ;;; Until 2004-01, SBCL's behaviour was unquestionably wrong, as it
637 ;;; did not translate the logical pathname at all, but instead treated
638 ;;; it as a physical one. Other Lisps seem to to treat this call as
639 ;;; equivalent to (DIRECTORY (TRANSLATE-LOGICAL-PATHNAME "SYS:*;")),
640 ;;; which is fine as far as it goes, but not very interesting, and
641 ;;; arguably counterintuitive. (PATHNAME-MATCH-P "SYS:SRC;" "SYS:*;")
642 ;;; is true, so why should "SYS:SRC;" not show up in the call to
643 ;;; DIRECTORY? (assuming the physical pathname corresponding to it
644 ;;; exists, of course).
646 ;;; So, the interpretation that I am pushing is for all pathnames
647 ;;; matching the input pathname to be queried. This means that we
648 ;;; need to compute the intersection of the input pathname and the
649 ;;; logical host FROM translations, and then translate the resulting
650 ;;; pathname using the host to the TO translation; this treatment is
651 ;;; recursively invoked until we get a physical pathname, whereupon
652 ;;; our physical DIRECTORY implementation takes over.
654 ;;; FIXME: this is an incomplete implementation. It only works when
655 ;;; both are logical pathnames (which is OK, because that's the only
656 ;;; case when we call it), but there are other pitfalls as well: see
657 ;;; the DIRECTORY-HELPER below for some, but others include a lack of
658 ;;; pattern handling.
659 (defun pathname-intersections (one two)
660 (aver (logical-pathname-p one))
661 (aver (logical-pathname-p two))
663 ((intersect-version (one two)
664 (aver (typep one '(or null (member :newest :wild :unspecific)
666 (aver (typep two '(or null (member :newest :wild :unspecific)
671 ((or (null one) (eq one :unspecific)) two)
672 ((or (null two) (eq two :unspecific)) one)
675 (intersect-name/type (one two)
676 (aver (typep one '(or null (member :wild :unspecific) string)))
677 (aver (typep two '(or null (member :wild :unspecific) string)))
681 ((or (null one) (eq one :unspecific)) two)
682 ((or (null two) (eq two :unspecific)) one)
683 ((string= one two) one)
685 (intersect-directory (one two)
686 (aver (typep one '(or null (member :wild :unspecific) list)))
687 (aver (typep two '(or null (member :wild :unspecific) list)))
691 ((or (null one) (eq one :unspecific)) two)
692 ((or (null two) (eq two :unspecific)) one)
693 (t (aver (eq (car one) (car two)))
695 (lambda (x) (cons (car one) x))
696 (intersect-directory-helper (cdr one) (cdr two)))))))
697 (let ((version (intersect-version
698 (pathname-version one) (pathname-version two)))
699 (name (intersect-name/type
700 (pathname-name one) (pathname-name two)))
701 (type (intersect-name/type
702 (pathname-type one) (pathname-type two)))
703 (host (pathname-host one)))
705 (make-pathname :host host :name name :type type
706 :version version :directory d))
708 (pathname-directory one) (pathname-directory two))))))
710 ;;; FIXME: written as its own function because I (CSR) don't
711 ;;; understand it, so helping both debuggability and modularity. In
712 ;;; case anyone is motivated to rewrite it, it returns a list of
713 ;;; sublists representing the intersection of the two input directory
714 ;;; paths (excluding the initial :ABSOLUTE or :RELATIVE).
716 ;;; FIXME: Does not work with :UP or :BACK
717 ;;; FIXME: Does not work with patterns
719 ;;; FIXME: PFD suggests replacing this implementation with a DFA
720 ;;; conversion of a NDFA. Find out (a) what this means and (b) if it
721 ;;; turns out to be worth it.
722 (defun intersect-directory-helper (one two)
723 (flet ((simple-intersection (cone ctwo)
725 ((eq cone :wild) ctwo)
726 ((eq ctwo :wild) cone)
727 (t (aver (typep cone 'string))
728 (aver (typep ctwo 'string))
729 (if (string= cone ctwo) cone nil)))))
731 ((loop-possible-wild-inferiors-matches
732 (lower-bound bounding-sequence order)
733 (let ((index (gensym)) (g2 (gensym)) (g3 (gensym)) (l (gensym)))
734 `(let ((,l (length ,bounding-sequence)))
735 (loop for ,index from ,lower-bound to ,l
736 append (mapcar (lambda (,g2)
738 (butlast ,bounding-sequence (- ,l ,index))
743 (if (eq (car (nthcdr ,index ,bounding-sequence))
747 (intersect-directory-helper
749 `((nthcdr ,index one) (cdr two))
750 `((cdr one) (nthcdr ,index two)))))))))))
752 ((and (eq (car one) :wild-inferiors)
753 (eq (car two) :wild-inferiors))
755 (append (mapcar (lambda (x) (cons :wild-inferiors x))
756 (intersect-directory-helper (cdr one) (cdr two)))
757 (loop-possible-wild-inferiors-matches 2 one t)
758 (loop-possible-wild-inferiors-matches 2 two nil))
760 ((eq (car one) :wild-inferiors)
761 (delete-duplicates (loop-possible-wild-inferiors-matches 0 two nil)
763 ((eq (car two) :wild-inferiors)
764 (delete-duplicates (loop-possible-wild-inferiors-matches 0 one t)
766 ((and (null one) (null two)) (list nil))
769 (t (and (simple-intersection (car one) (car two))
770 (mapcar (lambda (x) (cons (simple-intersection
771 (car one) (car two)) x))
772 (intersect-directory-helper (cdr one) (cdr two)))))))))
774 (defun directory (pathname &key)
776 "Return a list of PATHNAMEs, each the TRUENAME of a file that matched the
777 given pathname. Note that the interaction between this ANSI-specified
778 TRUENAMEing and the semantics of the Unix filesystem (symbolic links..)
779 means this function can sometimes return files which don't have the same
780 directory as PATHNAME."
781 (let (;; We create one entry in this hash table for each truename,
782 ;; as an asymptotically efficient way of removing duplicates
783 ;; (which can arise when e.g. multiple symlinks map to the
785 (truenames (make-hash-table :test #'equal))
786 ;; FIXME: Possibly this MERGE-PATHNAMES call should only
787 ;; happen once we get a physical pathname.
788 (merged-pathname (merge-pathnames pathname)))
789 (labels ((do-physical-directory (pathname)
790 (aver (not (logical-pathname-p pathname)))
791 (!enumerate-matches (match pathname)
792 (let* ((*ignore-wildcards* t)
793 ;; FIXME: Why not TRUENAME? As reported by
794 ;; Milan Zamazal sbcl-devel 2003-10-05, using
795 ;; TRUENAME causes a race condition whereby
796 ;; removal of a file during the directory
797 ;; operation causes an error. It's not clear
798 ;; what the right thing to do is, though. --
800 (truename (probe-file match)))
802 (setf (gethash (namestring truename) truenames)
804 (do-directory (pathname)
805 (if (logical-pathname-p pathname)
806 (let ((host (intern-logical-host (pathname-host pathname))))
807 (dolist (x (logical-host-canon-transls host))
808 (destructuring-bind (from to) x
810 (pathname-intersections pathname from)))
811 (dolist (p intersections)
812 (do-directory (translate-pathname p from to)))))))
813 (do-physical-directory pathname))))
814 (do-directory merged-pathname))
816 ;; Sorting isn't required by the ANSI spec, but sorting
817 ;; into some canonical order seems good just on the
818 ;; grounds that the implementation should have repeatable
819 ;; behavior when possible.
820 (sort (loop for name being each hash-key in truenames
821 using (hash-value truename)
822 collect (cons name truename))
826 (/show0 "filesys.lisp 899")
828 ;;; predicate to order pathnames by; goes by name
829 (defun pathname-order (x y)
830 (let ((xn (%pathname-name x))
831 (yn (%pathname-name y)))
833 (let ((res (string-lessp xn yn)))
834 (cond ((not res) nil)
835 ((= res (length (the simple-string xn))) t)
836 ((= res (length (the simple-string yn))) nil)
840 (defun ensure-directories-exist (pathspec &key verbose (mode #o777))
842 "Test whether the directories containing the specified file
843 actually exist, and attempt to create them if they do not.
844 The MODE argument is a CMUCL/SBCL-specific extension to control
845 the Unix permission bits."
846 (let ((pathname (physicalize-pathname (merge-pathnames (pathname pathspec))))
848 (when (wild-pathname-p pathname)
849 (error 'simple-file-error
850 :format-control "bad place for a wild pathname"
852 (let ((dir (pathname-directory pathname)))
853 (loop for i from 1 upto (length dir)
854 do (let ((newpath (make-pathname
855 :host (pathname-host pathname)
856 :device (pathname-device pathname)
857 :directory (subseq dir 0 i))))
858 (unless (probe-file newpath)
859 (let ((namestring (coerce (namestring newpath) 'string)))
861 (format *standard-output*
862 "~&creating directory: ~A~%"
864 (sb!unix:unix-mkdir namestring mode)
865 (unless (probe-file namestring)
866 (restart-case (error 'simple-file-error
868 :format-control "can't create directory ~A"
869 :format-arguments (list namestring))
871 :report "Retry directory creation."
872 (ensure-directories-exist pathspec :verbose verbose :mode mode))
874 :report "Continue as if directory creation was successful."
876 (setf created-p t)))))
877 (values pathspec created-p))))
879 (/show0 "filesys.lisp 1000")