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

Contents of /src/code/filesys.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5