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

Contents of /src/code/filesys.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.98 - (hide annotations)
Mon Nov 7 00:42:25 2005 UTC (8 years, 5 months ago) by rtoy
Branch: MAIN
CVS Tags: snapshot-2005-12
Changes since 1.97: +4 -3 lines
A pathname name may contain a dot if a pathname type is given.
1 ram 1.1 ;;; -*- Log: code.log; Package: Lisp -*-
2     ;;; **********************************************************************
3 ram 1.8 ;;; This code was written as part of the CMU Common Lisp project at
4     ;;; Carnegie Mellon University, and has been placed in the public domain.
5     ;;; If you want to use this code or any part of CMU Common Lisp, please contact
6     ;;; Scott Fahlman or slisp-group@cs.cmu.edu.
7     ;;;
8     (ext:file-comment
9 rtoy 1.98 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/filesys.lisp,v 1.98 2005/11/07 00:42:25 rtoy Exp $")
10 ram 1.8 ;;;
11 ram 1.1 ;;; **********************************************************************
12     ;;;
13 wlott 1.16 ;;; File system interface functions. This file is pretty UNIX specific.
14 ram 1.1 ;;;
15 wlott 1.16 ;;; Written by William Lott
16 ram 1.1 ;;;
17     ;;; **********************************************************************
18 ram 1.2
19     (in-package "LISP")
20    
21 wlott 1.16 (export '(truename probe-file user-homedir-pathname directory
22     rename-file delete-file file-write-date file-author))
23 ram 1.1
24     (use-package "EXTENSIONS")
25    
26     (in-package "EXTENSIONS")
27     (export '(print-directory complete-file ambiguous-files default-directory
28 toy 1.75 purge-backup-files file-writable unix-namestring))
29 ram 1.2 (in-package "LISP")
30 ram 1.1
31 wlott 1.5
32 wlott 1.16 ;;;; Unix pathname host support.
33 ram 1.1
34 wlott 1.16 ;;; Unix namestrings have the following format:
35     ;;;
36     ;;; namestring := [ directory ] [ file [ type [ version ]]]
37     ;;; directory := [ "/" | search-list ] { file "/" }*
38     ;;; search-list := [^:/]*:
39     ;;; file := [^/]*
40     ;;; type := "." [^/.]*
41 pw 1.62 ;;; version := ".*" | ".~" ([0-9]+ | "*") "~"
42 wlott 1.16 ;;;
43 toy 1.73 ;;; Note: this grammar is ambiguous. The string foo.bar.~5~ can be parsed
44 wlott 1.16 ;;; 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 toy 1.73 ;;; - If there is only one dot, it separates the file and the type.
52 wlott 1.16 ;;;
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 toy 1.73 ;;; Any of these special characters can be preceded by a backslash to
69 wlott 1.16 ;;; cause it to be treated as a regular character.
70     ;;;
71 ram 1.1
72 wlott 1.16 (defun remove-backslashes (namestr start end)
73 toy 1.73 "Remove any occurrences of \\ from the string because we've already
74     checked for whatever may have been backslashed."
75 wlott 1.16 (declare (type simple-base-string namestr)
76     (type index start end))
77     (let* ((result (make-string (- end start)))
78     (dst 0)
79     (quoted nil))
80     (do ((src start (1+ src)))
81     ((= src end))
82     (cond (quoted
83     (setf (schar result dst) (schar namestr src))
84     (setf quoted nil)
85     (incf dst))
86     (t
87     (let ((char (schar namestr src)))
88     (cond ((char= char #\\)
89     (setq quoted t))
90     (t
91     (setf (schar result dst) char)
92     (incf dst)))))))
93     (when quoted
94     (error 'namestring-parse-error
95     :complaint "Backslash in bad place."
96     :namestring namestr
97     :offset (1- end)))
98     (shrink-vector result dst)))
99 ram 1.1
100 wlott 1.16 (defvar *ignore-wildcards* nil)
101 ram 1.1
102 wlott 1.16 (defun maybe-make-pattern (namestr start end)
103     (declare (type simple-base-string namestr)
104     (type index start end))
105     (if *ignore-wildcards*
106     (subseq namestr start end)
107     (collect ((pattern))
108     (let ((quoted nil)
109     (any-quotes nil)
110     (last-regular-char nil)
111     (index start))
112     (flet ((flush-pending-regulars ()
113     (when last-regular-char
114     (pattern (if any-quotes
115     (remove-backslashes namestr
116     last-regular-char
117     index)
118     (subseq namestr last-regular-char index)))
119     (setf any-quotes nil)
120     (setf last-regular-char nil))))
121     (loop
122     (when (>= index end)
123     (return))
124     (let ((char (schar namestr index)))
125     (cond (quoted
126     (incf index)
127     (setf quoted nil))
128     ((char= char #\\)
129     (setf quoted t)
130     (setf any-quotes t)
131     (unless last-regular-char
132     (setf last-regular-char index))
133     (incf index))
134     ((char= char #\?)
135     (flush-pending-regulars)
136     (pattern :single-char-wild)
137     (incf index))
138     ((char= char #\*)
139     (flush-pending-regulars)
140     (pattern :multi-char-wild)
141     (incf index))
142     ((char= char #\[)
143     (flush-pending-regulars)
144     (let ((close-bracket
145     (position #\] namestr :start index :end end)))
146     (unless close-bracket
147     (error 'namestring-parse-error
148     :complaint "``['' with no corresponding ``]''"
149     :namestring namestr
150     :offset index))
151     (pattern (list :character-set
152     (subseq namestr
153     (1+ index)
154     close-bracket)))
155     (setf index (1+ close-bracket))))
156     (t
157     (unless last-regular-char
158     (setf last-regular-char index))
159     (incf index)))))
160     (flush-pending-regulars)))
161     (cond ((null (pattern))
162     "")
163 ram 1.31 ((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 wlott 1.16 (t
171     (make-pattern (pattern)))))))
172 ram 1.1
173 dtc 1.59 ;;; extract-name-type-and-version -- Internal.
174     ;;;
175 wlott 1.16 (defun extract-name-type-and-version (namestr start end)
176     (declare (type simple-base-string namestr)
177     (type index start end))
178 pw 1.62 (labels
179     ((explicit-version (namestr start end)
180 rtoy 1.82 ;; Look for something like "~*~" at the end of the
181     ;; namestring, where * can be #\* or some digits. This
182     ;; denotes a version.
183 rtoy 1.92 ;;(format t "explicit-version ~S ~A ~A~%" namestr start end)
184 rtoy 1.82 (cond ((or (< (- end start) 4)
185 rtoy 1.84 (and (char/= (schar namestr (1- end)) #\~)
186     (char/= (schar namestr (1- end)) #\*)))
187 toy 1.74 ;; No explicit version given, so return NIL to
188     ;; indicate we don't want file versions, unless
189     ;; requested in other ways.
190 rtoy 1.92 ;;(format t "case 1: ~A ~A~%" nil end)
191 toy 1.74 (values nil end))
192 rtoy 1.84 ((and (not *ignore-wildcards*)
193     (char= (schar namestr (- end 2)) #\*)
194 pw 1.62 (char= (schar namestr (- end 3)) #\~)
195     (char= (schar namestr (- end 4)) #\.))
196 rtoy 1.82 ;; Found "~*~", so it's a wild version
197 rtoy 1.92 ;;(format t "case 2: ~A ~A~%" :wild (- end 4))
198 pw 1.62 (values :wild (- end 4)))
199     (t
200 rtoy 1.82 ;; Look for a version number. Start at the end, just
201     ;; before the ~ and keep looking for digits. If the
202     ;; first non-digit is ~, we have a version number, so
203     ;; get it. If not, we didn't find a version number,
204     ;; so we call it :newest
205 pw 1.62 (do ((i (- end 2) (1- i)))
206 rtoy 1.92 ((< i (+ start 1))
207     ;;(format t "case 3: ~A ~A~%" :newest end)
208     (values :newest end))
209 pw 1.62 (let ((char (schar namestr i)))
210     (when (eql char #\~)
211     (return (if (char= (schar namestr (1- i)) #\.)
212     (values (parse-integer namestr :start (1+ i)
213     :end (1- end))
214     (1- i))
215     (values :newest end))))
216     (unless (char<= #\0 char #\9)
217 rtoy 1.92 ;; It's not a digit. Give up, and say the
218     ;; version is NIL.
219     ;;(format t "case 3 return: ~A ~A~%" nil end)
220     (return (values nil end))))))))
221 pw 1.62 (any-version (namestr start end)
222     ;; process end of string looking for a version candidate.
223     (multiple-value-bind (version where)
224 toy 1.73 (explicit-version namestr start end)
225 pw 1.62 (cond ((not (eq version :newest))
226     (values version where))
227 rtoy 1.84 ((and (not *ignore-wildcards*)
228     (>= (- end 2) start)
229 pmai 1.65 (char= (schar namestr (- end 1)) #\*)
230 pw 1.62 (char= (schar namestr (- end 2)) #\.)
231 pw 1.63 (find #\. namestr
232     :start (min (1+ start) (- end 2))
233     :end (- end 2)))
234 pw 1.62 (values :wild (- end 2)))
235     (t (values version where)))))
236     (any-type (namestr start end)
237 pw 1.63 ;; Process end of string looking for a type. A leading "."
238     ;; is part of the name.
239     (let ((where (position #\. namestr
240     :start (min (1+ start) end)
241     :end end :from-end t)))
242 pw 1.62 (when where
243     (values where end))))
244     (any-name (namestr start end)
245     (declare (ignore namestr))
246     (values start end)))
247 toy 1.73 (multiple-value-bind (version vstart)
248     (any-version namestr start end)
249     (multiple-value-bind (tstart tend)
250     (any-type namestr start vstart)
251     (multiple-value-bind (nstart nend)
252     (any-name namestr start (or tstart vstart))
253 pw 1.62 (values
254     (maybe-make-pattern namestr nstart nend)
255     (and tstart (maybe-make-pattern namestr (1+ tstart) tend))
256     version))))))
257 ram 1.1
258 ram 1.31 ;;; Take a string and return a list of cons cells that mark the char
259     ;;; separated subseq. The first value t if absolute directories location.
260     ;;;
261     (defun split-at-slashes (namestr start end)
262     (declare (type simple-base-string namestr)
263 wlott 1.16 (type index start end))
264     (let ((absolute (and (/= start end)
265 ram 1.31 (char= (schar namestr start) #\/))))
266 wlott 1.16 (when absolute
267     (incf start))
268 toy 1.73 ;; Next, split the remainder into slash separated chunks.
269 wlott 1.16 (collect ((pieces))
270     (loop
271 ram 1.31 (let ((slash (position #\/ namestr :start start :end end)))
272     (pieces (cons start (or slash end)))
273     (unless slash
274 wlott 1.16 (return))
275 ram 1.31 (setf start (1+ slash))))
276 wlott 1.16 (values absolute (pieces)))))
277 ram 1.1
278 wlott 1.16 (defun maybe-extract-search-list (namestr start end)
279     (declare (type simple-base-string namestr)
280     (type index start end))
281     (let ((quoted nil))
282     (do ((index start (1+ index)))
283     ((= index end)
284     (values nil start))
285     (if quoted
286     (setf quoted nil)
287     (case (schar namestr index)
288     (#\\
289     (setf quoted t))
290     (#\:
291     (return (values (remove-backslashes namestr start index)
292     (1+ index)))))))))
293 ram 1.1
294 wlott 1.16 (defun parse-unix-namestring (namestr start end)
295     (declare (type simple-base-string namestr)
296     (type index start end))
297     (multiple-value-bind
298     (absolute pieces)
299     (split-at-slashes namestr start end)
300     (let ((search-list
301     (if absolute
302     nil
303     (let ((first (car pieces)))
304     (multiple-value-bind
305 toy 1.73 (search-list new-start)
306 wlott 1.16 (maybe-extract-search-list namestr
307     (car first) (cdr first))
308     (when search-list
309 rtoy 1.93 ;; Lose if this search-list is already defined as
310     ;; a logical host. Since the syntax for
311     ;; search-lists and logical pathnames are the
312     ;; same, we can't allow the creation of one when
313     ;; the other is defined.
314     (when (find-logical-host search-list nil)
315     (error "~A already names a logical host" search-list))
316 wlott 1.16 (setf absolute t)
317     (setf (car first) new-start))
318     search-list)))))
319 rtoy 1.90 (multiple-value-bind (name type version)
320 wlott 1.16 (let* ((tail (car (last pieces)))
321     (tail-start (car tail))
322     (tail-end (cdr tail)))
323     (unless (= tail-start tail-end)
324     (setf pieces (butlast pieces))
325 rtoy 1.90 (cond ((string= namestr ".." :start1 tail-start :end1 tail-end)
326     ;; ".." is a directory. Add this piece to the
327     ;; list of pieces, and make the name/type/version
328     ;; nil.
329     (setf pieces (append pieces (list (cons tail-start tail-end))))
330     (values nil nil nil))
331     ((not (find-if-not #'(lambda (c)
332     (char= c #\.))
333     namestr :start tail-start :end tail-end))
334     ;; Got a bunch of dots. Make it a file of the same name, and type nil.
335     (values (subseq namestr tail-start tail-end) nil nil))
336     (t
337     (extract-name-type-and-version namestr tail-start tail-end)))))
338 dtc 1.54 ;; PVE: Make sure there are no illegal characters in the name
339     ;; such as #\Null and #\/.
340     (when (and (stringp name)
341     (find-if #'(lambda (x)
342     (or (char= x #\Null) (char= x #\/)))
343     name))
344     (error 'parse-error))
345 wlott 1.16 ;; Now we have everything we want. So return it.
346     (values nil ; no host for unix namestrings.
347     nil ; no devices for unix namestrings.
348     (collect ((dirs))
349     (when search-list
350     (dirs (intern-search-list search-list)))
351     (dolist (piece pieces)
352     (let ((piece-start (car piece))
353     (piece-end (cdr piece)))
354     (unless (= piece-start piece-end)
355 ram 1.31 (cond ((string= namestr ".." :start1 piece-start
356     :end1 piece-end)
357     (dirs :up))
358     ((string= namestr "**" :start1 piece-start
359     :end1 piece-end)
360     (dirs :wild-inferiors))
361     (t
362     (dirs (maybe-make-pattern namestr
363     piece-start
364     piece-end)))))))
365 wlott 1.25 (cond (absolute
366     (cons :absolute (dirs)))
367     ((dirs)
368 rtoy 1.89 ;; "." in a :relative directory is the same
369     ;; as if it weren't there, so remove them.
370     (cons :relative (delete "." (dirs) :test #'equal)))
371 wlott 1.25 (t
372 rtoy 1.89 ;; If there is no directory and the name is
373     ;; ".", we really got directory ".", so make it so.
374     (if (equal name ".")
375     (list :relative)
376     nil))))
377     ;; A file with name "." can't be the name of file on
378     ;; Unix because it's a directory. This was handled
379     ;; above, so we can just set the name to nil.
380     (if (equal name ".")
381     nil
382     name)
383 wlott 1.16 type
384     version)))))
385 ram 1.1
386 wlott 1.16 (defun unparse-unix-host (pathname)
387     (declare (type pathname pathname)
388     (ignore pathname))
389 toy 1.69 ;; this host designator needs to be recognized as a physical host in
390     ;; PARSE-NAMESTRING. Until sbcl-0.7.3.x, we had "Unix" here, but
391     ;; that's a valid Logical Hostname, so that's a bad choice. -- CSR,
392     "")
393 ram 1.1
394 wlott 1.16 (defun unparse-unix-piece (thing)
395     (etypecase thing
396 ram 1.31 ((member :wild) "*")
397 rtoy 1.86 ((member :unspecific)
398     ;; CLHS 19.2.2.2.3.1 says "That is, both nil and :unspecific
399     ;; cause the component not to appear in the namestring."
400     "")
401 wlott 1.16 (simple-string
402     (let* ((srclen (length thing))
403     (dstlen srclen))
404     (dotimes (i srclen)
405     (case (schar thing i)
406     ((#\* #\? #\[)
407     (incf dstlen))))
408     (let ((result (make-string dstlen))
409     (dst 0))
410     (dotimes (src srclen)
411     (let ((char (schar thing src)))
412     (case char
413     ((#\* #\? #\[)
414     (setf (schar result dst) #\\)
415     (incf dst)))
416     (setf (schar result dst) char)
417     (incf dst)))
418     result)))
419     (pattern
420     (collect ((strings))
421     (dolist (piece (pattern-pieces thing))
422     (etypecase piece
423     (simple-string
424     (strings piece))
425     (symbol
426 ram 1.31 (ecase piece
427 wlott 1.16 (:multi-char-wild
428     (strings "*"))
429     (:single-char-wild
430 ram 1.31 (strings "?"))))
431 wlott 1.16 (cons
432     (case (car piece)
433     (:character-set
434     (strings "[")
435 rtoy 1.81 (strings (second piece))
436 wlott 1.16 (strings "]"))
437     (t
438     (error "Invalid pattern piece: ~S" piece))))))
439     (apply #'concatenate
440     'simple-string
441     (strings))))))
442 ram 1.1
443 wlott 1.16 (defun unparse-unix-directory-list (directory)
444     (declare (type list directory))
445     (collect ((pieces))
446     (when directory
447     (ecase (pop directory)
448     (:absolute
449     (cond ((search-list-p (car directory))
450     (pieces (search-list-name (pop directory)))
451     (pieces ":"))
452     (t
453     (pieces "/"))))
454     (:relative
455 rtoy 1.89 ;; Nothing special, except if we were given '(:relative).
456     (unless directory
457     (pieces "./"))
458 wlott 1.16 ))
459     (dolist (dir directory)
460     (typecase dir
461     ((member :up)
462     (pieces "../"))
463     ((member :back)
464     (error ":BACK cannot be represented in namestrings."))
465 ram 1.31 ((member :wild-inferiors)
466     (pieces "**/"))
467 rtoy 1.85 ((or simple-string pattern (eql :wild))
468 wlott 1.16 (pieces (unparse-unix-piece dir))
469     (pieces "/"))
470     (t
471     (error "Invalid directory component: ~S" dir)))))
472     (apply #'concatenate 'simple-string (pieces))))
473 ram 1.1
474 wlott 1.16 (defun unparse-unix-directory (pathname)
475     (declare (type pathname pathname))
476     (unparse-unix-directory-list (%pathname-directory pathname)))
477 toy 1.64
478 wlott 1.16 (defun unparse-unix-file (pathname)
479     (declare (type pathname pathname))
480     (collect ((strings))
481     (let* ((name (%pathname-name pathname))
482     (type (%pathname-type pathname))
483     (type-supplied (not (or (null type) (eq type :unspecific))))
484 toy 1.64 (logical-p (logical-pathname-p pathname))
485 wlott 1.16 (version (%pathname-version pathname))
486 rtoy 1.93 ;; Preserve version :newest for logical pathnames.
487 toy 1.73 (version-supplied (not (or (null version)
488 rtoy 1.93 (member version (if logical-p
489     '(:unspecific)
490     '(:newest
491     :unspecific)))))))
492 wlott 1.16 (when name
493 rtoy 1.97 (when (stringp name)
494     (when (find #\/ name)
495     (error "Cannot specify a directory separator in a pathname name: ~S" name))
496 rtoy 1.98 (when (and (not type-supplied)
497     (find #\. name))
498     (error "Cannot specify a dot in a pathname name without a pathname type: ~S" name))
499 rtoy 1.97 (when (or (string= ".." name)
500     (string= "." name))
501     (error "Invalid value for a pathname name: ~S" name)))
502 wlott 1.16 (strings (unparse-unix-piece name)))
503     (when type-supplied
504     (unless name
505     (error "Cannot specify the type without a file: ~S" pathname))
506 rtoy 1.97 (when (stringp type)
507     (when (find #\/ type)
508     (error "Cannot specify a directory separator in a pathname type: ~S" type))
509     (when (find #\. type)
510     (error "Cannot specify a dot in a pathname type: ~S" type)))
511 wlott 1.16 (strings ".")
512     (strings (unparse-unix-piece type)))
513 rtoy 1.96 (when (and (not (member version '(nil :newest :unspecific)))
514     (not name))
515 rtoy 1.82 ;; We don't want version without a name, because when we try
516     ;; to read #p".~*~" back, the name is "", not NIL.
517     (error "Cannot specify a version without a file: ~S" pathname))
518 wlott 1.16 (when version-supplied
519     (strings (if (eq version :wild)
520 toy 1.64 (if logical-p ".*" ".~*~")
521 rtoy 1.93 (format nil (if logical-p ".~A" ".~~~D~~")
522 toy 1.64 version)))))
523 pw 1.55 (and (strings) (apply #'concatenate 'simple-string (strings)))))
524 ram 1.1
525 wlott 1.16 (defun unparse-unix-namestring (pathname)
526     (declare (type pathname pathname))
527     (concatenate 'simple-string
528     (unparse-unix-directory pathname)
529     (unparse-unix-file pathname)))
530 ram 1.1
531 wlott 1.16 (defun unparse-unix-enough (pathname defaults)
532     (declare (type pathname pathname defaults))
533     (flet ((lose ()
534     (error "~S cannot be represented relative to ~S"
535     pathname defaults)))
536 rtoy 1.91 ;; Only the first path in a search-list is considered.
537     (enumerate-search-list (pathname pathname)
538     (enumerate-search-list (defaults defaults)
539     (collect ((strings))
540     (let* ((pathname-directory (%pathname-directory pathname))
541     (defaults-directory (%pathname-directory defaults))
542     (prefix-len (length defaults-directory))
543     (result-dir
544     (cond ((null pathname-directory)
545 rtoy 1.92 ;; No directory, so relative to default. But
546     ;; if we're relative to default, NIL is as
547     ;; good as '(:relative) and it results in a
548     ;; shorter namestring.
549     #+nil (list :relative)
550     nil)
551 rtoy 1.91 ((and (>= prefix-len 1)
552 toy 1.72 (>= (length pathname-directory) prefix-len)
553     (compare-component (subseq pathname-directory
554     0 prefix-len)
555     defaults-directory))
556 rtoy 1.94 ;; Pathname starts with a prefix of default,
557     ;; which also means both are either :relative
558     ;; or :absolute directories. So just use a
559     ;; relative directory from then on out.
560     (let ((dir-tail (nthcdr prefix-len pathname-directory)))
561     ;; If both directories are identical, don't
562     ;; return just :relative. Returning NIL
563     ;; results in a shorter string.
564     (if dir-tail
565     (cons :relative dir-tail)
566     nil)))
567     ((and (eq (car pathname-directory) :relative)
568     (not (eq (car defaults-directory) :absolute)))
569     ;; Can't represent a relative directory
570     ;; relative to an absolute directory. But
571     ;; there's no problem if both are relative;
572     ;; we just return our path.
573     pathname-directory)
574 rtoy 1.91 ((eq (car pathname-directory) :absolute)
575     ;; We are an absolute pathname, so we can just use it.
576     pathname-directory)
577     (t
578     ;; We are a relative directory. So we lose.
579     (lose)))))
580     (strings (unparse-unix-directory-list result-dir)))
581     (let* ((pathname-version (%pathname-version pathname))
582     (version-needed (and pathname-version
583     (not (eq pathname-version :newest))))
584     (pathname-type (%pathname-type pathname))
585     (type-needed (or version-needed
586     (and pathname-type
587     (not (eq pathname-type :unspecific)))))
588     (pathname-name (%pathname-name pathname))
589     (name-needed (or type-needed
590     (and pathname-name
591     (not (compare-component pathname-name
592     (%pathname-name
593     defaults)))))))
594     (when name-needed
595     (unless pathname-name (lose))
596     (strings (unparse-unix-piece pathname-name)))
597     (when type-needed
598     (when (or (null pathname-type) (eq pathname-type :unspecific))
599     (lose))
600     (strings ".")
601     (strings (unparse-unix-piece pathname-type)))
602     (when version-needed
603     (typecase pathname-version
604     ((member :wild)
605     (strings ".~*~"))
606     (integer
607     (strings (format nil ".~~~D~~" pathname-version)))
608     (t
609     (lose)))))
610     (return-from unparse-unix-enough (apply #'concatenate 'simple-string (strings))))))))
611 ram 1.1
612    
613 wlott 1.16 (defstruct (unix-host
614     (:include host
615     (:parse #'parse-unix-namestring)
616     (:unparse #'unparse-unix-namestring)
617     (:unparse-host #'unparse-unix-host)
618     (:unparse-directory #'unparse-unix-directory)
619     (:unparse-file #'unparse-unix-file)
620     (:unparse-enough #'unparse-unix-enough)
621     (:customary-case :lower))
622     (:make-load-form-fun make-unix-host-load-form))
623     )
624 ram 1.1
625 wlott 1.16 (defvar *unix-host* (make-unix-host))
626 ram 1.1
627 wlott 1.16 (defun make-unix-host-load-form (host)
628     (declare (ignore host))
629     '*unix-host*)
630 ram 1.1
631 wlott 1.16
632     ;;;; Wildcard matching stuff.
633 ram 1.1
634 wlott 1.16 (defmacro enumerate-matches ((var pathname &optional result
635 dtc 1.57 &key (verify-existance t) (follow-links t))
636 wlott 1.16 &body body)
637     (let ((body-name (gensym)))
638     `(block nil
639     (flet ((,body-name (,var)
640     ,@body))
641     (%enumerate-matches (pathname ,pathname)
642 dtc 1.57 ,verify-existance ,follow-links
643 wlott 1.16 #',body-name)
644     ,result))))
645 ram 1.1
646 dtc 1.57 (defun %enumerate-matches (pathname verify-existance follow-links function)
647 ram 1.34 (when (pathname-type pathname)
648     (unless (pathname-name pathname)
649     (error "Cannot supply a type without a name:~% ~S" pathname)))
650     (let ((directory (pathname-directory pathname)))
651     (if directory
652     (ecase (car directory)
653     (:absolute
654     (%enumerate-directories "/" (cdr directory) pathname
655 dtc 1.57 verify-existance follow-links
656     nil function))
657 ram 1.34 (:relative
658     (%enumerate-directories "" (cdr directory) pathname
659 dtc 1.57 verify-existance follow-links
660     nil function)))
661 ram 1.34 (%enumerate-files "" pathname verify-existance function))))
662 ram 1.1
663 dtc 1.57 ;;; %enumerate-directories -- Internal
664     ;;;
665     ;;; The directory node and device numbers are maintained for the current path
666 toy 1.73 ;;; during the search for the detection of path loops upon :wild-inferiors.
667 dtc 1.57 ;;;
668     (defun %enumerate-directories (head tail pathname verify-existance
669     follow-links nodes function)
670 ram 1.37 (declare (simple-string head))
671 dtc 1.57 (macrolet ((unix-xstat (name)
672     `(if follow-links
673     (unix:unix-stat ,name)
674     (unix:unix-lstat ,name)))
675     (with-directory-node-noted ((head) &body body)
676     `(multiple-value-bind (res dev ino mode)
677     (unix-xstat ,head)
678     (when (and res (eql (logand mode unix:s-ifmt) unix:s-ifdir))
679     (let ((nodes (cons (cons dev ino) nodes)))
680     ,@body))))
681     (do-directory-entries ((name directory) &body body)
682     `(let ((dir (unix:open-dir ,directory)))
683     (when dir
684     (unwind-protect
685     (loop
686     (let ((,name (unix:read-dir dir)))
687     (cond ((null ,name)
688     (return))
689     ((string= ,name "."))
690     ((string= ,name ".."))
691     (t
692     ,@body))))
693     (unix:close-dir dir))))))
694     (if tail
695     (let ((piece (car tail)))
696     (etypecase piece
697     (simple-string
698     (let ((head (concatenate 'string head piece)))
699     (with-directory-node-noted (head)
700     (%enumerate-directories (concatenate 'string head "/")
701     (cdr tail) pathname
702     verify-existance follow-links
703     nodes function))))
704     ((member :wild-inferiors)
705     (%enumerate-directories head (rest tail) pathname
706     verify-existance follow-links
707     nodes function)
708     (do-directory-entries (name head)
709     (let ((subdir (concatenate 'string head name)))
710     (multiple-value-bind (res dev ino mode)
711     (unix-xstat subdir)
712     (declare (type (or fixnum null) mode))
713     (when (and res (eql (logand mode unix:s-ifmt) unix:s-ifdir))
714     (unless (dolist (dir nodes nil)
715     (when (and (eql (car dir) dev)
716     (eql (cdr dir) ino))
717     (return t)))
718     (let ((nodes (cons (cons dev ino) nodes))
719     (subdir (concatenate 'string subdir "/")))
720     (%enumerate-directories subdir tail pathname
721     verify-existance follow-links
722     nodes function))))))))
723     ((or pattern (member :wild))
724     (do-directory-entries (name head)
725     (when (or (eq piece :wild) (pattern-matches piece name))
726     (let ((subdir (concatenate 'string head name)))
727     (multiple-value-bind (res dev ino mode)
728     (unix-xstat subdir)
729     (declare (type (or fixnum null) mode))
730     (when (and res
731     (eql (logand mode unix:s-ifmt) unix:s-ifdir))
732     (let ((nodes (cons (cons dev ino) nodes))
733     (subdir (concatenate 'string subdir "/")))
734     (%enumerate-directories subdir (rest tail) pathname
735     verify-existance follow-links
736     nodes function))))))))
737     ((member :up)
738     (let ((head (concatenate 'string head "..")))
739     (with-directory-node-noted (head)
740     (%enumerate-directories (concatenate 'string head "/")
741     (rest tail) pathname
742     verify-existance follow-links
743     nodes function))))))
744     (%enumerate-files head pathname verify-existance function))))
745 ram 1.1
746 wlott 1.16 (defun %enumerate-files (directory pathname verify-existance function)
747 ram 1.37 (declare (simple-string directory))
748 ram 1.36 (let ((name (%pathname-name pathname))
749     (type (%pathname-type pathname))
750     (version (%pathname-version pathname)))
751 ram 1.33 (cond ((member name '(nil :unspecific))
752 wlott 1.16 (when (or (not verify-existance)
753 wlott 1.26 (unix:unix-file-kind directory))
754 wlott 1.16 (funcall function directory)))
755     ((or (pattern-p name)
756     (pattern-p type)
757 ram 1.33 (eq name :wild)
758 rtoy 1.84 (eq type :wild)
759     (eq version :wild))
760 wlott 1.26 (let ((dir (unix:open-dir directory)))
761 wlott 1.16 (when dir
762     (unwind-protect
763     (loop
764 wlott 1.26 (let ((file (unix:read-dir dir)))
765 wlott 1.16 (if file
766 wlott 1.21 (unless (or (string= file ".")
767     (string= file ".."))
768     (multiple-value-bind
769     (file-name file-type file-version)
770     (let ((*ignore-wildcards* t))
771     (extract-name-type-and-version
772     file 0 (length file)))
773 toy 1.76 ;; Match also happens if the file has
774     ;; no explicit version and we're asking
775     ;; for version :NEWEST, since that's
776     ;; what no version means.
777 wlott 1.21 (when (and (components-match file-name name)
778     (components-match file-type type)
779 toy 1.76 (or (components-match file-version
780     version)
781     (and (eq file-version nil)
782     (eq version :newest))))
783 wlott 1.21 (funcall function
784     (concatenate 'string
785     directory
786     file)))))
787 wlott 1.16 (return))))
788 wlott 1.26 (unix:close-dir dir)))))
789 wlott 1.16 (t
790     (let ((file (concatenate 'string directory name)))
791 wlott 1.19 (unless (or (null type) (eq type :unspecific))
792 wlott 1.16 (setf file (concatenate 'string file "." type)))
793 toy 1.68 (unless (member version '(nil :newest :wild :unspecific))
794 toy 1.66 (setf file (concatenate 'string file ".~"
795     (quick-integer-to-string version)
796     "~")))
797 wlott 1.16 (when (or (not verify-existance)
798 ram 1.40 (unix:unix-file-kind file t))
799 wlott 1.16 (funcall function file)))))))
800 ram 1.1
801     (defun quick-integer-to-string (n)
802 wlott 1.16 (declare (type integer n))
803 ram 1.38 (cond ((not (fixnump n))
804     (write-to-string n :base 10 :radix nil))
805     ((zerop n) "0")
806 ram 1.1 ((eql n 1) "1")
807     ((minusp n)
808     (concatenate 'simple-string "-"
809     (the simple-string (quick-integer-to-string (- n)))))
810     (t
811     (do* ((len (1+ (truncate (integer-length n) 3)))
812     (res (make-string len))
813     (i (1- len) (1- i))
814     (q n)
815     (r 0))
816     ((zerop q)
817     (incf i)
818     (replace res res :start2 i :end2 len)
819 wlott 1.3 (shrink-vector res (- len i)))
820 ram 1.1 (declare (simple-string res)
821 ram 1.38 (fixnum len i r q))
822 ram 1.1 (multiple-value-setq (q r) (truncate q 10))
823     (setf (schar res i) (schar "0123456789" r))))))
824    
825 wlott 1.16
826     ;;;; UNIX-NAMESTRING -- public
827 wlott 1.5 ;;;
828 wlott 1.39 (defun unix-namestring (pathname &optional (for-input t) executable-only)
829 wlott 1.16 "Convert PATHNAME into a string that can be used with UNIX system calls.
830 pw 1.56 Search-lists and wild-cards are expanded. If optional argument
831     FOR-INPUT is true and PATHNAME doesn't exist, NIL is returned.
832     If optional argument EXECUTABLE-ONLY is true, NIL is returned
833     unless an executable version of PATHNAME exists."
834 ram 1.41 ;; toy@rtp.ericsson.se: Let unix-namestring also handle logical
835     ;; pathnames too.
836     (let ((path (let ((lpn (pathname pathname)))
837     (if (logical-pathname-p lpn)
838     (namestring (translate-logical-pathname lpn))
839     pathname))))
840 wlott 1.16 (enumerate-search-list
841 ram 1.41 (pathname path)
842 wlott 1.16 (collect ((names))
843 dtc 1.57 (enumerate-matches (name pathname nil :verify-existance for-input
844     :follow-links t)
845 wlott 1.39 (when (or (not executable-only)
846     (and (eq (unix:unix-file-kind name) :file)
847     (unix:unix-access name unix:x_ok)))
848     (names name)))
849 wlott 1.16 (let ((names (names)))
850     (when names
851     (when (cdr names)
852 dtc 1.54 (error 'simple-file-error
853     :format-control "~S is ambiguous:~{~% ~A~}"
854     :format-arguments (list pathname names)))
855 ram 1.41 (return (car names))))))))
856 wlott 1.4
857 ram 1.1
858 wlott 1.16 ;;;; TRUENAME and PROBE-FILE.
859 ram 1.1
860     ;;; Truename -- Public
861     ;;;
862 wlott 1.4 ;;; Another silly file function trivially different from another function.
863 ram 1.1 ;;;
864     (defun truename (pathname)
865     "Return the pathname for the actual file described by the pathname
866 pw 1.47 An error of type file-error is signalled if no such file exists,
867     or the pathname is wild."
868     (if (wild-pathname-p pathname)
869 pw 1.51 (error 'simple-file-error
870 pw 1.47 :format-control "Bad place for a wild pathname."
871     :pathname pathname)
872     (let ((result (probe-file pathname)))
873     (unless result
874 pw 1.51 (error 'simple-file-error
875 pw 1.47 :pathname pathname
876     :format-control "The file ~S does not exist."
877     :format-arguments (list (namestring pathname))))
878     result)))
879 ram 1.1
880     ;;; Probe-File -- Public
881     ;;;
882 toy 1.71 ;;; If PATHNAME exists, return its truename, otherwise NIL.
883 ram 1.1 ;;;
884     (defun probe-file (pathname)
885     "Return a pathname which is the truename of the file if it exists, NIL
886 toy 1.73 otherwise. An error of type file-error is signalled if pathname is wild."
887 pw 1.47 (if (wild-pathname-p pathname)
888 pw 1.51 (error 'simple-file-error
889 pw 1.47 :pathname pathname
890     :format-control "Bad place for a wild pathname.")
891 toy 1.78 (let ((namestring (unix-namestring (merge-pathnames pathname) t)))
892 pw 1.47 (when (and namestring (unix:unix-file-kind namestring))
893     (let ((truename (unix:unix-resolve-links
894     (unix:unix-maybe-prepend-current-directory
895     namestring))))
896     (when truename
897     (let ((*ignore-wildcards* t))
898     (pathname (unix:unix-simplify-pathname truename)))))))))
899 ram 1.1
900    
901 wlott 1.4 ;;;; Other random operations.
902 ram 1.1
903     ;;; Rename-File -- Public
904     ;;;
905     (defun rename-file (file new-name)
906     "Rename File to have the specified New-Name. If file is a stream open to a
907 ram 1.35 file, then the associated file is renamed."
908 wlott 1.4 (let* ((original (truename file))
909 wlott 1.16 (original-namestring (unix-namestring original t))
910 wlott 1.4 (new-name (merge-pathnames new-name original))
911     (new-namestring (unix-namestring new-name nil)))
912 wlott 1.17 (unless new-namestring
913 pw 1.51 (error 'simple-file-error
914 pw 1.47 :pathname new-name
915     :format-control "~S can't be created."
916     :format-arguments (list new-name)))
917 wlott 1.4 (multiple-value-bind (res error)
918 wlott 1.26 (unix:unix-rename original-namestring
919 wlott 1.4 new-namestring)
920     (unless res
921 pw 1.51 (error 'simple-file-error
922 pw 1.47 :pathname new-name
923     :format-control "Failed to rename ~A to ~A: ~A"
924     :format-arguments (list original new-name
925     (unix:get-unix-error-msg error))))
926 wlott 1.4 (when (streamp file)
927     (file-name file new-namestring))
928 wlott 1.16 (values new-name original (truename new-name)))))
929 ram 1.1
930     ;;; Delete-File -- Public
931     ;;;
932     ;;; Delete the file, Man.
933     ;;;
934     (defun delete-file (file)
935     "Delete the specified file."
936 wlott 1.4 (let ((namestring (unix-namestring file t)))
937 ram 1.1 (when (streamp file)
938 rtoy 1.79 ;; Close the file, but don't try to revert or anything. We want
939     ;; to delete it, man!
940     (close file))
941 ram 1.24 (unless namestring
942 pw 1.51 (error 'simple-file-error
943 pw 1.47 :pathname file
944     :format-control "~S doesn't exist."
945     :format-arguments (list file)))
946 ram 1.24
947 wlott 1.26 (multiple-value-bind (res err) (unix:unix-unlink namestring)
948 ram 1.24 (unless res
949 pw 1.51 (error 'simple-file-error
950 pw 1.47 :pathname namestring
951     :format-control "Could not delete ~A: ~A."
952     :format-arguments (list namestring
953     (unix:get-unix-error-msg err))))))
954 ram 1.1 t)
955 wlott 1.4
956 toy 1.75 ;;; Purge-Backup-Files -- Public
957 toy 1.73 ;;;
958     ;;; Purge old file versions
959     ;;;
960 toy 1.75 (defun purge-backup-files (pathname &optional (keep 0))
961 toy 1.73 "Delete old versions of files matching the given Pathname,
962     optionally keeping some of the most recent old versions."
963     (declare (type (or pathname string stream) pathname)
964     (type (integer 0 *) keep))
965     (let ((hash (make-hash-table :test 'equal)))
966     (enumerate-search-list
967     (path (make-pathname :version :wild :defaults pathname))
968     (clrhash hash)
969     (enumerate-matches (name path nil :follow-links nil)
970     (let ((dot (position #\. name :from-end t))
971     (len (length name)))
972     (when (and dot
973     (> len (+ dot 3))
974     (char= (char name (1+ dot)) #\~)
975     (char= (char name (1- len)) #\~)
976     (eq (unix:unix-file-kind name) :file))
977     (multiple-value-bind (version next)
978     (parse-integer name :start (+ dot 2) :end (1- len)
979     :junk-allowed t)
980     (when (and version (= next (1- len)))
981     (push (cons version name)
982     (gethash (subseq name 0 dot) hash '())))))))
983     (maphash (lambda (key value)
984     (declare (ignore key))
985     (mapc #'unix:unix-unlink
986     (mapcar #'cdr (nthcdr keep
987     (sort value #'> :key #'car)))))
988     hash))))
989    
990 ram 1.1
991     ;;; User-Homedir-Pathname -- Public
992     ;;;
993 ram 1.12 ;;; Return Home:, which is set up for us at initialization time.
994 ram 1.1 ;;;
995     (defun user-homedir-pathname (&optional host)
996     "Returns the home directory of the logged in user as a pathname.
997 ram 1.12 This is obtained from the logical name \"home:\"."
998 ram 1.1 (declare (ignore host))
999 ram 1.12 #p"home:")
1000 ram 1.1
1001     ;;; File-Write-Date -- Public
1002     ;;;
1003     (defun file-write-date (file)
1004 pw 1.47 "Return file's creation date, or NIL if it doesn't exist.
1005 toy 1.73 An error of type file-error is signalled if file is a wild pathname"
1006 pw 1.47 (if (wild-pathname-p file)
1007 pw 1.51 (error 'simple-file-error
1008 pw 1.47 :pathname file
1009     :format-control "Bad place for a wild pathname.")
1010     (let ((name (unix-namestring file t)))
1011     (when name
1012     (multiple-value-bind
1013     (res dev ino mode nlink uid gid rdev size atime mtime)
1014     (unix:unix-stat name)
1015     (declare (ignore dev ino mode nlink uid gid rdev size atime))
1016     (when res
1017     (+ unix-to-universal-time mtime)))))))
1018 ram 1.1
1019     ;;; File-Author -- Public
1020     ;;;
1021     (defun file-author (file)
1022     "Returns the file author as a string, or nil if the author cannot be
1023 pw 1.47 determined. Signals an error of type file-error if file doesn't exist,
1024     or file is a wild pathname."
1025     (if (wild-pathname-p file)
1026 pw 1.51 (error 'simple-file-error
1027 pw 1.47 :pathname file
1028 emarsden 1.80 :format-control "Bad place for a wild pathname.")
1029 pw 1.47 (let ((name (unix-namestring (pathname file) t)))
1030     (unless name
1031 pw 1.51 (error 'simple-file-error
1032 pw 1.47 :pathname file
1033     :format-control "~S doesn't exist."
1034     :format-arguments (list file)))
1035     (multiple-value-bind (winp dev ino mode nlink uid)
1036     (unix:unix-stat name)
1037     (declare (ignore dev ino mode nlink))
1038 toy 1.71 (when winp
1039     (let ((user-info (unix:unix-getpwuid uid)))
1040     (when user-info
1041     (unix:user-info-name user-info))))))))
1042 ram 1.1
1043    
1044     ;;;; DIRECTORY.
1045    
1046 wlott 1.4 ;;; DIRECTORY -- public.
1047 toy 1.71 ;;;
1048 ram 1.23 (defun directory (pathname &key (all t) (check-for-subdirs t)
1049 dtc 1.58 (truenamep t) (follow-links t))
1050 ram 1.1 "Returns a list of pathnames, one for each file that matches the given
1051 ram 1.23 pathname. Supplying :ALL as nil causes this to ignore Unix dot files. This
1052 dtc 1.58 never includes Unix dot and dot-dot in the result. If :TRUENAMEP is NIL,
1053 toy 1.73 then symbolic links in the result are not expanded, which is not the
1054     default because TRUENAME does follow links and the result pathnames are
1055 ram 1.23 defined to be the TRUENAME of the pathname (the truename of a link may well
1056 toy 1.73 be in another directory). If FOLLOW-LINKS is NIL then symbolic links are
1057 dtc 1.58 not followed."
1058 wlott 1.16 (let ((results nil))
1059     (enumerate-search-list
1060 wlott 1.21 (pathname (merge-pathnames pathname
1061     (make-pathname :name :wild
1062     :type :wild
1063 toy 1.77 :version :wild
1064     :defaults *default-pathname-defaults*)
1065 toy 1.75 :wild))
1066 dtc 1.57 (enumerate-matches (name pathname nil :follow-links follow-links)
1067 wlott 1.16 (when (or all
1068     (let ((slash (position #\/ name :from-end t)))
1069     (or (null slash)
1070     (= (1+ slash) (length name))
1071     (char/= (schar name (1+ slash)) #\.))))
1072     (push name results))))
1073     (let ((*ignore-wildcards* t))
1074     (mapcar #'(lambda (name)
1075 ram 1.23 (let ((name (if (and check-for-subdirs
1076 wlott 1.26 (eq (unix:unix-file-kind name)
1077 ram 1.23 :directory))
1078     (concatenate 'string name "/")
1079     name)))
1080 dtc 1.58 (if truenamep (truename name) (pathname name))))
1081 wlott 1.16 (sort (delete-duplicates results :test #'string=) #'string<)))))
1082 ram 1.1
1083 wlott 1.4
1084     ;;;; Printing directories.
1085 ram 1.1
1086     ;;; PRINT-DIRECTORY is exported from the EXTENSIONS package.
1087     ;;;
1088     (defun print-directory (pathname &optional stream &key all verbose return-list)
1089 toy 1.73 "Like Directory, but prints a terse, multi-column directory listing
1090 ram 1.1 instead of returning a list of pathnames. When :all is supplied and
1091 toy 1.73 non-nil, then Unix dot files are included too (as ls -a). When :verbose
1092 ram 1.1 is supplied and non-nil, then a long listing of miscellaneous
1093     information is output one file per line."
1094 dtc 1.50 (let ((*standard-output* (out-synonym-of stream))
1095 wlott 1.16 (pathname pathname))
1096 ram 1.1 (if verbose
1097     (print-directory-verbose pathname all return-list)
1098     (print-directory-formatted pathname all return-list))))
1099    
1100     (defun print-directory-verbose (pathname all return-list)
1101 ram 1.23 (let ((contents (directory pathname :all all :check-for-subdirs nil
1102 dtc 1.58 :truenamep nil))
1103 wlott 1.7 (result nil))
1104 toy 1.73 (format t "Directory of ~A:~%" (namestring pathname))
1105 wlott 1.7 (dolist (file contents)
1106     (let* ((namestring (unix-namestring file))
1107     (tail (subseq namestring
1108     (1+ (or (position #\/ namestring
1109     :from-end t
1110     :test #'char=)
1111     -1)))))
1112     (multiple-value-bind
1113     (reslt dev-or-err ino mode nlink uid gid rdev size atime mtime)
1114 wlott 1.26 (unix:unix-stat namestring)
1115 wlott 1.7 (declare (ignore ino gid rdev atime)
1116     (fixnum uid mode))
1117     (cond (reslt
1118     ;;
1119     ;; Print characters for file modes.
1120     (macrolet ((frob (bit name &optional sbit sname negate)
1121     `(if ,(if negate
1122     `(not (logbitp ,bit mode))
1123     `(logbitp ,bit mode))
1124     ,(if sbit
1125     `(if (logbitp ,sbit mode)
1126     (write-char ,sname)
1127     (write-char ,name))
1128     `(write-char ,name))
1129     (write-char #\-))))
1130     (frob 15 #\d nil nil t)
1131     (frob 8 #\r)
1132     (frob 7 #\w)
1133     (frob 6 #\x 11 #\s)
1134     (frob 5 #\r)
1135     (frob 4 #\w)
1136     (frob 3 #\x 10 #\s)
1137     (frob 2 #\r)
1138     (frob 1 #\w)
1139     (frob 0 #\x))
1140     ;;
1141     ;; Print the rest.
1142     (multiple-value-bind (sec min hour date month year)
1143     (get-decoded-time)
1144     (declare (ignore sec min hour date month))
1145     (format t "~2D ~8A ~8D ~12A ~A~@[/~]~%"
1146     nlink
1147 toy 1.71 (let ((user-info (unix:unix-getpwuid uid)))
1148     (if user-info (unix:user-info-name user-info) uid))
1149 wlott 1.7 size
1150     (decode-universal-time-for-files mtime year)
1151     tail
1152 wlott 1.27 (= (logand mode unix:s-ifmt) unix:s-ifdir))))
1153 wlott 1.7 (t (format t "Couldn't stat ~A -- ~A.~%"
1154     tail
1155 wlott 1.26 (unix:get-unix-error-msg dev-or-err))))
1156 ram 1.1 (when return-list
1157 wlott 1.27 (push (if (= (logand mode unix:s-ifmt) unix:s-ifdir)
1158 wlott 1.7 (pathname (concatenate 'string namestring "/"))
1159     file)
1160     result)))))
1161     (nreverse result)))
1162 ram 1.1
1163     (defun decode-universal-time-for-files (time current-year)
1164     (multiple-value-bind (sec min hour day month year)
1165     (decode-universal-time (+ time unix-to-universal-time))
1166     (declare (ignore sec))
1167     (format nil "~A ~2,' D ~:[ ~D~;~*~2,'0D:~2,'0D~]"
1168     (svref '#("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug"
1169     "Sep" "Oct" "Nov" "Dec")
1170     (1- month))
1171     day (= current-year year) year hour min)))
1172    
1173     (defun print-directory-formatted (pathname all return-list)
1174     (let ((width (or (line-length *standard-output*) 80))
1175     (names ())
1176     (cnt 0)
1177     (max-len 0)
1178 dtc 1.58 (result (directory pathname :all all :truenamep nil)))
1179 ram 1.1 (declare (list names) (fixnum max-len cnt))
1180     ;;
1181     ;; Get the data.
1182 wlott 1.7 (dolist (file result)
1183     (let* ((name (unix-namestring file))
1184     (length (length name))
1185     (end (if (and (plusp length)
1186     (char= (schar name (1- length)) #\/))
1187     (1- length)
1188     length))
1189     (slash-name (subseq name
1190     (1+ (or (position #\/ name
1191     :from-end t
1192     :end end
1193     :test #'char=)
1194     -1))))
1195     (len (length slash-name)))
1196     (declare (simple-string slash-name)
1197     (fixnum len))
1198     (if (> len max-len) (setq max-len len))
1199     (incf cnt)
1200     (push slash-name names)))
1201     (setq names (nreverse names))
1202     ;;
1203     ;; Do the output.
1204     (let* ((col-width (1+ max-len))
1205     (cols (max (truncate width col-width) 1))
1206     (lines (ceiling cnt cols)))
1207     (declare (fixnum cols lines))
1208 toy 1.73 (format t "Directory of ~A:~%" (namestring pathname))
1209 wlott 1.7 (dotimes (i lines)
1210     (declare (fixnum i))
1211     (dotimes (j cols)
1212     (declare (fixnum j))
1213     (let ((name (nth (+ i (the fixnum (* j lines))) names)))
1214     (when name
1215     (write-string name)
1216     (unless (eql j (1- cols))
1217 wlott 1.15 (dotimes (i (- col-width (length (the simple-string name))))
1218     (write-char #\space))))))
1219 wlott 1.7 (terpri)))
1220     (when return-list
1221     result)))
1222 ram 1.1
1223    
1224 wlott 1.4 ;;;; File completion.
1225 ram 1.1
1226 chiles 1.13 ;;; COMPLETE-FILE -- Public
1227     ;;;
1228 wlott 1.4 (defun complete-file (pathname &key (defaults *default-pathname-defaults*)
1229     ignore-types)
1230 wlott 1.22 (let ((files (directory (complete-file-directory-arg pathname defaults)
1231 ram 1.23 :check-for-subdirs nil
1232 dtc 1.58 :truenamep nil)))
1233 wlott 1.4 (cond ((null files)
1234     (values nil nil))
1235     ((null (cdr files))
1236     (values (merge-pathnames (file-namestring (car files))
1237     pathname)
1238     t))
1239     (t
1240     (let ((good-files
1241     (delete-if #'(lambda (pathname)
1242 wlott 1.16 (and (simple-string-p
1243     (pathname-type pathname))
1244 wlott 1.4 (member (pathname-type pathname)
1245     ignore-types
1246     :test #'string=)))
1247     files)))
1248     (cond ((null good-files))
1249     ((null (cdr good-files))
1250     (return-from complete-file
1251     (values (merge-pathnames (file-namestring
1252     (car good-files))
1253     pathname)
1254     t)))
1255     (t
1256     (setf files good-files)))
1257     (let ((common (file-namestring (car files))))
1258     (dolist (file (cdr files))
1259     (let ((name (file-namestring file)))
1260     (dotimes (i (min (length common) (length name))
1261     (when (< (length name) (length common))
1262     (setf common name)))
1263     (unless (char= (schar common i) (schar name i))
1264     (setf common (subseq common 0 i))
1265     (return)))))
1266     (values (merge-pathnames common pathname)
1267     nil)))))))
1268 ram 1.1
1269 chiles 1.13 ;;; COMPLETE-FILE-DIRECTORY-ARG -- Internal.
1270     ;;;
1271     (defun complete-file-directory-arg (pathname defaults)
1272 wlott 1.22 (let* ((pathname (merge-pathnames pathname (directory-namestring defaults)))
1273 wlott 1.16 (type (pathname-type pathname)))
1274     (flet ((append-multi-char-wild (thing)
1275     (etypecase thing
1276     (null :wild)
1277     (pattern
1278     (make-pattern (append (pattern-pieces thing)
1279     (list :multi-char-wild))))
1280     (simple-string
1281     (make-pattern (list thing :multi-char-wild))))))
1282     (if (or (null type) (eq type :unspecific))
1283     ;; There is no type.
1284     (make-pathname :defaults pathname
1285     :name (append-multi-char-wild (pathname-name pathname))
1286     :type :wild)
1287     ;; There already is a type, so just extend it.
1288     (make-pathname :defaults pathname
1289     :name (pathname-name pathname)
1290     :type (append-multi-char-wild (pathname-type pathname)))))))
1291 chiles 1.13
1292 wlott 1.4 ;;; Ambiguous-Files -- Public
1293 ram 1.1 ;;;
1294 wlott 1.16 (defun ambiguous-files (pathname
1295     &optional (defaults *default-pathname-defaults*))
1296 wlott 1.4 "Return a list of all files which are possible completions of Pathname.
1297 chiles 1.13 We look in the directory specified by Defaults as well as looking down
1298     the search list."
1299 wlott 1.16 (directory (complete-file-directory-arg pathname defaults)
1300 dtc 1.58 :truenamep nil
1301 wlott 1.16 :check-for-subdirs nil))
1302 wlott 1.4
1303 wlott 1.16
1304 ram 1.1
1305     ;;; File-writable -- exported from extensions.
1306     ;;;
1307     ;;; Determines whether the single argument (which should be a pathname)
1308 dtc 1.46 ;;; can be written by the current task.
1309 wlott 1.16 ;;;
1310 ram 1.1 (defun file-writable (name)
1311     "File-writable accepts a pathname and returns T if the current
1312     process can write it, and NIL otherwise."
1313 wlott 1.16 (let ((name (unix-namestring name nil)))
1314 wlott 1.17 (cond ((null name)
1315     nil)
1316 wlott 1.26 ((unix:unix-file-kind name)
1317     (values (unix:unix-access name unix:w_ok)))
1318 wlott 1.17 (t
1319     (values
1320 wlott 1.26 (unix:unix-access (subseq name
1321 wlott 1.17 0
1322     (or (position #\/ name :from-end t)
1323     0))
1324 wlott 1.26 (logior unix:w_ok unix:x_ok)))))))
1325 ram 1.1
1326    
1327     ;;; Pathname-Order -- Internal
1328     ;;;
1329     ;;; Predicate to order pathnames by. Goes by name.
1330     ;;;
1331     (defun pathname-order (x y)
1332     (let ((xn (%pathname-name x))
1333     (yn (%pathname-name y)))
1334     (if (and xn yn)
1335     (let ((res (string-lessp xn yn)))
1336     (cond ((not res) nil)
1337     ((= res (length (the simple-string xn))) t)
1338     ((= res (length (the simple-string yn))) nil)
1339     (t t)))
1340     xn)))
1341    
1342    
1343     ;;; Default-Directory -- Public
1344     ;;;
1345     (defun default-directory ()
1346     "Returns the pathname for the default directory. This is the place where
1347     a file will be written if no directory is specified. This may be changed
1348     with setf."
1349     (multiple-value-bind (gr dir-or-error)
1350 wlott 1.26 (unix:unix-current-directory)
1351 ram 1.1 (if gr
1352 wlott 1.16 (let ((*ignore-wildcards* t))
1353 toy 1.70 (values
1354     (parse-namestring (concatenate 'simple-string dir-or-error "/")
1355     *unix-host*)))
1356 wlott 1.3 (error dir-or-error))))
1357 ram 1.1
1358     ;;; %Set-Default-Directory -- Internal
1359     ;;;
1360     (defun %set-default-directory (new-val)
1361 wlott 1.17 (let ((namestring (unix-namestring new-val t)))
1362     (unless namestring
1363 toy 1.67 (error 'simple-file-error
1364     :format-control "~S doesn't exist."
1365     :format-arguments (list new-val)))
1366 wlott 1.17 (multiple-value-bind (gr error)
1367 wlott 1.26 (unix:unix-chdir namestring)
1368 wlott 1.17 (if gr
1369     (setf (search-list "default:") (default-directory))
1370 wlott 1.26 (error (unix:get-unix-error-msg error))))
1371 wlott 1.17 new-val))
1372 wlott 1.16 ;;;
1373     (defsetf default-directory %set-default-directory)
1374    
1375     (defun filesys-init ()
1376 toy 1.74 ;; Use :unspecific so we don't create file versions whenever merging
1377     ;; happens. If the user wants that, let him change
1378     ;; *default-pathname-defaults* appropriately.
1379 wlott 1.16 (setf *default-pathname-defaults*
1380 toy 1.74 (%make-pathname *unix-host* nil nil nil nil :unspecific))
1381 wlott 1.16 (setf (search-list "default:") (default-directory))
1382     nil)
1383 dtc 1.42
1384     ;;; Ensure-Directories-Exist -- Public
1385     ;;;
1386     (defun ensure-directories-exist (pathspec &key verbose (mode #o777))
1387 pw 1.44 "Tests whether the directories containing the specified file
1388     actually exist, and attempts to create them if they do not.
1389     Portable programs should avoid using the :MODE keyword argument."
1390 dtc 1.43 (let* ((pathname (pathname pathspec))
1391     (pathname (if (logical-pathname-p pathname)
1392     (translate-logical-pathname pathname)
1393     pathname))
1394     (created-p nil))
1395     (when (wild-pathname-p pathname)
1396 pw 1.51 (error 'simple-file-error
1397 pw 1.47 :format-control "Bad place for a wild pathname."
1398     :pathname pathspec))
1399 dtc 1.45 (enumerate-search-list (pathname pathname)
1400     (let ((dir (pathname-directory pathname)))
1401     (loop for i from 1 upto (length dir)
1402     do (let ((newpath (make-pathname
1403     :host (pathname-host pathname)
1404     :device (pathname-device pathname)
1405     :directory (subseq dir 0 i))))
1406 rtoy 1.87 (tagbody
1407     retry
1408     (restart-case
1409     (unless (probe-file newpath)
1410     (let ((namestring (namestring newpath)))
1411     (when verbose
1412     (format *standard-output* "~&Creating directory: ~A~%"
1413     namestring))
1414     (unix:unix-mkdir namestring mode)
1415     (unless (probe-file namestring)
1416     (error 'simple-file-error
1417     :pathname pathspec
1418     :format-control "Can't create directory ~A."
1419     :format-arguments (list namestring)))
1420     (setf created-p t)))
1421     (retry () :report "Try to create the directory again"
1422     (go retry))))))
1423 dtc 1.45 ;; Only the first path in a search-list is considered.
1424     (return (values pathname created-p))))))

  ViewVC Help
Powered by ViewVC 1.1.5