]> code.delx.au - gnu-emacs/blob - lisp/net/tramp-cache.el
Remove leading `*' from docs of some defcustoms etc.
[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 (defvar tramp-cache-inhibit-cache nil
63 "Inhibit cache read access, when `t'.
64 `nil' means to accept cache entries unconditionally. If the
65 value is a timestamp (as returned by `current-time'), cache
66 entries are not used when they have been written before this
67 time.")
68
69 (defcustom tramp-persistency-file-name
70 (cond
71 ;; GNU Emacs.
72 ((and (boundp 'user-emacs-directory)
73 (stringp (symbol-value 'user-emacs-directory))
74 (file-directory-p (symbol-value 'user-emacs-directory)))
75 (expand-file-name "tramp" (symbol-value 'user-emacs-directory)))
76 ((and (not (featurep 'xemacs)) (file-directory-p "~/.emacs.d/"))
77 "~/.emacs.d/tramp")
78 ;; XEmacs.
79 ((and (boundp 'user-init-directory)
80 (stringp (symbol-value 'user-init-directory))
81 (file-directory-p (symbol-value 'user-init-directory)))
82 (expand-file-name "tramp" (symbol-value 'user-init-directory)))
83 ((and (featurep 'xemacs) (file-directory-p "~/.xemacs/"))
84 "~/.xemacs/tramp")
85 ;; For users without `~/.emacs.d/' or `~/.xemacs/'.
86 (t "~/.tramp"))
87 "File which keeps connection history for Tramp connections."
88 :group 'tramp
89 :type 'file)
90
91 (defvar tramp-cache-data-changed nil
92 "Whether persistent cache data have been changed.")
93
94 ;;;###tramp-autoload
95 (defun tramp-get-file-property (vec file property default)
96 "Get the PROPERTY of FILE from the cache context of VEC.
97 Returns DEFAULT if not set."
98 ;; Unify localname.
99 (setq vec (copy-sequence vec))
100 (aset vec 3 (tramp-run-real-handler 'directory-file-name (list file)))
101 (let* ((hash (or (gethash vec tramp-cache-data)
102 (puthash vec (make-hash-table :test 'equal)
103 tramp-cache-data)))
104 (value (when (hash-table-p hash) (gethash property hash))))
105 (if
106 ;; We take the value only if there is any, and
107 ;; `tramp-cache-inhibit-cache' indicates that it is still
108 ;; valid. Otherwise, DEFAULT is set.
109 (and (consp value)
110 (or (null tramp-cache-inhibit-cache)
111 (and (consp tramp-cache-inhibit-cache)
112 (tramp-time-less-p
113 tramp-cache-inhibit-cache (car value)))))
114 (setq value (cdr value))
115 (setq value default))
116
117 (if (consp tramp-cache-inhibit-cache)
118 (tramp-message vec 1 "%s %s %s" file property value))
119 (tramp-message vec 8 "%s %s %s" file property value)
120 value))
121
122 ;;;###tramp-autoload
123 (defun tramp-set-file-property (vec file property value)
124 "Set the PROPERTY of FILE to VALUE, in the cache context of VEC.
125 Returns VALUE."
126 ;; Unify localname.
127 (setq vec (copy-sequence vec))
128 (aset vec 3 (tramp-run-real-handler 'directory-file-name (list file)))
129 (let ((hash (or (gethash vec tramp-cache-data)
130 (puthash vec (make-hash-table :test 'equal)
131 tramp-cache-data))))
132 ;; We put the timestamp there.
133 (puthash property (cons (current-time) value) hash)
134 (tramp-message vec 8 "%s %s %s" file property value)
135 value))
136
137 ;;;###tramp-autoload
138 (defmacro with-file-property (vec file property &rest body)
139 "Check in Tramp cache for PROPERTY, otherwise execute BODY and set cache.
140 FILE must be a local file name on a connection identified via VEC."
141 `(if (file-name-absolute-p ,file)
142 (let ((value (tramp-get-file-property ,vec ,file ,property 'undef)))
143 (when (eq value 'undef)
144 ;; We cannot pass @body as parameter to
145 ;; `tramp-set-file-property' because it mangles our
146 ;; debug messages.
147 (setq value (progn ,@body))
148 (tramp-set-file-property ,vec ,file ,property value))
149 value)
150 ,@body))
151
152 ;;;###tramp-autoload
153 (put 'with-file-property 'lisp-indent-function 3)
154 (put 'with-file-property 'edebug-form-spec t)
155 (tramp-compat-font-lock-add-keywords
156 'emacs-lisp-mode '("\\<with-file-property\\>"))
157
158 ;;;###tramp-autoload
159 (defun tramp-flush-file-property (vec file)
160 "Remove all properties of FILE in the cache context of VEC."
161 ;; Unify localname.
162 (setq vec (copy-sequence vec))
163 (aset vec 3 (tramp-run-real-handler 'directory-file-name (list file)))
164 (tramp-message vec 8 "%s" file)
165 (remhash vec tramp-cache-data))
166
167 ;;;###tramp-autoload
168 (defun tramp-flush-directory-property (vec directory)
169 "Remove all properties of DIRECTORY in the cache context of VEC.
170 Remove also properties of all files in subdirectories."
171 (let ((directory (tramp-run-real-handler
172 'directory-file-name (list directory))))
173 (tramp-message vec 8 "%s" directory)
174 (maphash
175 '(lambda (key value)
176 (when (and (stringp (tramp-file-name-localname key))
177 (string-match directory (tramp-file-name-localname key)))
178 (remhash key tramp-cache-data)))
179 tramp-cache-data)))
180
181 ;; Reverting or killing a buffer should also flush file properties.
182 ;; They could have been changed outside Tramp. In eshell, "ls" would
183 ;; not show proper directory contents when a file has been copied or
184 ;; deleted before.
185 (defun tramp-flush-file-function ()
186 "Flush all Tramp cache properties from `buffer-file-name'."
187 (let ((bfn (if (stringp (buffer-file-name))
188 (buffer-file-name)
189 default-directory)))
190 (when (tramp-tramp-file-p bfn)
191 (with-parsed-tramp-file-name bfn nil
192 (tramp-flush-file-property v localname)))))
193
194 (add-hook 'before-revert-hook 'tramp-flush-file-function)
195 (add-hook 'eshell-pre-command-hook 'tramp-flush-file-function)
196 (add-hook 'kill-buffer-hook 'tramp-flush-file-function)
197 (add-hook 'tramp-cache-unload-hook
198 '(lambda ()
199 (remove-hook 'before-revert-hook
200 'tramp-flush-file-function)
201 (remove-hook 'eshell-pre-command-hook
202 'tramp-flush-file-function)
203 (remove-hook 'kill-buffer-hook
204 'tramp-flush-file-function)))
205
206 ;;; -- Properties --
207
208 ;;;###tramp-autoload
209 (defun tramp-get-connection-property (key property default)
210 "Get the named PROPERTY for the connection.
211 KEY identifies the connection, it is either a process or a vector.
212 If the value is not set for the connection, returns DEFAULT."
213 ;; Unify key by removing localname from vector. Work with a copy in
214 ;; order to avoid side effects.
215 (when (vectorp key)
216 (setq key (copy-sequence key))
217 (aset key 3 nil))
218 (let* ((hash (gethash key tramp-cache-data))
219 (value (if (hash-table-p hash)
220 (gethash property hash default)
221 default)))
222 (tramp-message key 7 "%s %s" property value)
223 value))
224
225 ;;;###tramp-autoload
226 (defun tramp-set-connection-property (key property value)
227 "Set the named PROPERTY of a connection to VALUE.
228 KEY identifies the connection, it is either a process or a vector.
229 PROPERTY is set persistent when KEY is a vector."
230 ;; Unify key by removing localname from vector. Work with a copy in
231 ;; order to avoid side effects.
232 (when (vectorp key)
233 (setq key (copy-sequence key))
234 (aset key 3 nil))
235 (let ((hash (or (gethash key tramp-cache-data)
236 (puthash key (make-hash-table :test 'equal)
237 tramp-cache-data))))
238 (puthash property value hash)
239 (setq tramp-cache-data-changed t)
240 (tramp-message key 7 "%s %s" property value)
241 value))
242
243 ;;;###tramp-autoload
244 (defmacro with-connection-property (key property &rest body)
245 "Check in Tramp for property PROPERTY, otherwise executes BODY and set."
246 `(let ((value (tramp-get-connection-property ,key ,property 'undef)))
247 (when (eq value 'undef)
248 ;; We cannot pass ,@body as parameter to
249 ;; `tramp-set-connection-property' because it mangles our debug
250 ;; messages.
251 (setq value (progn ,@body))
252 (tramp-set-connection-property ,key ,property value))
253 value))
254
255 ;;;###tramp-autoload
256 (put 'with-connection-property 'lisp-indent-function 2)
257 (put 'with-connection-property 'edebug-form-spec t)
258 (tramp-compat-font-lock-add-keywords
259 'emacs-lisp-mode '("\\<with-connection-property\\>"))
260
261 ;;;###tramp-autoload
262 (defun tramp-flush-connection-property (key)
263 "Remove all properties identified by KEY.
264 KEY identifies the connection, it is either a process or a vector."
265 ;; Unify key by removing localname from vector. Work with a copy in
266 ;; order to avoid side effects.
267 (when (vectorp key)
268 (setq key (copy-sequence key))
269 (aset key 3 nil))
270 (tramp-message
271 key 7 "%s %s" key
272 (let ((hash (gethash key tramp-cache-data))
273 properties)
274 (if (hash-table-p hash)
275 (maphash
276 (lambda (x y) (add-to-list 'properties x 'append))
277 (gethash key tramp-cache-data)))
278 properties))
279 (setq tramp-cache-data-changed t)
280 (remhash key tramp-cache-data))
281
282 ;;;###tramp-autoload
283 (defun tramp-cache-print (table)
284 "Print hash table TABLE."
285 (when (hash-table-p table)
286 (let (result)
287 (maphash
288 '(lambda (key value)
289 (let ((tmp (format
290 "(%s %s)"
291 (if (processp key)
292 (prin1-to-string (prin1-to-string key))
293 (prin1-to-string key))
294 (if (hash-table-p value)
295 (tramp-cache-print value)
296 (if (bufferp value)
297 (prin1-to-string (prin1-to-string value))
298 (prin1-to-string value))))))
299 (setq result (if result (concat result " " tmp) tmp))))
300 table)
301 result)))
302
303 ;;;###tramp-autoload
304 (defun tramp-list-connections ()
305 "Return a list of all known connection vectors according to `tramp-cache'."
306 (let (result)
307 (maphash
308 '(lambda (key value)
309 (when (and (vectorp key) (null (aref key 3)))
310 (add-to-list 'result key)))
311 tramp-cache-data)
312 result))
313
314 (defun tramp-dump-connection-properties ()
315 "Write persistent connection properties into file `tramp-persistency-file-name'."
316 ;; We shouldn't fail, otherwise (X)Emacs might not be able to be closed.
317 (ignore-errors
318 (when (and (hash-table-p tramp-cache-data)
319 (not (zerop (hash-table-count tramp-cache-data)))
320 tramp-cache-data-changed
321 (stringp tramp-persistency-file-name))
322 (let ((cache (copy-hash-table tramp-cache-data)))
323 ;; Remove temporary data.
324 (maphash
325 '(lambda (key value)
326 (if (and (vectorp key) (not (tramp-file-name-localname key)))
327 (progn
328 (remhash "process-name" value)
329 (remhash "process-buffer" value)
330 (remhash "first-password-request" value))
331 (remhash key cache)))
332 cache)
333 ;; Dump it.
334 (with-temp-buffer
335 (insert
336 ";; -*- emacs-lisp -*-"
337 ;; `time-stamp-string' might not exist in all (X)Emacs flavors.
338 (condition-case nil
339 (progn
340 (format
341 " <%s %s>\n"
342 (time-stamp-string "%02y/%02m/%02d %02H:%02M:%02S")
343 tramp-persistency-file-name))
344 (error "\n"))
345 ";; Tramp connection history. Don't change this file.\n"
346 ";; You can delete it, forcing Tramp to reapply the checks.\n\n"
347 (with-output-to-string
348 (pp (read (format "(%s)" (tramp-cache-print cache))))))
349 (write-region
350 (point-min) (point-max) tramp-persistency-file-name))))))
351
352 (add-hook 'kill-emacs-hook 'tramp-dump-connection-properties)
353 (add-hook 'tramp-cache-unload-hook
354 '(lambda ()
355 (remove-hook 'kill-emacs-hook
356 'tramp-dump-connection-properties)))
357
358 ;;;###tramp-autoload
359 (defun tramp-parse-connection-properties (method)
360 "Return a list of (user host) tuples allowed to access for METHOD.
361 This function is added always in `tramp-get-completion-function'
362 for all methods. Resulting data are derived from connection history."
363 (let (res)
364 (maphash
365 '(lambda (key value)
366 (if (and (vectorp key)
367 (string-equal method (tramp-file-name-method key))
368 (not (tramp-file-name-localname key)))
369 (push (list (tramp-file-name-user key)
370 (tramp-file-name-host key))
371 res)))
372 tramp-cache-data)
373 res))
374
375 ;; Read persistent connection history.
376 (when (and (stringp tramp-persistency-file-name)
377 (zerop (hash-table-count tramp-cache-data)))
378 (condition-case err
379 (with-temp-buffer
380 (insert-file-contents tramp-persistency-file-name)
381 (let ((list (read (current-buffer)))
382 element key item)
383 (while (setq element (pop list))
384 (setq key (pop element))
385 (while (setq item (pop element))
386 (tramp-set-connection-property key (pop item) (car item)))))
387 (setq tramp-cache-data-changed nil))
388 (file-error
389 ;; Most likely because the file doesn't exist yet. No message.
390 (clrhash tramp-cache-data))
391 (error
392 ;; File is corrupted.
393 (message "Tramp persistency file '%s' is corrupted: %s"
394 tramp-persistency-file-name (error-message-string err))
395 (clrhash tramp-cache-data))))
396
397 (add-hook 'tramp-unload-hook
398 (lambda ()
399 (unload-feature 'tramp-cache 'force)))
400
401 (provide 'tramp-cache)
402
403 ;; arch-tag: ee1739b7-7628-408c-9b96-d11a74b05d26
404 ;;; tramp-cache.el ends here