source: trunk/abcl/src/org/armedbear/lisp/directory.lisp

Last change on this file was 15376, checked in by Mark Evenson, 13 months ago

Fix regression in CL:DIRECTORY for wild pathnames

We re-enable listing sub-directories of directories for wild pathnames
that have NAME and TYPE of :wild. This behavior was inadvertently
disabled with
<https://github.com/armedbear/abcl/commit/889a50534dde062f037b01f4b8e2b6c108561709>.

This regression broke Quicklisp's indexing of local projects directory
which has implementation specific code which depends on

(directory "~/quicklisp/*.*")

returning both immediate sub-directories as well as files under
<file:~/quicklisp/>.

According to
<http://www.lispworks.com/documentation/HyperSpec/Body/f_pn_mat.htm>
CL:PATHNAME-MATCH-P is supposed to be consistent with CL:DIRECTORY, but
this doesn't seem to be the case for SBCL, CCL, LWpro, but *is* true
for ECL.

;;; This should return (t t t …) for every wildcard that one can pass
;;; to DIRECTORY

(let ((wildcard #p"/Users/evenson/quicklisp/local-projects/*.*"))

(mapcar

(lambda (e) (pathname-match-p e wildcard))
(directory wildcard)))

At this time, we choose to be compatible with the previous behavior of
ABCL and the current behavior of SBCL/CCL rather than strict
conformance here.

This should fix <https://github.com/qitab/cl-protobufs/issues/199>.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.4 KB
Line 
1;;; directory.lisp
2;;;
3;;; Copyright (C) 2004-2007 Peter Graves
4;;; Copyright (C) 2008 Ville Voutilainen
5;;; $Id: directory.lisp 15376 2020-09-09 17:09:45Z 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
113With :RESOLVE-SYMLINKS set to nil, not all pathnames returned may
114correspond to an existing file.  Symbolic links are considered to be
115be valid entries even if they do not currently have a valid file or
116directory as a target.  Therefore, subsequent CL:TRUENAME call on
117individual pathnames in the list may signal an error, i.e. the
118pathnames have been constructed as truenames, without calling the
119entire resolution routine of CL:TRUENAME.
120
121If called with :RESOLVE-SYMLINKS set to T, and any of the pathnames
122have truenames which do not exist, this routine will signal a file
123error to its caller."
124
125  (let ((pathname (merge-pathnames pathspec)))
126    (when (logical-pathname-p pathname)
127      (setq pathname (translate-logical-pathname pathname)))
128    (if (or (position #\* (namestring pathname))
129            (wild-pathname-p pathname))
130        (if (pathname-jar-p pathname)
131            (match-wild-jar-pathname pathname)
132            (let ((namestring (directory-namestring pathname)))
133              (when (and namestring (> (length namestring) 0))
134                (when (featurep :windows)
135                  (let ((host (pathname-host pathname))
136                        (device (pathname-device pathname)))
137                    (cond 
138                      ((and host device)
139                       (setq namestring 
140                             (concatenate 'string "//" host "/" device  namestring)))
141                      (device
142                       (setq namestring 
143                             (concatenate 'string device ":" namestring))))))
144                (let ((entries (list-directories-with-wildcards 
145                                namestring nil resolve-symlinks))
146                      matching-entries)
147                  (dolist (entry entries)
148                    (when
149                        (or
150                         (and 
151                          (file-directory-p entry :wild-error-p nil)
152                          (pathname-match-p
153                           (directory-as-file entry) pathname))
154                         (pathname-match-p entry pathname))
155                      (push 
156                       (if resolve-symlinks
157                           (truename entry) 
158                           ;; Normalize nil DEVICE to :UNSPECIFIC under non-Windows
159                           ;; fixes ANSI DIRECTORY.[67]
160                           (if (and (not (find :windows *features*))
161                                    (not (pathname-device entry)))
162                               (make-pathname :defaults entry :device :unspecific)
163                               entry))
164                       matching-entries)))
165                  matching-entries))))
166        ;; Not wild.
167        (let ((truename (probe-file pathname)))
168          (if truename
169              (list (pathname truename))
170              nil)))))
Note: See TracBrowser for help on using the repository browser.