5 ;;; Unix system call stuff
7 ;;; (c) 2005 Straylight/Edgeware
10 ;;;----- Licensing notice ---------------------------------------------------
12 ;;; This program is free software; you can redistribute it and/or modify
13 ;;; it under the terms of the GNU General Public License as published by
14 ;;; the Free Software Foundation; either version 2 of the License, or
15 ;;; (at your option) any later version.
17 ;;; This program is distributed in the hope that it will be useful,
18 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;;; GNU General Public License for more details.
22 ;;; You should have received a copy of the GNU General Public License
23 ;;; along with this program; if not, write to the Free Software Foundation,
24 ;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 (defpackage #:mdw.unix
27 (:use #:common-lisp #:mdw.base #:mdw.collect)
28 (:export #:unix-error #:errno-value #:with-errno-handlers
29 #:unix-try-func #:unix-try
31 #:with-unix-open #:copy-file))
32 (in-package #:mdw.unix)
34 (defmacro with-buffer ((var len) &body body)
35 "Evaluate BODY with VAR bound to a pointer (a SAP, if you must know) to a
42 (setf ,var (system:allocate-system-memory ,lenvar))
44 (when ,var (system:deallocate-system-memory ,var ,lenvar))))))
45 (define-condition unix-error (error)
46 ((func :initform 'unknown :initarg :func :reader unix-error-func)
47 (args :initform nil :initarg :args :reader unix-error-args)
48 (errno :initform 0 :initarg :errno :reader unix-error-errno))
49 (:report (lambda (c s)
50 (format s "Error from ~A: ~A (~D)"
51 (cons (unix-error-func c) (unix-error-args c))
52 (unix:get-unix-error-msg (unix-error-errno c))
53 (unix-error-errno c))))
54 (:documentation "Reports an error from a Unix system call."))
55 (compile-time-defun errno-value (err)
56 "Returns the numeric value corresponding to an errno name."
59 (symbol (symbol-value (intern (symbol-name err) :unix)))))
60 (defmacro with-errno-handlers ((&key cond
64 "Evaluate FORM but trap Unix errors according to CLAUSES. Each clause has
65 the form of a `case' clause, but may contain symbolic errno names as well as
69 ((atom sw) (list (errno-value sw)))
70 (t (mapcar #'errno-value sw)))))
71 (with-gensyms (block condtmp formfunc)
72 (let ((labels (mapcar (lambda (cl)
76 `(let (,@(when cond `(,cond))
77 ,@(when errstring `(,errstring))
79 (,formfunc (lambda () ,form)))
85 (setf ,errno (unix-error-errno ,condtmp))
87 `((setf ,cond ,condtmp)))
90 (unix:get-unix-error-msg ,errno))))
92 ,@(mapcar (lambda (cl lab)
93 `(,(fix (car cl)) (go ,lab)))
96 (return-from ,block (funcall ,formfunc)))
98 (mapc (lambda (cl lab)
100 (collect `(return-from ,block
101 (progn ,@(cdr cl)))))
104 (defun unix-try-func (name func &rest args)
105 "Call Unix system call FUNC, passing it ARGS. If it returns an error,
106 signal the unix-error condition, with NAME and ARGS."
107 (multiple-value-call (lambda (rc &rest stuff)
113 (apply #'values rc stuff))
115 (defmacro unix-try (syscall &rest args)
116 "Wrapper for unix-try-func. Call Unix system-call SYSCALL (without the
117 `unix-' prefix or other stuff), passing it ARGS."
118 (let ((func (intern (format nil "UNIX-~A" (symbol-name syscall)) :unix)))
119 `(unix-try-func ',syscall #',func ,@args)))
120 (macrolet ((doit (slots)
121 `(defstruct (stat (:predicate statp)
123 (:constructor %make-stat-boa ,slots))
124 "Structure representing all the useful information `stat'
125 returns about a file."
127 (doit (dev ino mode nlink uid gid rdev size
128 atime mtime ctime blksize blocks)))
130 "Return information about FILE in a structure rather than as inconvenient
132 (multiple-value-call (lambda (rc &rest results)
134 (error 'unix-error :func 'stat :args (list file)
135 :error (car results)))
136 (apply #'%make-stat-boa results))
137 (unix:unix-stat file)))
138 (defmacro with-unix-open ((fd file how &optional (mode #o666)) &body body)
139 "Evaluate BODY with FD bound to a file descriptor obtained from a Unix
140 `open' syscall with arguments FILE, HOW and MODE. Close the file descriptor
145 (setf ,fd (unix-try open ,file ,how ,mode))
147 (when ,fd (unix-try close ,fd)))))
148 (defun copy-file (from to &optional (how 0))
149 "Make a copy of the file FROM called TO. The copy has the same permissions
150 and timestamps (except for ctime) and attempts to have the same owner and
151 group as the original."
152 (let ((st (stat from)))
153 (with-unix-open (in from unix:o_rdonly)
156 (logior unix:o_wronly unix:o_creat how)
157 (logand (st-mode st) #o777))
158 (unix-try fchmod out (st-mode st))
159 (unix-try utimes to (st-atime st) 0 (st-mtime st) 0)
160 (with-errno-handlers ()
161 (unix-try fchown out (st-uid st) (st-gid st))
163 (with-buffer (buf 16384)
165 (let ((n (unix-try read in buf 16384)))
168 (unix-try write out buf 0 n))))))))
170 ;;;----- That's all, folks --------------------------------------------------