Add 2008 to copyright years.
[bpt/emacs.git] / lisp / net / tramp-cache.el
1 ;;; tramp-cache.el --- file information caching for Tramp
2
3 ;; Copyright (C) 2000, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4
5 ;; Author: Daniel Pittman <daniel@inanna.danann.net>
6 ;; Michael Albinus <michael.albinus@gmx.de>
7 ;; Keywords: comm, processes
8
9 ;; This file is part of GNU Emacs.
10
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.
15
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.
20
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/>.
24
25 ;;; Commentary:
26
27 ;; An implementation of information caching for remote files.
28
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:
32 ;;
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'.
38 ;;
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'.
44 ;;
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.
49
50 ;;; Code:
51
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"))
65
66 ;;; -- Cache --
67
68 (defvar tramp-cache-data (make-hash-table :test 'equal)
69 "Hash table for remote files properties.")
70
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)
92
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))
107
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))
120
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))
128
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)))
140
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)))))
154
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)))
166
167 ;;; -- Properties --
168
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))
184
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))
205
206 (defun tramp-flush-connection-property (key event)
207 "Remove all properties identified by KEY.
208 KEY identifies the connection, it is either a process or a
209 vector. EVENT is not used, it is just applied because this
210 function is intended to run also as process sentinel."
211 ;; Unify key by removing localname from vector. Work with a copy in
212 ;; order to avoid side effects.
213 (when (vectorp key)
214 (setq key (copy-sequence key))
215 (aset key 3 nil))
216 ; (tramp-message key 7 "%s" event)
217 (remhash key tramp-cache-data))
218
219 (defun tramp-cache-print (table)
220 "Print hash table TABLE."
221 (when (hash-table-p table)
222 (let (result)
223 (maphash
224 '(lambda (key value)
225 (let ((tmp (format
226 "(%s %s)"
227 (if (processp key)
228 (prin1-to-string (prin1-to-string key))
229 (prin1-to-string key))
230 (if (hash-table-p value)
231 (tramp-cache-print value)
232 (if (bufferp value)
233 (prin1-to-string (prin1-to-string value))
234 (prin1-to-string value))))))
235 (setq result (if result (concat result " " tmp) tmp))))
236 table)
237 result)))
238
239 (defun tramp-list-connections ()
240 "Return a list of all known connection vectors according to `tramp-cache'."
241 (let (result)
242 (maphash
243 '(lambda (key value)
244 (when (and (vectorp key) (null (aref key 3)))
245 (add-to-list 'result key)))
246 tramp-cache-data)
247 result))
248
249 (defun tramp-dump-connection-properties ()
250 "Write persistent connection properties into file `tramp-persistency-file-name'."
251 ;; We shouldn't fail, otherwise (X)Emacs might not be able to be closed.
252 (condition-case nil
253 (when (and (hash-table-p tramp-cache-data)
254 (not (zerop (hash-table-count tramp-cache-data)))
255 (stringp tramp-persistency-file-name))
256 (let ((cache (copy-hash-table tramp-cache-data)))
257 ;; Remove temporary data.
258 (maphash
259 '(lambda (key value)
260 (if (and (vectorp key) (not (tramp-file-name-localname key)))
261 (progn
262 (remhash "process-name" value)
263 (remhash "process-buffer" value))
264 (remhash key cache)))
265 cache)
266 ;; Dump it.
267 (with-temp-buffer
268 (insert
269 ";; -*- emacs-lisp -*-"
270 ;; `time-stamp-string' might not exist in all (X)Emacs flavors.
271 (condition-case nil
272 (progn
273 (format
274 " <%s %s>\n"
275 (time-stamp-string "%02y/%02m/%02d %02H:%02M:%02S")
276 tramp-persistency-file-name))
277 (error "\n"))
278 ";; Tramp connection history. Don't change this file.\n"
279 ";; You can delete it, forcing Tramp to reapply the checks.\n\n"
280 (with-output-to-string
281 (pp (read (format "(%s)" (tramp-cache-print cache))))))
282 (write-region
283 (point-min) (point-max) tramp-persistency-file-name))))
284 (error nil)))
285
286 (add-hook 'kill-emacs-hook 'tramp-dump-connection-properties)
287 (add-hook 'tramp-cache-unload-hook
288 '(lambda ()
289 (remove-hook 'kill-emacs-hook
290 'tramp-dump-connection-properties)))
291
292 (defun tramp-parse-connection-properties (method)
293 "Return a list of (user host) tuples allowed to access for METHOD.
294 This function is added always in `tramp-get-completion-function'
295 for all methods. Resulting data are derived from connection
296 history."
297 (let (res)
298 (maphash
299 '(lambda (key value)
300 (if (and (vectorp key)
301 (string-equal method (tramp-file-name-method key))
302 (not (tramp-file-name-localname key)))
303 (push (list (tramp-file-name-user key)
304 (tramp-file-name-host key))
305 res)))
306 tramp-cache-data)
307 res))
308
309 ;; Read persistent connection history.
310 (when (and (stringp tramp-persistency-file-name)
311 (zerop (hash-table-count tramp-cache-data)))
312 (condition-case err
313 (with-temp-buffer
314 (insert-file-contents tramp-persistency-file-name)
315 (let ((list (read (current-buffer)))
316 element key item)
317 (while (setq element (pop list))
318 (setq key (pop element))
319 (while (setq item (pop element))
320 (tramp-set-connection-property key (pop item) (car item))))))
321 (file-error
322 ;; Most likely because the file doesn't exist yet. No message.
323 (clrhash tramp-cache-data))
324 (error
325 ;; File is corrupted.
326 (message "Tramp persistency file '%s' is corrupted: %s"
327 tramp-persistency-file-name (error-message-string err))
328 (clrhash tramp-cache-data))))
329
330 (provide 'tramp-cache)
331
332 ;; arch-tag: ee1739b7-7628-408c-9b96-d11a74b05d26
333 ;;; tramp-cache.el ends here