ensure-directory-exists: didn't work when *d-p-d* had NAME or TYPE components.
[sbcl.git] / src / code / filesys.lisp
1 ;;;; file system interface functions -- fairly Unix-centric, but with
2 ;;;; differences between Unix and Win32 papered over.
3
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.
12
13 (in-package "SB!IMPL")
14 \f
15 ;;;; Unix pathname host support
16
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 a backslash to
53 ;;; cause it to be treated as a regular character.
54 (defun remove-backslashes (namestr start end)
55   #!+sb-doc
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))
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))
69             (t
70              (let ((char (schar namestr src)))
71                (cond ((char= char #\\)
72                       (setq quoted t))
73                      (t
74                       (setf (schar result dst) char)
75                       (incf dst)))))))
76     (when quoted
77       (error 'namestring-parse-error
78              :complaint "backslash in a bad place"
79              :namestring namestr
80              :offset (1- end)))
81     (%shrink-vector result dst)))
82
83 (defun maybe-make-pattern (namestr start end)
84   (declare (type simple-string namestr)
85            (type index start end))
86   (collect ((pattern))
87     (let ((quoted nil)
88           (any-quotes nil)
89           (last-regular-char nil)
90           (index start))
91       (flet ((flush-pending-regulars ()
92                (when last-regular-char
93                  (pattern (if any-quotes
94                               (remove-backslashes namestr
95                                                   last-regular-char
96                                                   index)
97                               (subseq namestr last-regular-char index)))
98                  (setf any-quotes nil)
99                  (setf last-regular-char nil))))
100         (loop
101           (when (>= index end)
102             (return))
103           (let ((char (schar namestr index)))
104             (cond (quoted
105                    (incf index)
106                    (setf quoted nil))
107                   ((char= char #\\)
108                    (setf quoted t)
109                    (setf any-quotes t)
110                    (unless last-regular-char
111                      (setf last-regular-char index))
112                    (incf index))
113                   ((char= char #\?)
114                    (flush-pending-regulars)
115                    (pattern :single-char-wild)
116                    (incf index))
117                   ((char= char #\*)
118                    (flush-pending-regulars)
119                    (pattern :multi-char-wild)
120                    (incf index))
121                   ((char= char #\[)
122                    (flush-pending-regulars)
123                    (let ((close-bracket
124                           (position #\] namestr :start index :end end)))
125                      (unless close-bracket
126                        (error 'namestring-parse-error
127                               :complaint "#\\[ with no corresponding #\\]"
128                               :namestring namestr
129                               :offset index))
130                      (pattern (cons :character-set
131                                     (subseq namestr
132                                             (1+ index)
133                                             close-bracket)))
134                      (setf index (1+ close-bracket))))
135                   (t
136                    (unless last-regular-char
137                      (setf last-regular-char index))
138                    (incf index)))))
139         (flush-pending-regulars)))
140     (cond ((null (pattern))
141            "")
142           ((null (cdr (pattern)))
143            (let ((piece (first (pattern))))
144              (typecase piece
145                ((member :multi-char-wild) :wild)
146                (simple-string piece)
147                (t
148                 (make-pattern (pattern))))))
149           (t
150            (make-pattern (pattern))))))
151
152 (defun unparse-physical-piece (thing)
153   (etypecase thing
154     ((member :wild) "*")
155     (simple-string
156      (let* ((srclen (length thing))
157             (dstlen srclen))
158        (dotimes (i srclen)
159          (case (schar thing i)
160            ((#\* #\? #\[)
161             (incf dstlen))))
162        (let ((result (make-string dstlen))
163              (dst 0))
164          (dotimes (src srclen)
165            (let ((char (schar thing src)))
166              (case char
167                ((#\* #\? #\[)
168                 (setf (schar result dst) #\\)
169                 (incf dst)))
170              (setf (schar result dst) char)
171              (incf dst)))
172          result)))
173     (pattern
174      (with-output-to-string (s)
175        (dolist (piece (pattern-pieces thing))
176          (etypecase piece
177            (simple-string
178             (write-string piece s))
179            (symbol
180             (ecase piece
181               (:multi-char-wild
182                (write-string "*" s))
183               (:single-char-wild
184                (write-string "?" s))))
185            (cons
186             (case (car piece)
187               (:character-set
188                (write-string "[" s)
189                (write-string (cdr piece) s)
190                (write-string "]" s))
191               (t
192                (error "invalid pattern piece: ~S" piece))))))))))
193
194 (defun make-matcher (piece)
195   (cond ((eq piece :wild)
196          (constantly t))
197         ((typep piece 'pattern)
198          (lambda (other)
199            (when (stringp other)
200              (pattern-matches piece other))))
201         (t
202          (lambda (other)
203            (equal piece other)))))
204
205 (/show0 "filesys.lisp 160")
206
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
211                              :from-end t)))
212     (cond
213       (last-dot
214        (values (maybe-make-pattern namestr start last-dot)
215                (maybe-make-pattern namestr (1+ last-dot) end)
216                :newest))
217       (t
218        (values (maybe-make-pattern namestr start end)
219                nil
220                :newest)))))
221
222 (/show0 "filesys.lisp 200")
223
224 \f
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)
228       #!-win32
229       (sb!unix:unix-lstat namestring)
230       #!+win32
231       (sb!unix:unix-stat namestring)
232     (declare (ignore errno ino))
233     (when existsp
234       (let ((ifmt (logand mode sb!unix:s-ifmt)))
235        (case ifmt
236          (#.sb!unix:s-ifreg :file)
237          (#.sb!unix:s-ifdir :directory)
238          #!-win32
239          (#.sb!unix:s-iflnk :symlink)
240          (t :special))))))
241 \f
242 ;;;; TRUENAME, PROBE-FILE, FILE-AUTHOR, FILE-WRITE-DATE.
243
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:
249 ;;;
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
269 ;;;   routine.
270 ;;;
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
277 ;;; use.
278 (defun query-file-system (pathspec query-for &optional (errorp t))
279   (let ((pathname (translate-logical-pathname
280                    (merge-pathnames
281                     (pathname pathspec)
282                     (sane-default-pathname-defaults)))))
283     (when (wild-pathname-p pathname)
284       (error 'simple-file-error
285              :pathname pathname
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)
290              (if errorp
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
295                                       atime mtime)
296             (sb!unix:unix-stat filename)
297           (declare (ignore ino nlink gid rdev size atime
298                            #!+win32 uid))
299           #!+win32
300           ;; On win32, stat regards UNC pathnames and device names as
301           ;; nonexisting, so we check once more with the native API.
302           (unless existsp
303             (setf existsp
304                   (let ((handle (sb!win32:create-file
305                                  filename 0 0 nil
306                                  sb!win32:file-open-existing
307                                  0 0)))
308                     (when (/= -1 handle)
309                       (setf mode
310                             (or mode
311                                 (if (logbitp 4
312                                              (sb!win32:get-file-attributes filename))
313                                     sb!unix:s-ifdir 0)))
314                       (progn (sb!win32:close-handle handle) t)))))
315           (if existsp
316               (case query-for
317                 (:existence (nth-value
318                              0
319                              (parse-native-namestring
320                               filename
321                               (pathname-host pathname)
322                               (sane-default-pathname-defaults)
323                               :as-directory (eql (logand mode sb!unix:s-ifmt)
324                                                  sb!unix:s-ifdir))))
325                 (:truename (nth-value
326                             0
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)
334                                (if realpath
335                                    realpath
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)
341                                                 sb!unix:s-ifdir))))
342                 (:author
343                  #!-win32
344                  (sb!unix:uid-username uid))
345                 (:write-date (+ unix-to-universal-time mtime)))
346               (progn
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.
354                 #!-win32
355                 (multiple-value-bind (linkp ignore ino mode nlink uid gid rdev
356                                             size atime mtime)
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))
361                              linkp)
362                     (return-from query-file-system
363                       (case query-for
364                         (:existence
365                          ;; We do this reparse so as to return a
366                          ;; normalized pathname.
367                          (parse-native-namestring
368                           filename (pathname-host pathname)))
369                         (:truename
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.
387                          (merge-pathnames
388                           (nth-value
389                            0
390                            (parse-native-namestring
391                             (multiple-value-bind (realpath errno)
392                                 (sb!unix:unix-realpath
393                                  (native-namestring
394                                   (make-pathname
395                                    :name :unspecific
396                                    :type :unspecific
397                                    :version :unspecific
398                                    :defaults (parse-native-namestring
399                                               filename
400                                               (pathname-host pathname)
401                                               (sane-default-pathname-defaults)))))
402                               (if realpath
403                                   realpath
404                                   (fail "couldn't resolve ~A" filename errno)))
405                             (pathname-host pathname)
406                             (sane-default-pathname-defaults)
407                             :as-directory t))
408                           pathname))
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.
412                 (fail
413                  (format nil "failed to find the ~A of ~~A" query-for)
414                  pathspec errno))))))))
415
416
417 (defun probe-file (pathspec)
418   #!+sb-doc
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))
422
423 (defun truename (pathspec)
424   #!+sb-doc
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
429 the stream.
430
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.
434
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)))
443
444 (defun file-author (pathspec)
445   #!+sb-doc
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
448 is a wild pathname."
449   (query-file-system pathspec :author))
450
451 (defun file-write-date (pathspec)
452   #!+sb-doc
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))
457 \f
458 ;;;; miscellaneous other operations
459
460 (/show0 "filesys.lisp 700")
461
462 (defun rename-file (file new-name)
463   #!+sb-doc
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)
469                                                  :as-file t))
470          (new-name (merge-pathnames new-name original))
471          (new-namestring (native-namestring (physicalize-pathname new-name)
472                                             :as-file t)))
473     (unless new-namestring
474       (error 'simple-file-error
475              :pathname new-name
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)
480       (unless res
481         (error 'simple-file-error
482                :pathname new-name
483                :format-control "~@<couldn't rename ~2I~_~A ~I~_to ~2I~_~A: ~
484                                 ~I~_~A~:>"
485                :format-arguments (list original new-name (strerror error))))
486       (when (streamp file)
487         (file-name file new-name))
488       (values new-name old-truename (truename new-name)))))
489
490 (defun delete-file (file)
491   #!+sb-doc
492   "Delete the specified FILE.
493
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)))
501     #!+win32
502     (when (streamp file)
503       (close file))
504     (multiple-value-bind (res err) (sb!unix:unix-unlink namestring)
505       (unless res
506         (simple-file-perror "Couldn't delete file ~A" namestring err))))
507   t)
508
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))
516       pathname))
517
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.
521
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.
526
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.
529
530 Both
531
532    \(DELETE-DIRECTORY \"/tmp/foo\")
533    \(DELETE-DIRECTORY \"/tmp/foo/\")
534
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
540                     (merge-pathnames
541                      pathspec (sane-default-pathname-defaults))))))
542     (labels ((recurse-merged (dir)
543                (lambda (sub)
544                  (recurse (merge-pathnames sub dir))))
545              (delete-merged (dir)
546                (lambda (file)
547                  (delete-file (merge-pathnames file dir))))
548              (recurse (dir)
549                (map-directory (recurse-merged dir) dir
550                               :files nil
551                               :directories t
552                               :classify-symlinks nil)
553                (map-directory (delete-merged dir) dir
554                               :files t
555                               :directories nil
556                               :classify-symlinks nil)
557                (delete-dir dir))
558              (delete-dir (dir)
559                (let* ((namestring (native-namestring dir :as-file t))
560                       (res (alien-funcall (extern-alien #!-win32 "rmdir"
561                                                         #!+win32 "_rmdir"
562                                                         (function int c-string))
563                                           namestring)))
564                  (if (minusp res)
565                      (simple-file-perror "Couldn't delete directory ~A"
566                                          namestring (get-errno))
567                      dir))))
568       (if recursive
569           (recurse physical)
570           (delete-dir physical)))))
571 \f
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*
580                                :as-directory t))))
581
582 (defun user-homedir-namestring (&optional username)
583   (if username
584       (sb!unix:user-homedir username)
585       (let ((env-home (posix-getenv "HOME")))
586         (if (and env-home (not (string= env-home "")))
587             env-home
588             #!-win32
589             (sb!unix:uid-homedir (sb!unix:unix-getuid))))))
590
591 ;;; (This is an ANSI Common Lisp function.)
592 (defun user-homedir-pathname (&optional host)
593   #!+sb-doc
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))
599   (values
600    (parse-native-namestring
601     (or (user-homedir-namestring)
602         #!+win32
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*
607     :as-directory t)))
608
609 \f
610 ;;;; DIRECTORY
611
612 (defun directory (pathspec &key (resolve-symlinks t))
613   #!+sb-doc
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
619 matching filenames."
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
623         ;; same truename).
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.  --
633                                    ;; CSR, 2003-10-13
634                                    (query-file-system pathname :truename nil)
635                                    (query-file-system pathname :existence nil))))
636                  (when truename
637                    (setf (gethash (namestring truename) truenames)
638                          truename))))
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
643                       ;; as #p"foo/./".
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
650                   (if (or name type)
651                       (lambda (directory)
652                         (map-matching-entries #'record
653                                               directory
654                                               match-name
655                                               match-type))
656                       #'record)
657                   pathname)))
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
663                          (let ((intersections
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)))
670     (mapcar #'cdr
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))
677                   #'string<
678                   :key #'car))))
679
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)
685                piece))
686            (canonicalize-directory (directory)
687              (let (pieces)
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.
692                      (case (car pieces)
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))
697                                :pathname pathname))
698                        ((:relative :up :back)
699                         (push piece pieces))
700                        (t
701                         (pop pieces)))
702                      (push piece pieces)))
703                (nreverse 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)
708              (cond ((not type)
709                     (make-pathname :name nil :defaults pathname))
710                    ((equal "" type)
711                     (make-pathname :name nil
712                                    :type nil
713                                    :directory (butlast dir)
714                                    :defaults pathname))))
715             (t
716              (make-pathname :name name :type type
717                             :directory dir
718                             :defaults pathname))))))
719
720 ;;; Given a native namestring, provides a WITH-HASH-TABLE-ITERATOR style
721 ;;; interface to mapping over namestrings of entries in the corresponding
722 ;;; directory.
723 (defmacro with-native-directory-iterator ((iterator namestring &key errorp) &body body)
724   (with-unique-names (one-iter)
725     `(dx-flet
726          ((iterate (,one-iter)
727             (declare (type function ,one-iter))
728             (macrolet ((,iterator ()
729                          `(funcall ,',one-iter)))
730               ,@body)))
731        (call-with-native-directory-iterator #'iterate ,namestring ,errorp))))
732
733 (defun call-with-native-directory-iterator (function namestring errorp)
734   (declare (type (or null string) namestring)
735            (function function))
736   (let (dp)
737     (when namestring
738       (dx-flet
739           ((one-iter ()
740              (tagbody
741               :next
742                 (let ((ent (sb!unix:unix-readdir dp nil)))
743                   (when ent
744                     (let ((name (sb!unix:unix-dirent-name ent)))
745                       (when name
746                         (cond ((equal "." name)
747                                (go :next))
748                               ((equal ".." name)
749                                (go :next))
750                               (t
751                                (return-from one-iter name))))))))))
752         (unwind-protect
753              (progn
754                (setf dp (sb!unix:unix-opendir namestring errorp))
755                (when dp
756                  (funcall function #'one-iter)))
757           (when dp
758             (sb!unix:unix-closedir dp nil)))))))
759
760 ;;; This is our core directory access interface that we use to implement
761 ;;; DIRECTORY.
762 (defun map-directory (function directory &key (files t) (directories t)
763                       (classify-symlinks t) (errorp t))
764   #!+sb-doc
765   "Map over entries in DIRECTORY. Keyword arguments specify which entries to
766 map over, and how:
767
768  :FILES
769     If true, call FUNCTION with the pathname of each file in DIRECTORY.
770     Defaults to T.
771
772  :DIRECTORIES
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.
777
778  :CLASSIFY-SYMLINKS
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.
785
786  :ERRORP
787    If true, signal an error if DIRECTORY does not exist, cannot be read, etc.
788    Defaults to T.
789
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
796          ;; network shares.
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)
802                                                  :as-directory t)
803                         (return-from map-directory nil)))
804          (dirname (native-namestring canonical)))
805     (flet ((map-it (name dirp)
806              (funcall fun
807                       (merge-pathnames (parse-native-namestring
808                                         name nil physical
809                                         :as-directory (and dirp (not as-files)))
810                                        physical))))
811       (with-native-directory-iterator (next dirname :errorp errorp)
812         (loop for name = (next)
813               while name
814               do (let* ((full (concatenate 'string dirname name))
815                         (kind (native-file-kind full)))
816                    (when kind
817                      (case kind
818                        (:directory
819                         (when directories
820                           (map-it name t)))
821                        (:symlink
822                         (if classify-symlinks
823                             (let* ((tmpname (merge-pathnames
824                                              (parse-native-namestring
825                                               name nil physical :as-directory nil)
826                                              physical))
827                                    (truename (query-file-system tmpname :truename nil)))
828                               (if (or (not truename)
829                                       (or (pathname-name truename) (pathname-type truename)))
830                                   (when files
831                                     (funcall fun tmpname))
832                                   (when directories
833                                     (map-it name t))))
834                             (when files
835                               (map-it name nil))))
836                        (t
837                         ;; Anything else parses as a file.
838                         (when files
839                           (map-it name nil)))))))))))
840
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)))
848                             dir))
849          (wild-inferiors (position :wild-inferiors dir))
850          (end (cond ((and wild wild-inferiors)
851                      (min wild wild-inferiors))
852                     (t
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)
858                                         :name nil
859                                         :type nil
860                                         :version nil)))
861     (cond (wild-inferiors
862            (map-wild-inferiors function rest starting-point))
863           (wild
864            (map-wild function rest starting-point))
865           (t
866            ;; Nothing wild -- the directory matches itself.
867            (funcall function starting-point))))
868   nil)
869
870 (defun last-directory-piece (pathname)
871   (car (last (pathname-directory pathname))))
872
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))
877         (next (car more)))
878     (flet ((cont (subdirectory)
879              (cond ((not more)
880                     ;; end of the line
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))
886                    (t
887                     (let ((this (pathname-directory subdirectory)))
888                       (map-matching-directories
889                        function
890                        (make-pathname :directory (append this more)
891                                       :defaults subdirectory)))))))
892       (map-directory
893        (if (eq :wild this)
894            #'cont
895            (lambda (sub)
896              (when (pattern-matches this (last-directory-piece sub))
897                (funcall #'cont sub))))
898        directory
899        :files nil
900        :directories t
901        :errorp nil))))
902
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))
907         do (pop more))
908   (let ((next (car more))
909         (rest (cdr more)))
910     (unless more
911       (funcall function directory))
912     (map-directory
913      (cond ((not more)
914             (lambda (pathname)
915               (funcall function pathname)
916               (map-wild-inferiors function more pathname)))
917            (t
918             (lambda (pathname)
919               (let ((this (pathname-directory pathname)))
920                 (when (equal next (car (last this)))
921                   (map-matching-directories
922                    function
923                    (make-pathname :directory (append this rest)
924                                   :defaults pathname)))
925                 (map-wild-inferiors function more pathname)))))
926      directory
927      :files nil
928      :directories t
929      :errorp nil)))
930
931 ;;; Part of DIRECTORY: implements iterating over entries in a directory, and
932 ;;; matching them.
933 (defun map-matching-entries (function directory match-name match-type)
934   (map-directory
935    (lambda (file)
936      (when (and (funcall match-name (pathname-name file))
937                 (funcall match-type (pathname-type file)))
938        (funcall function file)))
939    directory
940    :files t
941    :directories :as-files
942    :errorp nil))
943
944 ;;; NOTE: There is a fair amount of hair below that is probably not
945 ;;; strictly necessary.
946 ;;;
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).
957 ;;;
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.
965
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.
971
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.
985
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
1004 ;;; 2007-12-31.
1005
1006 (defun pathname-intersections (one two)
1007   (aver (logical-pathname-p one))
1008   (aver (logical-pathname-p two))
1009   (labels
1010       ((intersect-version (one two)
1011          (aver (typep one '(or null (member :newest :wild :unspecific)
1012                             integer)))
1013          (aver (typep two '(or null (member :newest :wild :unspecific)
1014                             integer)))
1015          (cond
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)
1020            ((eql one two) one)
1021            (t nil)))
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)))
1025          (cond
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)))
1035          (cond
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)))
1041               (mapcar
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)))
1051       (mapcar (lambda (d)
1052                 (make-pathname :host host :name name :type type
1053                                :version version :directory d))
1054               (intersect-directory
1055                (pathname-directory one) (pathname-directory two))))))
1056
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).
1062 ;;;
1063 ;;; FIXME: Does not work with :UP or :BACK
1064 ;;; FIXME: Does not work with patterns
1065 ;;;
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)
1071            (cond
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)))))
1077     (macrolet
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)
1084                                  (append
1085                                   (butlast ,bounding-sequence (- ,l ,index))
1086                                   ,g2))
1087                         (mapcar
1088                          (lambda (,g3)
1089                            (append
1090                             (if (eq (car (nthcdr ,index ,bounding-sequence))
1091                                     :wild-inferiors)
1092                                 '(:wild-inferiors)
1093                                 nil) ,g3))
1094                          (intersect-directory-helper
1095                           ,@(if order
1096                                 `((nthcdr ,index one) (cdr two))
1097                                 `((cdr one) (nthcdr ,index two)))))))))))
1098       (cond
1099         ((and (eq (car one) :wild-inferiors)
1100               (eq (car two) :wild-inferiors))
1101          (delete-duplicates
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))
1106           :test 'equal))
1107         ((eq (car one) :wild-inferiors)
1108          (delete-duplicates (loop-possible-wild-inferiors-matches 0 two nil)
1109                             :test 'equal))
1110         ((eq (car two) :wild-inferiors)
1111          (delete-duplicates (loop-possible-wild-inferiors-matches 0 one t)
1112                             :test 'equal))
1113         ((and (null one) (null two)) (list nil))
1114         ((null one) nil)
1115         ((null two) 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)))))))))
1120 \f
1121
1122 (defun directory-pathname-p (pathname)
1123   (and (pathnamep pathname)
1124        (null (pathname-name pathname))
1125        (null (pathname-type pathname))))
1126
1127 (defun ensure-directories-exist (pathspec &key verbose (mode #o777))
1128   #!+sb-doc
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))))
1134         (created-p nil))
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)
1145             do
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)
1153                                           'string)))
1154                   (when verbose
1155                     (format *standard-output*
1156                             "~&creating directory: ~A~%"
1157                             namestring))
1158                   (sb!unix:unix-mkdir namestring mode)
1159                   (unless (directory-pathname-p (probe-file newpath))
1160                     (restart-case
1161                         (error
1162                          'simple-file-error
1163                          :pathname pathspec
1164                          :format-control
1165                          (if (and probed
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))
1171                       (retry ()
1172                         :report "Retry directory creation."
1173                         (ensure-directories-exist
1174                          pathspec
1175                          :verbose verbose :mode mode))
1176                       (continue ()
1177                         :report
1178                         "Continue as if directory creation was successful."
1179                         nil)))
1180                   (setf created-p t)))))
1181       (values pathspec created-p))))
1182
1183 (/show0 "filesys.lisp 1000")