/[cmucl]/src/hemlock/dired.lisp
ViewVC logotype

Contents of /src/hemlock/dired.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.3.1.1 - (show annotations) (vendor branch)
Wed Feb 5 16:31:07 1992 UTC (22 years, 2 months ago) by wlott
Changes since 1.1.1.3: +25 -25 lines
New alien changes.
1 ;;; -*- Log: hemlock.log; Package: dired -*-
2 ;;;
3 ;;; **********************************************************************
4 ;;; This code was written as part of the CMU Common Lisp project at
5 ;;; Carnegie Mellon University, and has been placed in the public domain.
6 ;;; If you want to use this code or any part of CMU Common Lisp, please contact
7 ;;; Scott Fahlman or slisp-group@cs.cmu.edu.
8 ;;;
9 (ext:file-comment
10 "$Header: /tiger/var/lib/cvsroots/cmucl/src/hemlock/dired.lisp,v 1.1.1.3.1.1 1992/02/05 16:31:07 wlott Exp $")
11 ;;;
12 ;;; **********************************************************************
13 ;;;
14 ;;; This file contains site dependent code for dired.
15 ;;; Written by Bill Chiles.
16 ;;;
17
18 (in-package "DIRED")
19
20 (shadow '(rename-file delete-file))
21
22 (export '(copy-file rename-file find-file delete-file make-directory
23 *update-default* *clobber-default* *recursive-default*
24 *report-function* *error-function* *yesp-function*
25 pathnames-from-pattern))
26
27
28
29 ;;;; Exported parameters.
30
31 (defparameter *update-default* nil
32 "Update arguments to utilities default to this value.")
33
34 (defparameter *clobber-default* t
35 "Clobber arguments to utilities default to this value.")
36
37 (defparameter *recursive-default* nil
38 "Recursive arguments to utilities default to this value.")
39
40
41
42 ;;;; WILDCARDP
43
44 (defconstant wildcard-char #\*
45 "Wildcard designator for file names will match any substring.")
46
47 (defmacro wildcardp (file-namestring)
48 `(position wildcard-char (the simple-string ,file-namestring) :test #'char=))
49
50
51
52 ;;;; User interaction functions, variable declarations, and their defaults.
53
54 (defun default-error-function (string &rest args)
55 (apply #'error string args))
56 ;;;
57 (defvar *error-function* #'default-error-function
58 "This function is called when an error is encountered in dired code.")
59
60 (defun default-report-function (string &rest args)
61 (apply #'format t string args))
62 ;;;
63 (defvar *report-function* #'default-report-function
64 "This function is called when the user needs to be informed of something.")
65
66 (defun default-yesp-function (string &rest args)
67 (apply #'format t string args)
68 (let ((answer (nstring-downcase (string-trim '(#\space #\tab) (read-line)))))
69 (declare (simple-string answer))
70 (or (string= answer "")
71 (string= answer "y")
72 (string= answer "yes")
73 (string= answer "ye"))))
74 ;;;
75 (defvar *yesp-function* #'default-yesp-function
76 "Function to query the user about clobbering an already existent file.")
77
78
79
80 ;;;; Copy-File
81
82 ;;; WILD-MATCH objects contain information about wildcard matches. File is the
83 ;;; Sesame namestring of the file matched, and substitute is a substring of the
84 ;;; file-namestring of file.
85 ;;;
86 (defstruct (wild-match (:print-function print-wild-match)
87 (:constructor make-wild-match (file substitute)))
88 file
89 substitute)
90
91 (defun print-wild-match (obj str n)
92 (declare (ignore n))
93 (format str "#<Wild-Match ~S ~S>"
94 (wild-match-file obj) (wild-match-substitute obj)))
95
96
97 (defun copy-file (spec1 spec2 &key (update *update-default*)
98 (clobber *clobber-default*)
99 (directory () directoryp))
100 "Copy file spec1 to spec2. A single wildcard is acceptable, and directory
101 names may be used. If spec1 and spec2 are both directories, then a
102 recursive copy is done of the files and subdirectory structure of spec1;
103 if spec2 is in the subdirectory structure of spec1, the recursion will
104 not descend into it. Use spec1/* to copy only the files in spec1 to
105 directory spec2. If spec2 is a directory, and spec1 is a file, then
106 spec1 is copied into spec2 with the same pathname-name. Files are
107 copied maintaining the source's write date. If :update is non-nil, then
108 files are only copied if the source is newer than the destination, still
109 maintaining the source's write date; the user is not warned if the
110 destination is newer (not the same write date) than the source. If
111 :clobber and :update are nil, then if any file spec2 already exists, the
112 user will be asked whether it should be overwritten or not."
113 (cond
114 ((not directoryp)
115 (let* ((ses-name1 (ext:unix-namestring spec1 t))
116 (exists1p (unix:unix-file-kind ses-name1))
117 (ses-name2 (ext:unix-namestring spec2 nil))
118 (pname1 (pathname ses-name1))
119 (pname2 (pathname ses-name2))
120 (dirp1 (directoryp pname1))
121 (dirp2 (directoryp pname2))
122 (wildp1 (wildcardp (file-namestring pname1)))
123 (wildp2 (wildcardp (file-namestring pname2))))
124 (when (and dirp1 wildp1)
125 (funcall *error-function*
126 "Cannot have wildcards in directory names -- ~S." pname1))
127 (when (and dirp2 wildp2)
128 (funcall *error-function*
129 "Cannot have wildcards in directory names -- ~S." pname2))
130 (when (and dirp1 (not dirp2))
131 (funcall *error-function*
132 "Cannot handle spec1 being a directory and spec2 a file."))
133 (when (and wildp2 (not wildp1))
134 (funcall *error-function*
135 "Cannot handle destination having wildcards without ~
136 source having wildcards."))
137 (when (and wildp1 (not wildp2) (not dirp2))
138 (funcall *error-function*
139 "Cannot handle source with wildcards and destination ~
140 without, unless destination is a directory."))
141 (cond ((and dirp1 dirp2)
142 (unless (directory-existsp ses-name1)
143 (funcall *error-function*
144 "Directory does not exist -- ~S." pname1))
145 (unless (directory-existsp ses-name2)
146 (enter-directory ses-name2))
147 (recursive-copy pname1 pname2 update clobber pname2
148 ses-name1 ses-name2))
149 (dirp2
150 ;; merge pname2 with pname1 to pick up a similar file-namestring.
151 (copy-file-1 pname1 wildp1 exists1p
152 (merge-pathnames pname2 pname1)
153 wildp1 update clobber))
154 (t (copy-file-1 pname1 wildp1 exists1p
155 pname2 wildp2 update clobber)))))
156 (directory
157 (when (pathname-directory spec1)
158 (funcall *error-function*
159 "Spec1 is just a pattern when supplying directory -- ~S."
160 spec1))
161 (let* ((pname2 (pathname (ext:unix-namestring spec2 nil)))
162 (dirp2 (directoryp pname2))
163 (wildp1 (wildcardp spec1))
164 (wildp2 (wildcardp (file-namestring pname2))))
165 (unless wildp1
166 (funcall *error-function*
167 "Pattern, ~S, does not contain a wildcard."
168 spec1))
169 (when (and (not wildp2) (not dirp2))
170 (funcall *error-function*
171 "Cannot handle source with wildcards and destination ~
172 without, unless destination is a directory."))
173 (copy-wildcard-files spec1 wildp1
174 (if dirp2 (merge-pathnames pname2 spec1) pname2)
175 (if dirp2 wildp1 wildp2)
176 update clobber directory))))
177 (values))
178
179 ;;; RECURSIVE-COPY takes two pathnames that represent directories, and
180 ;;; the files in pname1 are copied into pname2, recursively descending into
181 ;;; subdirectories. If a subdirectory of pname1 does not exist in pname2,
182 ;;; it is created. Pname1 is known to exist. Forbidden-dir is originally
183 ;;; the same as pname2; this keeps us from infinitely recursing if pname2
184 ;;; is in the subdirectory structure of pname1. Returns t if some file gets
185 ;;; copied.
186 ;;;
187 (defun recursive-copy (pname1 pname2 update clobber
188 forbidden-dir ses-name1 ses-name2)
189 (funcall *report-function* "~&~S ==>~% ~S~%" ses-name1 ses-name2)
190 (dolist (spec (directory (directory-namestring pname1)))
191 (let ((spec-ses-name (namestring spec)))
192 (if (directoryp spec)
193 (unless (equal (pathname spec-ses-name) forbidden-dir)
194 (let* ((dir2-pname (merge-dirs spec pname2))
195 (dir2-ses-name (namestring dir2-pname)))
196 (unless (directory-existsp dir2-ses-name)
197 (enter-directory dir2-ses-name))
198 (recursive-copy spec dir2-pname update clobber forbidden-dir
199 spec-ses-name dir2-ses-name)
200 (funcall *report-function* "~&~S ==>~% ~S~%" ses-name1
201 ses-name2)))
202 (copy-file-2 spec-ses-name
203 (namestring (merge-pathnames pname2 spec))
204 update clobber)))))
205
206 ;;; MERGE-DIRS picks out the last directory name in the pathname pname1 and
207 ;;; adds it to the end of the sequence of directory names from pname2, returning
208 ;;; a pathname.
209 ;;;
210 (defun merge-dirs (pname1 pname2)
211 (let* ((dirs1 (pathname-directory pname1))
212 (dirs2 (pathname-directory pname2))
213 (dirs2-len (length dirs2))
214 (new-dirs2 (make-array (1+ dirs2-len))))
215 (declare (simple-vector dirs1 dirs2 new-dirs2))
216 (replace new-dirs2 dirs2)
217 (setf (svref new-dirs2 dirs2-len)
218 (svref dirs1 (1- (length dirs1))))
219 (make-pathname :directory new-dirs2 :device :absolute)))
220
221 ;;; COPY-FILE-1 takes pathnames which either both contain a single wildcard
222 ;;; or none. Wildp1 and Wildp2 are either nil or indexes into the
223 ;;; file-namestring of pname1 and pname2, respectively, indicating the position
224 ;;; of the wildcard character. If there is no wildcard, then simply call
225 ;;; COPY-FILE-2; otherwise, resolve the wildcard and copy those matching files.
226 ;;;
227 (defun copy-file-1 (pname1 wildp1 exists1p pname2 wildp2 update clobber)
228 (if wildp1
229 (copy-wildcard-files pname1 wildp1 pname2 wildp2 update clobber)
230 (let ((ses-name1 (namestring pname1)))
231 (unless exists1p (funcall *error-function*
232 "~S does not exist." ses-name1))
233 (copy-file-2 ses-name1 (namestring pname2) update clobber))))
234
235 (defun copy-wildcard-files (pname1 wildp1 pname2 wildp2 update clobber
236 &optional directory)
237 (multiple-value-bind (dst-before dst-after)
238 (before-wildcard-after (file-namestring pname2) wildp2)
239 (dolist (match (resolve-wildcard pname1 wildp1 directory))
240 (copy-file-2 (wild-match-file match)
241 (namestring (concatenate 'simple-string
242 (directory-namestring pname2)
243 dst-before
244 (wild-match-substitute match)
245 dst-after))
246 update clobber))))
247
248 ;;; COPY-FILE-2 copies ses-name1 to ses-name2 depending on the values of update
249 ;;; and clobber, with respect to the documentation of COPY-FILE. If ses-name2
250 ;;; doesn't exist, then just copy it; otherwise, if update, then only copy it
251 ;;; if the destination's write date precedes the source's, and if not clobber
252 ;;; and not update, then ask the user before doing the copy.
253 ;;;
254 (defun copy-file-2 (ses-name1 ses-name2 update clobber)
255 (let ((secs1 (get-write-date ses-name1)))
256 (cond ((not (probe-file ses-name2))
257 (do-the-copy ses-name1 ses-name2 secs1))
258 (update
259 (let ((secs2 (get-write-date ses-name2)))
260 (cond (clobber
261 (do-the-copy ses-name1 ses-name2 secs1))
262 ((and (> secs2 secs1)
263 (funcall *yesp-function*
264 "~&~S ==> ~S~% ~
265 ** Destination is newer than source. ~
266 Overwrite it? "
267 ses-name1 ses-name2))
268 (do-the-copy ses-name1 ses-name2 secs1))
269 ((< secs2 secs1)
270 (do-the-copy ses-name1 ses-name2 secs1)))))
271 ((not clobber)
272 (when (funcall *yesp-function*
273 "~&~S ==> ~S~% ** Destination already exists. ~
274 Overwrite it? "
275 ses-name1 ses-name2)
276 (do-the-copy ses-name1 ses-name2 secs1)))
277 (t (do-the-copy ses-name1 ses-name2 secs1)))))
278
279 (defun do-the-copy (ses-name1 ses-name2 secs1)
280 (let* ((fd (open-file ses-name1)))
281 (unwind-protect
282 (multiple-value-bind (data byte-count mode)
283 (read-file fd ses-name1)
284 (unwind-protect (write-file ses-name2 data byte-count mode)
285 (system:deallocate-system-memory data byte-count)))
286 (close-file fd)))
287 (set-write-date ses-name2 secs1)
288 (funcall *report-function* "~&~S ==>~% ~S~%" ses-name1 ses-name2))
289
290
291 ;;;; Rename-File
292
293 (defun rename-file (spec1 spec2 &key (clobber *clobber-default*)
294 (directory () directoryp))
295 "Rename file spec1 to spec2. A single wildcard is acceptable, and spec2 may
296 be a directory with the result spec being the merging of spec2 with spec1.
297 If clobber is nil and spec2 exists, then the user will be asked to confirm
298 the renaming. As with Unix mv, if you are renaming a directory, don't
299 specify the trailing slash."
300 (cond
301 ((not directoryp)
302 (let* ((ses-name1 (ext:unix-namestring spec1 t))
303 (exists1p (unix:unix-file-kind ses-name1))
304 (ses-name2 (ext:unix-namestring spec2 nil))
305 (pname1 (pathname ses-name1))
306 (pname2 (pathname ses-name2))
307 (dirp2 (directoryp pname2))
308 (wildp1 (wildcardp (file-namestring pname1)))
309 (wildp2 (wildcardp (file-namestring pname2))))
310 (if (and dirp2 wildp2)
311 (funcall *error-function*
312 "Cannot have wildcards in directory names -- ~S." pname2))
313 (if (and wildp2 (not wildp1))
314 (funcall *error-function*
315 "Cannot handle destination having wildcards without ~
316 source having wildcards."))
317 (if (and wildp1 (not wildp2) (not dirp2))
318 (funcall *error-function*
319 "Cannot handle source with wildcards and destination ~
320 without, unless destination is a directory."))
321 (if dirp2
322 (rename-file-1 pname1 wildp1 exists1p (merge-pathnames pname2
323 pname1)
324 wildp1 clobber)
325 (rename-file-1 pname1 wildp1 exists1p pname2 wildp2 clobber))))
326 (directory
327 (when (pathname-directory spec1)
328 (funcall *error-function*
329 "Spec1 is just a pattern when supplying directory -- ~S."
330 spec1))
331
332 (let* ((pname2 (pathname (ext:unix-namestring spec2 nil)))
333 (dirp2 (directoryp pname2))
334 (wildp1 (wildcardp spec1))
335 (wildp2 (wildcardp (file-namestring pname2))))
336 (unless wildp1
337 (funcall *error-function*
338 "Pattern, ~S, does not contain a wildcard."
339 spec1))
340 (when (and (not wildp2) (not dirp2))
341 (funcall *error-function*
342 "Cannot handle source with wildcards and destination ~
343 without, unless destination is a directory."))
344 (rename-wildcard-files spec1 wildp1
345 (if dirp2 (merge-pathnames pname2 spec1) pname2)
346 (if dirp2 wildp1 wildp2)
347 clobber directory))))
348 (values))
349
350 ;;; RENAME-FILE-1 takes pathnames which either both contain a single wildcard
351 ;;; or none. Wildp1 and Wildp2 are either nil or indexes into the
352 ;;; file-namestring of pname1 and pname2, respectively, indicating the position
353 ;;; of the wildcard character. If there is no wildcard, then simply call
354 ;;; RENAME-FILE-2; otherwise, resolve the wildcard and rename those matching files.
355 ;;;
356 (defun rename-file-1 (pname1 wildp1 exists1p pname2 wildp2 clobber)
357 (if wildp1
358 (rename-wildcard-files pname1 wildp1 pname2 wildp2 clobber)
359 (let ((ses-name1 (namestring pname1)))
360 (unless exists1p (funcall *error-function*
361 "~S does not exist." ses-name1))
362 (rename-file-2 ses-name1 (namestring pname2) clobber))))
363
364 (defun rename-wildcard-files (pname1 wildp1 pname2 wildp2 clobber
365 &optional directory)
366 (multiple-value-bind (dst-before dst-after)
367 (before-wildcard-after (file-namestring pname2) wildp2)
368 (dolist (match (resolve-wildcard pname1 wildp1 directory))
369 (rename-file-2 (wild-match-file match)
370 (namestring (concatenate 'simple-string
371 (directory-namestring pname2)
372 dst-before
373 (wild-match-substitute match)
374 dst-after))
375 clobber))))
376
377 (defun rename-file-2 (ses-name1 ses-name2 clobber)
378 (cond ((and (probe-file ses-name2) (not clobber))
379 (when (funcall *yesp-function*
380 "~&~S ==> ~S~% ** Destination already exists. ~
381 Overwrite it? "
382 ses-name1 ses-name2)
383 (sub-rename-file ses-name1 ses-name2)
384 (funcall *report-function* "~&~S ==>~% ~S~%" ses-name1 ses-name2)))
385 (t (sub-rename-file ses-name1 ses-name2)
386 (funcall *report-function* "~&~S ==>~% ~S~%" ses-name1 ses-name2))))
387
388
389
390 ;;;; Find-File
391
392 (defun find-file (file-name &optional (directory "")
393 (find-all-p nil find-all-suppliedp))
394 "Find the file with file-namestring file recursively looking in directory.
395 If find-all-p is non-nil, then do not stop searching upon finding the first
396 occurance of file. File may contain a single wildcard, which causes
397 find-all-p to default to t instead of nil."
398 (let* ((file (coerce file-name 'simple-string))
399 (wildp (wildcardp file))
400 (find-all-p (if find-all-suppliedp find-all-p wildp)))
401 (declare (simple-string file))
402 (catch 'found-file
403 (if wildp
404 (multiple-value-bind (before after)
405 (before-wildcard-after file wildp)
406 (find-file-aux file directory find-all-p before after))
407 (find-file-aux file directory find-all-p))))
408 (values))
409
410 (defun find-file-aux (the-file directory find-all-p &optional before after)
411 (declare (simple-string the-file))
412 (dolist (spec (directory directory))
413 (let* ((spec-ses-name (namestring spec))
414 (spec-file-name (file-namestring spec-ses-name)))
415 (declare (simple-string spec-ses-name spec-file-name))
416 (if (directoryp spec)
417 (find-file-aux the-file spec find-all-p before after)
418 (when (if before
419 (find-match before after spec-file-name :no-cons)
420 (string-equal the-file spec-file-name))
421 (print spec-ses-name)
422 (unless find-all-p (throw 'found-file t)))))))
423
424
425
426 ;;;; Delete-File
427
428 ;;; DELETE-FILE
429 ;;; If spec is a directory, but recursive is nil, just pass the directory
430 ;;; down through, letting LISP:DELETE-FILE signal an error if the directory
431 ;;; is not empty.
432 ;;;
433 (defun delete-file (spec &key (recursive *recursive-default*)
434 (clobber *clobber-default*))
435 "Delete spec asking confirmation on each file if clobber is nil. A single
436 wildcard is acceptable. If recursive is non-nil, then a directory spec may
437 be given to recursively delete the entirety of the directory and its
438 subdirectory structure. An empty directory may be specified without
439 recursive being non-nil. When specifying a directory, the trailing slash
440 must be included."
441 (let* ((ses-name (ext:unix-namestring spec t))
442 (pname (pathname ses-name))
443 (wildp (wildcardp (file-namestring pname)))
444 (dirp (directoryp pname)))
445 (if dirp
446 (if recursive
447 (recursive-delete pname ses-name clobber)
448 (delete-file-2 ses-name clobber))
449 (delete-file-1 pname ses-name wildp clobber)))
450 (values))
451
452 (defun recursive-delete (directory dir-ses-name clobber)
453 (dolist (spec (directory (directory-namestring directory)))
454 (let ((spec-ses-name (namestring spec)))
455 (if (directoryp spec)
456 (recursive-delete (pathname spec-ses-name) spec-ses-name clobber)
457 (delete-file-2 spec-ses-name clobber))))
458 (delete-file-2 dir-ses-name clobber))
459
460 (defun delete-file-1 (pname ses-name wildp clobber)
461 (if wildp
462 (dolist (match (resolve-wildcard pname wildp))
463 (delete-file-2 (wild-match-file match) clobber))
464 (delete-file-2 ses-name clobber)))
465
466 (defun delete-file-2 (ses-name clobber)
467 (when (or clobber (funcall *yesp-function* "~&Delete ~S? " ses-name))
468 (if (directoryp ses-name)
469 (delete-directory ses-name)
470 (lisp:delete-file ses-name))
471 (funcall *report-function* "~&~A~%" ses-name)))
472
473
474
475 ;;;; Wildcard resolution
476
477 (defun pathnames-from-pattern (pattern files)
478 "Return a list of pathnames from files whose file-namestrings match
479 pattern. Pattern must be a non-empty string and contains only one
480 asterisk. Files contains no directories."
481 (declare (simple-string pattern))
482 (when (string= pattern "")
483 (funcall *error-function* "Must be a non-empty pattern."))
484 (unless (= (count wildcard-char pattern :test #'char=) 1)
485 (funcall *error-function* "Pattern must contain one asterisk."))
486 (multiple-value-bind (before after)
487 (before-wildcard-after pattern (wildcardp pattern))
488 (let ((result nil))
489 (dolist (f files result)
490 (let* ((ses-namestring (namestring f))
491 (f-namestring (file-namestring ses-namestring))
492 (match (find-match before after f-namestring)))
493 (when match (push f result)))))))
494
495
496 ;;; RESOLVE-WILDCARD takes a pathname with a wildcard and the position of the
497 ;;; wildcard character in the file-namestring and returns a list of wild-match
498 ;;; objects. When directory is supplied, pname is just a pattern, or a
499 ;;; file-namestring. It is an error for directory to be anything other than
500 ;;; absolute pathnames in the same directory. Each wild-match object contains
501 ;;; the Sesame namestring of a file in the same directory as pname, or
502 ;;; directory, and a simple-string representing what the wildcard matched.
503 ;;;
504 (defun resolve-wildcard (pname wild-pos &optional directory)
505 (multiple-value-bind (before after)
506 (before-wildcard-after (if directory
507 pname
508 (file-namestring pname))
509 wild-pos)
510 (let (result)
511 (dolist (f (or directory (directory (directory-namestring pname)))
512 (nreverse result))
513 (unless (directoryp f)
514 (let* ((ses-namestring (namestring f))
515 (f-namestring (file-namestring ses-namestring))
516 (match (find-match before after f-namestring)))
517 (if match
518 (push (make-wild-match ses-namestring match) result))))))))
519
520 ;;; FIND-MATCH takes a "before wildcard" and "after wildcard" string and a
521 ;;; file-namestring. If before and after match a substring of file-namestring
522 ;;; and are respectively left bound and right bound, then anything left in
523 ;;; between is the match returned. If no match is found, nil is returned.
524 ;;; NOTE: if version numbers ever really exist, then this code will have to be
525 ;;; changed since the file-namestring of a pathname contains the version number.
526 ;;;
527 (defun find-match (before after file-namestring &optional no-cons)
528 (declare (simple-string before after file-namestring))
529 (let ((before-len (length before))
530 (after-len (length after))
531 (name-len (length file-namestring)))
532 (if (>= name-len (+ before-len after-len))
533 (let* ((start (if (string= before file-namestring
534 :end1 before-len :end2 before-len)
535 before-len))
536 (end (- name-len after-len))
537 (matchp (and start
538 (string= after file-namestring :end1 after-len
539 :start2 end :end2 name-len))))
540 (if matchp
541 (if no-cons
542 t
543 (subseq file-namestring start end)))))))
544
545 (defun before-wildcard-after (file-namestring wild-pos)
546 (declare (simple-string file-namestring))
547 (values (subseq file-namestring 0 wild-pos)
548 (subseq file-namestring (1+ wild-pos) (length file-namestring))))
549
550
551
552 ;;;; Miscellaneous Utilities (e.g., MAKEDIR).
553
554 (defun make-directory (name)
555 "Creates directory name. If name exists, then an error is signaled."
556 (let ((ses-name (ext:unix-namestring name nil)))
557 (when (unix:unix-file-kind ses-name)
558 (funcall *error-function* "Name already exists -- ~S" ses-name))
559 (enter-directory ses-name))
560 t)
561
562
563
564 ;;;; Mach Operations
565
566 (defun open-file (ses-name)
567 (multiple-value-bind (fd err)
568 (unix:unix-open ses-name unix:o_rdonly 0)
569 (unless fd
570 (funcall *error-function* "Opening ~S failed: ~A." ses-name err))
571 fd))
572
573 (defun close-file (fd)
574 (unix:unix-close fd))
575
576 (defun read-file (fd ses-name)
577 (multiple-value-bind (winp dev-or-err ino mode nlink uid gid rdev size)
578 (unix:unix-fstat fd)
579 (declare (ignore ino nlink uid gid rdev))
580 (unless winp (funcall *error-function*
581 "Opening ~S failed: ~A." ses-name dev-or-err))
582 (let ((storage (system:allocate-system-memory size)))
583 (multiple-value-bind (read-bytes err)
584 (unix:unix-read fd storage size)
585 (when (or (null read-bytes) (not (= size read-bytes)))
586 (system:deallocate-system-memory storage size)
587 (funcall *error-function*
588 "Reading file ~S failed: ~A." ses-name err)))
589 (values storage size mode))))
590
591 (defun write-file (ses-name data byte-count mode)
592 (multiple-value-bind (fd err) (unix:unix-creat ses-name #o644)
593 (unless fd
594 (funcall *error-function* "Couldn't create file ~S: ~A"
595 ses-name (unix:get-unix-error-msg err)))
596 (multiple-value-bind (winp err) (unix:unix-write fd data 0 byte-count)
597 (unless winp
598 (funcall *error-function* "Writing file ~S failed: ~A"
599 ses-name
600 (unix:get-unix-error-msg err))))
601 (unix:unix-fchmod fd (logand mode #o777))
602 (unix:unix-close fd)))
603
604 (defvar *utimes-buffer* (make-list 4 :initial-element 0))
605
606 (defun set-write-date (ses-name secs)
607 (multiple-value-bind (winp dev-or-err ino mode nlink uid gid rdev size atime)
608 (unix:unix-stat ses-name)
609 (declare (ignore ino mode nlink uid gid rdev size))
610 (unless winp (funcall *error-function*
611 "Couldn't stat file ~S failed: ~A." ses-name
612 dev-or-err))
613 (setf (car *utimes-buffer*) atime)
614 (setf (caddr *utimes-buffer*) secs))
615 (multiple-value-bind (winp err) (unix:unix-utimes ses-name *utimes-buffer*)
616 (unless winp
617 (funcall *error-function* "Couldn't set write date of file ~S: ~A"
618 ses-name
619 (unix:get-unix-error-msg err)))))
620
621 (defun get-write-date (ses-name)
622 (multiple-value-bind (winp dev-or-err ino mode nlink uid gid rdev size
623 atime mtime)
624 (unix:unix-stat ses-name)
625 (declare (ignore ino mode nlink uid gid rdev size atime))
626 (unless winp (funcall *error-function* "Couldn't stat file ~S failed: ~A."
627 ses-name dev-or-err))
628 mtime))
629
630 ;;; SUB-RENAME-FILE must exist because we can't use Common Lisp's RENAME-FILE.
631 ;;; This is because it merges the new name with the old name to pick up
632 ;;; defaults, and this conflicts with Unix-oid names. For example, renaming
633 ;;; "foo.bar" to ".baz" causes a result of "foo.baz"! This routine doesn't
634 ;;; have this problem.
635 ;;;
636 (defun sub-rename-file (ses-name1 ses-name2)
637 (multiple-value-bind (res err) (unix:unix-rename ses-name1 ses-name2)
638 (unless res
639 (funcall *error-function* "Failed to rename ~A to ~A: ~A."
640 ses-name1 ses-name2 (unix:get-unix-error-msg err)))))
641
642 (defun directory-existsp (ses-name)
643 (eq (unix:unix-file-kind ses-name) :directory))
644
645 (defun enter-directory (ses-name)
646 (declare (simple-string ses-name))
647 (let* ((length-1 (1- (length ses-name)))
648 (name (if (= (position #\/ ses-name :test #'char= :from-end t)
649 length-1)
650 (subseq ses-name 0 (1- (length ses-name)))
651 ses-name)))
652 (multiple-value-bind (winp err) (unix:unix-mkdir name #o755)
653 (unless winp
654 (funcall *error-function* "Couldn't make directory ~S: ~A"
655 name
656 (unix:get-unix-error-msg err))))))
657
658 (defun delete-directory (ses-name)
659 (declare (simple-string ses-name))
660 (multiple-value-bind (winp err)
661 (unix:unix-rmdir (subseq ses-name 0
662 (1- (length ses-name))))
663 (unless winp
664 (funcall *error-function* "Couldn't delete directory ~S: ~A"
665 ses-name
666 (unix:get-unix-error-msg err)))))
667
668
669
670 ;;;; Misc. Utility Utilities
671
672 ;;; NSEPARATE-FILES destructively returns a list of file specs from listing.
673 (defun nseparate-files (listing)
674 (do (files hold)
675 ((null listing) files)
676 (setf hold (cdr listing))
677 (unless (directoryp (car listing))
678 (setf (cdr listing) files)
679 (setf files listing))
680 (setf listing hold)))
681
682
683 (defun directoryp (p)
684 (not (or (pathname-name p) (pathname-type p))))

  ViewVC Help
Powered by ViewVC 1.1.5