/[meta-cvs]/meta-cvs/F-6BD7679FED76D6E5A7D36DC36C205FC1.lisp
ViewVC logotype

Contents of /meta-cvs/F-6BD7679FED76D6E5A7D36DC36C205FC1.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4.2.1 - (show annotations)
Sat Aug 24 22:37:32 2002 UTC (11 years, 8 months ago) by kaz
Branch: symlink-branch
Changes since 1.4: +12 -5 lines
* code/clisp-unix.lisp (link-error): New slot, kind. Holds
the string "symbolic" or "hard".
(initialize-instance link-error): Use new slot in constructing
message.
(link): Specify "hard" value for :kind slot of condition.
(symlink): New function.

* code/clisp-linux.lisp: Likewise.
1 ;;; This source file is part of the Meta-CVS program,
2 ;;; which is distributed under the GNU license.
3 ;;; Copyright 2002 Kaz Kylheku
4
5 (provide "clisp-unix")
6
7 ;;; Null pointer handling
8
9 #.(when (< (first (system::version)) 20020129)
10 (push :clisp-old *features*)
11 (values))
12
13 #+clisp-old
14 (defmacro pointer-null (p) `(unix-funcs:null-pointer-p ,p))
15
16 #-clisp-old
17 (defmacro pointer-null (p) `(ffi:foreign-address-null ,p))
18
19 (defmacro null-to-nil (p)
20 (let ((pointer (gensym)))
21 `(let ((,pointer ,p)) (if (pointer-null ,pointer) nil ,pointer))))
22
23 (defmacro when-not-null (p &body forms)
24 `(if (not (pointer-null ,p)) ,@forms))
25
26 ;;; Base condition
27
28 (define-condition system-error (error) ((message :initarg :message))
29 (:report (lambda (condition stream)
30 (format stream "System error: ~A"
31 (slot-value condition 'message)))))
32
33 ;;; Macro to catch ENOENT errors and turn them into nil
34 ;;; return value.
35
36 (defmacro no-existence-error (&body forms)
37 (let ((block-sym (gensym "BLOCK-")))
38 `(block ,block-sym
39 (handler-bind
40 ((system-error #'(lambda (con)
41 (declare (ignore con))
42 (if (= unix-funcs:errno unix-funcs:enoent)
43 (return-from ,block-sym nil)))))
44 ,@forms))))
45
46 ;;; Directory access
47
48 (define-condition open-dir-error (system-error) ((dir :initarg :dir)))
49
50 (defmethod initialize-instance :after ((c open-dir-error) &rest args)
51 (declare (ignore args))
52 (with-slots (dir message) c
53 (setf message (format nil "Unable to open ~A: ~A."
54 dir (unix-funcs:strerror unix-funcs:errno)))))
55
56 (define-condition open-error (system-error) ((path :initarg :path)))
57
58 (defmethod initialize-instance :after ((c open-error) &rest args)
59 (declare (ignore args))
60 (with-slots (path message) c
61 (setf message (format nil "Unable to open ~A: ~A."
62 path (unix-funcs:strerror unix-funcs:errno)))))
63
64
65 (defun opendir (dir)
66 (cond
67 ((null-to-nil (unix-funcs:opendir dir)))
68 (t (error (make-condition 'open-dir-error :dir dir)))))
69
70 (defun closedir (dir-stream)
71 (when-not-null dir-stream (unix-funcs:closedir dir-stream)))
72
73 (declaim (inline closedir))
74
75 (defun readdir (dir-stream)
76 (let ((dir-entry (unix-funcs:readdir dir-stream)))
77 (if dir-entry
78 (with-slots ((name unix-funcs:name) (ino unix-funcs:ino)) dir-entry
79 (values name ino))
80 nil)))
81
82 (defmacro with-open-dir ((var dir) &body forms)
83 `(let ((,var (opendir ,dir)))
84 (unwind-protect
85 (progn ,@forms)
86 (closedir ,var))))
87
88 (define-condition chdir-error (system-error) ((dir :initarg :dir)))
89
90 (defmethod initialize-instance :after ((c chdir-error) &rest args)
91 (declare (ignore args))
92 (with-slots (dir message) c
93 (setf message (format nil "Unable to change to directory ~A: ~A."
94 dir (unix-funcs:strerror unix-funcs:errno)))))
95
96 (defun chdir (dir)
97 (if (= -1 (unix-funcs:chdir dir))
98 (error (make-condition 'chdir-error :dir dir)))
99 (values))
100
101 (defun fchdir (descr)
102 (if (= -1 (unix-funcs:fchdir descr))
103 (error (make-condition 'chdir-error
104 :dir (format nil "[file descriptor ~a]" descr))))
105 (values))
106
107 (defun getcwd ()
108 (unix-funcs:getcwd))
109 (declaim (inline getcwd))
110
111 (defmacro current-dir-restore (&body forms)
112 (let ((saved-dir (gensym "SAVED-DIR-")))
113 `(let ((,saved-dir (unix-funcs:open "." unix-funcs:o-rdonly 0)))
114 (when (= ,saved-dir -1)
115 (error (make-condition 'open-error :path ".")))
116 (unwind-protect (progn ,@forms)
117 (fchdir ,saved-dir)
118 (unix-funcs:close ,saved-dir)))))
119
120 ;;; File information
121
122 (define-condition file-info-error (system-error) ((file :initarg :file)))
123
124 (defmethod initialize-instance :after ((c file-info-error) &rest args)
125 (declare (ignore args))
126 (with-slots (file message) c
127 (setf message (format nil "Unable to get status of ~A: ~A."
128 file (unix-funcs:strerror unix-funcs:errno)))))
129
130 (defclass file-info ()
131 ((file-name :initarg :file-name :accessor file-name)
132 (mode-flags :initarg :mode-flags :accessor mode-flags)
133 (mod-time :initarg :mod-time :accessor mod-time)
134 (inode :initarg :inode :accessor inode)
135 (num-links :initarg :num-links :accessor num-links)))
136
137 (defgeneric same-file-p (file1 file2))
138 (defgeneric older-p (file1 file2))
139 (defgeneric regular-p (file))
140 (defgeneric directory-p (file))
141 (defgeneric symlink-p (file))
142 (defgeneric is-root-p (file))
143 (defgeneric get-parent (file))
144
145 (defmethod same-file-p ((f1 file-info) (f2 file-info))
146 (= (inode f1) (inode f2)))
147
148 (defmethod same-file-p ((f1 string) (f2 string))
149 (= (stat f1) (stat f2)))
150
151 (defmethod older-p ((f1 file-info) (f2 file-info))
152 (< (mod-time f1) (mod-time f2)))
153
154 (defmethod older-p ((f1 string) (f2 string))
155 (older-p (stat f1) (stat f2)))
156
157 (defmethod regular-p ((file file-info))
158 (unix-funcs:s-isreg (mode-flags file)))
159
160 (defmethod regular-p ((filename string))
161 (regular-p (stat filename)))
162
163 (defmethod regular-p ((x null))
164 nil)
165
166 (defmethod directory-p ((file file-info))
167 (unix-funcs:s-isdir (mode-flags file)))
168
169 (defmethod directory-p ((filename string))
170 (directory-p (stat filename)))
171
172 (defmethod directory-p ((x null))
173 nil)
174
175 (defmethod symlink-p ((file file-info))
176 (unix-funcs:s-islnk (mode-flags file)))
177
178 (defmethod symlink-p ((filename string))
179 (symlink-p (stat filename)))
180
181 (defmethod symlink-p ((x null))
182 nil)
183
184 (defmethod is-root-p ((file file-info))
185 (and (directory-p file)
186 (same-file-p file (stat (format nil "~a/.." (file-name file))))))
187
188 (defmethod is-root-p ((filename string))
189 (is-root-p (stat filename)))
190
191 (defmethod get-parent ((file file-info))
192 (stat (format nil "~a/.." (file-name file))))
193
194 (defmethod get-parent ((filename string))
195 (stat (format nil "~a/.." filename)))
196
197 (defun stat (name &key through-link)
198 (if (typep name 'file-info)
199 name
200 (multiple-value-bind (result stat-info)
201 (if through-link
202 (unix-funcs:stat name)
203 (unix-funcs:lstat name))
204 (when (= result -1)
205 (error (make-condition 'file-info-error :file name)))
206 (with-slots ((mode unix-funcs:mode)
207 (mtime unix-funcs:mtime)
208 (inode unix-funcs:ino)
209 (nlink unix-funcs:nlink)) stat-info
210 (make-instance 'file-info :file-name name
211 :mode-flags mode
212 :mod-time mtime
213 :inode inode
214 :num-links nlink)))))
215
216 (defun exists (name &key through-link)
217 (no-existence-error (stat name :through-link through-link)))
218
219 ;;; Symbolic and hard links
220
221 (define-condition link-error (system-error)
222 ((from-path :initarg :from-path)
223 (to-path :initarg :to-path)
224 (kind :initarg :kind)))
225
226 (defmethod initialize-instance :after ((c link-error) &rest args)
227 (declare (ignore args))
228 (with-slots (message kind from-path to-path) c
229 (setf message (format nil "Unable to make ~A link from ~A to ~A: ~A."
230 kind from-path to-path
231 (unix-funcs:strerror unix-funcs:errno)))))
232
233 (defun link (from to)
234 (if (zerop (unix-funcs:link from to))
235 (values)
236 (error (make-condition 'link-error :from-path from
237 :to-path to :kind "hard"))))
238
239 (defun symlink (from to)
240 (if (zerop (unix-funcs:symlink from to))
241 (values)
242 (error (make-condition 'link-error :from-path from
243 :to-path to :kind "symbolic"))))
244 ;;; Directory removal
245
246 (define-condition rm-error (system-error)
247 ((path :initarg :path)))
248
249 (defmethod initialize-instance :after ((c rm-error) &rest args)
250 (declare (ignore args))
251 (with-slots (message path) c
252 (setf message (format nil "Unable to remove ~A: ~A."
253 path (unix-funcs:strerror unix-funcs:errno)))))
254
255 (defun rmdir (dir)
256 (if (zerop (unix-funcs:rmdir dir))
257 (values)
258 (error (make-condition 'rm-error :path dir))))
259
260 (defun unlink (file)
261 (if (zerop (unix-funcs:unlink file))
262 (values)
263 (error (make-condition 'rm-error :path file))))
264
265 ;;; Coprocesses
266
267 (defconstant *argument-limit* (* 64 1024))
268
269 (defun shell-interpreter (command)
270 (case (shell command)
271 ((0) T)
272 (otherwise nil)))
273
274 (defun execute-program (arglist)
275 (case (run-program (first arglist) :arguments (rest arglist))
276 ((0) T)
277 (otherwise nil)))
278
279 (defmacro with-input-from-program ((stream-var arg-list) &body forms)
280 `(let* ((,stream-var (make-pipe-input-stream
281 (arglist-to-command-string ,arg-list))))
282 (declare (dynamic-extent ,stream-var))
283 (when ,stream-var
284 (unwind-protect (progn ,@forms) (close ,stream-var)))))
285
286 (defmacro with-output-to-program ((stream-var arg-list) &body forms)
287 `(let* ((,stream-var (make-pipe-output-stream
288 (arglist-to-command-string ,arg-list))))
289 (declare (dynamic-extent ,stream-var))
290 (when ,stream-var
291 (unwind-protect (progn ,@forms) (close ,stream-var)))))
292
293 ;;; GUID generation
294
295 (defun guid-gen ()
296 (with-open-file (f "/dev/urandom"
297 :direction :input
298 :element-type '(unsigned-byte 128))
299 (read-byte f)))
300
301 ;;; Environment strings
302 (defun env-lookup (name &optional substitute-if-not-found)
303 (let ((value (getenv name)))
304 (if value value substitute-if-not-found)))

  ViewVC Help
Powered by ViewVC 1.1.5