]> code.delx.au - gnu-emacs/blob - lisp/net/tramp-cache.el
Merge from emacs-23
[gnu-emacs] / lisp / net / tramp-cache.el
1 ;;; tramp-cache.el --- file information caching for Tramp
2
3 ;; Copyright (C) 2000, 2005, 2006, 2007, 2008, 2009,
4 ;; 2010 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 ;; Package: tramp
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
28 ;; An implementation of information caching for remote files.
29
30 ;; Each connection, identified by a vector [method user host
31 ;; localname] or by a process, has a unique cache. We distinguish 3
32 ;; kind of caches, depending on the key:
33 ;;
34 ;; - localname is NIL. This are reusable properties. Examples:
35 ;; "remote-shell" identifies the POSIX shell to be called on the
36 ;; remote host, or "perl" is the command to be called on the remote
37 ;; host, when starting a Perl script. These properties are saved in
38 ;; the file `tramp-persistency-file-name'.
39 ;;
40 ;; - localname is a string. This are temporary properties, which are
41 ;; related to the file localname is referring to. Examples:
42 ;; "file-exists-p" is t or nile, depending on the file existence, or
43 ;; "file-attributes" caches the result of the function
44 ;; `file-attributes'.
45 ;;
46 ;; - The key is a process. This are temporary properties related to
47 ;; an open connection. Examples: "scripts" keeps shell script
48 ;; definitions already sent to the remote shell, "last-cmd-time" is
49 ;; the time stamp a command has been sent to the remote process.
50
51 ;;; Code:
52
53 (require 'tramp)
54 (autoload 'time-stamp-string "time-stamp")
55
56 ;;; -- Cache --
57
58 ;;;###tramp-autoload
59 (defvar tramp-cache-data (make-hash-table :test 'equal)
60 "Hash table for remote files properties.")
61
62 (defcustom tramp-persistency-file-name
63 (cond
64 ;; GNU Emacs.
65 ((and (boundp 'user-emacs-directory)
66 (stringp (symbol-value 'user-emacs-directory))
67 (file-directory-p (symbol-value 'user-emacs-directory)))
68 (expand-file-name "tramp" (symbol-value 'user-emacs-directory)))
69 ((and (not (featurep 'xemacs)) (file-directory-p "~/.emacs.d/"))
70 "~/.emacs.d/tramp")
71 ;; XEmacs.
72 ((and (boundp 'user-init-directory)
73 (stringp (symbol-value 'user-init-directory))
74 (file-directory-p (symbol-value 'user-init-directory)))
75 (expand-file-name "tramp" (symbol-value 'user-init-directory)))
76 ((and (featurep 'xemacs) (file-directory-p "~/.xemacs/"))
77 "~/.xemacs/tramp")
78 ;; For users without `~/.emacs.d/' or `~/.xemacs/'.
79 (t "~/.tramp"))
80 "File which keeps connection history for Tramp connections."
81 :group 'tramp
82 :type 'file)
83
84 (defvar tramp-cache-data-changed nil
85 "Whether persistent cache data have been changed.")
86
87 ;;;###tramp-autoload
88 (defun tramp-get-file-property (vec file property default)
89 "Get the PROPERTY of FILE from the cache context of VEC.
90 Returns DEFAULT if not set."
91 ;; Unify localname.
92 (setq vec (copy-sequence vec))
93 (aset vec 3 (tramp-run-real-handler 'directory-file-name (list file)))
94 (let* ((hash (or (gethash vec tramp-cache-data)
95 (puthash vec (make-hash-table :test 'equal)
96 tramp-cache-data)))
97 (value (when (hash-table-p hash) (gethash property hash))))
98 (if
99 ;; We take the value only if there is any, and
100 ;; `remote-file-name-inhibit-cache' indicates that it is still
101 ;; valid. Otherwise, DEFAULT is set.
102 (and (consp value)
103 (or (null remote-file-name-inhibit-cache)
104 (and (integerp remote-file-name-inhibit-cache)
105 (<=
106 (tramp-time-diff (current-time) (car value))
107 remote-file-name-inhibit-cache))
108 (and (consp remote-file-name-inhibit-cache)
109 (tramp-time-less-p
110 remote-file-name-inhibit-cache (car value)))))
111 (setq value (cdr value))
112 (setq value default))
113
114 (tramp-message vec 8 "%s %s %s" file property value)
115 (when (>= tramp-verbose 10)
116 (let* ((var (intern (concat "tramp-cache-get-count-" property)))
117 (val (or (ignore-errors (symbol-value var)) 0)))
118 (set var (1+ val))))
119 value))
120
121 ;;;###tramp-autoload
122 (defun tramp-set-file-property (vec file property value)
123 "Set the PROPERTY of FILE to VALUE, in the cache context of VEC.
124 Returns VALUE."
125 ;; Unify localname.
126 (setq vec (copy-sequence vec))
127 (aset vec 3 (tramp-run-real-handler 'directory-file-name (list file)))
128 (let ((hash (or (gethash vec tramp-cache-data)
129 (puthash vec (make-hash-table :test 'equal)
130 tramp-cache-data))))
131 ;; We put the timestamp there.
132 (puthash property (cons (current-time) value) hash)
133 (tramp-message vec 8 "%s %s %s" file property value)
134 (when (>= tramp-verbose 10)
135 (let* ((var (intern (concat "tramp-cache-set-count-" property)))
136 (val (or (ignore-errors (symbol-value var)) 0)))
137 (set var (1+ val))))
138 value))
139
140 ;;;###tramp-autoload
141 (defmacro with-file-property (vec file property &rest body)
142 "Check in Tramp cache for PROPERTY, otherwise execute BODY and set cache.
143 FILE must be a local file name on a connection identified via VEC."
144 `(if (file-name-absolute-p ,file)
145 (let ((value (tramp-get-file-property ,vec ,file ,property 'undef)))
146 (when (eq value 'undef)
147 ;; We cannot pass @body as parameter to
148 ;; `tramp-set-file-property' because it mangles our
149 ;; debug messages.
150 (setq value (progn ,@body))
151 (tramp-set-file-property ,vec ,file ,property value))
152 value)
153 ,@body))
154
155 ;;;###tramp-autoload
156 (put 'with-file-property 'lisp-indent-function 3)
157 (put 'with-file-property 'edebug-form-spec t)
158 (tramp-compat-font-lock-add-keywords
159 'emacs-lisp-mode '("\\<with-file-property\\>"))
160
161 ;;;###tramp-autoload
162 (defun tramp-flush-file-property (vec file)
163 "Remove all properties of FILE in the cache context of VEC."
164 ;; Unify localname.
165 (setq vec (copy-sequence vec))
166 (aset vec 3 (tramp-run-real-handler 'directory-file-name (list file)))
167 (tramp-message vec 8 "%s" file)
168 (remhash vec tramp-cache-data))
169
170 ;;;###tramp-autoload
171 (defun tramp-flush-directory-property (vec directory)
172 "Remove all properties of DIRECTORY in the cache context of VEC.
173 Remove also properties of all files in subdirectories."
174 (let ((directory (tramp-run-real-handler
175 'directory-file-name (list directory))))
176 (tramp-message vec 8 "%s" directory)
177 (maphash
178 '(lambda (key value)
179 (when (and (stringp (tramp-file-name-localname key))
180 (string-match directory (tramp-file-name-localname key)))
181 (remhash key tramp-cache-data)))
182 tramp-cache-data)))
183
184 ;; Reverting or killing a buffer should also flush file properties.
185 ;; They could have been changed outside Tramp. In eshell, "ls" would
186 ;; not show proper directory contents when a file has been copied or
187 ;; deleted before.
188 (defun tramp-flush-file-function ()
189 "Flush all Tramp cache properties from `buffer-file-name'."
190 (let ((bfn (if (stringp (buffer-file-name))
191 (buffer-file-name)
192 default-directory)))
193 (when (tramp-tramp-file-p bfn)
194 (with-parsed-tramp-file-name bfn nil
195 (tramp-flush-file-property v localname)))))
196
197 (add-hook 'before-revert-hook 'tramp-flush-file-function)
198 (add-hook 'eshell-pre-command-hook 'tramp-flush-file-function)
199 (add-hook 'kill-buffer-hook 'tramp-flush-file-function)
200 (add-hook 'tramp-cache-unload-hook
201 '(lambda ()
202 (remove-hook 'before-revert-hook
203 'tramp-flush-file-function)
204 (remove-hook 'eshell-pre-command-hook
205 'tramp-flush-file-function)
206 (remove-hook 'kill-buffer-hook
207 'tramp-flush-file-function)))
208
209 ;;; -- Properties --
210
211 ;;;###tramp-autoload
212 (defun tramp-get-connection-property (key property default)
213 "Get the named PROPERTY for the connection.
214 KEY identifies the connection, it is either a process or a vector.
215 If the value is not set for the connection, returns DEFAULT."
216 ;; Unify key by removing localname from vector. Work with a copy in
217 ;; order to avoid side effects.
218 (when (vectorp key)
219 (setq key (copy-sequence key))
220 (aset key 3 nil))
221 (let* ((hash (gethash key tramp-cache-data))
222 (value (if (hash-table-p hash)
223 (gethash property hash default)
224 default)))
225 (tramp-message key 7 "%s %s" property value)
226 value))
227
228 ;;;###tramp-autoload
229 (defun tramp-set-connection-property (key property value)
230 "Set the named PROPERTY of a connection to VALUE.
231 KEY identifies the connection, it is either a process or a vector.
232 PROPERTY is set persistent when KEY is a vector."
233 ;; Unify key by removing localname from vector. Work with a copy in
234 ;; order to avoid side effects.
235 (when (vectorp key)
236 (setq key (copy-sequence key))
237 (aset key 3 nil))
238 (let ((hash (or (gethash key tramp-cache-data)
239 (puthash key (make-hash-table :test 'equal)
240 tramp-cache-data))))
241 (puthash property value hash)
242 (setq tramp-cache-data-changed t)
243 (tramp-message key 7 "%s %s" property value)
244 value))
245
246 ;;;###tramp-autoload
247 (defmacro with-connection-property (key property &rest body)
248 "Check in Tramp for property PROPERTY, otherwise executes BODY and set."
249 `(let ((value (tramp-get-connection-property ,key ,property 'undef)))
250 (when (eq value 'undef)
251 ;; We cannot pass ,@body as parameter to
252 ;; `tramp-set-connection-property' because it mangles our debug
253 ;; messages.
254 (setq value (progn ,@body))
255 (tramp-set-connection-property ,key ,property value))
256 value))
257
258 ;;;###tramp-autoload
259 (put 'with-connection-property 'lisp-indent-function 2)
260 (put 'with-connection-property 'edebug-form-spec t)
261 (tramp-compat-font-lock-add-keywords
262 'emacs-lisp-mode '("\\<with-connection-property\\>"))
263
264 ;;;###tramp-autoload
265 (defun tramp-flush-connection-property (key)
266 "Remove all properties identified by KEY.
267 KEY identifies the connection, it is either a process or a vector."
268 ;; Unify key by removing localname from vector. Work with a copy in
269 ;; order to avoid side effects.
270 (when (vectorp key)
271 (setq key (copy-sequence key))
272 (aset key 3 nil))
273 (tramp-message
274 key 7 "%s %s" key
275 (let ((hash (gethash key tramp-cache-data))
276 properties)
277 (if (hash-table-p hash)
278 (maphash
279 (lambda (x y) (add-to-list 'properties x 'append))
280 (gethash key tramp-cache-data)))
281 properties))
282 (setq tramp-cache-data-changed t)
283 (remhash key tramp-cache-data))
284
285 ;;;###tramp-autoload
286 (defun tramp-cache-print (table)
287 "Print hash table TABLE."
288 (when (hash-table-p table)
289 (let (result)
290 (maphash
291 '(lambda (key value)
292 (let ((tmp (format
293 "(%s %s)"
294 (if (processp key)
295 (prin1-to-string (prin1-to-string key))
296 (prin1-to-string key))
297 (if (hash-table-p value)
298 (tramp-cache-print value)
299 (if (bufferp value)
300 (prin1-to-string (prin1-to-string value))
301 (prin1-to-string value))))))
302 (setq result (if result (concat result " " tmp) tmp))))
303 table)
304 result)))
305
306 ;;;###tramp-autoload
307 (defun tramp-list-connections ()
308 "Return a list of all known connection vectors according to `tramp-cache'."
309 (let (result)
310 (maphash
311 '(lambda (key value)
312 (when (and (vectorp key) (null (aref key 3)))
313 (add-to-list 'result key)))
314 tramp-cache-data)
315 result))
316
317 (defun tramp-dump-connection-properties ()
318 "Write persistent connection properties into file `tramp-persistency-file-name'."
319 ;; We shouldn't fail, otherwise (X)Emacs might not be able to be closed.
320 (ignore-errors
321 (when (and (hash-table-p tramp-cache-data)
322 (not (zerop (hash-table-count tramp-cache-data)))
323 tramp-cache-data-changed
324 (stringp tramp-persistency-file-name))
325 (let ((cache (copy-hash-table tramp-cache-data)))
326 ;; Remove temporary data.
327 (maphash
328 '(lambda (key value)
329 (if (and (vectorp key) (not (tramp-file-name-localname key)))
330 (progn
331 (remhash "process-name" value)
332 (remhash "process-buffer" value)
333 (remhash "first-password-request" value))
334 (remhash key cache)))
335 cache)
336 ;; Dump it.
337 (with-temp-buffer
338 (insert
339 ";; -*- emacs-lisp -*-"
340 ;; `time-stamp-string' might not exist in all (X)Emacs flavors.
341 (condition-case nil
342 (progn
343 (format
344 " <%s %s>\n"
345 (time-stamp-string "%02y/%02m/%02d %02H:%02M:%02S")
346 tramp-persistency-file-name))
347 (error "\n"))
348 ";; Tramp connection history. Don't change this file.\n"
349 ";; You can delete it, forcing Tramp to reapply the checks.\n\n"
350 (with-output-to-string
351 (pp (read (format "(%s)" (tramp-cache-print cache))))))
352 (write-region
353 (point-min) (point-max) tramp-persistency-file-name))))))
354
355 (add-hook 'kill-emacs-hook 'tramp-dump-connection-properties)
356 (add-hook 'tramp-cache-unload-hook
357 '(lambda ()
358 (remove-hook 'kill-emacs-hook
359 'tramp-dump-connection-properties)))
360
361 ;;;###tramp-autoload
362 (defun tramp-parse-connection-properties (method)
363 "Return a list of (user host) tuples allowed to access for METHOD.
364 This function is added always in `tramp-get-completion-function'
365 for all methods. Resulting data are derived from connection history."
366 (let (res)
367 (maphash
368 '(lambda (key value)
369 (if (and (vectorp key)
370 (string-equal method (tramp-file-name-method key))
371 (not (tramp-file-name-localname key)))
372 (push (list (tramp-file-name-user key)
373 (tramp-file-name-host key))
374 res)))
375 tramp-cache-data)
376 res))
377
378 ;; Read persistent connection history.
379 (when (and (stringp tramp-persistency-file-name)
380 (zerop (hash-table-count tramp-cache-data)))
381 (condition-case err
382 (with-temp-buffer
383 (insert-file-contents tramp-persistency-file-name)
384 (let ((list (read (current-buffer)))
385 element key item)
386 (while (setq element (pop list))
387 (setq key (pop element))
388 (while (setq item (pop element))
389 (tramp-set-connection-property key (pop item) (car item)))))
390 (setq tramp-cache-data-changed nil))
391 (file-error
392 ;; Most likely because the file doesn't exist yet. No message.
393 (clrhash tramp-cache-data))
394 (error
395 ;; File is corrupted.
396 (message "Tramp persistency file '%s' is corrupted: %s"
397 tramp-persistency-file-name (error-message-string err))
398 (clrhash tramp-cache-data))))
399
400 (add-hook 'tramp-unload-hook
401 (lambda ()
402 (unload-feature 'tramp-cache 'force)))
403
404 (provide 'tramp-cache)
405
406 ;; arch-tag: ee1739b7-7628-408c-9b96-d11a74b05d26
407 ;;; tramp-cache.el ends here