/
fad.lisp
282 lines (262 loc) · 13.7 KB
/
fad.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: CL-FAD; Base: 10 -*-
;;; $Header: /usr/local/cvsrep/cl-fad/fad.lisp,v 1.35 2009/09/30 14:23:10 edi Exp $
;;; Copyright (c) 2004, Peter Seibel. All rights reserved.
;;; Copyright (c) 2004-2010, Dr. Edmund Weitz. All rights reserved.
;;; Redistribution and use in source and binary forms, with or without
;;; modification, are permitted provided that the following conditions
;;; are met:
;;; * Redistributions of source code must retain the above copyright
;;; notice, this list of conditions and the following disclaimer.
;;; * Redistributions in binary form must reproduce the above
;;; copyright notice, this list of conditions and the following
;;; disclaimer in the documentation and/or other materials
;;; provided with the distribution.
;;; THIS SOFTWARE IS PROVIDED BY THE AUTHORS 'AS IS' AND ANY EXPRESSED
;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
(in-package :asia-fad)
(defun component-present-p (value)
"Helper function for DIRECTORY-PATHNAME-P which checks whether VALUE
is neither NIL nor the keyword :UNSPECIFIC."
(and value (not (eql value :unspecific))))
(defun directory-pathname-p (pathspec)
"Returns NIL if PATHSPEC \(a pathname designator) does not designate
a directory, PATHSPEC otherwise. It is irrelevant whether file or
directory designated by PATHSPEC does actually exist."
(and
(not (component-present-p (pathname-name pathspec)))
(not (component-present-p (pathname-type pathspec)))
pathspec))
(defun pathname-as-directory (pathspec)
"Converts the non-wild pathname designator PATHSPEC to directory
form."
(let ((pathname (pathname pathspec)))
(when (wild-pathname-p pathname)
(error "Can't reliably convert wild pathnames."))
(cond ((not (directory-pathname-p pathspec))
(make-pathname :directory (append (or (pathname-directory pathname)
(list :relative))
(list (file-namestring pathname)))
:name nil
:type nil
:defaults pathname))
(t pathname))))
(defun directory-wildcard (dirname)
"Returns a wild pathname designator that designates all files within
the directory named by the non-wild pathname designator DIRNAME."
(when (wild-pathname-p dirname)
(error "Can only make wildcard directories from non-wildcard directories."))
(make-pathname :name #-:cormanlisp :wild #+:cormanlisp "*"
:type #-(or :clisp :cormanlisp) :wild
#+:clisp nil
#+:cormanlisp "*"
:defaults (pathname-as-directory dirname)))
#+:clisp
(defun clisp-subdirectories-wildcard (wildcard)
"Creates a wild pathname specifically for CLISP such that
sub-directories are returned by DIRECTORY."
(make-pathname :directory (append (pathname-directory wildcard)
(list :wild))
:name nil
:type nil
:defaults wildcard))
(defun list-directory (dirname)
"Returns a fresh list of pathnames corresponding to the truenames of
all files within the directory named by the non-wild pathname
designator DIRNAME. The pathnames of sub-directories are returned in
directory form - see PATHNAME-AS-DIRECTORY."
(when (wild-pathname-p dirname)
(error "Can only list concrete directory names."))
#+:ecl
(let ((dir (pathname-as-directory dirname)))
(concatenate 'list
(directory (merge-pathnames (pathname "*/") dir))
(directory (merge-pathnames (pathname "*.*") dir))))
#-:ecl
(let ((wildcard (directory-wildcard dirname)))
#+:abcl (system::list-directory dirname)
#+(or :sbcl :cmu :scl :lispworks) (directory wildcard)
#+(or :openmcl :digitool) (directory wildcard :directories t)
#+:allegro (directory wildcard :directories-are-files nil)
#+:clisp (nconc (directory wildcard :if-does-not-exist :keep)
(directory (clisp-subdirectories-wildcard wildcard)))
#+:cormanlisp (nconc (directory wildcard)
(cl::directory-subdirs dirname)))
#-(or :sbcl :cmu :scl :lispworks :openmcl :allegro :clisp :cormanlisp :ecl :abcl :digitool)
(error "LIST-DIRECTORY not implemented"))
(defun pathname-as-file (pathspec)
"Converts the non-wild pathname designator PATHSPEC to file form."
(let ((pathname (pathname pathspec)))
(when (wild-pathname-p pathname)
(error "Can't reliably convert wild pathnames."))
(cond ((directory-pathname-p pathspec)
(let* ((directory (pathname-directory pathname))
(name-and-type (pathname (first (last directory)))))
(make-pathname :directory (butlast directory)
:name (pathname-name name-and-type)
:type (pathname-type name-and-type)
:defaults pathname)))
(t pathname))))
(defun file-exists-p (pathspec)
"Checks whether the file named by the pathname designator PATHSPEC
exists and returns its truename if this is the case, NIL otherwise.
The truename is returned in `canonical' form, i.e. the truename of a
directory is returned as if by PATHNAME-AS-DIRECTORY."
#+(or :sbcl :lispworks :openmcl :ecl :digitool) (probe-file pathspec)
#+:allegro (or (excl:probe-directory (pathname-as-directory pathspec))
(probe-file pathspec))
#+(or :cmu :scl :abcl) (or (probe-file (pathname-as-directory pathspec))
(probe-file pathspec))
#+:cormanlisp (or (and (ccl:directory-p pathspec)
(pathname-as-directory pathspec))
(probe-file pathspec))
#+:clisp (or (ignore-errors
(let ((directory-form (pathname-as-directory pathspec)))
(when (ext:probe-directory directory-form)
directory-form)))
(ignore-errors
(probe-file (pathname-as-file pathspec))))
#-(or :sbcl :cmu :scl :lispworks :openmcl :allegro :clisp :cormanlisp :ecl :abcl :digitool)
(error "FILE-EXISTS-P not implemented"))
(defun directory-exists-p (pathspec)
"Checks whether the file named by the pathname designator PATHSPEC
exists and if it is a directory. Returns its truename if this is the
case, NIL otherwise. The truename is returned in directory form as if
by PATHNAME-AS-DIRECTORY."
#+:allegro
(and (excl:probe-directory pathspec)
(pathname-as-directory (truename pathspec)))
#+:lispworks
(and (lw:file-directory-p pathspec)
(pathname-as-directory (truename pathspec)))
#-(or :allegro :lispworks)
(let ((result (file-exists-p pathspec)))
(and result
(directory-pathname-p result)
result)))
(defun walk-directory (dirname fn &key directories
(if-does-not-exist :error)
(test (constantly t)))
"Recursively applies the function FN to all files within the
directory named by the non-wild pathname designator DIRNAME and all of
its sub-directories. FN will only be applied to files for which the
function TEST returns a true value. If DIRECTORIES is not NIL, FN and
TEST are applied to directories as well. If DIRECTORIES is :DEPTH-FIRST,
FN will be applied to the directory's contents first. If
DIRECTORIES is :BREADTH-FIRST and TEST returns NIL, the
directory's content will be skipped. IF-DOES-NOT-EXIST must be
one of :ERROR or :IGNORE where :ERROR means that an error will be
signaled if the directory DIRNAME does not exist."
(labels ((walk (name)
(cond
((directory-pathname-p name)
;; the code is written in a slightly awkward way for
;; backward compatibility
(cond ((not directories)
(dolist (file (list-directory name))
(walk file)))
((eql directories :breadth-first)
(when (funcall test name)
(funcall fn name)
(dolist (file (list-directory name))
(walk file))))
;; :DEPTH-FIRST is implicit
(t (dolist (file (list-directory name))
(walk file))
(when (funcall test name)
(funcall fn name)))))
((funcall test name)
(funcall fn name)))))
(let ((pathname-as-directory (pathname-as-directory dirname)))
(case if-does-not-exist
((:error)
(cond ((not (file-exists-p pathname-as-directory))
(error "File ~S does not exist."
pathname-as-directory))
(t (walk pathname-as-directory))))
((:ignore)
(when (file-exists-p pathname-as-directory)
(walk pathname-as-directory)))
(otherwise
(error "IF-DOES-NOT-EXIST must be one of :ERROR or :IGNORE."))))
(values)))
(defvar *stream-buffer-size* 8192)
(defun copy-stream (from to &optional (checkp t))
"Copies into TO \(a stream) from FROM \(also a stream) until the end
of FROM is reached, in blocks of *stream-buffer-size*. The streams
should have the same element type. If CHECKP is true, the streams are
checked for compatibility of their types."
(when checkp
(unless (subtypep (stream-element-type to) (stream-element-type from))
(error "Incompatible streams ~A and ~A." from to)))
(let ((buf (make-array *stream-buffer-size*
:element-type (stream-element-type from))))
(loop
(let ((pos #-(or :clisp :cmu) (read-sequence buf from)
#+:clisp (ext:read-byte-sequence buf from :no-hang nil)
#+:cmu (sys:read-n-bytes from buf 0 *stream-buffer-size* nil)))
(when (zerop pos) (return))
(write-sequence buf to :end pos))))
(values))
(defun copy-file (from to &key overwrite)
"Copies the file designated by the non-wild pathname designator FROM
to the file designated by the non-wild pathname designator TO. If
OVERWRITE is true overwrites the file designtated by TO if it exists."
#+:allegro (excl.osi:copy-file from to :overwrite overwrite)
#-:allegro
(let ((element-type #-:cormanlisp '(unsigned-byte 8)
#+:cormanlisp 'unsigned-byte))
(with-open-file (in from :element-type element-type)
(with-open-file (out to :element-type element-type
:direction :output
:if-exists (if overwrite
:supersede
#-:cormanlisp :error
#+:cormanlisp nil))
#+:cormanlisp
(unless out
(error (make-condition 'file-error
:pathname to
:format-control "File already exists.")))
(copy-stream in out))))
(values))
(defun delete-directory-and-files (dirname &key (if-does-not-exist :error))
"Recursively deletes all files and directories within the directory
designated by the non-wild pathname designator DIRNAME including
DIRNAME itself. IF-DOES-NOT-EXIST must be one of :ERROR or :IGNORE
where :ERROR means that an error will be signaled if the directory
DIRNAME does not exist."
#+:allegro (excl.osi:delete-directory-and-files dirname
:if-does-not-exist if-does-not-exist)
#-:allegro (walk-directory dirname
(lambda (file)
(cond ((directory-pathname-p file)
#+:lispworks (lw:delete-directory file)
#+:cmu (multiple-value-bind (ok err-number)
(unix:unix-rmdir (namestring (truename file)))
(unless ok
(error "Error number ~A when trying to delete ~A"
err-number file)))
#+:scl (multiple-value-bind (ok errno)
(unix:unix-rmdir (ext:unix-namestring (truename file)))
(unless ok
(error "~@<Error deleting ~S: ~A~@:>"
file (unix:get-unix-error-msg errno))))
#+:sbcl (sb-posix:rmdir file)
#+:clisp (ext:delete-dir file)
#+:openmcl (asia-fad-ccl:delete-directory file)
#+:cormanlisp (win32:delete-directory file)
#+:ecl (si:rmdir file)
#+(or :abcl :digitool) (delete-file file))
(t (delete-file file))))
:directories t
:if-does-not-exist if-does-not-exist)
(values))