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

Last change on this file since 13230 was 13230, checked in by vvoutilainen, 11 years ago

Fix ticket #136: ABCL should allow DIRECTORY listings that don't follow symlinks, and/or provide a function for deleting a directory tree.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.2 KB
Line 
1;;; directory.lisp
2;;;
3;;; Copyright (C) 2004-2007 Peter Graves
4;;; Copyright (C) 2008 Ville Voutilainen
5;;; $Id: directory.lisp 13230 2011-03-02 20:34:38Z vvoutilainen $
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(defun pathname-as-file (pathname)
36  (let ((directory (pathname-directory pathname)))
37    (make-pathname :host nil
38                   :device (pathname-device pathname)
39                   :directory (butlast directory)
40                   :name (car (last directory))
41                   :type nil
42                   :version nil)))
43
44(defun wild-inferiors-p (component)
45  (eq component :wild-inferiors))
46
47(defun list-directories-with-wildcards (pathname 
48                                        wild-inferiors-found
49                                        resolve-symlinks)
50  (let* ((directory (pathname-directory pathname))
51   (first-wild-inferior (and (not wild-inferiors-found) 
52           (position-if #'wild-inferiors-p directory)))
53   (first-wild (position-if #'wild-p directory))
54   (wild (when (or first-wild-inferior first-wild)
55     (nthcdr (or first-wild-inferior first-wild) directory)))
56   (non-wild (if (or first-wild-inferior first-wild)
57           (nbutlast directory
58         (- (length directory) 
59            (or first-wild-inferior first-wild)))
60         directory))
61   (newpath (make-pathname :directory non-wild
62         :name nil :type nil :defaults pathname))
63   (entries (list-directory newpath resolve-symlinks)))
64    (if (not (or wild wild-inferiors-found))
65  entries
66  (let ((inferior-entries (when (or wild-inferiors-found first-wild-inferior) entries)))
67    (nconc 
68     (mapcan (lambda (entry) 
69         (when (pathname-match-p (pathname entry) pathname)
70           (list entry)))
71       inferior-entries)
72     (mapcan (lambda (entry)
73         (let* ((pathname (pathname entry))
74          (directory (pathname-directory pathname))
75          (rest-wild (cdr wild)))
76           (unless (pathname-name pathname)
77       (when (pathname-match-p (first (last directory))
78            (cond ((eql (car wild) :wild)
79             "*")
80            ((eql (car wild) :wild-inferiors)
81             "*")
82            (wild 
83             (car wild))
84            (t "")))
85         (when rest-wild
86           (setf directory (nconc directory rest-wild)))
87         (list-directories-with-wildcards
88          (make-pathname :directory directory
89             :defaults newpath)
90          (or first-wild-inferior wild-inferiors-found)
91                            resolve-symlinks)))))
92       entries))))))
93
94
95(defun directory (pathspec &key (resolve-symlinks t))
96  (let ((pathname (merge-pathnames pathspec)))
97    (when (logical-pathname-p pathname)
98      (setq pathname (translate-logical-pathname pathname)))
99    (if (or (position #\* (namestring pathname))
100      (wild-pathname-p pathname))
101        (if (pathname-jar-p pathname)
102            (match-wild-jar-pathname pathname)
103            (let ((namestring (directory-namestring pathname)))
104              (when (and namestring (> (length namestring) 0))
105                (when (featurep :windows)
106                  (let ((device (pathname-device pathname)))
107                    (when device
108                      (setq namestring (concatenate 'string device ":" namestring)))))
109                (let ((entries (list-directories-with-wildcards 
110                                namestring nil resolve-symlinks))
111                      (matching-entries ()))
112                  (dolist (entry entries)
113                    (cond ((file-directory-p entry)
114                           (when (pathname-match-p (file-namestring (pathname-as-file entry)) (file-namestring pathname))
115                             (push entry matching-entries)))
116                          ((pathname-match-p (file-namestring entry) (file-namestring pathname))
117                           (push entry matching-entries))))
118                  matching-entries))))
119        ;; Not wild.
120        (let ((truename (probe-file pathname)))
121          (if truename
122              (list (pathname truename))
123              nil)))))
Note: See TracBrowser for help on using the repository browser.