/[cmucl]/src/code/filesys.lisp
ViewVC logotype

Contents of /src/code/filesys.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.34 - (hide annotations)
Wed Aug 4 09:37:47 1993 UTC (20 years, 8 months ago) by ram
Branch: MAIN
Changes since 1.33: +17 -20 lines
Undid last change, because that's the wrong place to put it.
1 ram 1.1 ;;; -*- Log: code.log; Package: Lisp -*-
2     ;;; **********************************************************************
3 ram 1.8 ;;; This code was written as part of the CMU Common Lisp project at
4     ;;; Carnegie Mellon University, and has been placed in the public domain.
5     ;;; If you want to use this code or any part of CMU Common Lisp, please contact
6     ;;; Scott Fahlman or slisp-group@cs.cmu.edu.
7     ;;;
8     (ext:file-comment
9 ram 1.34 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/filesys.lisp,v 1.34 1993/08/04 09:37:47 ram Exp $")
10 ram 1.8 ;;;
11 ram 1.1 ;;; **********************************************************************
12     ;;;
13 wlott 1.16 ;;; File system interface functions. This file is pretty UNIX specific.
14 ram 1.1 ;;;
15 wlott 1.16 ;;; Written by William Lott
16 ram 1.1 ;;;
17     ;;; **********************************************************************
18 ram 1.2
19     (in-package "LISP")
20    
21 wlott 1.16 (export '(truename probe-file user-homedir-pathname directory
22     rename-file delete-file file-write-date file-author))
23 ram 1.1
24     (use-package "EXTENSIONS")
25    
26     (in-package "EXTENSIONS")
27     (export '(print-directory complete-file ambiguous-files default-directory
28 wlott 1.5 file-writable unix-namestring))
29 ram 1.2 (in-package "LISP")
30 ram 1.1
31 wlott 1.5
32 wlott 1.16 ;;;; Unix pathname host support.
33 ram 1.1
34 wlott 1.16 ;;; Unix namestrings have the following format:
35     ;;;
36     ;;; namestring := [ directory ] [ file [ type [ version ]]]
37     ;;; directory := [ "/" | search-list ] { file "/" }*
38     ;;; search-list := [^:/]*:
39     ;;; file := [^/]*
40     ;;; type := "." [^/.]*
41     ;;; version := "." ([0-9]+ | "*")
42     ;;;
43     ;;; Note: this grammer is ambiguous. The string foo.bar.5 can be parsed
44     ;;; as either just the file specified or as specifying the file, type, and
45     ;;; version. Therefore, we use the following rules when confronted with
46     ;;; an ambiguous file.type.version string:
47     ;;;
48     ;;; - If the first character is a dot, it's part of the file. It is not
49     ;;; considered a dot in the following rules.
50     ;;;
51     ;;; - If there is only one dot, it seperates the file and the type.
52     ;;;
53     ;;; - If there are multiple dots and the stuff following the last dot
54     ;;; is a valid version, then that is the version and the stuff between
55     ;;; the second to last dot and the last dot is the type.
56     ;;;
57     ;;; Wildcard characters:
58     ;;;
59     ;;; If the directory, file, type components contain any of the following
60     ;;; characters, it is considered part of a wildcard pattern and has the
61     ;;; following meaning.
62     ;;;
63     ;;; ? - matches any character
64     ;;; * - matches any zero or more characters.
65     ;;; [abc] - matches any of a, b, or c.
66     ;;; {str1,str2,...,strn} - matches any of str1, str2, ..., or strn.
67     ;;;
68     ;;; Any of these special characters can be preceeded by a backslash to
69     ;;; cause it to be treated as a regular character.
70     ;;;
71 ram 1.1
72 wlott 1.16 (defun remove-backslashes (namestr start end)
73     "Remove and occurences of \\ from the string because we've already
74     checked for whatever they may have been backslashed."
75     (declare (type simple-base-string namestr)
76     (type index start end))
77     (let* ((result (make-string (- end start)))
78     (dst 0)
79     (quoted nil))
80     (do ((src start (1+ src)))
81     ((= src end))
82     (cond (quoted
83     (setf (schar result dst) (schar namestr src))
84     (setf quoted nil)
85     (incf dst))
86     (t
87     (let ((char (schar namestr src)))
88     (cond ((char= char #\\)
89     (setq quoted t))
90     (t
91     (setf (schar result dst) char)
92     (incf dst)))))))
93     (when quoted
94     (error 'namestring-parse-error
95     :complaint "Backslash in bad place."
96     :namestring namestr
97     :offset (1- end)))
98     (shrink-vector result dst)))
99 ram 1.1
100 wlott 1.16 (defvar *ignore-wildcards* nil)
101 ram 1.1
102 wlott 1.16 (defun maybe-make-pattern (namestr start end)
103     (declare (type simple-base-string namestr)
104     (type index start end))
105     (if *ignore-wildcards*
106     (subseq namestr start end)
107     (collect ((pattern))
108     (let ((quoted nil)
109     (any-quotes nil)
110     (last-regular-char nil)
111     (index start))
112     (flet ((flush-pending-regulars ()
113     (when last-regular-char
114     (pattern (if any-quotes
115     (remove-backslashes namestr
116     last-regular-char
117     index)
118     (subseq namestr last-regular-char index)))
119     (setf any-quotes nil)
120     (setf last-regular-char nil))))
121     (loop
122     (when (>= index end)
123     (return))
124     (let ((char (schar namestr index)))
125     (cond (quoted
126     (incf index)
127     (setf quoted nil))
128     ((char= char #\\)
129     (setf quoted t)
130     (setf any-quotes t)
131     (unless last-regular-char
132     (setf last-regular-char index))
133     (incf index))
134     ((char= char #\?)
135     (flush-pending-regulars)
136     (pattern :single-char-wild)
137     (incf index))
138     ((char= char #\*)
139     (flush-pending-regulars)
140     (pattern :multi-char-wild)
141     (incf index))
142     ((char= char #\[)
143     (flush-pending-regulars)
144     (let ((close-bracket
145     (position #\] namestr :start index :end end)))
146     (unless close-bracket
147     (error 'namestring-parse-error
148     :complaint "``['' with no corresponding ``]''"
149     :namestring namestr
150     :offset index))
151     (pattern (list :character-set
152     (subseq namestr
153     (1+ index)
154     close-bracket)))
155     (setf index (1+ close-bracket))))
156     (t
157     (unless last-regular-char
158     (setf last-regular-char index))
159     (incf index)))))
160     (flush-pending-regulars)))
161     (cond ((null (pattern))
162     "")
163 ram 1.31 ((null (cdr (pattern)))
164     (let ((piece (first (pattern))))
165     (typecase piece
166     ((member :multi-char-wild) :wild)
167     (simple-string piece)
168     (t
169     (make-pattern (pattern))))))
170 wlott 1.16 (t
171     (make-pattern (pattern)))))))
172 ram 1.1
173 wlott 1.16 (defun extract-name-type-and-version (namestr start end)
174     (declare (type simple-base-string namestr)
175     (type index start end))
176     (let* ((last-dot (position #\. namestr :start (1+ start) :end end
177     :from-end t))
178     (second-to-last-dot (and last-dot
179     (position #\. namestr :start (1+ start)
180     :end last-dot :from-end t)))
181     (version :newest))
182     ;; If there is a second-to-last dot, check to see if there is a valid
183     ;; version after the last dot.
184     (when second-to-last-dot
185     (cond ((and (= (+ last-dot 2) end)
186     (char= (schar namestr (1+ last-dot)) #\*))
187     (setf version :wild))
188     ((and (< (1+ last-dot) end)
189     (do ((index (1+ last-dot) (1+ index)))
190     ((= index end) t)
191     (unless (char<= #\0 (schar namestr index) #\9)
192     (return nil))))
193     (setf version
194     (parse-integer namestr :start (1+ last-dot) :end end)))
195     (t
196     (setf second-to-last-dot nil))))
197     (cond (second-to-last-dot
198     (values (maybe-make-pattern namestr start second-to-last-dot)
199     (maybe-make-pattern namestr
200     (1+ second-to-last-dot)
201     last-dot)
202     version))
203     (last-dot
204     (values (maybe-make-pattern namestr start last-dot)
205     (maybe-make-pattern namestr (1+ last-dot) end)
206     version))
207     (t
208     (values (maybe-make-pattern namestr start end)
209     nil
210     version)))))
211 ram 1.1
212 ram 1.31 ;;; Take a string and return a list of cons cells that mark the char
213     ;;; separated subseq. The first value t if absolute directories location.
214     ;;;
215     (defun split-at-slashes (namestr start end)
216     (declare (type simple-base-string namestr)
217 wlott 1.16 (type index start end))
218     (let ((absolute (and (/= start end)
219 ram 1.31 (char= (schar namestr start) #\/))))
220 wlott 1.16 (when absolute
221     (incf start))
222 ram 1.31 ;; Next, split the remainder into slash seperated chunks.
223 wlott 1.16 (collect ((pieces))
224     (loop
225 ram 1.31 (let ((slash (position #\/ namestr :start start :end end)))
226     (pieces (cons start (or slash end)))
227     (unless slash
228 wlott 1.16 (return))
229 ram 1.31 (setf start (1+ slash))))
230 wlott 1.16 (values absolute (pieces)))))
231 ram 1.1
232 wlott 1.16 (defun maybe-extract-search-list (namestr start end)
233     (declare (type simple-base-string namestr)
234     (type index start end))
235     (let ((quoted nil))
236     (do ((index start (1+ index)))
237     ((= index end)
238     (values nil start))
239     (if quoted
240     (setf quoted nil)
241     (case (schar namestr index)
242     (#\\
243     (setf quoted t))
244     (#\:
245     (return (values (remove-backslashes namestr start index)
246     (1+ index)))))))))
247 ram 1.1
248 wlott 1.16 (defun parse-unix-namestring (namestr start end)
249     (declare (type simple-base-string namestr)
250     (type index start end))
251     (multiple-value-bind
252     (absolute pieces)
253     (split-at-slashes namestr start end)
254     (let ((search-list
255     (if absolute
256     nil
257     (let ((first (car pieces)))
258     (multiple-value-bind
259     (search-list new-start)
260     (maybe-extract-search-list namestr
261     (car first) (cdr first))
262     (when search-list
263     (setf absolute t)
264     (setf (car first) new-start))
265     search-list)))))
266     (multiple-value-bind
267     (name type version)
268     (let* ((tail (car (last pieces)))
269     (tail-start (car tail))
270     (tail-end (cdr tail)))
271     (unless (= tail-start tail-end)
272     (setf pieces (butlast pieces))
273     (extract-name-type-and-version namestr tail-start tail-end)))
274     ;; Now we have everything we want. So return it.
275     (values nil ; no host for unix namestrings.
276     nil ; no devices for unix namestrings.
277     (collect ((dirs))
278     (when search-list
279     (dirs (intern-search-list search-list)))
280     (dolist (piece pieces)
281     (let ((piece-start (car piece))
282     (piece-end (cdr piece)))
283     (unless (= piece-start piece-end)
284 ram 1.31 (cond ((string= namestr ".." :start1 piece-start
285     :end1 piece-end)
286     (dirs :up))
287     ((string= namestr "**" :start1 piece-start
288     :end1 piece-end)
289     (dirs :wild-inferiors))
290     (t
291     (dirs (maybe-make-pattern namestr
292     piece-start
293     piece-end)))))))
294 wlott 1.25 (cond (absolute
295     (cons :absolute (dirs)))
296     ((dirs)
297     (cons :relative (dirs)))
298     (t
299     nil)))
300 wlott 1.16 name
301     type
302     version)))))
303 ram 1.1
304 wlott 1.16 (defun unparse-unix-host (pathname)
305     (declare (type pathname pathname)
306     (ignore pathname))
307     "Unix")
308 ram 1.1
309 wlott 1.16 (defun unparse-unix-piece (thing)
310     (etypecase thing
311 ram 1.31 ((member :wild) "*")
312 wlott 1.16 (simple-string
313     (let* ((srclen (length thing))
314     (dstlen srclen))
315     (dotimes (i srclen)
316     (case (schar thing i)
317     ((#\* #\? #\[)
318     (incf dstlen))))
319     (let ((result (make-string dstlen))
320     (dst 0))
321     (dotimes (src srclen)
322     (let ((char (schar thing src)))
323     (case char
324     ((#\* #\? #\[)
325     (setf (schar result dst) #\\)
326     (incf dst)))
327     (setf (schar result dst) char)
328     (incf dst)))
329     result)))
330     (pattern
331     (collect ((strings))
332     (dolist (piece (pattern-pieces thing))
333     (etypecase piece
334     (simple-string
335     (strings piece))
336     (symbol
337 ram 1.31 (ecase piece
338 wlott 1.16 (:multi-char-wild
339     (strings "*"))
340     (:single-char-wild
341 ram 1.31 (strings "?"))))
342 wlott 1.16 (cons
343     (case (car piece)
344     (:character-set
345     (strings "[")
346     (strings (cdr piece))
347     (strings "]"))
348     (t
349     (error "Invalid pattern piece: ~S" piece))))))
350     (apply #'concatenate
351     'simple-string
352     (strings))))))
353 ram 1.1
354 wlott 1.16 (defun unparse-unix-directory-list (directory)
355     (declare (type list directory))
356     (collect ((pieces))
357     (when directory
358     (ecase (pop directory)
359     (:absolute
360     (cond ((search-list-p (car directory))
361     (pieces (search-list-name (pop directory)))
362     (pieces ":"))
363     (t
364     (pieces "/"))))
365     (:relative
366     ;; Nothing special.
367     ))
368     (dolist (dir directory)
369     (typecase dir
370     ((member :up)
371     (pieces "../"))
372     ((member :back)
373     (error ":BACK cannot be represented in namestrings."))
374 ram 1.31 ((member :wild-inferiors)
375     (pieces "**/"))
376 wlott 1.16 ((or simple-string pattern)
377     (pieces (unparse-unix-piece dir))
378     (pieces "/"))
379     (t
380     (error "Invalid directory component: ~S" dir)))))
381     (apply #'concatenate 'simple-string (pieces))))
382 ram 1.1
383 wlott 1.16 (defun unparse-unix-directory (pathname)
384     (declare (type pathname pathname))
385     (unparse-unix-directory-list (%pathname-directory pathname)))
386    
387     (defun unparse-unix-file (pathname)
388     (declare (type pathname pathname))
389     (collect ((strings))
390     (let* ((name (%pathname-name pathname))
391     (type (%pathname-type pathname))
392     (type-supplied (not (or (null type) (eq type :unspecific))))
393     (version (%pathname-version pathname))
394     (version-supplied (not (or (null version) (eq version :newest)))))
395     (when name
396     (strings (unparse-unix-piece name)))
397     (when type-supplied
398     (unless name
399     (error "Cannot specify the type without a file: ~S" pathname))
400     (strings ".")
401     (strings (unparse-unix-piece type)))
402     (when version-supplied
403     (unless type-supplied
404     (error "Cannot specify the version without a type: ~S" pathname))
405     (strings (if (eq version :wild)
406     ".*"
407     (format nil ".~D" version)))))
408     (apply #'concatenate 'simple-string (strings))))
409 ram 1.1
410 wlott 1.16 (defun unparse-unix-namestring (pathname)
411     (declare (type pathname pathname))
412     (concatenate 'simple-string
413     (unparse-unix-directory pathname)
414     (unparse-unix-file pathname)))
415 ram 1.1
416 wlott 1.16 (defun unparse-unix-enough (pathname defaults)
417     (declare (type pathname pathname defaults))
418     (flet ((lose ()
419     (error "~S cannot be represented relative to ~S"
420     pathname defaults)))
421     (collect ((strings))
422     (let* ((pathname-directory (%pathname-directory pathname))
423     (defaults-directory (%pathname-directory defaults))
424     (prefix-len (length defaults-directory))
425     (result-dir
426     (cond ((and (> prefix-len 1)
427     (>= (length pathname-directory) prefix-len)
428     (compare-component (subseq pathname-directory
429     0 prefix-len)
430     defaults-directory))
431     ;; Pathname starts with a prefix of default. So just
432     ;; use a relative directory from then on out.
433     (cons :relative (nthcdr prefix-len pathname-directory)))
434     ((eq (car pathname-directory) :absolute)
435     ;; We are an absolute pathname, so we can just use it.
436     pathname-directory)
437     (t
438     ;; We are a relative directory. So we lose.
439     (lose)))))
440     (strings (unparse-unix-directory-list result-dir)))
441     (let* ((pathname-version (%pathname-version pathname))
442     (version-needed (and pathname-version
443     (not (eq pathname-version :newest))))
444     (pathname-type (%pathname-type pathname))
445     (type-needed (or version-needed
446     (and pathname-type
447     (not (eq pathname-type :unspecific)))))
448     (pathname-name (%pathname-name pathname))
449     (name-needed (or type-needed
450     (and pathname-name
451     (not (compare-component pathname-name
452     (%pathname-name
453     defaults)))))))
454     (when name-needed
455     (unless pathname-name (lose))
456     (strings (unparse-unix-piece pathname-name)))
457     (when type-needed
458     (when (or (null pathname-type) (eq pathname-type :unspecific))
459     (lose))
460     (strings ".")
461     (strings (unparse-unix-piece pathname-type)))
462     (when version-needed
463     (typecase pathname-version
464     ((member :wild)
465     (strings ".*"))
466     (integer
467     (strings (format nil ".~D" pathname-version)))
468     (t
469     (lose)))))
470     (apply #'concatenate 'simple-string (strings)))))
471 ram 1.1
472    
473 wlott 1.16 (defstruct (unix-host
474     (:include host
475     (:parse #'parse-unix-namestring)
476     (:unparse #'unparse-unix-namestring)
477     (:unparse-host #'unparse-unix-host)
478     (:unparse-directory #'unparse-unix-directory)
479     (:unparse-file #'unparse-unix-file)
480     (:unparse-enough #'unparse-unix-enough)
481     (:customary-case :lower))
482     (:make-load-form-fun make-unix-host-load-form))
483     )
484 ram 1.1
485 wlott 1.16 (defvar *unix-host* (make-unix-host))
486 ram 1.1
487 wlott 1.16 (defun make-unix-host-load-form (host)
488     (declare (ignore host))
489     '*unix-host*)
490 ram 1.1
491 wlott 1.16
492     ;;;; Wildcard matching stuff.
493 ram 1.1
494 wlott 1.16 (defmacro enumerate-matches ((var pathname &optional result
495     &key (verify-existance t))
496     &body body)
497     (let ((body-name (gensym)))
498     `(block nil
499     (flet ((,body-name (,var)
500     ,@body))
501     (%enumerate-matches (pathname ,pathname)
502     ,verify-existance
503     #',body-name)
504     ,result))))
505 ram 1.1
506 wlott 1.16 (defun %enumerate-matches (pathname verify-existance function)
507 ram 1.34 (when (pathname-type pathname)
508     (unless (pathname-name pathname)
509     (error "Cannot supply a type without a name:~% ~S" pathname)))
510     (when (and (integerp (pathname-version pathname))
511     (member (pathname-type pathname) '(nil :unspecific)))
512     (error "Cannot supply a version without a type:~% ~S" pathname))
513     (let ((directory (pathname-directory pathname)))
514     (if directory
515     (ecase (car directory)
516     (:absolute
517     (%enumerate-directories "/" (cdr directory) pathname
518     verify-existance function))
519     (:relative
520     (%enumerate-directories "" (cdr directory) pathname
521     verify-existance function)))
522     (%enumerate-files "" pathname verify-existance function))))
523 ram 1.1
524 wlott 1.16 (defun %enumerate-directories (head tail pathname verify-existance function)
525     (if tail
526     (let ((piece (car tail)))
527     (etypecase piece
528     (simple-string
529     (%enumerate-directories (concatenate 'string head piece "/")
530     (cdr tail) pathname verify-existance
531     function))
532     (pattern
533 wlott 1.26 (let ((dir (unix:open-dir head)))
534 wlott 1.16 (when dir
535     (unwind-protect
536     (loop
537 wlott 1.26 (let ((name (unix:read-dir dir)))
538 wlott 1.16 (cond ((null name)
539     (return))
540     ((string= name "."))
541     ((string= name ".."))
542     ((pattern-matches piece name)
543     (let ((subdir (concatenate 'string
544     head name "/")))
545 wlott 1.26 (when (eq (unix:unix-file-kind subdir)
546 wlott 1.16 :directory)
547     (%enumerate-directories
548     subdir (cdr tail) pathname verify-existance
549     function)))))))
550 wlott 1.26 (unix:close-dir dir)))))
551 wlott 1.16 ((member :up)
552     (%enumerate-directories (concatenate 'string head "../")
553     (cdr tail) pathname verify-existance
554     function))))
555     (%enumerate-files head pathname verify-existance function)))
556 ram 1.1
557 wlott 1.16 (defun %enumerate-files (directory pathname verify-existance function)
558     (let ((name (pathname-name pathname))
559     (type (pathname-type pathname))
560     (version (pathname-version pathname)))
561 ram 1.33 (cond ((member name '(nil :unspecific))
562 wlott 1.16 (when (or (not verify-existance)
563 wlott 1.26 (unix:unix-file-kind directory))
564 wlott 1.16 (funcall function directory)))
565     ((or (pattern-p name)
566     (pattern-p type)
567 ram 1.33 (eq name :wild)
568     (eq type :wild))
569 wlott 1.26 (let ((dir (unix:open-dir directory)))
570 wlott 1.16 (when dir
571     (unwind-protect
572     (loop
573 wlott 1.26 (let ((file (unix:read-dir dir)))
574 wlott 1.16 (if file
575 wlott 1.21 (unless (or (string= file ".")
576     (string= file ".."))
577     (multiple-value-bind
578     (file-name file-type file-version)
579     (let ((*ignore-wildcards* t))
580     (extract-name-type-and-version
581     file 0 (length file)))
582     (when (and (components-match file-name name)
583     (components-match file-type type)
584     (components-match file-version
585     version))
586     (funcall function
587     (concatenate 'string
588     directory
589     file)))))
590 wlott 1.16 (return))))
591 wlott 1.26 (unix:close-dir dir)))))
592 wlott 1.16 (t
593     (let ((file (concatenate 'string directory name)))
594 wlott 1.19 (unless (or (null type) (eq type :unspecific))
595 wlott 1.16 (setf file (concatenate 'string file "." type)))
596 ram 1.33 (unless (member version '(nil :newest :wild))
597 wlott 1.19 (setf file (concatenate 'string file "."
598 wlott 1.20 (quick-integer-to-string version))))
599 wlott 1.16 (when (or (not verify-existance)
600 wlott 1.26 (unix:unix-file-kind file))
601 wlott 1.16 (funcall function file)))))))
602 ram 1.1
603     (defun quick-integer-to-string (n)
604 wlott 1.16 (declare (type integer n))
605 ram 1.1 (cond ((zerop n) "0")
606     ((eql n 1) "1")
607     ((minusp n)
608     (concatenate 'simple-string "-"
609     (the simple-string (quick-integer-to-string (- n)))))
610     (t
611     (do* ((len (1+ (truncate (integer-length n) 3)))
612     (res (make-string len))
613     (i (1- len) (1- i))
614     (q n)
615     (r 0))
616     ((zerop q)
617     (incf i)
618     (replace res res :start2 i :end2 len)
619 wlott 1.3 (shrink-vector res (- len i)))
620 ram 1.1 (declare (simple-string res)
621     (fixnum len i r))
622     (multiple-value-setq (q r) (truncate q 10))
623     (setf (schar res i) (schar "0123456789" r))))))
624    
625 wlott 1.16
626     ;;;; UNIX-NAMESTRING -- public
627 wlott 1.5 ;;;
628     (defun unix-namestring (pathname &optional (for-input t))
629 wlott 1.16 "Convert PATHNAME into a string that can be used with UNIX system calls.
630     Search-lists and wild-cards are expanded."
631     (enumerate-search-list
632     (pathname pathname)
633     (collect ((names))
634     (enumerate-matches (name pathname nil :verify-existance for-input)
635     (names name))
636     (let ((names (names)))
637     (when names
638     (when (cdr names)
639     (error "~S is ambiguous:~{~% ~A~}" pathname names))
640     (return (car names)))))))
641 wlott 1.4
642 ram 1.1
643 wlott 1.16 ;;;; TRUENAME and PROBE-FILE.
644 ram 1.1
645     ;;; Truename -- Public
646     ;;;
647 wlott 1.4 ;;; Another silly file function trivially different from another function.
648 ram 1.1 ;;;
649     (defun truename (pathname)
650     "Return the pathname for the actual file described by the pathname
651     An error is signalled if no such file exists."
652     (let ((result (probe-file pathname)))
653     (unless result
654     (error "The file ~S does not exist." (namestring pathname)))
655     result))
656    
657     ;;; Probe-File -- Public
658     ;;;
659 wlott 1.4 ;;; If PATHNAME exists, return it's truename, otherwise NIL.
660 ram 1.1 ;;;
661     (defun probe-file (pathname)
662     "Return a pathname which is the truename of the file if it exists, NIL
663 wlott 1.16 otherwise."
664 wlott 1.4 (let ((namestring (unix-namestring pathname t)))
665 wlott 1.26 (when (and namestring (unix:unix-file-kind namestring))
666     (let ((truename (unix:unix-resolve-links
667     (unix:unix-maybe-prepend-current-directory
668 wlott 1.4 namestring))))
669     (when truename
670 wlott 1.16 (let ((*ignore-wildcards* t))
671 wlott 1.26 (pathname (unix:unix-simplify-pathname truename))))))))
672 ram 1.1
673    
674 wlott 1.4 ;;;; Other random operations.
675 ram 1.1
676     ;;; Rename-File -- Public
677     ;;;
678     (defun rename-file (file new-name)
679     "Rename File to have the specified New-Name. If file is a stream open to a
680     file, then the associated file is renamed. If the file does not yet exist
681     then the file is created with the New-Name when the stream is closed."
682 wlott 1.4 (let* ((original (truename file))
683 wlott 1.16 (original-namestring (unix-namestring original t))
684 wlott 1.4 (new-name (merge-pathnames new-name original))
685     (new-namestring (unix-namestring new-name nil)))
686 wlott 1.17 (unless original-namestring
687     (error "~S doesn't exist." file))
688     (unless new-namestring
689     (error "~S can't be created." new-name))
690 wlott 1.4 (multiple-value-bind (res error)
691 wlott 1.26 (unix:unix-rename original-namestring
692 wlott 1.4 new-namestring)
693     (unless res
694     (error "Failed to rename ~A to ~A: ~A"
695 wlott 1.26 original new-name (unix:get-unix-error-msg error)))
696 wlott 1.4 (when (streamp file)
697     (file-name file new-namestring))
698 wlott 1.16 (values new-name original (truename new-name)))))
699 ram 1.1
700     ;;; Delete-File -- Public
701     ;;;
702     ;;; Delete the file, Man.
703     ;;;
704     (defun delete-file (file)
705     "Delete the specified file."
706 wlott 1.4 (let ((namestring (unix-namestring file t)))
707 ram 1.1 (when (streamp file)
708     (close file :abort t))
709 ram 1.24 (unless namestring
710     (error "~S doesn't exist." file))
711    
712 wlott 1.26 (multiple-value-bind (res err) (unix:unix-unlink namestring)
713 ram 1.24 (unless res
714     (error "Could not delete ~A: ~A."
715     namestring
716 wlott 1.26 (unix:get-unix-error-msg err)))))
717 ram 1.1 t)
718 wlott 1.4
719 ram 1.1
720     ;;; User-Homedir-Pathname -- Public
721     ;;;
722 ram 1.12 ;;; Return Home:, which is set up for us at initialization time.
723 ram 1.1 ;;;
724     (defun user-homedir-pathname (&optional host)
725     "Returns the home directory of the logged in user as a pathname.
726 ram 1.12 This is obtained from the logical name \"home:\"."
727 ram 1.1 (declare (ignore host))
728 ram 1.12 #p"home:")
729 ram 1.1
730     ;;; File-Write-Date -- Public
731     ;;;
732     (defun file-write-date (file)
733     "Return file's creation date, or NIL if it doesn't exist."
734 wlott 1.17 (let ((name (unix-namestring file t)))
735     (when name
736     (multiple-value-bind
737     (res dev ino mode nlink uid gid rdev size atime mtime)
738 wlott 1.26 (unix:unix-stat name)
739 wlott 1.17 (declare (ignore dev ino mode nlink uid gid rdev size atime))
740     (when res
741     (+ unix-to-universal-time mtime))))))
742 ram 1.1
743     ;;; File-Author -- Public
744     ;;;
745     (defun file-author (file)
746     "Returns the file author as a string, or nil if the author cannot be
747     determined. Signals an error if file doesn't exist."
748 wlott 1.17 (let ((name (unix-namestring (pathname file) t)))
749     (unless name
750     (error "~S doesn't exist." file))
751     (multiple-value-bind (winp dev ino mode nlink uid)
752 wlott 1.26 (unix:unix-stat file)
753 wlott 1.17 (declare (ignore dev ino mode nlink))
754     (if winp (lookup-login-name uid)))))
755 ram 1.1
756    
757    
758     ;;;; DIRECTORY.
759    
760 wlott 1.4 ;;; DIRECTORY -- public.
761     ;;;
762 ram 1.23 (defun directory (pathname &key (all t) (check-for-subdirs t)
763     (follow-links t))
764 ram 1.1 "Returns a list of pathnames, one for each file that matches the given
765 ram 1.23 pathname. Supplying :ALL as nil causes this to ignore Unix dot files. This
766     never includes Unix dot and dot-dot in the result. If :FOLLOW-LINKS is NIL,
767     then symblolic links in the result are not expanded. This is not the
768     default because TRUENAME does follow links, and the result pathnames are
769     defined to be the TRUENAME of the pathname (the truename of a link may well
770     be in another directory.)"
771 wlott 1.16 (let ((results nil))
772     (enumerate-search-list
773 wlott 1.21 (pathname (merge-pathnames pathname
774     (make-pathname :name :wild
775     :type :wild
776     :version :wild)))
777 wlott 1.16 (enumerate-matches (name pathname)
778     (when (or all
779     (let ((slash (position #\/ name :from-end t)))
780     (or (null slash)
781     (= (1+ slash) (length name))
782     (char/= (schar name (1+ slash)) #\.))))
783     (push name results))))
784     (let ((*ignore-wildcards* t))
785     (mapcar #'(lambda (name)
786 ram 1.23 (let ((name (if (and check-for-subdirs
787 wlott 1.26 (eq (unix:unix-file-kind name)
788 ram 1.23 :directory))
789     (concatenate 'string name "/")
790     name)))
791     (if follow-links (truename name) (pathname name))))
792 wlott 1.16 (sort (delete-duplicates results :test #'string=) #'string<)))))
793 ram 1.1
794 wlott 1.4
795     ;;;; Printing directories.
796 ram 1.1
797     ;;; PRINT-DIRECTORY is exported from the EXTENSIONS package.
798     ;;;
799     (defun print-directory (pathname &optional stream &key all verbose return-list)
800     "Like Directory, but prints a terse, multi-coloumn directory listing
801     instead of returning a list of pathnames. When :all is supplied and
802     non-nil, then Unix dot files are included too (as ls -a). When :vervose
803     is supplied and non-nil, then a long listing of miscellaneous
804     information is output one file per line."
805 wlott 1.16 (let ((*standard-output* (out-synonym-of stream))
806     (pathname pathname))
807 ram 1.1 (if verbose
808     (print-directory-verbose pathname all return-list)
809     (print-directory-formatted pathname all return-list))))
810    
811     (defun print-directory-verbose (pathname all return-list)
812 ram 1.23 (let ((contents (directory pathname :all all :check-for-subdirs nil
813     :follow-links nil))
814 wlott 1.7 (result nil))
815     (format t "Directory of ~A :~%" (namestring pathname))
816     (dolist (file contents)
817     (let* ((namestring (unix-namestring file))
818     (tail (subseq namestring
819     (1+ (or (position #\/ namestring
820     :from-end t
821     :test #'char=)
822     -1)))))
823     (multiple-value-bind
824     (reslt dev-or-err ino mode nlink uid gid rdev size atime mtime)
825 wlott 1.26 (unix:unix-stat namestring)
826 wlott 1.7 (declare (ignore ino gid rdev atime)
827     (fixnum uid mode))
828     (cond (reslt
829     ;;
830     ;; Print characters for file modes.
831     (macrolet ((frob (bit name &optional sbit sname negate)
832     `(if ,(if negate
833     `(not (logbitp ,bit mode))
834     `(logbitp ,bit mode))
835     ,(if sbit
836     `(if (logbitp ,sbit mode)
837     (write-char ,sname)
838     (write-char ,name))
839     `(write-char ,name))
840     (write-char #\-))))
841     (frob 15 #\d nil nil t)
842     (frob 8 #\r)
843     (frob 7 #\w)
844     (frob 6 #\x 11 #\s)
845     (frob 5 #\r)
846     (frob 4 #\w)
847     (frob 3 #\x 10 #\s)
848     (frob 2 #\r)
849     (frob 1 #\w)
850     (frob 0 #\x))
851     ;;
852     ;; Print the rest.
853     (multiple-value-bind (sec min hour date month year)
854     (get-decoded-time)
855     (declare (ignore sec min hour date month))
856     (format t "~2D ~8A ~8D ~12A ~A~@[/~]~%"
857     nlink
858     (or (lookup-login-name uid) uid)
859     size
860     (decode-universal-time-for-files mtime year)
861     tail
862 wlott 1.27 (= (logand mode unix:s-ifmt) unix:s-ifdir))))
863 wlott 1.7 (t (format t "Couldn't stat ~A -- ~A.~%"
864     tail
865 wlott 1.26 (unix:get-unix-error-msg dev-or-err))))
866 ram 1.1 (when return-list
867 wlott 1.27 (push (if (= (logand mode unix:s-ifmt) unix:s-ifdir)
868 wlott 1.7 (pathname (concatenate 'string namestring "/"))
869     file)
870     result)))))
871     (nreverse result)))
872 ram 1.1
873     (defun decode-universal-time-for-files (time current-year)
874     (multiple-value-bind (sec min hour day month year)
875     (decode-universal-time (+ time unix-to-universal-time))
876     (declare (ignore sec))
877     (format nil "~A ~2,' D ~:[ ~D~;~*~2,'0D:~2,'0D~]"
878     (svref '#("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug"
879     "Sep" "Oct" "Nov" "Dec")
880     (1- month))
881     day (= current-year year) year hour min)))
882    
883     (defun print-directory-formatted (pathname all return-list)
884     (let ((width (or (line-length *standard-output*) 80))
885     (names ())
886     (cnt 0)
887     (max-len 0)
888 ram 1.23 (result (directory pathname :all all :follow-links nil)))
889 ram 1.1 (declare (list names) (fixnum max-len cnt))
890     ;;
891     ;; Get the data.
892 wlott 1.7 (dolist (file result)
893     (let* ((name (unix-namestring file))
894     (length (length name))
895     (end (if (and (plusp length)
896     (char= (schar name (1- length)) #\/))
897     (1- length)
898     length))
899     (slash-name (subseq name
900     (1+ (or (position #\/ name
901     :from-end t
902     :end end
903     :test #'char=)
904     -1))))
905     (len (length slash-name)))
906     (declare (simple-string slash-name)
907     (fixnum len))
908     (if (> len max-len) (setq max-len len))
909     (incf cnt)
910     (push slash-name names)))
911     (setq names (nreverse names))
912     ;;
913     ;; Do the output.
914     (let* ((col-width (1+ max-len))
915     (cols (max (truncate width col-width) 1))
916     (lines (ceiling cnt cols)))
917     (declare (fixnum cols lines))
918     (format t "Directory of ~A :~%" (namestring pathname))
919     (dotimes (i lines)
920     (declare (fixnum i))
921     (dotimes (j cols)
922     (declare (fixnum j))
923     (let ((name (nth (+ i (the fixnum (* j lines))) names)))
924     (when name
925     (write-string name)
926     (unless (eql j (1- cols))
927 wlott 1.15 (dotimes (i (- col-width (length (the simple-string name))))
928     (write-char #\space))))))
929 wlott 1.7 (terpri)))
930     (when return-list
931     result)))
932 ram 1.1
933    
934    
935     ;;;; Translating uid's and gid's.
936    
937     (defvar *uid-hash-table* (make-hash-table)
938     "Hash table for keeping track of uid's and login names.")
939    
940     ;;; LOOKUP-LOGIN-NAME translates a user id into a login name. Previous
941     ;;; lookups are cached in a hash table since groveling the passwd(s) files
942     ;;; is somewhat expensive. The table may hold nil for id's that cannot
943     ;;; be looked up since this means the files are searched in their entirety
944     ;;; each time this id is translated.
945     ;;;
946     (defun lookup-login-name (uid)
947     (multiple-value-bind (login-name foundp) (gethash uid *uid-hash-table*)
948     (if foundp
949     login-name
950     (setf (gethash uid *uid-hash-table*)
951     (get-group-or-user-name :user uid)))))
952    
953     (defvar *gid-hash-table* (make-hash-table)
954     "Hash table for keeping track of gid's and group names.")
955    
956     ;;; LOOKUP-GROUP-NAME translates a group id into a group name. Previous
957     ;;; lookups are cached in a hash table since groveling the group(s) files
958     ;;; is somewhat expensive. The table may hold nil for id's that cannot
959     ;;; be looked up since this means the files are searched in their entirety
960     ;;; each time this id is translated.
961     ;;;
962     (defun lookup-group-name (gid)
963     (multiple-value-bind (group-name foundp) (gethash gid *gid-hash-table*)
964     (if foundp
965     group-name
966     (setf (gethash gid *gid-hash-table*)
967     (get-group-or-user-name :group gid)))))
968    
969    
970     ;;; GET-GROUP-OR-USER-NAME first tries "/etc/passwd" ("/etc/group") since it is
971     ;;; a much smaller file, contains all the local id's, and most uses probably
972     ;;; involve id's on machines one would login into. Then if necessary, we look
973     ;;; in "/etc/passwds" ("/etc/groups") which is really long and has to be
974     ;;; fetched over the net.
975     ;;;
976     (defun get-group-or-user-name (group-or-user id)
977     "Returns the simple-string user or group name of the user whose uid or gid
978     is id, or NIL if no such user or group exists. Group-or-user is either
979     :group or :user."
980     (let ((id-string (let ((*print-base* 10)) (prin1-to-string id))))
981     (declare (simple-string id-string))
982     (multiple-value-bind (file1 file2)
983     (ecase group-or-user
984     (:group (values "/etc/group" "/etc/groups"))
985     (:user (values "/etc/passwd" "/etc/passwd")))
986     (or (get-group-or-user-name-aux id-string file1)
987     (get-group-or-user-name-aux id-string file2)))))
988    
989     (defun get-group-or-user-name-aux (id-string passwd-file)
990     (with-open-file (stream passwd-file)
991     (loop
992     (let ((entry (read-line stream nil)))
993     (unless entry (return nil))
994     (let ((name-end (position #\: (the simple-string entry)
995     :test #'char=)))
996     (when name-end
997     (let ((id-start (position #\: (the simple-string entry)
998     :start (1+ name-end) :test #'char=)))
999     (when id-start
1000     (incf id-start)
1001     (let ((id-end (position #\: (the simple-string entry)
1002     :start id-start :test #'char=)))
1003     (when (and id-end
1004     (string= id-string entry
1005     :start2 id-start :end2 id-end))
1006     (return (subseq entry 0 name-end))))))))))))
1007    
1008    
1009 wlott 1.4 ;;;; File completion.
1010 ram 1.1
1011 chiles 1.13 ;;; COMPLETE-FILE -- Public
1012     ;;;
1013 wlott 1.4 (defun complete-file (pathname &key (defaults *default-pathname-defaults*)
1014     ignore-types)
1015 wlott 1.22 (let ((files (directory (complete-file-directory-arg pathname defaults)
1016 ram 1.23 :check-for-subdirs nil
1017     :follow-links nil)))
1018 wlott 1.4 (cond ((null files)
1019     (values nil nil))
1020     ((null (cdr files))
1021     (values (merge-pathnames (file-namestring (car files))
1022     pathname)
1023     t))
1024     (t
1025     (let ((good-files
1026     (delete-if #'(lambda (pathname)
1027 wlott 1.16 (and (simple-string-p
1028     (pathname-type pathname))
1029 wlott 1.4 (member (pathname-type pathname)
1030     ignore-types
1031     :test #'string=)))
1032     files)))
1033     (cond ((null good-files))
1034     ((null (cdr good-files))
1035     (return-from complete-file
1036     (values (merge-pathnames (file-namestring
1037     (car good-files))
1038     pathname)
1039     t)))
1040     (t
1041     (setf files good-files)))
1042     (let ((common (file-namestring (car files))))
1043     (dolist (file (cdr files))
1044     (let ((name (file-namestring file)))
1045     (dotimes (i (min (length common) (length name))
1046     (when (< (length name) (length common))
1047     (setf common name)))
1048     (unless (char= (schar common i) (schar name i))
1049     (setf common (subseq common 0 i))
1050     (return)))))
1051     (values (merge-pathnames common pathname)
1052     nil)))))))
1053 ram 1.1
1054 chiles 1.13 ;;; COMPLETE-FILE-DIRECTORY-ARG -- Internal.
1055     ;;;
1056     (defun complete-file-directory-arg (pathname defaults)
1057 wlott 1.22 (let* ((pathname (merge-pathnames pathname (directory-namestring defaults)))
1058 wlott 1.16 (type (pathname-type pathname)))
1059     (flet ((append-multi-char-wild (thing)
1060     (etypecase thing
1061     (null :wild)
1062     (pattern
1063     (make-pattern (append (pattern-pieces thing)
1064     (list :multi-char-wild))))
1065     (simple-string
1066     (make-pattern (list thing :multi-char-wild))))))
1067     (if (or (null type) (eq type :unspecific))
1068     ;; There is no type.
1069     (make-pathname :defaults pathname
1070     :name (append-multi-char-wild (pathname-name pathname))
1071     :type :wild)
1072     ;; There already is a type, so just extend it.
1073     (make-pathname :defaults pathname
1074     :name (pathname-name pathname)
1075     :type (append-multi-char-wild (pathname-type pathname)))))))
1076 chiles 1.13
1077 wlott 1.4 ;;; Ambiguous-Files -- Public
1078 ram 1.1 ;;;
1079 wlott 1.16 (defun ambiguous-files (pathname
1080     &optional (defaults *default-pathname-defaults*))
1081 wlott 1.4 "Return a list of all files which are possible completions of Pathname.
1082 chiles 1.13 We look in the directory specified by Defaults as well as looking down
1083     the search list."
1084 wlott 1.16 (directory (complete-file-directory-arg pathname defaults)
1085 ram 1.23 :follow-links nil
1086 wlott 1.16 :check-for-subdirs nil))
1087 wlott 1.4
1088 wlott 1.16
1089 ram 1.1
1090     ;;; File-writable -- exported from extensions.
1091     ;;;
1092     ;;; Determines whether the single argument (which should be a pathname)
1093     ;;; can be written by the the current task.
1094 wlott 1.16 ;;;
1095 ram 1.1 (defun file-writable (name)
1096     "File-writable accepts a pathname and returns T if the current
1097     process can write it, and NIL otherwise."
1098 wlott 1.16 (let ((name (unix-namestring name nil)))
1099 wlott 1.17 (cond ((null name)
1100     nil)
1101 wlott 1.26 ((unix:unix-file-kind name)
1102     (values (unix:unix-access name unix:w_ok)))
1103 wlott 1.17 (t
1104     (values
1105 wlott 1.26 (unix:unix-access (subseq name
1106 wlott 1.17 0
1107     (or (position #\/ name :from-end t)
1108     0))
1109 wlott 1.26 (logior unix:w_ok unix:x_ok)))))))
1110 ram 1.1
1111    
1112     ;;; Pathname-Order -- Internal
1113     ;;;
1114     ;;; Predicate to order pathnames by. Goes by name.
1115     ;;;
1116     (defun pathname-order (x y)
1117     (let ((xn (%pathname-name x))
1118     (yn (%pathname-name y)))
1119     (if (and xn yn)
1120     (let ((res (string-lessp xn yn)))
1121     (cond ((not res) nil)
1122     ((= res (length (the simple-string xn))) t)
1123     ((= res (length (the simple-string yn))) nil)
1124     (t t)))
1125     xn)))
1126    
1127    
1128     ;;; Default-Directory -- Public
1129     ;;;
1130     (defun default-directory ()
1131     "Returns the pathname for the default directory. This is the place where
1132     a file will be written if no directory is specified. This may be changed
1133     with setf."
1134     (multiple-value-bind (gr dir-or-error)
1135 wlott 1.26 (unix:unix-current-directory)
1136 ram 1.1 (if gr
1137 wlott 1.16 (let ((*ignore-wildcards* t))
1138     (pathname (concatenate 'simple-string dir-or-error "/")))
1139 wlott 1.3 (error dir-or-error))))
1140 ram 1.1
1141     ;;; %Set-Default-Directory -- Internal
1142     ;;;
1143     (defun %set-default-directory (new-val)
1144 wlott 1.17 (let ((namestring (unix-namestring new-val t)))
1145     (unless namestring
1146     (error "~S doesn't exist." new-val))
1147     (multiple-value-bind (gr error)
1148 wlott 1.26 (unix:unix-chdir namestring)
1149 wlott 1.17 (if gr
1150     (setf (search-list "default:") (default-directory))
1151 wlott 1.26 (error (unix:get-unix-error-msg error))))
1152 wlott 1.17 new-val))
1153 wlott 1.16 ;;;
1154     (defsetf default-directory %set-default-directory)
1155    
1156     (defun filesys-init ()
1157     (setf *default-pathname-defaults*
1158     (%make-pathname *unix-host* nil nil nil nil :newest))
1159     (setf (search-list "default:") (default-directory))
1160     nil)

  ViewVC Help
Powered by ViewVC 1.1.5