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

Contents of /src/code/filesys.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5