Allocate symbols into permgen if enabled
[sbcl.git] / src / code / filesys.lisp
blob1ad86a2637363025a0a6043aabca072258477a7e
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
5 ;;;; more information.
6 ;;;;
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.
21 ;;;
22 ;;; Unix namestrings have the following format:
23 ;;;
24 ;;; namestring := [ directory ] [ file [ type [ version ]]]
25 ;;; directory := [ "/" ] { file "/" }*
26 ;;; file := [^/]*
27 ;;; type := "." [^/.]*
28 ;;; version := "." ([0-9]+ | "*")
29 ;;;
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:
34 ;;;
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.
37 ;;;
38 ;;; - Otherwise, the last dot separates the file and the type.
39 ;;;
40 ;;; Wildcard characters:
41 ;;;
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.
45 ;;;
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)
51 ;;;
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
57 protected."
58 (declare (type simple-string namestr)
59 (type index start end))
60 (let* ((result (make-string (- end start) :element-type 'character))
61 (dst 0)
62 (quoted nil))
63 (do ((src start (1+ src)))
64 ((= src end))
65 (cond (quoted
66 (setf (schar result dst) (schar namestr src))
67 (setf quoted nil)
68 (incf dst))
70 (let ((char (schar namestr src)))
71 (cond ((char= char escape-char)
72 (setq quoted t))
74 (setf (schar result dst) char)
75 (incf dst)))))))
76 (when quoted
77 (error 'namestring-parse-error
78 :complaint "escape char in a bad place"
79 :namestring namestr
80 :offset (1- end)))
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))
87 (collect ((pattern))
88 (let ((quoted nil)
89 (any-quotes nil)
90 (last-regular-char nil)
91 (index start))
92 (labels ((regulars ()
93 (if any-quotes
94 (remove-escape-characters namestr last-regular-char
95 index escape-char)
96 (subseq namestr last-regular-char index)))
97 (flush-pending-regulars ()
98 (when last-regular-char
99 (pattern (regulars))
100 (setf any-quotes nil)
101 (setf last-regular-char nil))))
102 (loop
103 (when (>= index end)
104 (return))
105 (let ((char (schar namestr index)))
106 (cond (quoted
107 (incf index)
108 (setf quoted nil))
109 ((char= char escape-char)
110 (setf quoted t)
111 (setf any-quotes t)
112 (unless last-regular-char
113 (setf last-regular-char index))
114 (incf index))
115 ((char= char #\?)
116 (flush-pending-regulars)
117 (pattern :single-char-wild)
118 (incf index))
119 ((char= char #\*)
120 (flush-pending-regulars)
121 (pattern :multi-char-wild)
122 (incf index))
123 ((char= char #\[)
124 (flush-pending-regulars)
125 (let ((close-bracket
126 (position #\] namestr :start index :end end)))
127 (unless close-bracket
128 (error 'namestring-parse-error
129 :complaint "#\\[ with no corresponding #\\]"
130 :namestring namestr
131 :offset index))
132 (pattern (cons :character-set
133 (subseq namestr
134 (1+ index)
135 close-bracket)))
136 (setf index (1+ close-bracket))))
138 (unless last-regular-char
139 (setf last-regular-char index))
140 (incf index)))))
141 (cond ((null (pattern))
142 (if last-regular-char
143 (regulars)
144 ""))
145 ((progn
146 (flush-pending-regulars)
147 (null (cdr (pattern))))
148 (let ((piece (first (pattern))))
149 (if (eq piece :multi-char-wild)
150 :wild
151 (make-pattern (pattern)))))
153 (make-pattern (pattern))))))))
155 (declaim (ftype (sfunction ((or (eql :wild) simple-string pattern) character &key (:escape-dot t))
156 simple-string)
157 unparse-physical-piece))
158 (defun unparse-physical-piece (thing escape-char &key escape-dot)
159 (let ((length 0)
160 (complicated nil))
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)
165 (case escape-dot
166 (:unless-at-start (and (plusp index) (char= char #\.)))
167 ((t) (char= char #\.)))))
168 (inspect-fragment (fragment)
169 (etypecase fragment
170 ((eql :wild)
171 (incf length)
173 (simple-string
174 (incf length (length fragment))
175 (Loop with complicated = nil
176 for char across (the simple-string fragment)
177 for i from 0
178 when (needs-escaping-p char i)
179 do (setf complicated t)
180 (incf length)
181 finally (return complicated)))
182 (pattern
183 (mapcar (lambda (piece)
184 (etypecase piece
185 (simple-string
186 (inspect-fragment piece))
187 ((member :multi-char-wild :single-char-wild)
188 (incf length 1)
190 ((cons (eql :character-set))
191 (incf length (+ 2 (length (cdr piece))))
192 t)))
193 (pattern-pieces fragment))))))
194 (setf complicated (inspect-fragment thing))
195 (unless complicated
196 (return-from unparse-physical-piece thing))
197 (let ((result (make-string length))
198 (index 0))
199 (declare (type (simple-array character 1) result)
200 (type index index))
201 (labels ((output-character (character)
202 (setf (aref result index) character)
203 (incf index))
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)
209 (etypecase fragment
210 ((eql :wild)
211 (output-character #\*))
212 (simple-string
213 (loop for char across (the simple-string fragment)
214 for i from 0
215 when (needs-escaping-p char i)
216 do (output-character escape-char)
217 do (output-character char)))
218 (pattern
219 (mapc (lambda (piece piece-complicated)
220 (etypecase piece
221 (simple-string
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))
236 result))))
238 (defun make-matcher (piece)
239 (cond ((eq piece :wild)
240 (constantly t))
241 ((typep piece 'pattern)
242 (lambda (other)
243 (when (stringp other)
244 (pattern-matches piece other))))
246 (lambda (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))
252 (flet ((escape-p (i)
253 (and (>= i start) (char= (aref namestr i) escape-char))))
254 (let ((last-dot
255 (loop for i from (1- end) downto (1+ start)
256 when (and (char= (aref namestr i) #\.)
257 (or (not (escape-p (1- i)))
258 (escape-p (- i 2))))
259 return i)))
260 (if last-dot
261 (values (maybe-make-pattern namestr start last-dot escape-char)
262 (maybe-make-pattern namestr (1+ last-dot) end escape-char)
263 :newest)
264 (values (maybe-make-pattern namestr start end escape-char)
266 :newest)))))
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))
273 #-win32
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.
279 (if resolve-symlinks
280 (sb-unix:unix-stat namestring)
281 (sb-unix:unix-lstat namestring))
282 (declare (ignore errno ino))
283 (when existsp
284 (let ((ifmt (logand mode sb-unix:s-ifmt)))
285 (case ifmt
286 (#.sb-unix:s-ifreg :file)
287 (#.sb-unix:s-ifdir :directory)
288 (#.sb-unix:s-iflnk :symlink)
289 (t :special))))))
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
318 ;;; routine.
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
326 ;;; use.
327 (defun query-file-system (pathspec query-for &optional (errorp t))
328 (block nil
329 (tagbody
330 retry
331 (restart-case
332 (let ((pathname (translate-logical-pathname
333 (merge-pathnames
334 (pathname pathspec)
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)))
341 (use-value (value)
342 :report "Specify a different path."
343 :interactive read-evaluated-form
344 (setf pathspec value)
345 (go retry))))))
347 #+win32
348 (defun %query-file-system (pathname query-for errorp)
349 (let ((filename (native-namestring pathname :as-file t)))
350 (case query-for
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
359 (cond
360 ((or file kind)
361 (values (parse-native-namestring
362 file
363 (pathname-host pathname)
364 (sane-default-pathname-defaults)
365 :as-directory (eq :directory kind))))
366 (errorp
367 (file-perror filename (sb-win32:get-last-error)
368 "Failed to find the ~A of ~S" query-for filename)))))
369 (:write-date
370 (cond
371 ((sb-win32::native-file-write-date filename))
372 (errorp
373 (file-perror filename (sb-win32:get-last-error)
374 "Failed to find the ~A of ~S" query-for filename)))))))
376 #-win32
377 (defun %query-file-system (pathname query-for errorp)
378 (labels ((parse (filename &key as-directory)
379 (values (parse-native-namestring
380 filename
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
402 (native-namestring
403 (make-pathname
404 :name :unspecific
405 :type :unspecific
406 :version :unspecific
407 :defaults (parse filename))))
408 (cond
409 (realpath
410 (parse realpath :as-directory t))
411 (errorp
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
424 size atime mtime)
425 (sb-unix:unix-lstat filename)
426 (declare (ignore ignore ino mode nlink gid rdev size atime))
427 (cond
428 ((and (or (= errno sb-unix:enoent)
429 (= errno sb-unix:eloop)
430 realpath-failed)
431 linkp)
432 (case query-for
433 (:existence
434 ;; We do this reparse so as to return a
435 ;; normalized pathname.
436 (parse filename))
437 (:truename
438 (let ((realpath (directory-part-realpath filename)))
439 (when realpath
440 (merge-pathnames
441 realpath
442 (if (directory-pathname-p pathname)
443 (parse (car (last (pathname-directory pathname))))
444 pathname)))))
445 (:author (sb-unix:uid-username uid))
446 (:write-date (+ unix-to-universal-time mtime))))
447 ;; The file doesn't exist; maybe error.
448 (errorp
449 (file-perror
450 pathname errno
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)))
455 (if existsp
456 (case query-for
457 (:existence
458 (parse filename :as-directory (eql (logand mode sb-unix:s-ifmt)
459 sb-unix:s-ifdir)))
460 (:truename
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)
467 sb-unix:s-ifdir)))
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
482 the stream.
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
500 is a wild pathname."
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)
517 :as-file t))
518 (new-name (merge-pathnames new-name original))
519 (new-namestring (native-namestring (physicalize-pathname new-name)
520 :as-file t)))
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)
525 (unless res
526 (file-perror
527 new-name error
528 "~@<couldn't rename ~2I~_~A ~I~_to ~2I~_~A~:>" original new-name))
529 (when (streamp file)
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)))
543 #+win32
544 (when (streamp file)
545 (close file))
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)))
550 (unless res
551 (with-simple-restart (continue "Return T")
552 (file-perror namestring err 'delete-file-error)))))
555 (defun directorize-pathname (pathname)
556 (cond
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)))
567 (make-pathname
568 :host (pathname-host pathname)
569 :device (pathname-device pathname)
570 :directory (append (pathname-directory pathname)
571 (list from-file)))))))
573 pathname)))
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.
587 Both
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)
596 (lambda (sub)
597 (recurse (merge-pathnames sub dir))))
598 (delete-merged (dir)
599 (lambda (file)
600 (delete-file (merge-pathnames file dir))))
601 (recurse (dir)
602 (map-directory (recurse-merged dir) dir
603 :files nil
604 :directories t
605 :classify-symlinks nil)
606 (map-directory (delete-merged dir) dir
607 :files t
608 :directories nil
609 :classify-symlinks nil)
610 (delete-dir dir))
611 (delete-dir (dir)
612 (let ((namestring (native-namestring dir :as-file t)))
613 (multiple-value-bind (res errno)
614 #+win32
615 (or (sb-win32::native-delete-directory namestring)
616 (values nil (sb-win32:get-last-error)))
617 #-win32
618 (values
619 (not (minusp (alien-funcall
620 (extern-alien "rmdir"
621 (function int c-string))
622 namestring)))
623 (get-errno))
624 (if res
626 (file-perror namestring errno
627 "Could not delete directory ~S" namestring))))))
628 (let ((physical (directorize-pathname
629 (physicalize-pathname
630 (merge-pathnames
631 pathspec (sane-default-pathname-defaults))))))
632 (if recursive
633 (recurse physical)
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
645 *physical-host*
646 *default-pathname-defaults*
647 :as-directory directory))
648 (probe (path)
649 (let ((contrib (merge-pathnames "contrib/" path)))
650 (when (probe-file contrib)
651 path)))
652 (try-runtime-home (path)
653 (or (probe path)
654 (probe (merge-pathnames "../lib/sbcl/" path)))))
655 (let* ((env (posix-getenv "SBCL_HOME"))
656 (env (and env (not (string= env ""))
657 (parse env))))
658 (or (and env
659 (probe env))
660 (try-runtime-home (parse (or (extern-alien "sbcl_runtime_home" c-string)
661 "")))
662 (probe (parse "."))
663 ;; Handle a symlink
664 (let ((runtime (extern-alien "sbcl_runtime" c-string)))
665 (and runtime
666 (let ((truename (probe-file (parse runtime nil))))
667 (when truename
668 (try-runtime-home (make-pathname :name nil
669 :type 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)))
677 #-win32
678 (defun user-homedir-namestring (&optional username)
679 (if 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)))
683 (lose))))
685 #+win32
686 (defun user-homedir-namestring (&optional username)
687 (if username
688 (lose 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"))))
693 (and drive path
694 (concatenate 'string drive path)))
695 (lose)))))
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))
704 (values
705 (parse-native-namestring
706 (or (user-homedir-namestring)
707 #+win32
708 (sb-win32::get-folder-namestring sb-win32::csidl_profile))
709 *physical-host*
710 *default-pathname-defaults*
711 :as-directory t)))
714 ;;;; DIRECTORY
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
725 filenames."
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
729 ;; same truename).
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. --
739 ;; CSR, 2003-10-13
740 (%query-file-system pathname :truename nil)
741 (%query-file-system pathname :existence nil))))
742 (when truename
743 (setf (gethash (namestring truename) truenames)
744 truename))))
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
749 ;; as #p"foo/./".
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
756 (if (or name type)
757 (lambda (directory)
758 (map-matching-entries #'record
759 directory
760 match-name
761 match-type))
762 #'record)
763 pathname)))
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
769 (let ((intersections
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)
790 piece))
791 (canonicalize-directory (directory)
792 (let (pieces)
793 (dolist (piece directory)
794 (cond
795 ((and pieces (member piece '(:back :up)))
796 ;; FIXME: We should really canonicalize when we construct
797 ;; pathnames. This is just wrong.
798 (case (car pieces)
799 ((:absolute :wild-inferiors)
800 (sb-kernel::%file-error
801 pathname
802 "Invalid use of ~S after ~S." piece (car pieces)))
803 ((:relative :up :back)
804 (push piece pieces))
806 (pop pieces))))
807 ((equal piece ".")
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))))
817 (nreverse pieces))))
818 (let ((name (simplify (pathname-name pathname)))
819 (type (simplify (pathname-type pathname)))
820 (dir (canonicalize-directory (pathname-directory pathname))))
821 (cond
822 ((not (equal "." name))
823 (make-pathname :name name :type type :directory dir
824 :defaults pathname))
825 ((not type)
826 (make-pathname :name nil :defaults pathname))
827 ((equal "" type)
828 (make-pathname :name nil :type nil :directory (butlast dir)
829 :defaults pathname))
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
836 ;;; directory.
837 (defmacro with-native-directory-iterator ((iterator namestring &key errorp) &body body)
838 (with-unique-names (one-iter)
839 `(dx-flet
840 ((iterate (,one-iter)
841 (declare (type function ,one-iter))
842 (macrolet ((,iterator ()
843 `(funcall ,',one-iter)))
844 ,@body)))
845 #+win32
846 (sb-win32::native-call-with-directory-iterator
847 #'iterate ,namestring ,errorp)
848 #-win32
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)
853 (function function))
854 (let (dp)
855 (when namestring
856 (dx-flet
857 ((one-iter ()
858 (tagbody
859 :next
860 (let ((ent (sb-unix:unix-readdir dp nil)))
861 (when ent
862 (let ((name (sb-unix:unix-dirent-name ent)))
863 (when name
864 (cond ((equal "." name)
865 (go :next))
866 ((equal ".." name)
867 (go :next))
869 (return-from one-iter name))))))))))
870 (unwind-protect
871 (progn
872 (setf dp (sb-unix:unix-opendir namestring errorp))
873 (when dp
874 (funcall function #'one-iter)))
875 (when dp
876 (sb-unix:unix-closedir dp nil)))))))
878 ;;; This is our core directory access interface that we use to implement
879 ;;; DIRECTORY.
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
883 map over, and how:
885 :FILES
886 If true, call FUNCTION with the pathname of each file in DIRECTORY.
887 Defaults to T.
889 :DIRECTORIES
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.
895 :CLASSIFY-SYMLINKS
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.
903 :ERRORP
904 If true, signal an error if DIRECTORY does not exist, cannot be read, etc.
905 Defaults to T.
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)
917 :as-directory t)
918 (return-from map-directory nil)))
919 (dirname (native-namestring canonical)))
920 (flet ((map-it (name dirp)
921 (funcall fun
922 (merge-pathnames (parse-native-namestring
923 name nil physical
924 :as-directory (and dirp (not as-files)))
925 physical))))
926 (with-native-directory-iterator (next dirname :errorp errorp)
927 (loop
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))
933 (unless kind
934 (setf kind (native-file-kind
935 (concatenate 'string dirname name))))
936 (when kind
937 (case kind
938 (:directory
939 (when directories
940 (map-it name t)))
941 (:symlink
942 (if classify-symlinks
943 (let* ((tmpname (merge-pathnames
944 (parse-native-namestring
945 name nil physical :as-directory nil)
946 physical))
947 (truename (%query-file-system tmpname :truename nil)))
948 (if (or (not truename)
949 (or (pathname-name truename) (pathname-type truename)))
950 (when files
951 (funcall fun tmpname))
952 (when directories
953 (map-it name t))))
954 (when files
955 (map-it name nil))))
957 ;; Anything else parses as a file.
958 (when files
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)
973 (if end
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)
979 :name nil
980 :type nil
981 :version nil)))
982 (case mode
983 (:wild-inferiors
984 (map-wild-inferiors function directory-rest starting-point))
985 (:wild
986 (map-wild function directory-rest starting-point))
987 (t ; Nothing wild -- the directory matches itself.
988 (funcall function starting-point))))
989 nil)
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))
998 (next (car more)))
999 (flet ((cont (subdirectory)
1000 (cond ((not more)
1001 ;; end of the line
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
1010 function
1011 (make-pathname :directory (append this more)
1012 :defaults subdirectory)))))))
1013 (map-directory
1014 (if (eq :wild this)
1015 #'cont
1016 (lambda (sub)
1017 (when (pattern-matches this (last-directory-piece sub))
1018 (cont sub))))
1019 directory
1020 :files nil
1021 :directories t
1022 :errorp nil))))
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))
1028 do (pop more))
1029 (let ((next (car more))
1030 (rest (cdr more)))
1031 (unless more
1032 (funcall function directory))
1033 (map-directory
1034 (cond ((not more)
1035 (lambda (pathname)
1036 (funcall function pathname)
1037 (map-wild-inferiors function more pathname)))
1039 (lambda (pathname)
1040 (let ((this (pathname-directory pathname)))
1041 (when (equal next (car (last this)))
1042 (map-matching-directories
1043 function
1044 (make-pathname :directory (append this rest)
1045 :defaults pathname)))
1046 (map-wild-inferiors function more pathname)))))
1047 directory
1048 :files nil
1049 :directories t
1050 :errorp nil)))
1052 ;;; Part of DIRECTORY: implements iterating over entries in a directory, and
1053 ;;; matching them.
1054 (defun map-matching-entries (function directory match-name match-type)
1055 (map-directory
1056 (lambda (file)
1057 (when (and (funcall match-name (pathname-name file))
1058 (funcall match-type (pathname-type file)))
1059 (funcall function file)))
1060 directory
1061 :files t
1062 :directories :as-files
1063 :errorp nil))
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
1125 ;;; 2007-12-31.
1127 (defun pathname-intersections (one two)
1128 (aver (logical-pathname-p one))
1129 (aver (logical-pathname-p two))
1130 (labels
1131 ((intersect-version (one two)
1132 (aver (typep one '(or null (member :newest :wild :unspecific)
1133 integer)))
1134 (aver (typep two '(or null (member :newest :wild :unspecific)
1135 integer)))
1136 (cond
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)
1141 ((eql one two) one)
1142 (t nil)))
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)))
1146 (cond
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)))
1156 (cond
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)))
1162 (mapcar
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)))
1172 (mapcar (lambda (d)
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)
1192 (cond
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)))))
1198 (macrolet
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)
1205 (append
1206 (butlast ,bounding-sequence (- ,l ,index))
1207 ,g2))
1208 (mapcar
1209 (lambda (,g3)
1210 (append
1211 (if (eq (car (nthcdr ,index ,bounding-sequence))
1212 :wild-inferiors)
1213 '(:wild-inferiors)
1214 nil) ,g3))
1215 (intersect-directory-helper
1216 ,@(if order
1217 `((nthcdr ,index one) (cdr two))
1218 `((cdr one) (nthcdr ,index two)))))))))))
1219 (cond
1220 ((and (eq (car one) :wild-inferiors)
1221 (eq (car two) :wild-inferiors))
1222 (delete-duplicates
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))
1227 :test 'equal))
1228 ((eq (car one) :wild-inferiors)
1229 (delete-duplicates (loop-possible-wild-inferiors-matches 0 two nil)
1230 :test 'equal))
1231 ((eq (car two) :wild-inferiors)
1232 (delete-duplicates (loop-possible-wild-inferiors-matches 0 one t)
1233 :test 'equal))
1234 ((and (null one) (null two)) (list nil))
1235 ((null one) nil)
1236 ((null two) 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
1256 ;; reliable?)
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))))
1263 (created-p nil))
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))
1270 upto (length dir)
1272 (let* ((newpath (make-pathname
1273 :host host
1274 :device dev
1275 :directory (subseq dir 0 i)))
1276 (namestring (coerce (native-namestring newpath :as-file t)
1277 'string))
1278 (kind (native-file-kind namestring t)))
1279 (unless (eq :directory kind)
1280 (when verbose
1281 (format *standard-output*
1282 "~&creating directory: ~A~%"
1283 namestring))
1284 (sb-unix:unix-mkdir namestring mode)
1285 (let ((newkind (native-file-kind namestring t)))
1286 (unless (eq :directory newkind)
1287 (restart-case
1288 (sb-kernel::%file-error
1289 pathspec
1290 "Can't create directory ~A~:[~;,~%a file with ~
1291 the same name already exists.~]"
1292 namestring
1293 (and kind (not (eq :directory newkind))))
1294 (retry ()
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))))