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

Contents of /src/code/filesys.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5