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

Contents of /src/code/filesys.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.29 - (show annotations)
Fri Sep 4 15:22:16 1992 UTC (21 years, 7 months ago) by phg
Branch: MAIN
Changes since 1.28: +2 -2 lines
Change to the function %enumerate-files to permit versions to match on :wild
and :newest for UNIX filesystems (the only kind currently supported by
CMUCL).
1 ;;; -*- Log: code.log; Package: Lisp -*-
2 ;;; **********************************************************************
3 ;;; 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 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/filesys.lisp,v 1.29 1992/09/04 15:22:16 phg Exp $")
10 ;;;
11 ;;; **********************************************************************
12 ;;;
13 ;;; File system interface functions. This file is pretty UNIX specific.
14 ;;;
15 ;;; Written by William Lott
16 ;;;
17 ;;; **********************************************************************
18
19 (in-package "LISP")
20
21 (export '(truename probe-file user-homedir-pathname directory
22 rename-file delete-file file-write-date file-author))
23
24 (use-package "EXTENSIONS")
25
26 (in-package "EXTENSIONS")
27 (export '(print-directory complete-file ambiguous-files default-directory
28 file-writable unix-namestring))
29 (in-package "LISP")
30
31
32 ;;;; Unix pathname host support.
33
34 ;;; 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
72 (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
100 (defvar *ignore-wildcards* nil)
101
102 (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
169 (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
208 (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 (type index start end))
213 (let ((absolute (and (/= start end)
214 (char= (schar namestr start) char))))
215 (when absolute
216 (incf start))
217 ;; Next, split the remainder into ; separated chunks.
218 (collect ((pieces))
219 (loop
220 (let ((char-pos (position char namestr :start start :end end)))
221 (pieces (cons start (or char-pos end)))
222 (unless char-pos
223 (return))
224 (setf start (1+ char-pos))))
225 (values absolute (pieces)))))
226
227 (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
243 (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 (cond (absolute
287 (cons :absolute (dirs)))
288 ((dirs)
289 (cons :relative (dirs)))
290 (t
291 nil)))
292 name
293 type
294 version)))))
295
296 (defun unparse-unix-host (pathname)
297 (declare (type pathname pathname)
298 (ignore pathname))
299 "Unix")
300
301 (defun unparse-unix-piece (thing)
302 (etypecase thing
303 (keyword
304 (cond ((eq thing ':wild) "*")
305 ((eq thing ':wild-inferiors) "**")
306 (t (error "Invalid keyword piece: ~S~%" thing))))
307 (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 (:wild
334 (strings "*"))
335 (: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
353 (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
380 (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
407 (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
413 (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
469
470 (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
482 (defvar *unix-host* (make-unix-host))
483
484 (defun make-unix-host-load-form (host)
485 (declare (ignore host))
486 '*unix-host*)
487
488
489 ;;;; Wildcard matching stuff.
490
491 (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
503 (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
521 (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 (let ((dir (unix:open-dir head)))
531 (when dir
532 (unwind-protect
533 (loop
534 (let ((name (unix:read-dir dir)))
535 (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 (when (eq (unix:unix-file-kind subdir)
543 :directory)
544 (%enumerate-directories
545 subdir (cdr tail) pathname verify-existance
546 function)))))))
547 (unix:close-dir dir)))))
548 ((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
554 (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 (unix:unix-file-kind directory))
561 (funcall function directory)))
562 ((or (pattern-p name)
563 (pattern-p type)
564 (eq version :wild))
565 (let ((dir (unix:open-dir directory)))
566 (when dir
567 (unwind-protect
568 (loop
569 (let ((file (unix:read-dir dir)))
570 (if file
571 (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 (return))))
587 (unix:close-dir dir)))))
588 (t
589 (let ((file (concatenate 'string directory name)))
590 (unless (or (null type) (eq type :unspecific))
591 (setf file (concatenate 'string file "." type)))
592 (unless (member version '(nil :newest :wild))
593 (setf file (concatenate 'string file "."
594 (quick-integer-to-string version))))
595 (when (or (not verify-existance)
596 (unix:unix-file-kind file))
597 (funcall function file)))))))
598
599 (defun quick-integer-to-string (n)
600 (declare (type integer n))
601 (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 (shrink-vector res (- len i)))
616 (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
622 ;;;; UNIX-NAMESTRING -- public
623 ;;;
624 (defun unix-namestring (pathname &optional (for-input t))
625 "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
638
639 ;;;; TRUENAME and PROBE-FILE.
640
641 ;;; Truename -- Public
642 ;;;
643 ;;; Another silly file function trivially different from another function.
644 ;;;
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 ;;; If PATHNAME exists, return it's truename, otherwise NIL.
656 ;;;
657 (defun probe-file (pathname)
658 "Return a pathname which is the truename of the file if it exists, NIL
659 otherwise."
660 (let ((namestring (unix-namestring pathname t)))
661 (when (and namestring (unix:unix-file-kind namestring))
662 (let ((truename (unix:unix-resolve-links
663 (unix:unix-maybe-prepend-current-directory
664 namestring))))
665 (when truename
666 (let ((*ignore-wildcards* t))
667 (pathname (unix:unix-simplify-pathname truename))))))))
668
669
670 ;;;; Other random operations.
671
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 (let* ((original (truename file))
679 (original-namestring (unix-namestring original t))
680 (new-name (merge-pathnames new-name original))
681 (new-namestring (unix-namestring new-name nil)))
682 (unless original-namestring
683 (error "~S doesn't exist." file))
684 (unless new-namestring
685 (error "~S can't be created." new-name))
686 (multiple-value-bind (res error)
687 (unix:unix-rename original-namestring
688 new-namestring)
689 (unless res
690 (error "Failed to rename ~A to ~A: ~A"
691 original new-name (unix:get-unix-error-msg error)))
692 (when (streamp file)
693 (file-name file new-namestring))
694 (values new-name original (truename new-name)))))
695
696 ;;; Delete-File -- Public
697 ;;;
698 ;;; Delete the file, Man.
699 ;;;
700 (defun delete-file (file)
701 "Delete the specified file."
702 (let ((namestring (unix-namestring file t)))
703 (when (streamp file)
704 (close file :abort t))
705 (unless namestring
706 (error "~S doesn't exist." file))
707
708 (multiple-value-bind (res err) (unix:unix-unlink namestring)
709 (unless res
710 (error "Could not delete ~A: ~A."
711 namestring
712 (unix:get-unix-error-msg err)))))
713 t)
714
715
716 ;;; User-Homedir-Pathname -- Public
717 ;;;
718 ;;; Return Home:, which is set up for us at initialization time.
719 ;;;
720 (defun user-homedir-pathname (&optional host)
721 "Returns the home directory of the logged in user as a pathname.
722 This is obtained from the logical name \"home:\"."
723 (declare (ignore host))
724 #p"home:")
725
726 ;;; File-Write-Date -- Public
727 ;;;
728 (defun file-write-date (file)
729 "Return file's creation date, or NIL if it doesn't exist."
730 (let ((name (unix-namestring file t)))
731 (when name
732 (multiple-value-bind
733 (res dev ino mode nlink uid gid rdev size atime mtime)
734 (unix:unix-stat name)
735 (declare (ignore dev ino mode nlink uid gid rdev size atime))
736 (when res
737 (+ unix-to-universal-time mtime))))))
738
739 ;;; File-Author -- Public
740 ;;;
741 (defun file-author (file)
742 "Returns the file author as a string, or nil if the author cannot be
743 determined. Signals an error if file doesn't exist."
744 (let ((name (unix-namestring (pathname file) t)))
745 (unless name
746 (error "~S doesn't exist." file))
747 (multiple-value-bind (winp dev ino mode nlink uid)
748 (unix:unix-stat file)
749 (declare (ignore dev ino mode nlink))
750 (if winp (lookup-login-name uid)))))
751
752
753
754 ;;;; DIRECTORY.
755
756 ;;; DIRECTORY -- public.
757 ;;;
758 (defun directory (pathname &key (all t) (check-for-subdirs t)
759 (follow-links t))
760 "Returns a list of pathnames, one for each file that matches the given
761 pathname. Supplying :ALL as nil causes this to ignore Unix dot files. This
762 never includes Unix dot and dot-dot in the result. If :FOLLOW-LINKS is NIL,
763 then symblolic links in the result are not expanded. This is not the
764 default because TRUENAME does follow links, and the result pathnames are
765 defined to be the TRUENAME of the pathname (the truename of a link may well
766 be in another directory.)"
767 (let ((results nil))
768 (enumerate-search-list
769 (pathname (merge-pathnames pathname
770 (make-pathname :name :wild
771 :type :wild
772 :version :wild)))
773 (enumerate-matches (name pathname)
774 (when (or all
775 (let ((slash (position #\/ name :from-end t)))
776 (or (null slash)
777 (= (1+ slash) (length name))
778 (char/= (schar name (1+ slash)) #\.))))
779 (push name results))))
780 (let ((*ignore-wildcards* t))
781 (mapcar #'(lambda (name)
782 (let ((name (if (and check-for-subdirs
783 (eq (unix:unix-file-kind name)
784 :directory))
785 (concatenate 'string name "/")
786 name)))
787 (if follow-links (truename name) (pathname name))))
788 (sort (delete-duplicates results :test #'string=) #'string<)))))
789
790
791 ;;;; Printing directories.
792
793 ;;; PRINT-DIRECTORY is exported from the EXTENSIONS package.
794 ;;;
795 (defun print-directory (pathname &optional stream &key all verbose return-list)
796 "Like Directory, but prints a terse, multi-coloumn directory listing
797 instead of returning a list of pathnames. When :all is supplied and
798 non-nil, then Unix dot files are included too (as ls -a). When :vervose
799 is supplied and non-nil, then a long listing of miscellaneous
800 information is output one file per line."
801 (let ((*standard-output* (out-synonym-of stream))
802 (pathname pathname))
803 (if verbose
804 (print-directory-verbose pathname all return-list)
805 (print-directory-formatted pathname all return-list))))
806
807 (defun print-directory-verbose (pathname all return-list)
808 (let ((contents (directory pathname :all all :check-for-subdirs nil
809 :follow-links nil))
810 (result nil))
811 (format t "Directory of ~A :~%" (namestring pathname))
812 (dolist (file contents)
813 (let* ((namestring (unix-namestring file))
814 (tail (subseq namestring
815 (1+ (or (position #\/ namestring
816 :from-end t
817 :test #'char=)
818 -1)))))
819 (multiple-value-bind
820 (reslt dev-or-err ino mode nlink uid gid rdev size atime mtime)
821 (unix:unix-stat namestring)
822 (declare (ignore ino gid rdev atime)
823 (fixnum uid mode))
824 (cond (reslt
825 ;;
826 ;; Print characters for file modes.
827 (macrolet ((frob (bit name &optional sbit sname negate)
828 `(if ,(if negate
829 `(not (logbitp ,bit mode))
830 `(logbitp ,bit mode))
831 ,(if sbit
832 `(if (logbitp ,sbit mode)
833 (write-char ,sname)
834 (write-char ,name))
835 `(write-char ,name))
836 (write-char #\-))))
837 (frob 15 #\d nil nil t)
838 (frob 8 #\r)
839 (frob 7 #\w)
840 (frob 6 #\x 11 #\s)
841 (frob 5 #\r)
842 (frob 4 #\w)
843 (frob 3 #\x 10 #\s)
844 (frob 2 #\r)
845 (frob 1 #\w)
846 (frob 0 #\x))
847 ;;
848 ;; Print the rest.
849 (multiple-value-bind (sec min hour date month year)
850 (get-decoded-time)
851 (declare (ignore sec min hour date month))
852 (format t "~2D ~8A ~8D ~12A ~A~@[/~]~%"
853 nlink
854 (or (lookup-login-name uid) uid)
855 size
856 (decode-universal-time-for-files mtime year)
857 tail
858 (= (logand mode unix:s-ifmt) unix:s-ifdir))))
859 (t (format t "Couldn't stat ~A -- ~A.~%"
860 tail
861 (unix:get-unix-error-msg dev-or-err))))
862 (when return-list
863 (push (if (= (logand mode unix:s-ifmt) unix:s-ifdir)
864 (pathname (concatenate 'string namestring "/"))
865 file)
866 result)))))
867 (nreverse result)))
868
869 (defun decode-universal-time-for-files (time current-year)
870 (multiple-value-bind (sec min hour day month year)
871 (decode-universal-time (+ time unix-to-universal-time))
872 (declare (ignore sec))
873 (format nil "~A ~2,' D ~:[ ~D~;~*~2,'0D:~2,'0D~]"
874 (svref '#("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug"
875 "Sep" "Oct" "Nov" "Dec")
876 (1- month))
877 day (= current-year year) year hour min)))
878
879 (defun print-directory-formatted (pathname all return-list)
880 (let ((width (or (line-length *standard-output*) 80))
881 (names ())
882 (cnt 0)
883 (max-len 0)
884 (result (directory pathname :all all :follow-links nil)))
885 (declare (list names) (fixnum max-len cnt))
886 ;;
887 ;; Get the data.
888 (dolist (file result)
889 (let* ((name (unix-namestring file))
890 (length (length name))
891 (end (if (and (plusp length)
892 (char= (schar name (1- length)) #\/))
893 (1- length)
894 length))
895 (slash-name (subseq name
896 (1+ (or (position #\/ name
897 :from-end t
898 :end end
899 :test #'char=)
900 -1))))
901 (len (length slash-name)))
902 (declare (simple-string slash-name)
903 (fixnum len))
904 (if (> len max-len) (setq max-len len))
905 (incf cnt)
906 (push slash-name names)))
907 (setq names (nreverse names))
908 ;;
909 ;; Do the output.
910 (let* ((col-width (1+ max-len))
911 (cols (max (truncate width col-width) 1))
912 (lines (ceiling cnt cols)))
913 (declare (fixnum cols lines))
914 (format t "Directory of ~A :~%" (namestring pathname))
915 (dotimes (i lines)
916 (declare (fixnum i))
917 (dotimes (j cols)
918 (declare (fixnum j))
919 (let ((name (nth (+ i (the fixnum (* j lines))) names)))
920 (when name
921 (write-string name)
922 (unless (eql j (1- cols))
923 (dotimes (i (- col-width (length (the simple-string name))))
924 (write-char #\space))))))
925 (terpri)))
926 (when return-list
927 result)))
928
929
930
931 ;;;; Translating uid's and gid's.
932
933 (defvar *uid-hash-table* (make-hash-table)
934 "Hash table for keeping track of uid's and login names.")
935
936 ;;; LOOKUP-LOGIN-NAME translates a user id into a login name. Previous
937 ;;; lookups are cached in a hash table since groveling the passwd(s) files
938 ;;; is somewhat expensive. The table may hold nil for id's that cannot
939 ;;; be looked up since this means the files are searched in their entirety
940 ;;; each time this id is translated.
941 ;;;
942 (defun lookup-login-name (uid)
943 (multiple-value-bind (login-name foundp) (gethash uid *uid-hash-table*)
944 (if foundp
945 login-name
946 (setf (gethash uid *uid-hash-table*)
947 (get-group-or-user-name :user uid)))))
948
949 (defvar *gid-hash-table* (make-hash-table)
950 "Hash table for keeping track of gid's and group names.")
951
952 ;;; LOOKUP-GROUP-NAME translates a group id into a group name. Previous
953 ;;; lookups are cached in a hash table since groveling the group(s) files
954 ;;; is somewhat expensive. The table may hold nil for id's that cannot
955 ;;; be looked up since this means the files are searched in their entirety
956 ;;; each time this id is translated.
957 ;;;
958 (defun lookup-group-name (gid)
959 (multiple-value-bind (group-name foundp) (gethash gid *gid-hash-table*)
960 (if foundp
961 group-name
962 (setf (gethash gid *gid-hash-table*)
963 (get-group-or-user-name :group gid)))))
964
965
966 ;;; GET-GROUP-OR-USER-NAME first tries "/etc/passwd" ("/etc/group") since it is
967 ;;; a much smaller file, contains all the local id's, and most uses probably
968 ;;; involve id's on machines one would login into. Then if necessary, we look
969 ;;; in "/etc/passwds" ("/etc/groups") which is really long and has to be
970 ;;; fetched over the net.
971 ;;;
972 (defun get-group-or-user-name (group-or-user id)
973 "Returns the simple-string user or group name of the user whose uid or gid
974 is id, or NIL if no such user or group exists. Group-or-user is either
975 :group or :user."
976 (let ((id-string (let ((*print-base* 10)) (prin1-to-string id))))
977 (declare (simple-string id-string))
978 (multiple-value-bind (file1 file2)
979 (ecase group-or-user
980 (:group (values "/etc/group" "/etc/groups"))
981 (:user (values "/etc/passwd" "/etc/passwd")))
982 (or (get-group-or-user-name-aux id-string file1)
983 (get-group-or-user-name-aux id-string file2)))))
984
985 (defun get-group-or-user-name-aux (id-string passwd-file)
986 (with-open-file (stream passwd-file)
987 (loop
988 (let ((entry (read-line stream nil)))
989 (unless entry (return nil))
990 (let ((name-end (position #\: (the simple-string entry)
991 :test #'char=)))
992 (when name-end
993 (let ((id-start (position #\: (the simple-string entry)
994 :start (1+ name-end) :test #'char=)))
995 (when id-start
996 (incf id-start)
997 (let ((id-end (position #\: (the simple-string entry)
998 :start id-start :test #'char=)))
999 (when (and id-end
1000 (string= id-string entry
1001 :start2 id-start :end2 id-end))
1002 (return (subseq entry 0 name-end))))))))))))
1003
1004
1005 ;;;; File completion.
1006
1007 ;;; COMPLETE-FILE -- Public
1008 ;;;
1009 (defun complete-file (pathname &key (defaults *default-pathname-defaults*)
1010 ignore-types)
1011 (let ((files (directory (complete-file-directory-arg pathname defaults)
1012 :check-for-subdirs nil
1013 :follow-links nil)))
1014 (cond ((null files)
1015 (values nil nil))
1016 ((null (cdr files))
1017 (values (merge-pathnames (file-namestring (car files))
1018 pathname)
1019 t))
1020 (t
1021 (let ((good-files
1022 (delete-if #'(lambda (pathname)
1023 (and (simple-string-p
1024 (pathname-type pathname))
1025 (member (pathname-type pathname)
1026 ignore-types
1027 :test #'string=)))
1028 files)))
1029 (cond ((null good-files))
1030 ((null (cdr good-files))
1031 (return-from complete-file
1032 (values (merge-pathnames (file-namestring
1033 (car good-files))
1034 pathname)
1035 t)))
1036 (t
1037 (setf files good-files)))
1038 (let ((common (file-namestring (car files))))
1039 (dolist (file (cdr files))
1040 (let ((name (file-namestring file)))
1041 (dotimes (i (min (length common) (length name))
1042 (when (< (length name) (length common))
1043 (setf common name)))
1044 (unless (char= (schar common i) (schar name i))
1045 (setf common (subseq common 0 i))
1046 (return)))))
1047 (values (merge-pathnames common pathname)
1048 nil)))))))
1049
1050 ;;; COMPLETE-FILE-DIRECTORY-ARG -- Internal.
1051 ;;;
1052 (defun complete-file-directory-arg (pathname defaults)
1053 (let* ((pathname (merge-pathnames pathname (directory-namestring defaults)))
1054 (type (pathname-type pathname)))
1055 (flet ((append-multi-char-wild (thing)
1056 (etypecase thing
1057 (null :wild)
1058 (pattern
1059 (make-pattern (append (pattern-pieces thing)
1060 (list :multi-char-wild))))
1061 (simple-string
1062 (make-pattern (list thing :multi-char-wild))))))
1063 (if (or (null type) (eq type :unspecific))
1064 ;; There is no type.
1065 (make-pathname :defaults pathname
1066 :name (append-multi-char-wild (pathname-name pathname))
1067 :type :wild)
1068 ;; There already is a type, so just extend it.
1069 (make-pathname :defaults pathname
1070 :name (pathname-name pathname)
1071 :type (append-multi-char-wild (pathname-type pathname)))))))
1072
1073 ;;; Ambiguous-Files -- Public
1074 ;;;
1075 (defun ambiguous-files (pathname
1076 &optional (defaults *default-pathname-defaults*))
1077 "Return a list of all files which are possible completions of Pathname.
1078 We look in the directory specified by Defaults as well as looking down
1079 the search list."
1080 (directory (complete-file-directory-arg pathname defaults)
1081 :follow-links nil
1082 :check-for-subdirs nil))
1083
1084
1085
1086 ;;; File-writable -- exported from extensions.
1087 ;;;
1088 ;;; Determines whether the single argument (which should be a pathname)
1089 ;;; can be written by the the current task.
1090 ;;;
1091 (defun file-writable (name)
1092 "File-writable accepts a pathname and returns T if the current
1093 process can write it, and NIL otherwise."
1094 (let ((name (unix-namestring name nil)))
1095 (cond ((null name)
1096 nil)
1097 ((unix:unix-file-kind name)
1098 (values (unix:unix-access name unix:w_ok)))
1099 (t
1100 (values
1101 (unix:unix-access (subseq name
1102 0
1103 (or (position #\/ name :from-end t)
1104 0))
1105 (logior unix:w_ok unix:x_ok)))))))
1106
1107
1108 ;;; Pathname-Order -- Internal
1109 ;;;
1110 ;;; Predicate to order pathnames by. Goes by name.
1111 ;;;
1112 (defun pathname-order (x y)
1113 (let ((xn (%pathname-name x))
1114 (yn (%pathname-name y)))
1115 (if (and xn yn)
1116 (let ((res (string-lessp xn yn)))
1117 (cond ((not res) nil)
1118 ((= res (length (the simple-string xn))) t)
1119 ((= res (length (the simple-string yn))) nil)
1120 (t t)))
1121 xn)))
1122
1123
1124 ;;; Default-Directory -- Public
1125 ;;;
1126 (defun default-directory ()
1127 "Returns the pathname for the default directory. This is the place where
1128 a file will be written if no directory is specified. This may be changed
1129 with setf."
1130 (multiple-value-bind (gr dir-or-error)
1131 (unix:unix-current-directory)
1132 (if gr
1133 (let ((*ignore-wildcards* t))
1134 (pathname (concatenate 'simple-string dir-or-error "/")))
1135 (error dir-or-error))))
1136
1137 ;;; %Set-Default-Directory -- Internal
1138 ;;;
1139 (defun %set-default-directory (new-val)
1140 (let ((namestring (unix-namestring new-val t)))
1141 (unless namestring
1142 (error "~S doesn't exist." new-val))
1143 (multiple-value-bind (gr error)
1144 (unix:unix-chdir namestring)
1145 (if gr
1146 (setf (search-list "default:") (default-directory))
1147 (error (unix:get-unix-error-msg error))))
1148 new-val))
1149 ;;;
1150 (defsetf default-directory %set-default-directory)
1151
1152 (defun filesys-init ()
1153 (setf *default-pathname-defaults*
1154 (%make-pathname *unix-host* nil nil nil nil :newest))
1155 (setf (search-list "default:") (default-directory))
1156 nil)

  ViewVC Help
Powered by ViewVC 1.1.5