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

Contents of /src/code/filesys.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.24 - (hide annotations)
Fri Dec 20 21:17:37 1991 UTC (22 years, 4 months ago) by ram
Branch: MAIN
Changes since 1.23: +9 -7 lines
Changed DELETE-FILE to signal an error if the file doesn't exist.
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.24 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/filesys.lisp,v 1.24 1991/12/20 21:17:37 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     ((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 wlott 1.21 (unless (or (string= file ".")
561     (string= file ".."))
562     (multiple-value-bind
563     (file-name file-type file-version)
564     (let ((*ignore-wildcards* t))
565     (extract-name-type-and-version
566     file 0 (length file)))
567     (when (and (components-match file-name name)
568     (components-match file-type type)
569     (components-match file-version
570     version))
571     (funcall function
572     (concatenate 'string
573     directory
574     file)))))
575 wlott 1.16 (return))))
576     (mach:close-dir dir)))))
577     (t
578     (let ((file (concatenate 'string directory name)))
579 wlott 1.19 (unless (or (null type) (eq type :unspecific))
580 wlott 1.16 (setf file (concatenate 'string file "." type)))
581 wlott 1.19 (unless (or (null version) (eq version :newest))
582     (setf file (concatenate 'string file "."
583 wlott 1.20 (quick-integer-to-string version))))
584 wlott 1.16 (when (or (not verify-existance)
585     (mach:unix-file-kind file))
586     (funcall function file)))))))
587 ram 1.1
588     (defun quick-integer-to-string (n)
589 wlott 1.16 (declare (type integer n))
590 ram 1.1 (cond ((zerop n) "0")
591     ((eql n 1) "1")
592     ((minusp n)
593     (concatenate 'simple-string "-"
594     (the simple-string (quick-integer-to-string (- n)))))
595     (t
596     (do* ((len (1+ (truncate (integer-length n) 3)))
597     (res (make-string len))
598     (i (1- len) (1- i))
599     (q n)
600     (r 0))
601     ((zerop q)
602     (incf i)
603     (replace res res :start2 i :end2 len)
604 wlott 1.3 (shrink-vector res (- len i)))
605 ram 1.1 (declare (simple-string res)
606     (fixnum len i r))
607     (multiple-value-setq (q r) (truncate q 10))
608     (setf (schar res i) (schar "0123456789" r))))))
609    
610 wlott 1.16
611     ;;;; UNIX-NAMESTRING -- public
612 wlott 1.5 ;;;
613     (defun unix-namestring (pathname &optional (for-input t))
614 wlott 1.16 "Convert PATHNAME into a string that can be used with UNIX system calls.
615     Search-lists and wild-cards are expanded."
616     (enumerate-search-list
617     (pathname pathname)
618     (collect ((names))
619     (enumerate-matches (name pathname nil :verify-existance for-input)
620     (names name))
621     (let ((names (names)))
622     (when names
623     (when (cdr names)
624     (error "~S is ambiguous:~{~% ~A~}" pathname names))
625     (return (car names)))))))
626 wlott 1.4
627 ram 1.1
628 wlott 1.16 ;;;; TRUENAME and PROBE-FILE.
629 ram 1.1
630     ;;; Truename -- Public
631     ;;;
632 wlott 1.4 ;;; Another silly file function trivially different from another function.
633 ram 1.1 ;;;
634     (defun truename (pathname)
635     "Return the pathname for the actual file described by the pathname
636     An error is signalled if no such file exists."
637     (let ((result (probe-file pathname)))
638     (unless result
639     (error "The file ~S does not exist." (namestring pathname)))
640     result))
641    
642     ;;; Probe-File -- Public
643     ;;;
644 wlott 1.4 ;;; If PATHNAME exists, return it's truename, otherwise NIL.
645 ram 1.1 ;;;
646     (defun probe-file (pathname)
647     "Return a pathname which is the truename of the file if it exists, NIL
648 wlott 1.16 otherwise."
649 wlott 1.4 (let ((namestring (unix-namestring pathname t)))
650 wlott 1.16 (when (and namestring (mach:unix-file-kind namestring))
651 wlott 1.4 (let ((truename (mach:unix-resolve-links
652     (mach:unix-maybe-prepend-current-directory
653     namestring))))
654     (when truename
655 wlott 1.16 (let ((*ignore-wildcards* t))
656     (pathname (mach:unix-simplify-pathname truename))))))))
657 ram 1.1
658    
659 wlott 1.4 ;;;; Other random operations.
660 ram 1.1
661     ;;; Rename-File -- Public
662     ;;;
663     (defun rename-file (file new-name)
664     "Rename File to have the specified New-Name. If file is a stream open to a
665     file, then the associated file is renamed. If the file does not yet exist
666     then the file is created with the New-Name when the stream is closed."
667 wlott 1.4 (let* ((original (truename file))
668 wlott 1.16 (original-namestring (unix-namestring original t))
669 wlott 1.4 (new-name (merge-pathnames new-name original))
670     (new-namestring (unix-namestring new-name nil)))
671 wlott 1.17 (unless original-namestring
672     (error "~S doesn't exist." file))
673     (unless new-namestring
674     (error "~S can't be created." new-name))
675 wlott 1.4 (multiple-value-bind (res error)
676     (mach:unix-rename original-namestring
677     new-namestring)
678     (unless res
679     (error "Failed to rename ~A to ~A: ~A"
680     original new-name (mach:get-unix-error-msg error)))
681     (when (streamp file)
682     (file-name file new-namestring))
683 wlott 1.16 (values new-name original (truename new-name)))))
684 ram 1.1
685     ;;; Delete-File -- Public
686     ;;;
687     ;;; Delete the file, Man.
688     ;;;
689     (defun delete-file (file)
690     "Delete the specified file."
691 wlott 1.4 (let ((namestring (unix-namestring file t)))
692 ram 1.1 (when (streamp file)
693     (close file :abort t))
694 ram 1.24 (unless namestring
695     (error "~S doesn't exist." file))
696    
697     (multiple-value-bind (res err) (mach:unix-unlink namestring)
698     (unless res
699     (error "Could not delete ~A: ~A."
700     namestring
701     (mach:get-unix-error-msg err)))))
702 ram 1.1 t)
703 wlott 1.4
704 ram 1.1
705     ;;; User-Homedir-Pathname -- Public
706     ;;;
707 ram 1.12 ;;; Return Home:, which is set up for us at initialization time.
708 ram 1.1 ;;;
709     (defun user-homedir-pathname (&optional host)
710     "Returns the home directory of the logged in user as a pathname.
711 ram 1.12 This is obtained from the logical name \"home:\"."
712 ram 1.1 (declare (ignore host))
713 ram 1.12 #p"home:")
714 ram 1.1
715     ;;; File-Write-Date -- Public
716     ;;;
717     (defun file-write-date (file)
718     "Return file's creation date, or NIL if it doesn't exist."
719 wlott 1.17 (let ((name (unix-namestring file t)))
720     (when name
721     (multiple-value-bind
722     (res dev ino mode nlink uid gid rdev size atime mtime)
723     (mach:unix-stat name)
724     (declare (ignore dev ino mode nlink uid gid rdev size atime))
725     (when res
726     (+ unix-to-universal-time mtime))))))
727 ram 1.1
728     ;;; File-Author -- Public
729     ;;;
730     (defun file-author (file)
731     "Returns the file author as a string, or nil if the author cannot be
732     determined. Signals an error if file doesn't exist."
733 wlott 1.17 (let ((name (unix-namestring (pathname file) t)))
734     (unless name
735     (error "~S doesn't exist." file))
736     (multiple-value-bind (winp dev ino mode nlink uid)
737     (mach:unix-stat file)
738     (declare (ignore dev ino mode nlink))
739     (if winp (lookup-login-name uid)))))
740 ram 1.1
741    
742    
743     ;;;; DIRECTORY.
744    
745 wlott 1.4 ;;; DIRECTORY -- public.
746     ;;;
747 ram 1.23 (defun directory (pathname &key (all t) (check-for-subdirs t)
748     (follow-links t))
749 ram 1.1 "Returns a list of pathnames, one for each file that matches the given
750 ram 1.23 pathname. Supplying :ALL as nil causes this to ignore Unix dot files. This
751     never includes Unix dot and dot-dot in the result. If :FOLLOW-LINKS is NIL,
752     then symblolic links in the result are not expanded. This is not the
753     default because TRUENAME does follow links, and the result pathnames are
754     defined to be the TRUENAME of the pathname (the truename of a link may well
755     be in another directory.)"
756 wlott 1.16 (let ((results nil))
757     (enumerate-search-list
758 wlott 1.21 (pathname (merge-pathnames pathname
759     (make-pathname :name :wild
760     :type :wild
761     :version :wild)))
762 wlott 1.16 (enumerate-matches (name pathname)
763     (when (or all
764     (let ((slash (position #\/ name :from-end t)))
765     (or (null slash)
766     (= (1+ slash) (length name))
767     (char/= (schar name (1+ slash)) #\.))))
768     (push name results))))
769     (let ((*ignore-wildcards* t))
770     (mapcar #'(lambda (name)
771 ram 1.23 (let ((name (if (and check-for-subdirs
772     (eq (mach:unix-file-kind name)
773     :directory))
774     (concatenate 'string name "/")
775     name)))
776     (if follow-links (truename name) (pathname name))))
777 wlott 1.16 (sort (delete-duplicates results :test #'string=) #'string<)))))
778 ram 1.1
779 wlott 1.4
780     ;;;; Printing directories.
781 ram 1.1
782     ;;; PRINT-DIRECTORY is exported from the EXTENSIONS package.
783     ;;;
784     (defun print-directory (pathname &optional stream &key all verbose return-list)
785     "Like Directory, but prints a terse, multi-coloumn directory listing
786     instead of returning a list of pathnames. When :all is supplied and
787     non-nil, then Unix dot files are included too (as ls -a). When :vervose
788     is supplied and non-nil, then a long listing of miscellaneous
789     information is output one file per line."
790 wlott 1.16 (let ((*standard-output* (out-synonym-of stream))
791     (pathname pathname))
792 ram 1.1 (if verbose
793     (print-directory-verbose pathname all return-list)
794     (print-directory-formatted pathname all return-list))))
795    
796     (defun print-directory-verbose (pathname all return-list)
797 ram 1.23 (let ((contents (directory pathname :all all :check-for-subdirs nil
798     :follow-links nil))
799 wlott 1.7 (result nil))
800     (format t "Directory of ~A :~%" (namestring pathname))
801     (dolist (file contents)
802     (let* ((namestring (unix-namestring file))
803     (tail (subseq namestring
804     (1+ (or (position #\/ namestring
805     :from-end t
806     :test #'char=)
807     -1)))))
808     (multiple-value-bind
809     (reslt dev-or-err ino mode nlink uid gid rdev size atime mtime)
810     (mach:unix-stat namestring)
811     (declare (ignore ino gid rdev atime)
812     (fixnum uid mode))
813     (cond (reslt
814     ;;
815     ;; Print characters for file modes.
816     (macrolet ((frob (bit name &optional sbit sname negate)
817     `(if ,(if negate
818     `(not (logbitp ,bit mode))
819     `(logbitp ,bit mode))
820     ,(if sbit
821     `(if (logbitp ,sbit mode)
822     (write-char ,sname)
823     (write-char ,name))
824     `(write-char ,name))
825     (write-char #\-))))
826     (frob 15 #\d nil nil t)
827     (frob 8 #\r)
828     (frob 7 #\w)
829     (frob 6 #\x 11 #\s)
830     (frob 5 #\r)
831     (frob 4 #\w)
832     (frob 3 #\x 10 #\s)
833     (frob 2 #\r)
834     (frob 1 #\w)
835     (frob 0 #\x))
836     ;;
837     ;; Print the rest.
838     (multiple-value-bind (sec min hour date month year)
839     (get-decoded-time)
840     (declare (ignore sec min hour date month))
841     (format t "~2D ~8A ~8D ~12A ~A~@[/~]~%"
842     nlink
843     (or (lookup-login-name uid) uid)
844     size
845     (decode-universal-time-for-files mtime year)
846     tail
847     (= (logand mode mach::s_ifmt) mach::s_ifdir))))
848     (t (format t "Couldn't stat ~A -- ~A.~%"
849     tail
850     (mach:get-unix-error-msg dev-or-err))))
851 ram 1.1 (when return-list
852 wlott 1.7 (push (if (= (logand mode mach::s_ifmt) mach::s_ifdir)
853     (pathname (concatenate 'string namestring "/"))
854     file)
855     result)))))
856     (nreverse result)))
857 ram 1.1
858     (defun decode-universal-time-for-files (time current-year)
859     (multiple-value-bind (sec min hour day month year)
860     (decode-universal-time (+ time unix-to-universal-time))
861     (declare (ignore sec))
862     (format nil "~A ~2,' D ~:[ ~D~;~*~2,'0D:~2,'0D~]"
863     (svref '#("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug"
864     "Sep" "Oct" "Nov" "Dec")
865     (1- month))
866     day (= current-year year) year hour min)))
867    
868     (defun print-directory-formatted (pathname all return-list)
869     (let ((width (or (line-length *standard-output*) 80))
870     (names ())
871     (cnt 0)
872     (max-len 0)
873 ram 1.23 (result (directory pathname :all all :follow-links nil)))
874 ram 1.1 (declare (list names) (fixnum max-len cnt))
875     ;;
876     ;; Get the data.
877 wlott 1.7 (dolist (file result)
878     (let* ((name (unix-namestring file))
879     (length (length name))
880     (end (if (and (plusp length)
881     (char= (schar name (1- length)) #\/))
882     (1- length)
883     length))
884     (slash-name (subseq name
885     (1+ (or (position #\/ name
886     :from-end t
887     :end end
888     :test #'char=)
889     -1))))
890     (len (length slash-name)))
891     (declare (simple-string slash-name)
892     (fixnum len))
893     (if (> len max-len) (setq max-len len))
894     (incf cnt)
895     (push slash-name names)))
896     (setq names (nreverse names))
897     ;;
898     ;; Do the output.
899     (let* ((col-width (1+ max-len))
900     (cols (max (truncate width col-width) 1))
901     (lines (ceiling cnt cols)))
902     (declare (fixnum cols lines))
903     (format t "Directory of ~A :~%" (namestring pathname))
904     (dotimes (i lines)
905     (declare (fixnum i))
906     (dotimes (j cols)
907     (declare (fixnum j))
908     (let ((name (nth (+ i (the fixnum (* j lines))) names)))
909     (when name
910     (write-string name)
911     (unless (eql j (1- cols))
912 wlott 1.15 (dotimes (i (- col-width (length (the simple-string name))))
913     (write-char #\space))))))
914 wlott 1.7 (terpri)))
915     (when return-list
916     result)))
917 ram 1.1
918    
919    
920     ;;;; Translating uid's and gid's.
921    
922     (defvar *uid-hash-table* (make-hash-table)
923     "Hash table for keeping track of uid's and login names.")
924    
925     ;;; LOOKUP-LOGIN-NAME translates a user id into a login name. Previous
926     ;;; lookups are cached in a hash table since groveling the passwd(s) files
927     ;;; is somewhat expensive. The table may hold nil for id's that cannot
928     ;;; be looked up since this means the files are searched in their entirety
929     ;;; each time this id is translated.
930     ;;;
931     (defun lookup-login-name (uid)
932     (multiple-value-bind (login-name foundp) (gethash uid *uid-hash-table*)
933     (if foundp
934     login-name
935     (setf (gethash uid *uid-hash-table*)
936     (get-group-or-user-name :user uid)))))
937    
938     (defvar *gid-hash-table* (make-hash-table)
939     "Hash table for keeping track of gid's and group names.")
940    
941     ;;; LOOKUP-GROUP-NAME translates a group id into a group name. Previous
942     ;;; lookups are cached in a hash table since groveling the group(s) files
943     ;;; is somewhat expensive. The table may hold nil for id's that cannot
944     ;;; be looked up since this means the files are searched in their entirety
945     ;;; each time this id is translated.
946     ;;;
947     (defun lookup-group-name (gid)
948     (multiple-value-bind (group-name foundp) (gethash gid *gid-hash-table*)
949     (if foundp
950     group-name
951     (setf (gethash gid *gid-hash-table*)
952     (get-group-or-user-name :group gid)))))
953    
954    
955     ;;; GET-GROUP-OR-USER-NAME first tries "/etc/passwd" ("/etc/group") since it is
956     ;;; a much smaller file, contains all the local id's, and most uses probably
957     ;;; involve id's on machines one would login into. Then if necessary, we look
958     ;;; in "/etc/passwds" ("/etc/groups") which is really long and has to be
959     ;;; fetched over the net.
960     ;;;
961     (defun get-group-or-user-name (group-or-user id)
962     "Returns the simple-string user or group name of the user whose uid or gid
963     is id, or NIL if no such user or group exists. Group-or-user is either
964     :group or :user."
965     (let ((id-string (let ((*print-base* 10)) (prin1-to-string id))))
966     (declare (simple-string id-string))
967     (multiple-value-bind (file1 file2)
968     (ecase group-or-user
969     (:group (values "/etc/group" "/etc/groups"))
970     (:user (values "/etc/passwd" "/etc/passwd")))
971     (or (get-group-or-user-name-aux id-string file1)
972     (get-group-or-user-name-aux id-string file2)))))
973    
974     (defun get-group-or-user-name-aux (id-string passwd-file)
975     (with-open-file (stream passwd-file)
976     (loop
977     (let ((entry (read-line stream nil)))
978     (unless entry (return nil))
979     (let ((name-end (position #\: (the simple-string entry)
980     :test #'char=)))
981     (when name-end
982     (let ((id-start (position #\: (the simple-string entry)
983     :start (1+ name-end) :test #'char=)))
984     (when id-start
985     (incf id-start)
986     (let ((id-end (position #\: (the simple-string entry)
987     :start id-start :test #'char=)))
988     (when (and id-end
989     (string= id-string entry
990     :start2 id-start :end2 id-end))
991     (return (subseq entry 0 name-end))))))))))))
992    
993    
994 wlott 1.4 ;;;; File completion.
995 ram 1.1
996 chiles 1.13 ;;; COMPLETE-FILE -- Public
997     ;;;
998 wlott 1.4 (defun complete-file (pathname &key (defaults *default-pathname-defaults*)
999     ignore-types)
1000 wlott 1.22 (let ((files (directory (complete-file-directory-arg pathname defaults)
1001 ram 1.23 :check-for-subdirs nil
1002     :follow-links nil)))
1003 wlott 1.4 (cond ((null files)
1004     (values nil nil))
1005     ((null (cdr files))
1006     (values (merge-pathnames (file-namestring (car files))
1007     pathname)
1008     t))
1009     (t
1010     (let ((good-files
1011     (delete-if #'(lambda (pathname)
1012 wlott 1.16 (and (simple-string-p
1013     (pathname-type pathname))
1014 wlott 1.4 (member (pathname-type pathname)
1015     ignore-types
1016     :test #'string=)))
1017     files)))
1018     (cond ((null good-files))
1019     ((null (cdr good-files))
1020     (return-from complete-file
1021     (values (merge-pathnames (file-namestring
1022     (car good-files))
1023     pathname)
1024     t)))
1025     (t
1026     (setf files good-files)))
1027     (let ((common (file-namestring (car files))))
1028     (dolist (file (cdr files))
1029     (let ((name (file-namestring file)))
1030     (dotimes (i (min (length common) (length name))
1031     (when (< (length name) (length common))
1032     (setf common name)))
1033     (unless (char= (schar common i) (schar name i))
1034     (setf common (subseq common 0 i))
1035     (return)))))
1036     (values (merge-pathnames common pathname)
1037     nil)))))))
1038 ram 1.1
1039 chiles 1.13 ;;; COMPLETE-FILE-DIRECTORY-ARG -- Internal.
1040     ;;;
1041     (defun complete-file-directory-arg (pathname defaults)
1042 wlott 1.22 (let* ((pathname (merge-pathnames pathname (directory-namestring defaults)))
1043 wlott 1.16 (type (pathname-type pathname)))
1044     (flet ((append-multi-char-wild (thing)
1045     (etypecase thing
1046     (null :wild)
1047     (pattern
1048     (make-pattern (append (pattern-pieces thing)
1049     (list :multi-char-wild))))
1050     (simple-string
1051     (make-pattern (list thing :multi-char-wild))))))
1052     (if (or (null type) (eq type :unspecific))
1053     ;; There is no type.
1054     (make-pathname :defaults pathname
1055     :name (append-multi-char-wild (pathname-name pathname))
1056     :type :wild)
1057     ;; There already is a type, so just extend it.
1058     (make-pathname :defaults pathname
1059     :name (pathname-name pathname)
1060     :type (append-multi-char-wild (pathname-type pathname)))))))
1061 chiles 1.13
1062 wlott 1.4 ;;; Ambiguous-Files -- Public
1063 ram 1.1 ;;;
1064 wlott 1.16 (defun ambiguous-files (pathname
1065     &optional (defaults *default-pathname-defaults*))
1066 wlott 1.4 "Return a list of all files which are possible completions of Pathname.
1067 chiles 1.13 We look in the directory specified by Defaults as well as looking down
1068     the search list."
1069 wlott 1.16 (directory (complete-file-directory-arg pathname defaults)
1070 ram 1.23 :follow-links nil
1071 wlott 1.16 :check-for-subdirs nil))
1072 wlott 1.4
1073 wlott 1.16
1074 ram 1.1
1075     ;;; File-writable -- exported from extensions.
1076     ;;;
1077     ;;; Determines whether the single argument (which should be a pathname)
1078     ;;; can be written by the the current task.
1079 wlott 1.16 ;;;
1080 ram 1.1 (defun file-writable (name)
1081     "File-writable accepts a pathname and returns T if the current
1082     process can write it, and NIL otherwise."
1083 wlott 1.16 (let ((name (unix-namestring name nil)))
1084 wlott 1.17 (cond ((null name)
1085     nil)
1086     ((mach:unix-file-kind name)
1087     (values (mach:unix-access name mach:w_ok)))
1088     (t
1089     (values
1090     (mach:unix-access (subseq name
1091     0
1092     (or (position #\/ name :from-end t)
1093     0))
1094     (logior mach:w_ok mach:x_ok)))))))
1095 ram 1.1
1096    
1097     ;;; Pathname-Order -- Internal
1098     ;;;
1099     ;;; Predicate to order pathnames by. Goes by name.
1100     ;;;
1101     (defun pathname-order (x y)
1102     (let ((xn (%pathname-name x))
1103     (yn (%pathname-name y)))
1104     (if (and xn yn)
1105     (let ((res (string-lessp xn yn)))
1106     (cond ((not res) nil)
1107     ((= res (length (the simple-string xn))) t)
1108     ((= res (length (the simple-string yn))) nil)
1109     (t t)))
1110     xn)))
1111    
1112    
1113     ;;; Default-Directory -- Public
1114     ;;;
1115     (defun default-directory ()
1116     "Returns the pathname for the default directory. This is the place where
1117     a file will be written if no directory is specified. This may be changed
1118     with setf."
1119     (multiple-value-bind (gr dir-or-error)
1120     (mach:unix-current-directory)
1121     (if gr
1122 wlott 1.16 (let ((*ignore-wildcards* t))
1123     (pathname (concatenate 'simple-string dir-or-error "/")))
1124 wlott 1.3 (error dir-or-error))))
1125 ram 1.1
1126     ;;; %Set-Default-Directory -- Internal
1127     ;;;
1128     (defun %set-default-directory (new-val)
1129 wlott 1.17 (let ((namestring (unix-namestring new-val t)))
1130     (unless namestring
1131     (error "~S doesn't exist." new-val))
1132     (multiple-value-bind (gr error)
1133     (mach:unix-chdir namestring)
1134     (if gr
1135     (setf (search-list "default:") (default-directory))
1136     (error (mach:get-unix-error-msg error))))
1137     new-val))
1138 wlott 1.16 ;;;
1139     (defsetf default-directory %set-default-directory)
1140    
1141     (defun filesys-init ()
1142     (setf *default-pathname-defaults*
1143     (%make-pathname *unix-host* nil nil nil nil :newest))
1144     (setf (search-list "default:") (default-directory))
1145     nil)

  ViewVC Help
Powered by ViewVC 1.1.5