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

Contents of /src/code/filesys.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5