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

  ViewVC Help
Powered by ViewVC 1.1.5