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 an escape
53 ;;; character to cause it to be treated as a regular character.
54 (defun remove-escape-characters (namestr start end escape-char
)
55 "Remove any occurrences of escape characters from the string
56 because we've already checked for whatever they may have
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 escape-char
)
74 (setf (schar result dst
) char
)
77 (error 'namestring-parse-error
78 :complaint
"escape char in a bad place"
81 (%shrink-vector result dst
)))
83 (defun maybe-make-pattern (namestr start end escape-char
)
84 (declare (type simple-string namestr
)
85 (type index start end
)
86 (type character escape-char
))
90 (last-regular-char nil
)
94 (remove-escape-characters namestr last-regular-char
96 (subseq namestr last-regular-char index
)))
97 (flush-pending-regulars ()
98 (when last-regular-char
100 (setf any-quotes nil
)
101 (setf last-regular-char nil
))))
105 (let ((char (schar namestr index
)))
109 ((char= char escape-char
)
112 (unless last-regular-char
113 (setf last-regular-char index
))
116 (flush-pending-regulars)
117 (pattern :single-char-wild
)
120 (flush-pending-regulars)
121 (pattern :multi-char-wild
)
124 (flush-pending-regulars)
126 (position #\
] namestr
:start index
:end end
)))
127 (unless close-bracket
128 (error 'namestring-parse-error
129 :complaint
"#\\[ with no corresponding #\\]"
132 (pattern (cons :character-set
136 (setf index
(1+ close-bracket
))))
138 (unless last-regular-char
139 (setf last-regular-char index
))
141 (cond ((null (pattern))
142 (if last-regular-char
146 (flush-pending-regulars)
147 (null (cdr (pattern))))
148 (let ((piece (first (pattern))))
149 (if (eq piece
:multi-char-wild
)
151 (make-pattern (pattern)))))
153 (make-pattern (pattern))))))))
155 (declaim (ftype (sfunction ((or (eql :wild
) simple-string pattern
) character
&key
(:escape-dot t
))
157 unparse-physical-piece
))
158 (defun unparse-physical-piece (thing escape-char
&key escape-dot
)
161 (declare (type index length
))
162 (labels ((needs-escaping-p (char index
)
163 (or (char= char
#\
*) (char= char
#\?)
164 (char= char
#\
[) (char= char escape-char
)
166 (:unless-at-start
(and (plusp index
) (char= char
#\.
)))
167 ((t) (char= char
#\.
)))))
168 (inspect-fragment (fragment)
174 (incf length
(length fragment
))
175 (Loop with complicated
= nil
176 for char across
(the simple-string fragment
)
178 when
(needs-escaping-p char i
)
179 do
(setf complicated t
)
181 finally
(return complicated
)))
183 (mapcar (lambda (piece)
186 (inspect-fragment piece
))
187 ((member :multi-char-wild
:single-char-wild
)
190 ((cons (eql :character-set
))
191 (incf length
(+ 2 (length (cdr piece
))))
193 (pattern-pieces fragment
))))))
194 (setf complicated
(inspect-fragment thing
))
196 (return-from unparse-physical-piece thing
))
197 (let ((result (make-string length
))
199 (declare (type (simple-array character
1) result
)
201 (labels ((output-character (character)
202 (setf (aref result index
) character
)
204 (output-string (string)
205 (declare (type (simple-array character
1) string
))
206 (setf (subseq result index
) string
)
207 (incf index
(length string
)))
208 (unparse-fragment (fragment)
211 (output-character #\
*))
213 (loop for char across
(the simple-string fragment
)
215 when
(needs-escaping-p char i
)
216 do
(output-character escape-char
)
217 do
(output-character char
)))
219 (mapc (lambda (piece piece-complicated
)
222 (if piece-complicated
223 (unparse-fragment piece
)
224 (output-string piece
)))
225 ((eql :multi-char-wild
)
226 (output-character #\
*))
227 ((eql :single-char-wild
)
228 (output-character #\?))
229 ((cons (eql :character-set
))
230 (output-character #\
[)
231 (output-string (cdr piece
))
232 (output-character #\
]))))
233 (pattern-pieces fragment
) complicated
)))))
234 (declare (inline output-character output-string
))
235 (unparse-fragment thing
))
238 (defun make-matcher (piece)
239 (cond ((eq piece
:wild
)
241 ((typep piece
'pattern
)
243 (when (stringp other
)
244 (pattern-matches piece other
))))
247 (equal piece other
)))))
249 (defun extract-name-type-and-version (namestr start end escape-char
)
250 (declare (type simple-string namestr
)
251 (type index start end
))
253 (and (>= i start
) (char= (aref namestr i
) escape-char
))))
255 (loop for i from
(1- end
) downto
(1+ start
)
256 when
(and (char= (aref namestr i
) #\.
)
257 (or (not (escape-p (1- i
)))
261 (values (maybe-make-pattern namestr start last-dot escape-char
)
262 (maybe-make-pattern namestr
(1+ last-dot
) end escape-char
)
264 (values (maybe-make-pattern namestr start end escape-char
)
269 ;;;; Grabbing the kind of file when we have a native-namestring.
270 (defun native-file-kind (namestring &optional resolve-symlinks
)
271 #+win32
(declare (ignore resolve-symlinks
))
272 #+win32
(nth-value 1 (sb-win32::native-probe-file-name namestring
))
274 (multiple-value-bind (existsp errno ino mode
)
275 ;; Note that when resolve-symlinks is true, we'll return NIL if
276 ;; there are circular and dangling symlinks anywhere in the
277 ;; path. That's different than what our TRUENAME does; this is
278 ;; intended as an efficient internal routine.
280 (sb-unix:unix-stat namestring
)
281 (sb-unix:unix-lstat namestring
))
282 (declare (ignore errno ino
))
284 (let ((ifmt (logand mode sb-unix
:s-ifmt
)))
286 (#.sb-unix
:s-ifreg
:file
)
287 (#.sb-unix
:s-ifdir
:directory
)
288 (#.sb-unix
:s-iflnk
:symlink
)
291 ;;;; TRUENAME, PROBE-FILE, FILE-AUTHOR, FILE-WRITE-DATE.
293 ;;; Rewritten in 12/2007 by RMK, replacing 13+ year old CMU code that
294 ;;; made a mess of things in order to support search lists (which SBCL
295 ;;; has never had). These are now all relatively straightforward
296 ;;; wrappers around stat(2) and realpath(2), with the same basic logic
297 ;;; in all cases. The wrinkles to be aware of:
299 ;;; * SBCL defines the truename of an existing, dangling or
300 ;;; self-referring symlink to be the symlink itself.
301 ;;; * The old version of PROBE-FILE merged the pathspec against
302 ;;; *DEFAULT-PATHNAME-DEFAULTS* twice, and so lost when *D-P-D*
303 ;;; was a relative pathname. Even if the case where *D-P-D* is a
304 ;;; relative pathname is problematic, there's no particular reason
305 ;;; to get that wrong, so let's try not to.
306 ;;; * Note that while stat(2) is probably atomic, getting the truename
307 ;;; for a filename involves poking all over the place, and so is
308 ;;; subject to race conditions if other programs mutate the file
309 ;;; system while we're resolving symlinks. So it's not implausible for
310 ;;; realpath(3) to fail even if stat(2) succeeded. There's nothing
311 ;;; obvious we can do about this, however.
312 ;;; * Windows' apparent analogue of realpath(3) is called
313 ;;; GetFullPathName, and it's a bit less useful than realpath(3).
314 ;;; In particular, while realpath(3) errors in case the file doesn't
315 ;;; exist, GetFullPathName seems to return a filename in all cases.
316 ;;; As realpath(3) is not atomic anyway, we only ever call it when
317 ;;; we think a file exists, so just be careful when rewriting this
320 ;;; Given a pathname designator, some quality to query for, return one
321 ;;; of a pathname, a universal time, or a string (a file-author), or
322 ;;; NIL. QUERY-FOR may be one of :TRUENAME, :EXISTENCE, :WRITE-DATE,
323 ;;; :AUTHOR. If ERRORP is false, return NIL in case the file system
324 ;;; returns an error code; otherwise, signal an error. Accepts
325 ;;; logical pathnames, too (but never returns LPNs). For internal
327 (defun query-file-system (pathspec query-for
&optional
(errorp t
))
332 (let ((pathname (translate-logical-pathname
335 (sane-default-pathname-defaults)))))
336 (when (wild-pathname-p pathname
)
337 (sb-kernel::%file-error
338 pathname
"Can't find the ~A of wild pathname ~A (physicalized from ~A)."
339 query-for pathname pathspec
))
340 (return (%query-file-system pathname query-for errorp
)))
342 :report
"Specify a different path."
343 :interactive read-evaluated-form
344 (setf pathspec value
)
348 (defun %query-file-system
(pathname query-for errorp
)
349 (let ((filename (native-namestring pathname
:as-file t
)))
351 ((:existence
:truename
)
352 (multiple-value-bind (file kind
)
353 (sb-win32::native-probe-file-name filename
)
354 (when (and (not file
) kind
)
355 (setf file filename
))
356 ;; The following OR was an AND, but that breaks files like NUL,
357 ;; for which GetLongPathName succeeds yet GetFileAttributesEx
358 ;; fails to return the file kind. --DFL
361 (values (parse-native-namestring
363 (pathname-host pathname
)
364 (sane-default-pathname-defaults)
365 :as-directory
(eq :directory kind
))))
367 (file-perror filename
(sb-win32:get-last-error
)
368 "Failed to find the ~A of ~S" query-for filename
)))))
371 ((sb-win32::native-file-write-date filename
))
373 (file-perror filename
(sb-win32:get-last-error
)
374 "Failed to find the ~A of ~S" query-for filename
)))))))
377 (defun %query-file-system
(pathname query-for errorp
)
378 (labels ((parse (filename &key as-directory
)
379 (values (parse-native-namestring
381 (pathname-host pathname
)
382 (sane-default-pathname-defaults)
383 :as-directory as-directory
)))
384 (directory-part-realpath (filename)
385 ;; So here's a trick: since lstat succeeded, FILENAME
386 ;; exists, so its directory exists and only the
387 ;; non-directory part is loopy. So let's resolve
388 ;; FILENAME's directory part with realpath(3), in order
389 ;; to get a canonical absolute name for the directory,
390 ;; and then return a pathname having PATHNAME's name,
391 ;; type, and version, but the rest from the truename of
392 ;; the directory. Since we turned PATHNAME into FILENAME
393 ;; "as a file", FILENAME does not end in a slash, and so
394 ;; we get the directory part of FILENAME by reparsing
395 ;; FILENAME and masking off its name, type, and version
396 ;; bits. But note not to call ourselves recursively,
397 ;; because we don't want to re-merge against
398 ;; *DEFAULT-PATHNAME-DEFAULTS*, since PATHNAME may be a
399 ;; relative pathname.
400 (multiple-value-bind (realpath errno
)
401 (sb-unix:unix-realpath
407 :defaults
(parse filename
))))
410 (parse realpath
:as-directory t
))
412 (file-perror filename errno
"Couldn't resolve ~S" filename
)))))
413 (resolve-problematic-symlink (filename errno realpath-failed
)
414 ;; SBCL has for many years had a policy that a pathname
415 ;; that names an existing, dangling or self-referential
416 ;; symlink denotes the symlink itself. stat(2) fails
417 ;; and sets errno to ENOENT or ELOOP respectively, but
418 ;; we must distinguish cases where the symlink exists
419 ;; from ones where there's a loop in the apparent
420 ;; containing directory.
421 ;; Also handles symlinks in /proc/pid/fd/ to
422 ;; pipes or sockets on Linux
423 (multiple-value-bind (linkp ignore ino mode nlink uid gid rdev
425 (sb-unix:unix-lstat filename
)
426 (declare (ignore ignore ino mode nlink gid rdev size atime
))
428 ((and (or (= errno sb-unix
:enoent
)
429 (= errno sb-unix
:eloop
)
434 ;; We do this reparse so as to return a
435 ;; normalized pathname.
438 (let ((realpath (directory-part-realpath filename
)))
442 (if (directory-pathname-p pathname
)
443 (parse (car (last (pathname-directory pathname
))))
445 (:author
(sb-unix:uid-username uid
))
446 (:write-date
(+ unix-to-universal-time mtime
))))
447 ;; The file doesn't exist; maybe error.
451 "Failed to find the ~A of ~A" query-for pathname
))))))
452 (binding* ((filename (native-namestring pathname
:as-file t
))
453 ((existsp errno nil mode nil uid nil nil nil nil mtime
)
454 (sb-unix:unix-stat filename
)))
458 (parse filename
:as-directory
(eql (logand mode sb-unix
:s-ifmt
)
461 ;; Note: in case the file is stat'able, POSIX
462 ;; realpath(3) gets us a canonical absolute filename,
463 ;; even if the post-merge PATHNAME is not absolute
464 (parse (or (sb-unix:unix-realpath filename
)
465 (resolve-problematic-symlink filename errno t
))
466 :as-directory
(eql (logand mode sb-unix
:s-ifmt
)
468 (:author
(sb-unix:uid-username uid
))
469 (:write-date
(+ unix-to-universal-time mtime
)))
470 (resolve-problematic-symlink filename errno nil
)))))
472 (defun probe-file (pathspec)
473 "Return the truename of PATHSPEC if the truename can be found,
474 or NIL otherwise. See TRUENAME for more information."
475 (query-file-system pathspec
:truename nil
))
477 (defun truename (pathspec)
478 "If PATHSPEC is a pathname that names an existing file, return
479 a pathname that denotes a canonicalized name for the file. If
480 pathspec is a stream associated with a file, return a pathname
481 that denotes a canonicalized name for the file associated with
484 An error of type FILE-ERROR is signalled if no such file exists
485 or if the file system is such that a canonicalized file name
486 cannot be determined or if the pathname is wild.
488 Under Unix, the TRUENAME of a symlink that links to itself or to
489 a file that doesn't exist is considered to be the name of the
490 broken symlink itself."
491 ;; Note that eventually this routine might be different for streams
492 ;; than for other pathname designators.
493 (if (streamp pathspec
)
494 (query-file-system pathspec
:truename
)
495 (query-file-system pathspec
:truename
)))
497 (defun file-author (pathspec)
498 "Return the author of the file specified by PATHSPEC. Signal an
499 error of type FILE-ERROR if no such file exists, or if PATHSPEC
501 (query-file-system pathspec
:author
))
503 (defun file-write-date (pathspec)
504 "Return the write date of the file specified by PATHSPEC.
505 An error of type FILE-ERROR is signaled if no such file exists,
506 or if PATHSPEC is a wild pathname."
507 (query-file-system pathspec
:write-date
))
509 ;;;; miscellaneous other operations
511 (defun rename-file (file new-name
)
512 "Rename FILE to have the specified NEW-NAME. If FILE is a stream open to a
513 file, then the associated file is renamed."
514 (let* ((original (merge-pathnames file
(sane-default-pathname-defaults)))
515 (old-truename (truename original
))
516 (original-namestring (native-namestring (physicalize-pathname original
)
518 (new-name (merge-pathnames new-name original
))
519 (new-namestring (native-namestring (physicalize-pathname new-name
)
521 (unless new-namestring
522 (sb-kernel::%file-error new-name
"~S can't be created." new-name
))
523 (multiple-value-bind (res error
)
524 (sb-unix:unix-rename original-namestring new-namestring
)
528 "~@<couldn't rename ~2I~_~A ~I~_to ~2I~_~A~:>" original new-name
))
530 (file-name file new-name
))
531 (values new-name old-truename
(truename new-name
)))))
533 (defun delete-file (file)
534 "Delete the specified FILE.
536 If FILE is a stream, on Windows the stream is closed immediately. On Unix
537 platforms the stream remains open, allowing IO to continue: the OS resources
538 associated with the deleted file remain available till the stream is closed as
539 per standard Unix unlink() behaviour."
540 (let* ((pathname (translate-logical-pathname
541 (merge-pathnames file
(sane-default-pathname-defaults))))
542 (namestring (native-namestring pathname
:as-file t
)))
546 (multiple-value-bind (res err
)
547 #-win32
(sb-unix:unix-unlink namestring
)
548 #+win32
(or (sb-win32::native-delete-file namestring
)
549 (values nil
(sb-win32:get-last-error
)))
551 (with-simple-restart (continue "Return T")
552 (file-perror namestring err
'delete-file-error
)))))
555 (defun directorize-pathname (pathname)
557 ((wild-pathname-p pathname
)
558 (sb-kernel::%file-error
559 pathname
"Cannot compute directory pathname for wild pathname ~S" pathname
))
560 ((let* ((name (pathname-name pathname
))
561 (namep (pathname-component-present-p name
))
562 (type (pathname-type pathname
))
563 (typep (pathname-component-present-p type
)))
564 (when (or namep typep
)
565 (let ((from-file (format nil
"~:[~*~;~A~]~:[~*~;.~A~]"
566 namep name typep type
)))
568 :host
(pathname-host pathname
)
569 :device
(pathname-device pathname
)
570 :directory
(append (pathname-directory pathname
)
571 (list from-file
)))))))
575 (defun delete-directory (pathspec &key recursive
)
576 "Deletes the directory designated by PATHSPEC (a pathname designator).
577 Returns the truename of the directory deleted.
579 If RECURSIVE is false \(the default), signals an error unless the directory is
580 empty. If RECURSIVE is true, first deletes all files and subdirectories. If
581 RECURSIVE is true and the directory contains symbolic links, the links are
582 deleted, not the files and directories they point to.
584 Signals an error if PATHSPEC designates a file or a symbolic link instead of a
585 directory, or if the directory could not be deleted for any reason.
589 \(DELETE-DIRECTORY \"/tmp/foo\")
590 \(DELETE-DIRECTORY \"/tmp/foo/\")
592 delete the \"foo\" subdirectory of \"/tmp\", or signal an error if it does not
593 exist or if is a file or a symbolic link."
594 (declare (type pathname-designator pathspec
))
595 (labels ((recurse-merged (dir)
597 (recurse (merge-pathnames sub dir
))))
600 (delete-file (merge-pathnames file dir
))))
602 (map-directory (recurse-merged dir
) dir
605 :classify-symlinks nil
)
606 (map-directory (delete-merged dir
) dir
609 :classify-symlinks nil
)
612 (let ((namestring (native-namestring dir
:as-file t
)))
613 (multiple-value-bind (res errno
)
615 (or (sb-win32::native-delete-directory namestring
)
616 (values nil
(sb-win32:get-last-error
)))
619 (not (minusp (alien-funcall
620 (extern-alien "rmdir"
621 (function int c-string
))
626 (file-perror namestring errno
627 "Could not delete directory ~S" namestring
))))))
628 (let ((physical (directorize-pathname
629 (physicalize-pathname
631 pathspec
(sane-default-pathname-defaults))))))
634 (delete-dir physical
)))))
637 (defun sbcl-homedir-pathname ()
638 sb-sys
::*sbcl-homedir-pathname
*)
640 (defun %sbcl-homedir-pathname
()
641 ;; Should we absoluteize this if it was obtained automatically?
642 ;; Depends whether people are in the habit of using chdir within Lisp.
643 (labels ((parse (namestring &optional
(directory t
))
644 (parse-native-namestring namestring
646 *default-pathname-defaults
*
647 :as-directory directory
))
649 (let ((contrib (merge-pathnames "contrib/" path
)))
650 (when (probe-file contrib
)
652 (try-runtime-home (path)
654 (probe (merge-pathnames "../lib/sbcl/" path
)))))
655 (let* ((env (posix-getenv "SBCL_HOME"))
656 (env (and env
(not (string= env
""))
660 (try-runtime-home (parse (or (extern-alien "sbcl_runtime_home" c-string
)
664 (let ((runtime (extern-alien "sbcl_runtime" c-string
)))
666 (let ((truename (probe-file (parse runtime nil
))))
668 (try-runtime-home (make-pathname :name nil
670 :defaults truename
))))))))))
672 (flet ((not-empty (x)
673 (and (not (equal x
"")) x
))
674 (lose (&optional username
)
675 (error "Couldn't find home directory~@[ for ~S~]." username
)))
678 (defun user-homedir-namestring (&optional username
)
680 (sb-unix:user-homedir username
)
681 (or (not-empty (posix-getenv "HOME"))
682 (not-empty (sb-unix:uid-homedir
(sb-unix:unix-getuid
)))
686 (defun user-homedir-namestring (&optional username
)
689 (or (not-empty (posix-getenv "HOME"))
690 (not-empty (posix-getenv "USERPROFILE"))
691 (let ((drive (not-empty (posix-getenv "HOMEDRIVE")))
692 (path (not-empty (posix-getenv "HOMEPATH"))))
694 (concatenate 'string drive path
)))
697 ;;; (This is an ANSI Common Lisp function.)
698 (defun user-homedir-pathname (&optional host
)
699 "Return the home directory of the user as a pathname. If the HOME
700 environment variable has been specified, the directory it designates
701 is returned; otherwise obtains the home directory from the operating
702 system. HOST argument is ignored by SBCL."
703 (declare (ignore host
))
705 (parse-native-namestring
706 (or (user-homedir-namestring)
708 (sb-win32::get-folder-namestring sb-win32
::csidl_profile
))
710 *default-pathname-defaults
*
716 (defun directory (pathspec &key
(resolve-symlinks t
))
717 "Return a list of PATHNAMEs, each the TRUENAME of a file matching PATHSPEC.
719 Note that the interaction between this ANSI-specified TRUENAMEing and
720 the semantics of the Unix filesystem (symbolic links..) means this
721 function can sometimes return files which don't have the same
722 directory as PATHSPEC.
724 If :RESOLVE-SYMLINKS is NIL, don't resolve symbolic links in matching
726 (let (;; We create one entry in this hash table for each truename,
727 ;; as an asymptotically efficient way of removing duplicates
728 ;; (which can arise when e.g. multiple symlinks map to the
730 (truenames (make-hash-table :test
#'equal
)))
731 (labels ((record (pathname)
732 (let ((truename (if resolve-symlinks
733 ;; FIXME: Why not not TRUENAME? As reported by
734 ;; Milan Zamazal sbcl-devel 2003-10-05, using
735 ;; TRUENAME causes a race condition whereby
736 ;; removal of a file during the directory
737 ;; operation causes an error. It's not clear
738 ;; what the right thing to do is, though. --
740 (%query-file-system pathname
:truename nil
)
741 (%query-file-system pathname
:existence nil
))))
743 (setf (gethash (namestring truename
) truenames
)
745 (do-physical-pathnames (pathname)
746 (aver (not (logical-pathname-p pathname
)))
747 (let* (;; KLUDGE: Since we don't canonize pathnames on construction,
748 ;; we really have to do it here to get #p"foo/." mean the same
750 (pathname (canonicalize-pathname pathname
))
751 (name (pathname-name pathname
))
752 (type (pathname-type pathname
))
753 (match-name (make-matcher name
))
754 (match-type (make-matcher type
)))
755 (map-matching-directories
758 (map-matching-entries #'record
764 (do-pathnames (pathname)
765 (if (logical-pathname-p pathname
)
766 (let ((host (intern-logical-host (pathname-host pathname
))))
767 (dolist (x (logical-host-canon-transls host
))
768 (destructuring-bind (from to
) x
770 (pathname-intersections pathname from
)))
771 (dolist (p intersections
)
772 (do-pathnames (translate-pathname p from to
)))))))
773 (do-physical-pathnames pathname
))))
774 (declare (dynamic-extent #'record
))
775 (do-pathnames (merge-pathnames pathspec
)))
776 ;; Sorting isn't required by the ANSI spec, but sorting into some
777 ;; canonical order seems good just on the grounds that the
778 ;; implementation should have repeatable behavior when possible.
779 (let ((result (sort (loop for namestring being each hash-key in truenames
780 using
(hash-value truename
)
781 collect
(cons namestring truename
))
782 #'string
< :key
#'car
)))
783 (map-into result
#'cdr result
))))
785 (defun canonicalize-pathname (pathname)
786 ;; We're really only interested in :UNSPECIFIC -> NIL, :BACK and :UP,
787 ;; and dealing with #p"foo/.." and #p"foo/."
788 (labels ((simplify (piece)
789 (unless (eq :unspecific piece
)
791 (canonicalize-directory (directory)
793 (dolist (piece directory
)
795 ((and pieces
(member piece
'(:back
:up
)))
796 ;; FIXME: We should really canonicalize when we construct
797 ;; pathnames. This is just wrong.
799 ((:absolute
:wild-inferiors
)
800 (sb-kernel::%file-error
802 "Invalid use of ~S after ~S." piece
(car pieces
)))
803 ((:relative
:up
:back
)
808 ;; This case only really matters on Windows,
809 ;; because on POSIX, our call site (TRUENAME via
810 ;; QUERY-FILE-SYSTEM) only passes in pathnames from
811 ;; realpath(3), in which /./ has been removed
812 ;; already. Windows, however, depends on us to
813 ;; perform this fixup. -- DFL
816 (push piece pieces
))))
818 (let ((name (simplify (pathname-name pathname
)))
819 (type (simplify (pathname-type pathname
)))
820 (dir (canonicalize-directory (pathname-directory pathname
))))
822 ((not (equal "." name
))
823 (make-pathname :name name
:type type
:directory dir
826 (make-pathname :name nil
:defaults pathname
))
828 (make-pathname :name nil
:type nil
:directory
(butlast dir
)
831 (make-pathname :name name
:type type
:directory dir
832 :defaults pathname
))))))
834 ;;; Given a native namestring, provides a WITH-HASH-TABLE-ITERATOR style
835 ;;; interface to mapping over namestrings of entries in the corresponding
837 (defmacro with-native-directory-iterator
((iterator namestring
&key errorp
) &body body
)
838 (with-unique-names (one-iter)
840 ((iterate (,one-iter
)
841 (declare (type function
,one-iter
))
842 (macrolet ((,iterator
()
843 `(funcall ,',one-iter
)))
846 (sb-win32::native-call-with-directory-iterator
847 #'iterate
,namestring
,errorp
)
849 (call-with-native-directory-iterator #'iterate
,namestring
,errorp
))))
851 (defun call-with-native-directory-iterator (function namestring errorp
)
852 (declare (type (or null string
) namestring
)
860 (let ((ent (sb-unix:unix-readdir dp nil
)))
862 (let ((name (sb-unix:unix-dirent-name ent
)))
864 (cond ((equal "." name
)
869 (return-from one-iter name
))))))))))
872 (setf dp
(sb-unix:unix-opendir namestring errorp
))
874 (funcall function
#'one-iter
)))
876 (sb-unix:unix-closedir dp nil
)))))))
878 ;;; This is our core directory access interface that we use to implement
880 (defun map-directory (function directory
&key
(files t
) (directories t
)
881 (classify-symlinks t
) (errorp t
))
882 "Map over entries in DIRECTORY. Keyword arguments specify which entries to
886 If true, call FUNCTION with the pathname of each file in DIRECTORY.
890 If true, call FUNCTION with a pathname for each subdirectory of DIRECTORY.
891 If :AS-FILES, the pathname used is a pathname designating the subdirectory
892 as a file in DIRECTORY. Otherwise the pathname used is a directory
893 pathname. Defaults to T.
896 If true, the decision to call FUNCTION with the pathname of a symbolic link
897 depends on the resolution of the link: if it points to a directory, it is
898 considered a directory entry, otherwise a file entry. If false, all
899 symbolic links are considered file entries. In both cases the pathname used
900 for the symbolic link is not fully resolved, but names it as an immediate
901 child of DIRECTORY. Defaults to T.
904 If true, signal an error if DIRECTORY does not exist, cannot be read, etc.
907 Experimental: interface subject to change."
908 (declare (pathname-designator directory
))
909 (let* ((fun (%coerce-callable-to-fun function
))
910 (as-files (eq :as-files directories
))
911 (physical (physicalize-pathname directory
))
912 (realname (%query-file-system physical
:existence nil
))
913 (canonical (if realname
914 (parse-native-namestring realname
915 (pathname-host physical
)
916 (sane-default-pathname-defaults)
918 (return-from map-directory nil
)))
919 (dirname (native-namestring canonical
)))
920 (flet ((map-it (name dirp
)
922 (merge-pathnames (parse-native-namestring
924 :as-directory
(and dirp
(not as-files
)))
926 (with-native-directory-iterator (next dirname
:errorp errorp
)
928 ;; provision for FindFirstFileExW-based iterator that should be used
929 ;; on Windows: file kind is known instantly there, so we'll have it
930 ;; returned by (next) soon.
931 (multiple-value-bind (name kind
) (next)
932 (unless (or name kind
) (return))
934 (setf kind
(native-file-kind
935 (concatenate 'string dirname name
))))
942 (if classify-symlinks
943 (let* ((tmpname (merge-pathnames
944 (parse-native-namestring
945 name nil physical
:as-directory nil
)
947 (truename (%query-file-system tmpname
:truename nil
)))
948 (if (or (not truename
)
949 (or (pathname-name truename
) (pathname-type truename
)))
951 (funcall fun tmpname
))
957 ;; Anything else parses as a file.
959 (map-it name nil
)))))))))))
961 ;;; Part of DIRECTORY: implements matching the directory spec. Calls FUNCTION
962 ;;; with all DIRECTORIES that match the directory portion of PATHSPEC.
963 (defun map-matching-directories (function pathspec
)
964 (binding* ((directory (pathname-directory pathspec
))
965 ((mode end
) (loop for component in directory
966 for i
:of-type index from
0
967 do
(typecase component
968 ((or (eql :wild
) pattern
)
969 (return (values :wild i
)))
970 ((eql :wild-inferiors
)
971 (return (values :wild-inferiors i
))))))
972 ((directory-start directory-rest
)
974 (values (subseq directory
0 end
) (subseq directory end
))
975 (values directory directory
)))
976 (starting-point (make-pathname :directory directory-start
977 :device
(pathname-device pathspec
)
978 :host
(pathname-host pathspec
)
984 (map-wild-inferiors function directory-rest starting-point
))
986 (map-wild function directory-rest starting-point
))
987 (t ; Nothing wild -- the directory matches itself.
988 (funcall function starting-point
))))
991 (defun last-directory-piece (pathname)
992 (car (last (pathname-directory pathname
))))
994 ;;; Part of DIRECTORY: implements iterating over a :WILD or pattern component
995 ;;; in the directory spec.
996 (defun map-wild (function more directory
)
997 (let ((this (pop more
))
999 (flet ((cont (subdirectory)
1002 (funcall function subdirectory
))
1003 ((or (eq :wild next
) (typep next
'pattern
))
1004 (map-wild function more subdirectory
))
1005 ((eq :wild-inferiors next
)
1006 (map-wild-inferiors function more subdirectory
))
1008 (let ((this (pathname-directory subdirectory
)))
1009 (map-matching-directories
1011 (make-pathname :directory
(append this more
)
1012 :defaults subdirectory
)))))))
1017 (when (pattern-matches this
(last-directory-piece sub
))
1024 ;;; Part of DIRECTORY: implements iterating over a :WILD-INFERIORS component
1025 ;;; in the directory spec.
1026 (defun map-wild-inferiors (function more directory
)
1027 (loop while
(member (car more
) '(:wild
:wild-inferiors
))
1029 (let ((next (car more
))
1032 (funcall function directory
))
1036 (funcall function pathname
)
1037 (map-wild-inferiors function more pathname
)))
1040 (let ((this (pathname-directory pathname
)))
1041 (when (equal next
(car (last this
)))
1042 (map-matching-directories
1044 (make-pathname :directory
(append this rest
)
1045 :defaults pathname
)))
1046 (map-wild-inferiors function more pathname
)))))
1052 ;;; Part of DIRECTORY: implements iterating over entries in a directory, and
1054 (defun map-matching-entries (function directory match-name match-type
)
1057 (when (and (funcall match-name
(pathname-name file
))
1058 (funcall match-type
(pathname-type file
)))
1059 (funcall function file
)))
1062 :directories
:as-files
1065 ;;; NOTE: There is a fair amount of hair below that is probably not
1066 ;;; strictly necessary.
1068 ;;; The issue is the following: what does (DIRECTORY "SYS:*;") mean?
1069 ;;; Until 2004-01, SBCL's behaviour was unquestionably wrong, as it
1070 ;;; did not translate the logical pathname at all, but instead treated
1071 ;;; it as a physical one. Other Lisps seem to to treat this call as
1072 ;;; equivalent to (DIRECTORY (TRANSLATE-LOGICAL-PATHNAME "SYS:*;")),
1073 ;;; which is fine as far as it goes, but not very interesting, and
1074 ;;; arguably counterintuitive. (PATHNAME-MATCH-P "SYS:SRC;" "SYS:*;")
1075 ;;; is true, so why should "SYS:SRC;" not show up in the call to
1076 ;;; DIRECTORY? (assuming the physical pathname corresponding to it
1077 ;;; exists, of course).
1079 ;;; So, the interpretation that I am pushing is for all pathnames
1080 ;;; matching the input pathname to be queried. This means that we
1081 ;;; need to compute the intersection of the input pathname and the
1082 ;;; logical host FROM translations, and then translate the resulting
1083 ;;; pathname using the host to the TO translation; this treatment is
1084 ;;; recursively invoked until we get a physical pathname, whereupon
1085 ;;; our physical DIRECTORY implementation takes over.
1087 ;;; FIXME: this is an incomplete implementation. It only works when
1088 ;;; both are logical pathnames (which is OK, because that's the only
1089 ;;; case when we call it), but there are other pitfalls as well: see
1090 ;;; the DIRECTORY-HELPER below for some, but others include a lack of
1091 ;;; pattern handling.
1093 ;;; The above was written by CSR, I (RMK) believe. The argument that
1094 ;;; motivates the interpretation is faulty, however: PATHNAME-MATCH-P
1095 ;;; returns true for (PATHNAME-MATCH-P #P"/tmp/*/" #P"/tmp/../"), but
1096 ;;; the latter pathname is not in the result of DIRECTORY on the
1097 ;;; former. Indeed, if DIRECTORY were constrained to return the
1098 ;;; truename for every pathname for which PATHNAME-MATCH-P returned
1099 ;;; true and which denoted a filename that named an existing file,
1100 ;;; (DIRECTORY #P"/tmp/**/") would be required to list every file on a
1101 ;;; Unix system, since any file can be named as though it were "below"
1102 ;;; /tmp, given the dotdot entries. So I think the strongest
1103 ;;; "consistency" we can define between PATHNAME-MATCH-P and DIRECTORY
1104 ;;; is that PATHNAME-MATCH-P returns true of everything DIRECTORY
1105 ;;; returns, but not vice versa.
1107 ;;; In any case, even if the motivation were sound, DIRECTORY on a
1108 ;;; wild logical pathname has no portable semantics. I see nothing in
1109 ;;; ANSI that requires implementations to support wild physical
1110 ;;; pathnames, and so there need not be any translation of a wild
1111 ;;; logical pathname to a phyiscal pathname. So a program that calls
1112 ;;; DIRECTORY on a wild logical pathname is doing something
1113 ;;; non-portable at best. And if the only sensible semantics for
1114 ;;; DIRECTORY on a wild logical pathname is something like the
1115 ;;; following, it would be just as well if it signaled an error, since
1116 ;;; a program can't possibly rely on the result of an intersection of
1117 ;;; user-defined translations with a file system probe. (Potentially
1118 ;;; useful kinds of "pathname" that might not support wildcards could
1119 ;;; include pathname hosts that model unqueryable namespaces like HTTP
1120 ;;; URIs, or that model namespaces that it's not convenient to
1121 ;;; investigate, such as the namespace of TCP ports that some network
1122 ;;; host listens on. I happen to think it a bad idea to try to
1123 ;;; shoehorn such namespaces into a pathnames system, but people
1124 ;;; sometimes claim to want pathnames for these things.) -- RMK
1127 (defun pathname-intersections (one two
)
1128 (aver (logical-pathname-p one
))
1129 (aver (logical-pathname-p two
))
1131 ((intersect-version (one two
)
1132 (aver (typep one
'(or null
(member :newest
:wild
:unspecific
)
1134 (aver (typep two
'(or null
(member :newest
:wild
:unspecific
)
1137 ((eq one
:wild
) two
)
1138 ((eq two
:wild
) one
)
1139 ((not (pathname-component-present-p one
)) two
)
1140 ((not (pathname-component-present-p two
)) one
)
1143 (intersect-name/type
(one two
)
1144 (aver (typep one
'(or null
(member :wild
:unspecific
) string
)))
1145 (aver (typep two
'(or null
(member :wild
:unspecific
) string
)))
1147 ((eq one
:wild
) two
)
1148 ((eq two
:wild
) one
)
1149 ((not (pathname-component-present-p one
)) two
)
1150 ((not (pathname-component-present-p two
)) one
)
1151 ((string= one two
) one
)
1152 (t (return-from pathname-intersections nil
))))
1153 (intersect-directory (one two
)
1154 (aver (typep one
'(or null
(member :wild
:unspecific
) list
)))
1155 (aver (typep two
'(or null
(member :wild
:unspecific
) list
)))
1157 ((eq one
:wild
) two
)
1158 ((eq two
:wild
) one
)
1159 ((not (pathname-component-present-p one
)) two
)
1160 ((not (pathname-component-present-p two
)) one
)
1161 (t (aver (eq (car one
) (car two
)))
1163 (lambda (x) (cons (car one
) x
))
1164 (intersect-directory-helper (cdr one
) (cdr two
)))))))
1165 (let ((version (intersect-version
1166 (pathname-version one
) (pathname-version two
)))
1167 (name (intersect-name/type
1168 (pathname-name one
) (pathname-name two
)))
1169 (type (intersect-name/type
1170 (pathname-type one
) (pathname-type two
)))
1171 (host (pathname-host one
)))
1173 (make-pathname :host host
:name name
:type type
1174 :version version
:directory d
))
1175 (intersect-directory
1176 (pathname-directory one
) (pathname-directory two
))))))
1178 ;;; FIXME: written as its own function because I (CSR) don't
1179 ;;; understand it, so helping both debuggability and modularity. In
1180 ;;; case anyone is motivated to rewrite it, it returns a list of
1181 ;;; sublists representing the intersection of the two input directory
1182 ;;; paths (excluding the initial :ABSOLUTE or :RELATIVE).
1184 ;;; FIXME: Does not work with :UP or :BACK
1185 ;;; FIXME: Does not work with patterns
1187 ;;; FIXME: PFD suggests replacing this implementation with a DFA
1188 ;;; conversion of a NDFA. Find out (a) what this means and (b) if it
1189 ;;; turns out to be worth it.
1190 (defun intersect-directory-helper (one two
)
1191 (flet ((simple-intersection (cone ctwo
)
1193 ((eq cone
:wild
) ctwo
)
1194 ((eq ctwo
:wild
) cone
)
1195 (t (aver (typep cone
'string
))
1196 (aver (typep ctwo
'string
))
1197 (if (string= cone ctwo
) cone nil
)))))
1199 ((loop-possible-wild-inferiors-matches
1200 (lower-bound bounding-sequence order
)
1201 (let ((index (gensym)) (g2 (gensym)) (g3 (gensym)) (l (gensym)))
1202 `(let ((,l
(length ,bounding-sequence
)))
1203 (loop for
,index from
,lower-bound to
,l
1204 append
(mapcar (lambda (,g2
)
1206 (butlast ,bounding-sequence
(- ,l
,index
))
1211 (if (eq (car (nthcdr ,index
,bounding-sequence
))
1215 (intersect-directory-helper
1217 `((nthcdr ,index one
) (cdr two
))
1218 `((cdr one
) (nthcdr ,index two
)))))))))))
1220 ((and (eq (car one
) :wild-inferiors
)
1221 (eq (car two
) :wild-inferiors
))
1223 (append (mapcar (lambda (x) (cons :wild-inferiors x
))
1224 (intersect-directory-helper (cdr one
) (cdr two
)))
1225 (loop-possible-wild-inferiors-matches 2 one t
)
1226 (loop-possible-wild-inferiors-matches 2 two nil
))
1228 ((eq (car one
) :wild-inferiors
)
1229 (delete-duplicates (loop-possible-wild-inferiors-matches 0 two nil
)
1231 ((eq (car two
) :wild-inferiors
)
1232 (delete-duplicates (loop-possible-wild-inferiors-matches 0 one t
)
1234 ((and (null one
) (null two
)) (list nil
))
1237 (t (and (simple-intersection (car one
) (car two
))
1238 (mapcar (lambda (x) (cons (simple-intersection
1239 (car one
) (car two
)) x
))
1240 (intersect-directory-helper (cdr one
) (cdr two
)))))))))
1243 (defun directory-pathname-p (pathname)
1244 (and (pathnamep pathname
)
1245 (null (pathname-name pathname
))
1246 (null (pathname-type pathname
))))
1248 ;; FIXME: repeatedly poking at the file system ought to be
1249 ;; unnecessary, and walking the directory hierarchy from the top down
1250 ;; is probably a waste of effort in most cases. If we can trust mkdir
1251 ;; returning ENOENT and EEXIST always and only where it's supposed to,
1252 ;; we could get this down to two syscalls in the optimal success case
1253 ;; case, one in the pessimal failure case, and no worse than this in a
1254 ;; hypothetical average. (It looks like we inherited this approach
1255 ;; from CMUCL; maybe some ancient Unix's mkdir errno values weren't
1257 (defun ensure-directories-exist (pathspec &key verbose
(mode #o777
))
1258 "Test whether the directories containing the specified file
1259 actually exist, and attempt to create them if they do not.
1260 The MODE argument is a CMUCL/SBCL-specific extension to control
1261 the Unix permission bits."
1262 (let ((pathname (physicalize-pathname (merge-pathnames (pathname pathspec
))))
1264 (when (wild-pathname-p pathname
)
1265 (sb-kernel::%file-error pathspec
"bad place for a wild pathname"))
1266 (let* ((host (pathname-host pathname
))
1267 (dir (pathname-directory pathname
))
1268 (dev (pathname-device pathname
)))
1269 (loop for i from
(case dev
(:unc
3) (otherwise 2))
1272 (let* ((newpath (make-pathname
1275 :directory
(subseq dir
0 i
)))
1276 (namestring (coerce (native-namestring newpath
:as-file t
)
1278 (kind (native-file-kind namestring t
)))
1279 (unless (eq :directory kind
)
1281 (format *standard-output
*
1282 "~&creating directory: ~A~%"
1284 (sb-unix:unix-mkdir namestring mode
)
1285 (let ((newkind (native-file-kind namestring t
)))
1286 (unless (eq :directory newkind
)
1288 (sb-kernel::%file-error
1290 "Can't create directory ~A~:[~;,~%a file with ~
1291 the same name already exists.~]"
1293 (and kind
(not (eq :directory newkind
))))
1295 :report
"Retry directory creation."
1296 (ensure-directories-exist
1297 pathspec
:verbose verbose
:mode mode
)))))
1298 (setf created-p t
))))
1299 (values pathspec created-p
))))