Tom Tromey <tromey at redhat.com>
[emacs.git] / lisp / net / tramp-cache.el
blob945823959cd349e4b561524e9c5407b71deeef48
1 ;;; tramp-cache.el --- file information caching for Tramp
3 ;; Copyright (C) 2000, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 ;; Author: Daniel Pittman <daniel@inanna.danann.net>
6 ;; Michael Albinus <michael.albinus@gmx.de>
7 ;; Keywords: comm, processes
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, see
23 ;; <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; An implementation of information caching for remote files.
29 ;; Each connection, identified by a vector [method user host
30 ;; localname] or by a process, has a unique cache. We distinguish 3
31 ;; kind of caches, depending on the key:
33 ;; - localname is NIL. This are reusable properties. Examples:
34 ;; "remote-shell" identifies the POSIX shell to be called on the
35 ;; remote host, or "perl" is the command to be called on the remote
36 ;; host, when starting a Perl script. These properties are saved in
37 ;; the file `tramp-persistency-file-name'.
39 ;; - localname is a string. This are temporary properties, which are
40 ;; related to the file localname is referring to. Examples:
41 ;; "file-exists-p" is t or nile, depending on the file existence, or
42 ;; "file-attributes" caches the result of the function
43 ;; `file-attributes'.
45 ;; - The key is a process. This are temporary properties related to
46 ;; an open connection. Examples: "scripts" keeps shell script
47 ;; definitions already sent to the remote shell, "last-cmd-time" is
48 ;; the time stamp a command has been sent to the remote process.
50 ;;; Code:
52 ;; Pacify byte-compiler.
53 (eval-when-compile
54 (require 'cl)
55 (autoload 'tramp-message "tramp")
56 (autoload 'tramp-tramp-file-p "tramp")
57 ;; We cannot autoload macro `with-parsed-tramp-file-name', it
58 ;; results in problems of byte-compiled code.
59 (autoload 'tramp-dissect-file-name "tramp")
60 (autoload 'tramp-file-name-method "tramp")
61 (autoload 'tramp-file-name-user "tramp")
62 (autoload 'tramp-file-name-host "tramp")
63 (autoload 'tramp-file-name-localname "tramp")
64 (autoload 'time-stamp-string "time-stamp"))
66 ;;; -- Cache --
68 (defvar tramp-cache-data (make-hash-table :test 'equal)
69 "Hash table for remote files properties.")
71 (defcustom tramp-persistency-file-name
72 (cond
73 ;; GNU Emacs.
74 ((and (boundp 'user-emacs-directory)
75 (stringp (symbol-value 'user-emacs-directory))
76 (file-directory-p (symbol-value 'user-emacs-directory)))
77 (expand-file-name "tramp" (symbol-value 'user-emacs-directory)))
78 ((and (not (featurep 'xemacs)) (file-directory-p "~/.emacs.d/"))
79 "~/.emacs.d/tramp")
80 ;; XEmacs.
81 ((and (boundp 'user-init-directory)
82 (stringp (symbol-value 'user-init-directory))
83 (file-directory-p (symbol-value 'user-init-directory)))
84 (expand-file-name "tramp" (symbol-value 'user-init-directory)))
85 ((and (featurep 'xemacs) (file-directory-p "~/.xemacs/"))
86 "~/.xemacs/tramp")
87 ;; For users without `~/.emacs.d/' or `~/.xemacs/'.
88 (t "~/.tramp"))
89 "File which keeps connection history for Tramp connections."
90 :group 'tramp
91 :type 'file)
93 (defun tramp-get-file-property (vec file property default)
94 "Get the PROPERTY of FILE from the cache context of VEC.
95 Returns DEFAULT if not set."
96 ;; Unify localname.
97 (setq vec (copy-sequence vec))
98 (aset vec 3 (directory-file-name file))
99 (let* ((hash (or (gethash vec tramp-cache-data)
100 (puthash vec (make-hash-table :test 'equal)
101 tramp-cache-data)))
102 (value (if (hash-table-p hash)
103 (gethash property hash default)
104 default)))
105 (tramp-message vec 8 "%s %s %s" file property value)
106 value))
108 (defun tramp-set-file-property (vec file property value)
109 "Set the PROPERTY of FILE to VALUE, in the cache context of VEC.
110 Returns VALUE."
111 ;; Unify localname.
112 (setq vec (copy-sequence vec))
113 (aset vec 3 (directory-file-name file))
114 (let ((hash (or (gethash vec tramp-cache-data)
115 (puthash vec (make-hash-table :test 'equal)
116 tramp-cache-data))))
117 (puthash property value hash)
118 (tramp-message vec 8 "%s %s %s" file property value)
119 value))
121 (defun tramp-flush-file-property (vec file)
122 "Remove all properties of FILE in the cache context of VEC."
123 ;; Unify localname.
124 (setq vec (copy-sequence vec))
125 (aset vec 3 (directory-file-name file))
126 (tramp-message vec 8 "%s" file)
127 (remhash vec tramp-cache-data))
129 (defun tramp-flush-directory-property (vec directory)
130 "Remove all properties of DIRECTORY in the cache context of VEC.
131 Remove also properties of all files in subdirectories."
132 (let ((directory (directory-file-name directory)))
133 (tramp-message vec 8 "%s" directory)
134 (maphash
135 '(lambda (key value)
136 (when (and (stringp key)
137 (string-match directory (tramp-file-name-localname key)))
138 (remhash key tramp-cache-data)))
139 tramp-cache-data)))
141 ;; Reverting or killing a buffer should also flush file properties.
142 ;; They could have been changed outside Tramp. In eshell, "ls" would
143 ;; not show proper directory contents when a file has been copied or
144 ;; deleted before.
145 (defun tramp-flush-file-function ()
146 "Flush all Tramp cache properties from buffer-file-name."
147 (let ((bfn (if (stringp (buffer-file-name))
148 (buffer-file-name)
149 default-directory)))
150 (when (tramp-tramp-file-p bfn)
151 (let* ((v (tramp-dissect-file-name bfn))
152 (localname (tramp-file-name-localname v)))
153 (tramp-flush-file-property v localname)))))
155 (add-hook 'before-revert-hook 'tramp-flush-file-function)
156 (add-hook 'eshell-pre-command-hook 'tramp-flush-file-function)
157 (add-hook 'kill-buffer-hook 'tramp-flush-file-function)
158 (add-hook 'tramp-cache-unload-hook
159 '(lambda ()
160 (remove-hook 'before-revert-hook
161 'tramp-flush-file-function)
162 (remove-hook 'eshell-pre-command-hook
163 'tramp-flush-file-function)
164 (remove-hook 'kill-buffer-hook
165 'tramp-flush-file-function)))
167 ;;; -- Properties --
169 (defun tramp-get-connection-property (key property default)
170 "Get the named PROPERTY for the connection.
171 KEY identifies the connection, it is either a process or a vector.
172 If the value is not set for the connection, returns DEFAULT."
173 ;; Unify key by removing localname from vector. Work with a copy in
174 ;; order to avoid side effects.
175 (when (vectorp key)
176 (setq key (copy-sequence key))
177 (aset key 3 nil))
178 (let* ((hash (gethash key tramp-cache-data))
179 (value (if (hash-table-p hash)
180 (gethash property hash default)
181 default)))
182 (tramp-message key 7 "%s %s" property value)
183 value))
185 (defun tramp-set-connection-property (key property value)
186 "Set the named PROPERTY of a connection to VALUE.
187 KEY identifies the connection, it is either a process or a vector.
188 PROPERTY is set persistent when KEY is a vector."
189 ;; Unify key by removing localname from vector. Work with a copy in
190 ;; order to avoid side effects.
191 (when (vectorp key)
192 (setq key (copy-sequence key))
193 (aset key 3 nil))
194 (let ((hash (or (gethash key tramp-cache-data)
195 (puthash key (make-hash-table :test 'equal)
196 tramp-cache-data))))
197 (puthash property value hash)
198 ;; This function is called also during initialization of
199 ;; tramp-cache.el. `tramp-messageĀ“ is not defined yet at this
200 ;; time, so we ignore the corresponding error.
201 (condition-case nil
202 (tramp-message key 7 "%s %s" property value)
203 (error nil))
204 value))
206 (defun tramp-flush-connection-property (key)
207 "Remove all properties identified by KEY.
208 KEY identifies the connection, it is either a process or a vector."
209 ;; Unify key by removing localname from vector. Work with a copy in
210 ;; order to avoid side effects.
211 (when (vectorp key)
212 (setq key (copy-sequence key))
213 (aset key 3 nil))
214 (remhash key tramp-cache-data))
216 (defun tramp-cache-print (table)
217 "Print hash table TABLE."
218 (when (hash-table-p table)
219 (let (result)
220 (maphash
221 '(lambda (key value)
222 (let ((tmp (format
223 "(%s %s)"
224 (if (processp key)
225 (prin1-to-string (prin1-to-string key))
226 (prin1-to-string key))
227 (if (hash-table-p value)
228 (tramp-cache-print value)
229 (if (bufferp value)
230 (prin1-to-string (prin1-to-string value))
231 (prin1-to-string value))))))
232 (setq result (if result (concat result " " tmp) tmp))))
233 table)
234 result)))
236 (defun tramp-list-connections ()
237 "Return a list of all known connection vectors according to `tramp-cache'."
238 (let (result)
239 (maphash
240 '(lambda (key value)
241 (when (and (vectorp key) (null (aref key 3)))
242 (add-to-list 'result key)))
243 tramp-cache-data)
244 result))
246 (defun tramp-dump-connection-properties ()
247 "Write persistent connection properties into file `tramp-persistency-file-name'."
248 ;; We shouldn't fail, otherwise (X)Emacs might not be able to be closed.
249 (condition-case nil
250 (when (and (hash-table-p tramp-cache-data)
251 (not (zerop (hash-table-count tramp-cache-data)))
252 (stringp tramp-persistency-file-name))
253 (let ((cache (copy-hash-table tramp-cache-data)))
254 ;; Remove temporary data.
255 (maphash
256 '(lambda (key value)
257 (if (and (vectorp key) (not (tramp-file-name-localname key)))
258 (progn
259 (remhash "process-name" value)
260 (remhash "process-buffer" value))
261 (remhash key cache)))
262 cache)
263 ;; Dump it.
264 (with-temp-buffer
265 (insert
266 ";; -*- emacs-lisp -*-"
267 ;; `time-stamp-string' might not exist in all (X)Emacs flavors.
268 (condition-case nil
269 (progn
270 (format
271 " <%s %s>\n"
272 (time-stamp-string "%02y/%02m/%02d %02H:%02M:%02S")
273 tramp-persistency-file-name))
274 (error "\n"))
275 ";; Tramp connection history. Don't change this file.\n"
276 ";; You can delete it, forcing Tramp to reapply the checks.\n\n"
277 (with-output-to-string
278 (pp (read (format "(%s)" (tramp-cache-print cache))))))
279 (write-region
280 (point-min) (point-max) tramp-persistency-file-name))))
281 (error nil)))
283 (add-hook 'kill-emacs-hook 'tramp-dump-connection-properties)
284 (add-hook 'tramp-cache-unload-hook
285 '(lambda ()
286 (remove-hook 'kill-emacs-hook
287 'tramp-dump-connection-properties)))
289 (defun tramp-parse-connection-properties (method)
290 "Return a list of (user host) tuples allowed to access for METHOD.
291 This function is added always in `tramp-get-completion-function'
292 for all methods. Resulting data are derived from connection
293 history."
294 (let (res)
295 (maphash
296 '(lambda (key value)
297 (if (and (vectorp key)
298 (string-equal method (tramp-file-name-method key))
299 (not (tramp-file-name-localname key)))
300 (push (list (tramp-file-name-user key)
301 (tramp-file-name-host key))
302 res)))
303 tramp-cache-data)
304 res))
306 ;; Read persistent connection history.
307 (when (and (stringp tramp-persistency-file-name)
308 (zerop (hash-table-count tramp-cache-data)))
309 (condition-case err
310 (with-temp-buffer
311 (insert-file-contents tramp-persistency-file-name)
312 (let ((list (read (current-buffer)))
313 element key item)
314 (while (setq element (pop list))
315 (setq key (pop element))
316 (while (setq item (pop element))
317 (tramp-set-connection-property key (pop item) (car item))))))
318 (file-error
319 ;; Most likely because the file doesn't exist yet. No message.
320 (clrhash tramp-cache-data))
321 (error
322 ;; File is corrupted.
323 (message "Tramp persistency file '%s' is corrupted: %s"
324 tramp-persistency-file-name (error-message-string err))
325 (clrhash tramp-cache-data))))
327 (provide 'tramp-cache)
329 ;; arch-tag: ee1739b7-7628-408c-9b96-d11a74b05d26
330 ;;; tramp-cache.el ends here