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

Contents of /src/code/filesys.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5