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 (defun maybe-make-pattern (namestr start end)
84 (declare (type simple-string namestr)
85 (type index start end))
89 (last-regular-char nil)
91 (flet ((flush-pending-regulars ()
92 (when last-regular-char
93 (pattern (if any-quotes
94 (remove-backslashes namestr
97 (subseq namestr last-regular-char index)))
99 (setf last-regular-char nil))))
103 (let ((char (schar namestr index)))
110 (unless last-regular-char
111 (setf last-regular-char index))
114 (flush-pending-regulars)
115 (pattern :single-char-wild)
118 (flush-pending-regulars)
119 (pattern :multi-char-wild)
122 (flush-pending-regulars)
124 (position #\] namestr :start index :end end)))
125 (unless close-bracket
126 (error 'namestring-parse-error
127 :complaint "#\\[ with no corresponding #\\]"
130 (pattern (cons :character-set
134 (setf index (1+ close-bracket))))
136 (unless last-regular-char
137 (setf last-regular-char index))
139 (flush-pending-regulars)))
140 (cond ((null (pattern))
142 ((null (cdr (pattern)))
143 (let ((piece (first (pattern))))
145 ((member :multi-char-wild) :wild)
146 (simple-string piece)
148 (make-pattern (pattern))))))
150 (make-pattern (pattern))))))
152 (defun unparse-physical-piece (thing)
156 (let* ((srclen (length thing))
159 (case (schar thing i)
162 (let ((result (make-string dstlen))
164 (dotimes (src srclen)
165 (let ((char (schar thing src)))
168 (setf (schar result dst) #\\)
170 (setf (schar result dst) char)
174 (with-output-to-string (s)
175 (dolist (piece (pattern-pieces thing))
178 (write-string piece s))
182 (write-string "*" s))
184 (write-string "?" s))))
189 (write-string (cdr piece) s)
190 (write-string "]" s))
192 (error "invalid pattern piece: ~S" piece))))))))))
194 (defun make-matcher (piece)
195 (cond ((eq piece :wild)
197 ((typep piece 'pattern)
199 (when (stringp other)
200 (pattern-matches piece other))))
203 (equal piece other)))))
205 (/show0 "filesys.lisp 160")
207 (defun extract-name-type-and-version (namestr start end)
208 (declare (type simple-string namestr)
209 (type index start end))
210 (let* ((last-dot (position #\. namestr :start (1+ start) :end end
214 (values (maybe-make-pattern namestr start last-dot)
215 (maybe-make-pattern namestr (1+ last-dot) end)
218 (values (maybe-make-pattern namestr start end)
222 (/show0 "filesys.lisp 200")
225 ;;;; Grabbing the kind of file when we have a namestring.
226 (defun native-file-kind (namestring)
227 (multiple-value-bind (existsp errno ino mode)
229 (sb!unix:unix-lstat namestring)
231 (sb!unix:unix-stat namestring)
232 (declare (ignore errno ino))
234 (let ((ifmt (logand mode sb!unix:s-ifmt)))
236 (#.sb!unix:s-ifreg :file)
237 (#.sb!unix:s-ifdir :directory)
239 (#.sb!unix:s-iflnk :symlink)
242 ;;;; TRUENAME, PROBE-FILE, FILE-AUTHOR, FILE-WRITE-DATE.
244 ;;; Rewritten in 12/2007 by RMK, replacing 13+ year old CMU code that
245 ;;; made a mess of things in order to support search lists (which SBCL
246 ;;; has never had). These are now all relatively straightforward
247 ;;; wrappers around stat(2) and realpath(2), with the same basic logic
248 ;;; in all cases. The wrinkles to be aware of:
250 ;;; * SBCL defines the truename of an existing, dangling or
251 ;;; self-referring symlink to be the symlink itself.
252 ;;; * The old version of PROBE-FILE merged the pathspec against
253 ;;; *DEFAULT-PATHNAME-DEFAULTS* twice, and so lost when *D-P-D*
254 ;;; was a relative pathname. Even if the case where *D-P-D* is a
255 ;;; relative pathname is problematic, there's no particular reason
256 ;;; to get that wrong, so let's try not to.
257 ;;; * Note that while stat(2) is probably atomic, getting the truename
258 ;;; for a filename involves poking all over the place, and so is
259 ;;; subject to race conditions if other programs mutate the file
260 ;;; system while we're resolving symlinks. So it's not implausible for
261 ;;; realpath(3) to fail even if stat(2) succeeded. There's nothing
262 ;;; obvious we can do about this, however.
263 ;;; * Windows' apparent analogue of realpath(3) is called
264 ;;; GetFullPathName, and it's a bit less useful than realpath(3).
265 ;;; In particular, while realpath(3) errors in case the file doesn't
266 ;;; exist, GetFullPathName seems to return a filename in all cases.
267 ;;; As realpath(3) is not atomic anyway, we only ever call it when
268 ;;; we think a file exists, so just be careful when rewriting this
271 ;;; Given a pathname designator, some quality to query for, return one
272 ;;; of a pathname, a universal time, or a string (a file-author), or
273 ;;; NIL. QUERY-FOR may be one of :TRUENAME, :EXISTENCE, :WRITE-DATE,
274 ;;; :AUTHOR. If ERRORP is false, return NIL in case the file system
275 ;;; returns an error code; otherwise, signal an error. Accepts
276 ;;; logical pathnames, too (but never returns LPNs). For internal
278 (defun query-file-system (pathspec query-for &optional (errorp t))
279 (let ((pathname (translate-logical-pathname
282 (sane-default-pathname-defaults)))))
283 (when (wild-pathname-p pathname)
284 (error 'simple-file-error
286 :format-control "~@<can't find the ~A of wild pathname ~A~
287 (physicalized from ~A).~:>"
288 :format-arguments (list query-for pathname pathspec)))
289 (flet ((fail (note-format pathname errno)
291 (simple-file-perror note-format pathname errno)
292 (return-from query-file-system nil))))
293 (let ((filename (native-namestring pathname :as-file t)))
294 (multiple-value-bind (existsp errno ino mode nlink uid gid rdev size
296 (sb!unix:unix-stat filename)
297 (declare (ignore ino nlink gid rdev size atime
300 ;; On win32, stat regards UNC pathnames and device names as
301 ;; nonexisting, so we check once more with the native API.
304 (let ((handle (sb!win32:create-file
306 sb!win32:file-open-existing
312 (sb!win32:get-file-attributes filename))
314 (progn (sb!win32:close-handle handle) t)))))
317 (:existence (nth-value
319 (parse-native-namestring
321 (pathname-host pathname)
322 (sane-default-pathname-defaults)
323 :as-directory (eql (logand mode sb!unix:s-ifmt)
325 (:truename (nth-value
327 (parse-native-namestring
328 ;; Note: in case the file is stat'able, POSIX
329 ;; realpath(3) gets us a canonical absolute
330 ;; filename, even if the post-merge PATHNAME
331 ;; is not absolute...
332 (multiple-value-bind (realpath errno)
333 (sb!unix:unix-realpath filename)
336 (fail "couldn't resolve ~A" filename errno)))
337 (pathname-host pathname)
338 (sane-default-pathname-defaults)
339 ;; ... but without any trailing slash.
340 :as-directory (eql (logand mode sb!unix:s-ifmt)
344 (sb!unix:uid-username uid))
345 (:write-date (+ unix-to-universal-time mtime)))
347 ;; SBCL has for many years had a policy that a pathname
348 ;; that names an existing, dangling or self-referential
349 ;; symlink denotes the symlink itself. stat(2) fails
350 ;; and sets errno to ENOENT or ELOOP respectively, but
351 ;; we must distinguish cases where the symlink exists
352 ;; from ones where there's a loop in the apparent
353 ;; containing directory.
355 (multiple-value-bind (linkp ignore ino mode nlink uid gid rdev
357 (sb!unix:unix-lstat filename)
358 (declare (ignore ignore ino mode nlink gid rdev size atime))
359 (when (and (or (= errno sb!unix:enoent)
360 (= errno sb!unix:eloop))
362 (return-from query-file-system
365 ;; We do this reparse so as to return a
366 ;; normalized pathname.
367 (parse-native-namestring
368 filename (pathname-host pathname)))
370 ;; So here's a trick: since lstat succeded,
371 ;; FILENAME exists, so its directory exists and
372 ;; only the non-directory part is loopy. So
373 ;; let's resolve FILENAME's directory part with
374 ;; realpath(3), in order to get a canonical
375 ;; absolute name for the directory, and then
376 ;; return a pathname having PATHNAME's name,
377 ;; type, and version, but the rest from the
378 ;; truename of the directory. Since we turned
379 ;; PATHNAME into FILENAME "as a file", FILENAME
380 ;; does not end in a slash, and so we get the
381 ;; directory part of FILENAME by reparsing
382 ;; FILENAME and masking off its name, type, and
383 ;; version bits. But note not to call ourselves
384 ;; recursively, because we don't want to
385 ;; re-merge against *DEFAULT-PATHNAME-DEFAULTS*,
386 ;; since PATHNAME may be a relative pathname.
390 (parse-native-namestring
391 (multiple-value-bind (realpath errno)
392 (sb!unix:unix-realpath
398 :defaults (parse-native-namestring
400 (pathname-host pathname)
401 (sane-default-pathname-defaults)))))
404 (fail "couldn't resolve ~A" filename errno)))
405 (pathname-host pathname)
406 (sane-default-pathname-defaults)
409 (:author (sb!unix:uid-username uid))
410 (:write-date (+ unix-to-universal-time mtime))))))
411 ;; If we're still here, the file doesn't exist; error.
413 (format nil "failed to find the ~A of ~~A" query-for)
414 pathspec errno))))))))
417 (defun probe-file (pathspec)
419 "Return the truename of PATHSPEC if the truename can be found,
420 or NIL otherwise. See TRUENAME for more information."
421 (query-file-system pathspec :truename nil))
423 (defun truename (pathspec)
425 "If PATHSPEC is a pathname that names an existing file, return
426 a pathname that denotes a canonicalized name for the file. If
427 pathspec is a stream associated with a file, return a pathname
428 that denotes a canonicalized name for the file associated with
431 An error of type FILE-ERROR is signalled if no such file exists
432 or if the file system is such that a canonicalized file name
433 cannot be determined or if the pathname is wild.
435 Under Unix, the TRUENAME of a symlink that links to itself or to
436 a file that doesn't exist is considered to be the name of the
437 broken symlink itself."
438 ;; Note that eventually this routine might be different for streams
439 ;; than for other pathname designators.
440 (if (streamp pathspec)
441 (query-file-system pathspec :truename)
442 (query-file-system pathspec :truename)))
444 (defun file-author (pathspec)
446 "Return the author of the file specified by PATHSPEC. Signal an
447 error of type FILE-ERROR if no such file exists, or if PATHSPEC
449 (query-file-system pathspec :author))
451 (defun file-write-date (pathspec)
453 "Return the write date of the file specified by PATHSPEC.
454 An error of type FILE-ERROR is signaled if no such file exists,
455 or if PATHSPEC is a wild pathname."
456 (query-file-system pathspec :write-date))
458 ;;;; miscellaneous other operations
460 (/show0 "filesys.lisp 700")
462 (defun rename-file (file new-name)
464 "Rename FILE to have the specified NEW-NAME. If FILE is a stream open to a
465 file, then the associated file is renamed."
466 (let* ((original (merge-pathnames file (sane-default-pathname-defaults)))
467 (old-truename (truename original))
468 (original-namestring (native-namestring (physicalize-pathname original)
470 (new-name (merge-pathnames new-name original))
471 (new-namestring (native-namestring (physicalize-pathname new-name)
473 (unless new-namestring
474 (error 'simple-file-error
476 :format-control "~S can't be created."
477 :format-arguments (list new-name)))
478 (multiple-value-bind (res error)
479 (sb!unix:unix-rename original-namestring new-namestring)
481 (error 'simple-file-error
483 :format-control "~@<couldn't rename ~2I~_~A ~I~_to ~2I~_~A: ~
485 :format-arguments (list original new-name (strerror error))))
487 (file-name file new-name))
488 (values new-name old-truename (truename new-name)))))
490 (defun delete-file (file)
492 "Delete the specified FILE.
494 If FILE is a stream, on Windows the stream is closed immediately. On Unix
495 plaforms the stream remains open, allowing IO to continue: the OS resources
496 associated with the deleted file remain available till the stream is closed as
497 per standard Unix unlink() behaviour."
498 (let* ((pathname (translate-logical-pathname
499 (merge-pathnames file (sane-default-pathname-defaults))))
500 (namestring (native-namestring pathname :as-file t)))
504 (multiple-value-bind (res err) (sb!unix:unix-unlink namestring)
506 (simple-file-perror "Couldn't delete file ~A" namestring err))))
509 (defun directorize-pathname (pathname)
510 (if (or (pathname-name pathname)
511 (pathname-type pathname))
512 (make-pathname :directory (append (pathname-directory pathname)
513 (list (file-namestring pathname)))
514 :host (pathname-host pathname)
515 :device (pathname-device pathname))
518 (defun delete-directory (pathspec &key recursive)
519 "Deletes the directory designated by PATHSPEC (a pathname designator).
520 Returns the truename of the directory deleted.
522 If RECURSIVE is false \(the default), signals an error unless the directory is
523 empty. If RECURSIVE is true, first deletes all files and subdirectories. If
524 RECURSIVE is true and the directory contains symbolic links, the links are
525 deleted, not the files and directories they point to.
527 Signals an error if PATHSPEC designates a file or a symbolic link instead of a
528 directory, or if the directory could not be deleted for any reason.
532 \(DELETE-DIRECTORY \"/tmp/foo\")
533 \(DELETE-DIRECTORY \"/tmp/foo/\")
535 delete the \"foo\" subdirectory of \"/tmp\", or signal an error if it does not
536 exist or if is a file or a symbolic link."
537 (declare (type pathname-designator pathspec))
538 (let ((physical (directorize-pathname
539 (physicalize-pathname
541 pathspec (sane-default-pathname-defaults))))))
542 (labels ((recurse-merged (dir)
544 (recurse (merge-pathnames sub dir))))
547 (delete-file (merge-pathnames file dir))))
549 (map-directory (recurse-merged dir) dir
552 :classify-symlinks nil)
553 (map-directory (delete-merged dir) dir
556 :classify-symlinks nil)
559 (let* ((namestring (native-namestring dir :as-file t))
560 (res (alien-funcall (extern-alien #!-win32 "rmdir"
562 (function int c-string))
565 (simple-file-perror "Couldn't delete directory ~A"
566 namestring (get-errno))
570 (delete-dir physical)))))
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 sbcl-home
577 #!-win32 sb!impl::*unix-host*
578 #!+win32 sb!impl::*win32-host*
579 *default-pathname-defaults*
582 (defun user-homedir-namestring (&optional username)
584 (sb!unix:user-homedir username)
585 (let ((env-home (posix-getenv "HOME")))
586 (if (and env-home (not (string= env-home "")))
589 (sb!unix:uid-homedir (sb!unix:unix-getuid))))))
591 ;;; (This is an ANSI Common Lisp function.)
592 (defun user-homedir-pathname (&optional host)
594 "Return the home directory of the user as a pathname. If the HOME
595 environment variable has been specified, the directory it designates
596 is returned; otherwise obtains the home directory from the operating
597 system. HOST argument is ignored by SBCL."
598 (declare (ignore host))
600 (parse-native-namestring
601 (or (user-homedir-namestring)
603 (sb!win32::get-folder-namestring sb!win32::csidl_profile))
604 #!-win32 sb!impl::*unix-host*
605 #!+win32 sb!impl::*win32-host*
606 *default-pathname-defaults*
612 (defun directory (pathspec &key (resolve-symlinks t))
614 "Return a list of PATHNAMEs, each the TRUENAME of a file that matched the
615 given pathname. Note that the interaction between this ANSI-specified
616 TRUENAMEing and the semantics of the Unix filesystem (symbolic links..) means
617 this function can sometimes return files which don't have the same directory
618 as PATHNAME. If :RESOLVE-SYMLINKS is NIL, don't resolve symbolic links in
620 (let (;; We create one entry in this hash table for each truename,
621 ;; as an asymptotically efficient way of removing duplicates
622 ;; (which can arise when e.g. multiple symlinks map to the
624 (truenames (make-hash-table :test #'equal)))
625 (labels ((record (pathname)
626 (let ((truename (if resolve-symlinks
627 ;; FIXME: Why not not TRUENAME? As reported by
628 ;; Milan Zamazal sbcl-devel 2003-10-05, using
629 ;; TRUENAME causes a race condition whereby
630 ;; removal of a file during the directory
631 ;; operation causes an error. It's not clear
632 ;; what the right thing to do is, though. --
634 (query-file-system pathname :truename nil)
635 (query-file-system pathname :existence nil))))
637 (setf (gethash (namestring truename) truenames)
639 (do-physical-pathnames (pathname)
640 (aver (not (logical-pathname-p pathname)))
641 (let* (;; KLUDGE: Since we don't canonize pathnames on construction,
642 ;; we really have to do it here to get #p"foo/." mean the same
644 (pathname (canonicalize-pathname pathname))
645 (name (pathname-name pathname))
646 (type (pathname-type pathname))
647 (match-name (make-matcher name))
648 (match-type (make-matcher type)))
649 (map-matching-directories
652 (map-matching-entries #'record
658 (do-pathnames (pathname)
659 (if (logical-pathname-p pathname)
660 (let ((host (intern-logical-host (pathname-host pathname))))
661 (dolist (x (logical-host-canon-transls host))
662 (destructuring-bind (from to) x
664 (pathname-intersections pathname from)))
665 (dolist (p intersections)
666 (do-pathnames (translate-pathname p from to)))))))
667 (do-physical-pathnames pathname))))
668 (declare (truly-dynamic-extent #'record))
669 (do-pathnames (merge-pathnames pathspec)))
671 ;; Sorting isn't required by the ANSI spec, but sorting into some
672 ;; canonical order seems good just on the grounds that the
673 ;; implementation should have repeatable behavior when possible.
674 (sort (loop for namestring being each hash-key in truenames
675 using (hash-value truename)
676 collect (cons namestring truename))
680 (defun canonicalize-pathname (pathname)
681 ;; We're really only interested in :UNSPECIFIC -> NIL, :BACK and :UP,
682 ;; and dealing with #p"foo/.." and #p"foo/."
683 (labels ((simplify (piece)
684 (unless (eq :unspecific piece)
686 (canonicalize-directory (directory)
688 (dolist (piece directory)
689 (if (and pieces (member piece '(:back :up)))
690 ;; FIXME: We should really canonicalize when we construct
691 ;; pathnames. This is just wrong.
693 ((:absolute :wild-inferiors)
694 (error 'simple-file-error
695 :format-control "Invalid use of ~S after ~S."
696 :format-arguments (list piece (car pieces))
698 ((:relative :up :back)
702 (push piece pieces)))
704 (let ((name (simplify (pathname-name pathname)))
705 (type (simplify (pathname-type pathname)))
706 (dir (canonicalize-directory (pathname-directory pathname))))
707 (cond ((equal "." name)
709 (make-pathname :name nil :defaults pathname))
711 (make-pathname :name nil
713 :directory (butlast dir)
714 :defaults pathname))))
716 (make-pathname :name name :type type
718 :defaults pathname))))))
720 ;;; Given a native namestring, provides a WITH-HASH-TABLE-ITERATOR style
721 ;;; interface to mapping over namestrings of entries in the corresponding
723 (defmacro with-native-directory-iterator ((iterator namestring &key errorp) &body body)
724 (with-unique-names (one-iter)
726 ((iterate (,one-iter)
727 (declare (type function ,one-iter))
728 (macrolet ((,iterator ()
729 `(funcall ,',one-iter)))
731 (call-with-native-directory-iterator #'iterate ,namestring ,errorp))))
733 (defun call-with-native-directory-iterator (function namestring errorp)
734 (declare (type (or null string) namestring)
742 (let ((ent (sb!unix:unix-readdir dp nil)))
744 (let ((name (sb!unix:unix-dirent-name ent)))
746 (cond ((equal "." name)
751 (return-from one-iter name))))))))))
754 (setf dp (sb!unix:unix-opendir namestring errorp))
756 (funcall function #'one-iter)))
758 (sb!unix:unix-closedir dp nil)))))))
760 ;;; This is our core directory access interface that we use to implement
762 (defun map-directory (function directory &key (files t) (directories t)
763 (classify-symlinks t) (errorp t))
765 "Map over entries in DIRECTORY. Keyword arguments specify which entries to
769 If true, call FUNCTION with the pathname of each file in DIRECTORY.
773 If true, call FUNCTION with a pathname for each subdirectory of DIRECTORY.
774 If :AS-FILES, the pathname used is a pathname designating the subdirectory
775 as a file in DIRECTORY. Otherwise the pathname used is a directory
776 pathname. Defaults to T.
779 If true, the decision to call FUNCTION with the pathname of a symbolic link
780 depends on the resolution of the link: if it points to a directory, it is
781 considered a directory entry, otherwise a file entry. If false, all
782 symbolic links are considered file entries. In both cases the pathname used
783 for the symbolic link is not fully resolved, but names it as an immediate
784 child of DIRECTORY. Defaults to T.
787 If true, signal an error if DIRECTORY does not exist, cannot be read, etc.
790 Experimental: interface subject to change."
791 (declare (pathname-designator directory))
792 (let* ((fun (%coerce-callable-to-fun function))
793 (as-files (eq :as-files directories))
794 (physical (physicalize-pathname directory))
795 ;; Not QUERY-FILE-SYSTEM :EXISTENCE, since it doesn't work on Windows
797 (realname (sb!unix:unix-realpath (native-namestring physical :as-file t)))
798 (canonical (if realname
799 (parse-native-namestring realname
800 (pathname-host physical)
801 (sane-default-pathname-defaults)
803 (return-from map-directory nil)))
804 (dirname (native-namestring canonical)))
805 (flet ((map-it (name dirp)
807 (merge-pathnames (parse-native-namestring
809 :as-directory (and dirp (not as-files)))
811 (with-native-directory-iterator (next dirname :errorp errorp)
812 (loop for name = (next)
814 do (let* ((full (concatenate 'string dirname name))
815 (kind (native-file-kind full)))
822 (if classify-symlinks
823 (let* ((tmpname (merge-pathnames
824 (parse-native-namestring
825 name nil physical :as-directory nil)
827 (truename (query-file-system tmpname :truename nil)))
828 (if (or (not truename)
829 (or (pathname-name truename) (pathname-type truename)))
831 (funcall fun tmpname))
837 ;; Anything else parses as a file.
839 (map-it name nil)))))))))))
841 ;;; Part of DIRECTORY: implements matching the directory spec. Calls FUNCTION
842 ;;; with all DIRECTORIES that match the directory portion of PATHSPEC.
843 (defun map-matching-directories (function pathspec)
844 (let* ((dir (pathname-directory pathspec))
845 (length (length dir))
846 (wild (position-if (lambda (elt)
847 (or (eq :wild elt) (typep elt 'pattern)))
849 (wild-inferiors (position :wild-inferiors dir))
850 (end (cond ((and wild wild-inferiors)
851 (min wild wild-inferiors))
853 (or wild wild-inferiors length))))
854 (rest (subseq dir end))
855 (starting-point (make-pathname :directory (subseq dir 0 end)
856 :device (pathname-device pathspec)
857 :host (pathname-host pathspec)
861 (cond (wild-inferiors
862 (map-wild-inferiors function rest starting-point))
864 (map-wild function rest starting-point))
866 ;; Nothing wild -- the directory matches itself.
867 (funcall function starting-point))))
870 (defun last-directory-piece (pathname)
871 (car (last (pathname-directory pathname))))
873 ;;; Part of DIRECTORY: implements iterating over a :WILD or pattern component
874 ;;; in the directory spec.
875 (defun map-wild (function more directory)
876 (let ((this (pop more))
878 (flet ((cont (subdirectory)
881 (funcall function subdirectory))
882 ((or (eq :wild next) (typep next 'pattern))
883 (map-wild function more subdirectory))
884 ((eq :wild-inferiors next)
885 (map-wild-inferiors function more subdirectory))
887 (let ((this (pathname-directory subdirectory)))
888 (map-matching-directories
890 (make-pathname :directory (append this more)
891 :defaults subdirectory)))))))
896 (when (pattern-matches this (last-directory-piece sub))
897 (funcall #'cont sub))))
903 ;;; Part of DIRECTORY: implements iterating over a :WILD-INFERIORS component
904 ;;; in the directory spec.
905 (defun map-wild-inferiors (function more directory)
906 (loop while (member (car more) '(:wild :wild-inferiors))
908 (let ((next (car more))
911 (funcall function directory))
915 (funcall function pathname)
916 (map-wild-inferiors function more pathname)))
919 (let ((this (pathname-directory pathname)))
920 (when (equal next (car (last this)))
921 (map-matching-directories
923 (make-pathname :directory (append this rest)
924 :defaults pathname)))
925 (map-wild-inferiors function more pathname)))))
931 ;;; Part of DIRECTORY: implements iterating over entries in a directory, and
933 (defun map-matching-entries (function directory match-name match-type)
936 (when (and (funcall match-name (pathname-name file))
937 (funcall match-type (pathname-type file)))
938 (funcall function file)))
941 :directories :as-files
944 ;;; NOTE: There is a fair amount of hair below that is probably not
945 ;;; strictly necessary.
947 ;;; The issue is the following: what does (DIRECTORY "SYS:*;") mean?
948 ;;; Until 2004-01, SBCL's behaviour was unquestionably wrong, as it
949 ;;; did not translate the logical pathname at all, but instead treated
950 ;;; it as a physical one. Other Lisps seem to to treat this call as
951 ;;; equivalent to (DIRECTORY (TRANSLATE-LOGICAL-PATHNAME "SYS:*;")),
952 ;;; which is fine as far as it goes, but not very interesting, and
953 ;;; arguably counterintuitive. (PATHNAME-MATCH-P "SYS:SRC;" "SYS:*;")
954 ;;; is true, so why should "SYS:SRC;" not show up in the call to
955 ;;; DIRECTORY? (assuming the physical pathname corresponding to it
956 ;;; exists, of course).
958 ;;; So, the interpretation that I am pushing is for all pathnames
959 ;;; matching the input pathname to be queried. This means that we
960 ;;; need to compute the intersection of the input pathname and the
961 ;;; logical host FROM translations, and then translate the resulting
962 ;;; pathname using the host to the TO translation; this treatment is
963 ;;; recursively invoked until we get a physical pathname, whereupon
964 ;;; our physical DIRECTORY implementation takes over.
966 ;;; FIXME: this is an incomplete implementation. It only works when
967 ;;; both are logical pathnames (which is OK, because that's the only
968 ;;; case when we call it), but there are other pitfalls as well: see
969 ;;; the DIRECTORY-HELPER below for some, but others include a lack of
970 ;;; pattern handling.
972 ;;; The above was written by CSR, I (RMK) believe. The argument that
973 ;;; motivates the interpretation is faulty, however: PATHNAME-MATCH-P
974 ;;; returns true for (PATHNAME-MATCH-P #P"/tmp/*/" #P"/tmp/../"), but
975 ;;; the latter pathname is not in the result of DIRECTORY on the
976 ;;; former. Indeed, if DIRECTORY were constrained to return the
977 ;;; truename for every pathname for which PATHNAME-MATCH-P returned
978 ;;; true and which denoted a filename that named an existing file,
979 ;;; (DIRECTORY #P"/tmp/**/") would be required to list every file on a
980 ;;; Unix system, since any file can be named as though it were "below"
981 ;;; /tmp, given the dotdot entries. So I think the strongest
982 ;;; "consistency" we can define between PATHNAME-MATCH-P and DIRECTORY
983 ;;; is that PATHNAME-MATCH-P returns true of everything DIRECTORY
984 ;;; returns, but not vice versa.
986 ;;; In any case, even if the motivation were sound, DIRECTORY on a
987 ;;; wild logical pathname has no portable semantics. I see nothing in
988 ;;; ANSI that requires implementations to support wild physical
989 ;;; pathnames, and so there need not be any translation of a wild
990 ;;; logical pathname to a phyiscal pathname. So a program that calls
991 ;;; DIRECTORY on a wild logical pathname is doing something
992 ;;; non-portable at best. And if the only sensible semantics for
993 ;;; DIRECTORY on a wild logical pathname is something like the
994 ;;; following, it would be just as well if it signaled an error, since
995 ;;; a program can't possibly rely on the result of an intersection of
996 ;;; user-defined translations with a file system probe. (Potentially
997 ;;; useful kinds of "pathname" that might not support wildcards could
998 ;;; include pathname hosts that model unqueryable namespaces like HTTP
999 ;;; URIs, or that model namespaces that it's not convenient to
1000 ;;; investigate, such as the namespace of TCP ports that some network
1001 ;;; host listens on. I happen to think it a bad idea to try to
1002 ;;; shoehorn such namespaces into a pathnames system, but people
1003 ;;; sometimes claim to want pathnames for these things.) -- RMK
1006 (defun pathname-intersections (one two)
1007 (aver (logical-pathname-p one))
1008 (aver (logical-pathname-p two))
1010 ((intersect-version (one two)
1011 (aver (typep one '(or null (member :newest :wild :unspecific)
1013 (aver (typep two '(or null (member :newest :wild :unspecific)
1016 ((eq one :wild) two)
1017 ((eq two :wild) one)
1018 ((or (null one) (eq one :unspecific)) two)
1019 ((or (null two) (eq two :unspecific)) one)
1022 (intersect-name/type (one two)
1023 (aver (typep one '(or null (member :wild :unspecific) string)))
1024 (aver (typep two '(or null (member :wild :unspecific) string)))
1026 ((eq one :wild) two)
1027 ((eq two :wild) one)
1028 ((or (null one) (eq one :unspecific)) two)
1029 ((or (null two) (eq two :unspecific)) one)
1030 ((string= one two) one)
1031 (t (return-from pathname-intersections nil))))
1032 (intersect-directory (one two)
1033 (aver (typep one '(or null (member :wild :unspecific) list)))
1034 (aver (typep two '(or null (member :wild :unspecific) list)))
1036 ((eq one :wild) two)
1037 ((eq two :wild) one)
1038 ((or (null one) (eq one :unspecific)) two)
1039 ((or (null two) (eq two :unspecific)) one)
1040 (t (aver (eq (car one) (car two)))
1042 (lambda (x) (cons (car one) x))
1043 (intersect-directory-helper (cdr one) (cdr two)))))))
1044 (let ((version (intersect-version
1045 (pathname-version one) (pathname-version two)))
1046 (name (intersect-name/type
1047 (pathname-name one) (pathname-name two)))
1048 (type (intersect-name/type
1049 (pathname-type one) (pathname-type two)))
1050 (host (pathname-host one)))
1052 (make-pathname :host host :name name :type type
1053 :version version :directory d))
1054 (intersect-directory
1055 (pathname-directory one) (pathname-directory two))))))
1057 ;;; FIXME: written as its own function because I (CSR) don't
1058 ;;; understand it, so helping both debuggability and modularity. In
1059 ;;; case anyone is motivated to rewrite it, it returns a list of
1060 ;;; sublists representing the intersection of the two input directory
1061 ;;; paths (excluding the initial :ABSOLUTE or :RELATIVE).
1063 ;;; FIXME: Does not work with :UP or :BACK
1064 ;;; FIXME: Does not work with patterns
1066 ;;; FIXME: PFD suggests replacing this implementation with a DFA
1067 ;;; conversion of a NDFA. Find out (a) what this means and (b) if it
1068 ;;; turns out to be worth it.
1069 (defun intersect-directory-helper (one two)
1070 (flet ((simple-intersection (cone ctwo)
1072 ((eq cone :wild) ctwo)
1073 ((eq ctwo :wild) cone)
1074 (t (aver (typep cone 'string))
1075 (aver (typep ctwo 'string))
1076 (if (string= cone ctwo) cone nil)))))
1078 ((loop-possible-wild-inferiors-matches
1079 (lower-bound bounding-sequence order)
1080 (let ((index (gensym)) (g2 (gensym)) (g3 (gensym)) (l (gensym)))
1081 `(let ((,l (length ,bounding-sequence)))
1082 (loop for ,index from ,lower-bound to ,l
1083 append (mapcar (lambda (,g2)
1085 (butlast ,bounding-sequence (- ,l ,index))
1090 (if (eq (car (nthcdr ,index ,bounding-sequence))
1094 (intersect-directory-helper
1096 `((nthcdr ,index one) (cdr two))
1097 `((cdr one) (nthcdr ,index two)))))))))))
1099 ((and (eq (car one) :wild-inferiors)
1100 (eq (car two) :wild-inferiors))
1102 (append (mapcar (lambda (x) (cons :wild-inferiors x))
1103 (intersect-directory-helper (cdr one) (cdr two)))
1104 (loop-possible-wild-inferiors-matches 2 one t)
1105 (loop-possible-wild-inferiors-matches 2 two nil))
1107 ((eq (car one) :wild-inferiors)
1108 (delete-duplicates (loop-possible-wild-inferiors-matches 0 two nil)
1110 ((eq (car two) :wild-inferiors)
1111 (delete-duplicates (loop-possible-wild-inferiors-matches 0 one t)
1113 ((and (null one) (null two)) (list nil))
1116 (t (and (simple-intersection (car one) (car two))
1117 (mapcar (lambda (x) (cons (simple-intersection
1118 (car one) (car two)) x))
1119 (intersect-directory-helper (cdr one) (cdr two)))))))))
1122 (defun directory-pathname-p (pathname)
1123 (and (pathnamep pathname)
1124 (null (pathname-name pathname))
1125 (null (pathname-type pathname))))
1127 (defun ensure-directories-exist (pathspec &key verbose (mode #o777))
1129 "Test whether the directories containing the specified file
1130 actually exist, and attempt to create them if they do not.
1131 The MODE argument is a CMUCL/SBCL-specific extension to control
1132 the Unix permission bits."
1133 (let ((pathname (physicalize-pathname (merge-pathnames (pathname pathspec))))
1135 (when (wild-pathname-p pathname)
1136 (error 'simple-file-error
1137 :format-control "bad place for a wild pathname"
1138 :pathname pathspec))
1139 (let* ((dir (pathname-directory pathname))
1140 ;; *d-p-d* can have name and type components which would prevent
1141 ;; PROBE-FILE below from working
1142 (*default-pathname-defaults*
1143 (make-pathname :directory dir :device (pathname-device pathname))))
1144 (loop for i from 1 upto (length dir)
1146 (let* ((newpath (make-pathname
1147 :host (pathname-host pathname)
1148 :device (pathname-device pathname)
1149 :directory (subseq dir 0 i)))
1150 (probed (probe-file newpath)))
1151 (unless (directory-pathname-p probed)
1152 (let ((namestring (coerce (native-namestring newpath)
1155 (format *standard-output*
1156 "~&creating directory: ~A~%"
1158 (sb!unix:unix-mkdir namestring mode)
1159 (unless (directory-pathname-p (probe-file newpath))
1166 (not (directory-pathname-p probed)))
1167 "Can't create directory ~A,~
1168 ~%a file with the same name already exists."
1169 "Can't create directory ~A")
1170 :format-arguments (list namestring))
1172 :report "Retry directory creation."
1173 (ensure-directories-exist
1175 :verbose verbose :mode mode))
1178 "Continue as if directory creation was successful."
1180 (setf created-p t)))))
1181 (values pathspec created-p))))
1183 (/show0 "filesys.lisp 1000")