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