| 1 | ;;; directory.lisp |
|---|
| 2 | ;;; |
|---|
| 3 | ;;; Copyright (C) 2004-2007 Peter Graves |
|---|
| 4 | ;;; Copyright (C) 2008 Ville Voutilainen |
|---|
| 5 | ;;; $Id: directory.lisp 15587 2022-05-23 06:23:44Z mevenson $ |
|---|
| 6 | ;;; |
|---|
| 7 | ;;; This program is free software; you can redistribute it and/or |
|---|
| 8 | ;;; modify it under the terms of the GNU General Public License |
|---|
| 9 | ;;; as published by the Free Software Foundation; either version 2 |
|---|
| 10 | ;;; of the License, or (at your option) any later version. |
|---|
| 11 | ;;; |
|---|
| 12 | ;;; This program is distributed in the hope that it will be useful, |
|---|
| 13 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 15 | ;;; GNU General Public License for more details. |
|---|
| 16 | ;;; |
|---|
| 17 | ;;; You should have received a copy of the GNU General Public License |
|---|
| 18 | ;;; along with this program; if not, write to the Free Software |
|---|
| 19 | ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|---|
| 20 | ;;; |
|---|
| 21 | ;;; As a special exception, the copyright holders of this library give you |
|---|
| 22 | ;;; permission to link this library with independent modules to produce an |
|---|
| 23 | ;;; executable, regardless of the license terms of these independent |
|---|
| 24 | ;;; modules, and to copy and distribute the resulting executable under |
|---|
| 25 | ;;; terms of your choice, provided that you also meet, for each linked |
|---|
| 26 | ;;; independent module, the terms and conditions of the license of that |
|---|
| 27 | ;;; module. An independent module is a module which is not derived from |
|---|
| 28 | ;;; or based on this library. If you modify this library, you may extend |
|---|
| 29 | ;;; this exception to your version of the library, but you are not |
|---|
| 30 | ;;; obligated to do so. If you do not wish to do so, delete this |
|---|
| 31 | ;;; exception statement from your version. |
|---|
| 32 | |
|---|
| 33 | (in-package "SYSTEM") |
|---|
| 34 | |
|---|
| 35 | ;;; utility function for LIST-DIRECTORIES-WITH-WILDCARDS |
|---|
| 36 | (defun directory-as-file (pathname) |
|---|
| 37 | "Convert a PATHNAME referencing a directory to a file" |
|---|
| 38 | (let ((directory (pathname-directory pathname))) |
|---|
| 39 | (make-pathname :host nil |
|---|
| 40 | :device (pathname-device pathname) |
|---|
| 41 | :directory (butlast directory) |
|---|
| 42 | :name (car (last directory)) |
|---|
| 43 | :type nil |
|---|
| 44 | :version nil))) |
|---|
| 45 | |
|---|
| 46 | ;;; utility function for LIST-DIRECTORIES-WITH-WILDCARDS |
|---|
| 47 | (defun wild-inferiors-p (component) |
|---|
| 48 | (eq component :wild-inferiors)) |
|---|
| 49 | |
|---|
| 50 | (defun list-directories-with-wildcards (pathname |
|---|
| 51 | wild-inferiors-found |
|---|
| 52 | resolve-symlinks) |
|---|
| 53 | (let* ((directory (pathname-directory pathname)) |
|---|
| 54 | (first-wild-inferior (and (not wild-inferiors-found) |
|---|
| 55 | (position-if #'wild-inferiors-p directory))) |
|---|
| 56 | (first-wild (position-if #'wild-p directory)) |
|---|
| 57 | (wild (when (or first-wild-inferior first-wild) |
|---|
| 58 | (nthcdr (or first-wild-inferior first-wild) directory))) |
|---|
| 59 | (non-wild (if (or first-wild-inferior first-wild) |
|---|
| 60 | (nbutlast directory |
|---|
| 61 | (- (length directory) |
|---|
| 62 | (or first-wild-inferior first-wild))) |
|---|
| 63 | directory)) |
|---|
| 64 | (newpath (make-pathname :directory non-wild |
|---|
| 65 | :name nil :type nil :defaults pathname)) |
|---|
| 66 | (entries (list-directory newpath resolve-symlinks))) |
|---|
| 67 | (when (not (or wild wild-inferiors-found)) ;; no further recursion necessary |
|---|
| 68 | (return-from list-directories-with-wildcards entries)) |
|---|
| 69 | (let ((inferior-entries (when (or wild-inferiors-found first-wild-inferior) entries))) |
|---|
| 70 | (nconc |
|---|
| 71 | (mapcan (lambda (entry) |
|---|
| 72 | (when (pathname-match-p (pathname entry) pathname) |
|---|
| 73 | (list entry))) |
|---|
| 74 | inferior-entries) |
|---|
| 75 | (mapcan (lambda (entry) |
|---|
| 76 | (let* ((pathname (pathname entry)) |
|---|
| 77 | (directory (pathname-directory pathname)) |
|---|
| 78 | (rest-wild (cdr wild))) |
|---|
| 79 | (unless (pathname-name pathname) |
|---|
| 80 | (when (pathname-match-p (first (last directory)) |
|---|
| 81 | (cond ((eql (car wild) :wild) |
|---|
| 82 | "*") |
|---|
| 83 | ((eql (car wild) :wild-inferiors) |
|---|
| 84 | "*") |
|---|
| 85 | (wild |
|---|
| 86 | (car wild)) |
|---|
| 87 | (t ""))) |
|---|
| 88 | (when (and |
|---|
| 89 | (not (or first-wild-inferior |
|---|
| 90 | wild-inferiors-found)) |
|---|
| 91 | rest-wild) |
|---|
| 92 | (setf directory (nconc directory rest-wild))) |
|---|
| 93 | (let ((recurse (make-pathname :directory directory |
|---|
| 94 | :defaults newpath))) |
|---|
| 95 | (when (not (equal recurse newpath)) |
|---|
| 96 | (list-directories-with-wildcards |
|---|
| 97 | recurse |
|---|
| 98 | (or first-wild-inferior wild-inferiors-found) |
|---|
| 99 | resolve-symlinks))))))) |
|---|
| 100 | entries))))) |
|---|
| 101 | |
|---|
| 102 | ;;; The extension to ANSI via :RESOLVE-SYMLINKS was added as |
|---|
| 103 | ;;; <https://abcl.org/trac/ticket/340>, in which it was argued that |
|---|
| 104 | ;;; symlinks should be considered contents of a directory, and that in |
|---|
| 105 | ;;; any event, performing a DIRECTORY on a dangling symlink should not |
|---|
| 106 | ;;; signal an error. |
|---|
| 107 | ;;; |
|---|
| 108 | ;;; See <https://abcl.org/trac/changeset/14624> for additional |
|---|
| 109 | ;;; information on implementation decision. |
|---|
| 110 | (defun directory (pathspec &key (resolve-symlinks nil)) |
|---|
| 111 | "Determines which, if any, files that are present in the file system have names matching PATHSPEC, and returns a fresh list of pathnames corresponding to the potential truenames of those files. |
|---|
| 112 | |
|---|
| 113 | With :RESOLVE-SYMLINKS set to nil, not all pathnames returned may |
|---|
| 114 | correspond to an existing file. Symbolic links are considered to be |
|---|
| 115 | be valid entries even if they do not currently have a valid file or |
|---|
| 116 | directory as a target. Therefore, subsequent CL:TRUENAME call on |
|---|
| 117 | individual pathnames in the list may signal an error, i.e. the |
|---|
| 118 | pathnames have been constructed as truenames, without calling the |
|---|
| 119 | entire resolution routine of CL:TRUENAME. |
|---|
| 120 | |
|---|
| 121 | If called with :RESOLVE-SYMLINKS set to T, and any of the pathnames |
|---|
| 122 | have truenames which do not exist, this routine will signal a file |
|---|
| 123 | error to its caller." |
|---|
| 124 | |
|---|
| 125 | (let ((pathname (merge-pathnames pathspec))) |
|---|
| 126 | (when (equalp (pathname-host pathname) '(:scheme "file")) |
|---|
| 127 | (setq pathname (subseq (namestring pathname) #.(length "file://")))) |
|---|
| 128 | (when (logical-pathname-p pathname) |
|---|
| 129 | (setq pathname (translate-logical-pathname pathname))) |
|---|
| 130 | (if (or (position #\* (namestring pathname)) |
|---|
| 131 | (wild-pathname-p pathname)) |
|---|
| 132 | (if (pathname-jar-p pathname) |
|---|
| 133 | (match-wild-jar-pathname pathname) |
|---|
| 134 | (let ((namestring (directory-namestring pathname))) |
|---|
| 135 | (when (and namestring (> (length namestring) 0)) |
|---|
| 136 | (when (featurep :windows) |
|---|
| 137 | (let ((host (pathname-host pathname)) |
|---|
| 138 | (device (pathname-device pathname))) |
|---|
| 139 | (cond |
|---|
| 140 | ((and host device) |
|---|
| 141 | (setq namestring |
|---|
| 142 | (concatenate 'string "//" host "/" device namestring))) |
|---|
| 143 | (device |
|---|
| 144 | (setq namestring |
|---|
| 145 | (concatenate 'string device ":" namestring)))))) |
|---|
| 146 | (let ((entries (list-directories-with-wildcards |
|---|
| 147 | namestring nil resolve-symlinks)) |
|---|
| 148 | (matching-entries nil)) |
|---|
| 149 | (flet ((no-dots (path) |
|---|
| 150 | (merge-pathnames |
|---|
| 151 | (make-pathname :directory |
|---|
| 152 | (let ((reversed nil)) |
|---|
| 153 | (dolist (el (pathname-directory path)) |
|---|
| 154 | (if (eq el :up) |
|---|
| 155 | (pop reversed) |
|---|
| 156 | (unless (equal el ".") |
|---|
| 157 | (push el reversed)))) |
|---|
| 158 | (reverse reversed))) |
|---|
| 159 | path))) |
|---|
| 160 | (let ((pathname (no-dots pathname))) |
|---|
| 161 | (dolist (entry entries) |
|---|
| 162 | (when |
|---|
| 163 | (or |
|---|
| 164 | (and |
|---|
| 165 | (file-directory-p entry :wild-error-p nil) |
|---|
| 166 | (pathname-match-p |
|---|
| 167 | (directory-as-file entry) pathname)) |
|---|
| 168 | (pathname-match-p entry pathname)) |
|---|
| 169 | (push |
|---|
| 170 | (if resolve-symlinks |
|---|
| 171 | (truename entry) |
|---|
| 172 | ;; Normalize nil DEVICE to :UNSPECIFIC under non-Windows |
|---|
| 173 | ;; fixes ANSI DIRECTORY.[67] |
|---|
| 174 | (if (and (not (find :windows *features*)) |
|---|
| 175 | (not (pathname-device entry))) |
|---|
| 176 | (make-pathname :defaults entry :device :unspecific) |
|---|
| 177 | entry)) |
|---|
| 178 | matching-entries))))) |
|---|
| 179 | matching-entries)))) |
|---|
| 180 | ;; Not wild. |
|---|
| 181 | (let ((truename (probe-file pathname))) |
|---|
| 182 | (if truename |
|---|
| 183 | (list (pathname truename)) |
|---|
| 184 | nil))))) |
|---|