Skip to content
Snippets Groups Projects
filesys.lisp 43.8 KiB
Newer Older
ram's avatar
ram committed
	  
	  (if (> len max-len) (setq max-len len))
	  (incf cnt)
	  (push slash-name names)))
      (setq names (nreverse names))
      ;;
      ;; Do the output.
      (let* ((col-width (1+ max-len))
	     (cols (max (truncate width col-width) 1))
	     (lines (ceiling cnt cols)))
	(declare (fixnum cols lines))
	(format t "Directory of ~A :~%" pattern)
	(dotimes (i lines)
	  (declare (fixnum i))
	  (dotimes (j cols)
	    (declare (fixnum j))
	    (let ((name (nth (+ i (the fixnum (* j lines))) names)))
	      (when name
		(write-string name)
		(unless (eql j (1- cols))
		  (tab-over 
		   (- col-width (length (the simple-string name))))))))
	  (terpri))))
    (when return-list (nreverse result))))



;;;; Translating uid's and gid's.

(defvar *uid-hash-table* (make-hash-table)
  "Hash table for keeping track of uid's and login names.")

;;; LOOKUP-LOGIN-NAME translates a user id into a login name.  Previous
;;; lookups are cached in a hash table since groveling the passwd(s) files
;;; is somewhat expensive.  The table may hold nil for id's that cannot
;;; be looked up since this means the files are searched in their entirety
;;; each time this id is translated.
;;; 
(defun lookup-login-name (uid)
  (multiple-value-bind (login-name foundp) (gethash uid *uid-hash-table*)
    (if foundp
	login-name
	(setf (gethash uid *uid-hash-table*)
	      (get-group-or-user-name :user uid)))))

(defvar *gid-hash-table* (make-hash-table)
  "Hash table for keeping track of gid's and group names.")

;;; LOOKUP-GROUP-NAME translates a group id into a group name.  Previous
;;; lookups are cached in a hash table since groveling the group(s) files
;;; is somewhat expensive.  The table may hold nil for id's that cannot
;;; be looked up since this means the files are searched in their entirety
;;; each time this id is translated.
;;; 
(defun lookup-group-name (gid)
  (multiple-value-bind (group-name foundp) (gethash gid *gid-hash-table*)
    (if foundp
	group-name
	(setf (gethash gid *gid-hash-table*)
	      (get-group-or-user-name :group gid)))))


;;; GET-GROUP-OR-USER-NAME first tries "/etc/passwd" ("/etc/group") since it is
;;; a much smaller file, contains all the local id's, and most uses probably
;;; involve id's on machines one would login into.  Then if necessary, we look
;;; in "/etc/passwds" ("/etc/groups") which is really long and has to be
;;; fetched over the net.
;;;
(defun get-group-or-user-name (group-or-user id)
  "Returns the simple-string user or group name of the user whose uid or gid
   is id, or NIL if no such user or group exists.  Group-or-user is either
   :group or :user."
  (let ((id-string (let ((*print-base* 10)) (prin1-to-string id))))
    (declare (simple-string id-string))
    (multiple-value-bind (file1 file2)
			 (ecase group-or-user
			   (:group (values "/etc/group" "/etc/groups"))
			   (:user (values "/etc/passwd" "/etc/passwd")))
      (or (get-group-or-user-name-aux id-string file1)
	  (get-group-or-user-name-aux id-string file2)))))

(defun get-group-or-user-name-aux (id-string passwd-file)
  (with-open-file (stream passwd-file)
    (loop
      (let ((entry (read-line stream nil)))
	(unless entry (return nil))
	(let ((name-end (position #\: (the simple-string entry)
				  :test #'char=)))
	  (when name-end
	    (let ((id-start (position #\: (the simple-string entry)
				      :start (1+ name-end) :test #'char=)))
	      (when id-start
		(incf id-start)
		(let ((id-end (position #\: (the simple-string entry)
					:start id-start :test #'char=)))
		  (when (and id-end
			     (string= id-string entry
				      :start2 id-start :end2 id-end))
		    (return (subseq entry 0 name-end))))))))))))



;;; Complete-One-File  --  Internal
;;;
;;;    Return as values a string and the greatest common prefix of all
;;; the files corresponding to pattern.
;;;
(defun complete-one-file (pattern default-type ignore-types)
  (let ((first nil)
	(length nil))
    (do-directory (name etype pattern nil)
      (declare (ignore etype))
      (let* ((last-dot (position #\. name :from-end t))
	     (type (if last-dot (subseq name (1+ last-dot)))))
	(cond ((and (not (string= type default-type))
		    (member type ignore-types :test #'string=)))
	      (first
	       (let ((msm (string-not-equal name first :end2 length)))
		 (when (and msm (< msm length))
		   (setq length msm))))
	      (t
	       (setq first name)
	       (setq length (length name))))))
    (values first length)))


;;; Complete-File  --  Public
;;;
;;;    If the pathname is absolute, just call Complete-One-File on and test
;;; whether the result is a file.  If a relative pathname, do it on each
;;; directory, accumulating the result.
;;;
(defun complete-file (pathname &key defaults ignore-types)
  "Attempt to complete Pathname as the name of a file.  If the resulting
  completion is unique, return T as the second value.  If there is no
  possible completion, return both values NIL."
  (setq pathname (pathname pathname))
  (setq defaults (if defaults (pathname defaults) *default-pathname-defaults*))
  (flet ((pathnamify (res len ambiguous pathname)
		     (values 
		      (make-pathname :device (%pathname-device pathname)
				     :directory (%pathname-directory pathname)
				     :defaults (parse-namestring (subseq res 0 len)))
		      (not ambiguous))))
    (let ((dev (or (%pathname-device pathname) "default"))
	  (default-type (%pathname-type defaults)))
      (if (eq dev :absolute)
	  (multiple-value-bind
	      (res len)
	      (complete-one-file (concatenate 'simple-string
					      (namestring pathname)
					      "*")
				 default-type ignore-types)
	    (declare (fixnum len))
	    (if res
		(pathnamify res len
			    (/= (the fixnum (length res)) len)
			    pathname)
		(values nil nil)))
	  (let ((namestring (%ses-get-useful-name pathname))		 
		(dirs (if (and (%pathname-directory defaults)
			       (string-equal dev "default"))
			  (list (directory-namestring defaults))
			  ()))
		(max most-positive-fixnum)
		(max-str nil)
		(ambiguous nil))
	    (declare (simple-string namestring))
	    (do-search-list (entry dev)
			    (pushnew entry dirs :test #'string-equal))
	    (dolist (entry dirs)
	      (let ((str (concatenate 'simple-string entry namestring "*")))
		(declare (simple-string str))
		(multiple-value-bind
		    (res len)
		    (complete-one-file str default-type ignore-types)
		  (when res
		    (unless ambiguous
		      (setq ambiguous (or max-str (/= (length res) len))))
		    (if max-str
			(setq max (or (string-not-equal res max-str :end1 len
							:end2 max)
				      max))
			(setq max len  max-str res))))))
	    (if max-str
		(pathnamify max-str max ambiguous pathname)
		(values nil nil)))))))

;;; File-writable -- exported from extensions.
;;;
;;;   Determines whether the single argument (which should be a pathname)
;;;   can be written by the the current task.

(defun file-writable (name)
  "File-writable accepts a pathname and returns T if the current
  process can write it, and NIL otherwise."
  (multiple-value-bind (tn exists) (predict-name name nil)
    (if exists
	(values (mach:unix-access tn mach:w_ok))
	(values (mach:unix-access (directory-namestring tn)
				  (logior mach:w_ok mach:x_ok))))))


;;; Pathname-Order  --  Internal
;;;
;;;    Predicate to order pathnames by.  Goes by name.
;;;
(defun pathname-order (x y)
  (let ((xn (%pathname-name x))
	(yn (%pathname-name y)))
    (if (and xn yn)
	(let ((res (string-lessp xn yn)))
	  (cond ((not res) nil)
		((= res (length (the simple-string xn))) t)
		((= res (length (the simple-string yn))) nil)
		(t t)))
	xn)))

;;; Ambiguous-Files  --  Public
;;;
;;;    If the pathname is absolute, just do a directory.  If it is relative,
;;; do a directory on each directory in the search-list and merge the results.
;;;
(defun ambiguous-files (pathname &optional defaults)
  "Return a list of all files which are possible completions of Pathname.
  We look in the directory specified by Defaults as well as looking down
  the search list."
  (setq pathname (pathname pathname)
	defaults (if defaults (pathname defaults) *default-pathname-defaults*))
  (let ((dev (or (%pathname-device pathname) "default")))
    (if (eq dev :absolute)
	(directory (concatenate 'simple-string (namestring pathname) "*"))
	(let ((namestring (%ses-get-useful-name pathname))		 
	      (dirs (if (and (%pathname-directory defaults)
			     (string-equal dev "default"))
			(list (directory-namestring defaults))
			()))
	      (res ()))
	  (declare (simple-string namestring))
	  (do-search-list (entry dev) (pushnew entry dirs :test #'string-equal))
	  (dolist (entry dirs)
	    (let ((str (concatenate 'simple-string entry namestring "*")))
	      (declare (simple-string str))
	      (setq res (merge 'list res (directory str)
			       #'pathname-order))))
	  res))))

;;; Default-Directory  --  Public
;;;
;;;    This fills in a hole in Common Lisp.  We return the first thing we
;;; find by doing a ResolveSearchList on Default.
;;; 
(defun default-directory ()
  "Returns the pathname for the default directory.  This is the place where
  a file will be written if no directory is specified.  This may be changed
  with setf."
  (multiple-value-bind (gr dir-or-error)
		       (mach:unix-current-directory)
    (if gr
	dir-or-error
	(error (mach:get-unix-error-msg dir-or-error)))))

;;;
;;; Maybe this shouldn't go here...
(defsetf default-directory %set-default-directory)

;;; %Set-Default-Directory  --  Internal
;;;
;;;    The setf method for Default-Directory.  We actually set the environment
;;; variable Current which is by convention the head of the search list.
;;; 
(defun %set-default-directory (new-val)
  (multiple-value-bind (gr error)
		       (mach:unix-chdir (predict-name new-val nil))
    (if gr
	(car (setf (search-list "default:")
		   (cdr (multiple-value-list (mach:unix-current-directory)))))
	(error (mach:get-unix-error-msg error)))))