* lisp/dired-aux.el (dired-diff): Remove (require 'diff) since
[emacs.git] / lisp / net / tramp-cache.el
blobfe5eb0049d089c82a1a8272303842237d2d329a5
1 ;;; tramp-cache.el --- file information caching for Tramp
3 ;; Copyright (C) 2000, 2005-2012 Free Software Foundation, Inc.
5 ;; Author: Daniel Pittman <daniel@inanna.danann.net>
6 ;; Michael Albinus <michael.albinus@gmx.de>
7 ;; Keywords: comm, processes
8 ;; Package: tramp
10 ;; This file is part of GNU Emacs.
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.
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.
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/>.
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 (require 'tramp)
53 (autoload 'time-stamp-string "time-stamp")
55 ;;; -- Cache --
57 ;;;###tramp-autoload
58 (defvar tramp-cache-data (make-hash-table :test 'equal)
59 "Hash table for remote files properties.")
61 (defcustom tramp-persistency-file-name
62 (cond
63 ;; GNU Emacs.
64 ((and (fboundp 'locate-user-emacs-file))
65 (expand-file-name (tramp-compat-funcall 'locate-user-emacs-file "tramp")))
66 ((and (boundp 'user-emacs-directory)
67 (stringp (symbol-value 'user-emacs-directory))
68 (file-directory-p (symbol-value 'user-emacs-directory)))
69 (expand-file-name "tramp" (symbol-value 'user-emacs-directory)))
70 ((and (not (featurep 'xemacs)) (file-directory-p "~/.emacs.d/"))
71 "~/.emacs.d/tramp")
72 ;; XEmacs.
73 ((and (boundp 'user-init-directory)
74 (stringp (symbol-value 'user-init-directory))
75 (file-directory-p (symbol-value 'user-init-directory)))
76 (expand-file-name "tramp" (symbol-value 'user-init-directory)))
77 ((and (featurep 'xemacs) (file-directory-p "~/.xemacs/"))
78 "~/.xemacs/tramp")
79 ;; For users without `~/.emacs.d/' or `~/.xemacs/'.
80 (t "~/.tramp"))
81 "File which keeps connection history for Tramp connections."
82 :group 'tramp
83 :type 'file)
85 (defvar tramp-cache-data-changed nil
86 "Whether persistent cache data have been changed.")
88 ;;;###tramp-autoload
89 (defun tramp-get-file-property (vec file property default)
90 "Get the PROPERTY of FILE from the cache context of VEC.
91 Returns DEFAULT if not set."
92 ;; Unify localname.
93 (setq vec (copy-sequence vec))
94 (aset vec 3 (tramp-run-real-handler 'directory-file-name (list file)))
95 (let* ((hash (or (gethash vec tramp-cache-data)
96 (puthash vec (make-hash-table :test 'equal)
97 tramp-cache-data)))
98 (value (when (hash-table-p hash) (gethash property hash))))
99 (if
100 ;; We take the value only if there is any, and
101 ;; `remote-file-name-inhibit-cache' indicates that it is still
102 ;; valid. Otherwise, DEFAULT is set.
103 (and (consp value)
104 (or (null remote-file-name-inhibit-cache)
105 (and (integerp remote-file-name-inhibit-cache)
107 (tramp-time-diff (current-time) (car value))
108 remote-file-name-inhibit-cache))
109 (and (consp remote-file-name-inhibit-cache)
110 (tramp-time-less-p
111 remote-file-name-inhibit-cache (car value)))))
112 (setq value (cdr value))
113 (setq value default))
115 (tramp-message vec 8 "%s %s %s" file property value)
116 (when (>= tramp-verbose 10)
117 (let* ((var (intern (concat "tramp-cache-get-count-" property)))
118 (val (or (ignore-errors (symbol-value var)) 0)))
119 (set var (1+ val))))
120 value))
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 (when (>= tramp-verbose 10)
136 (let* ((var (intern (concat "tramp-cache-set-count-" property)))
137 (val (or (ignore-errors (symbol-value var)) 0)))
138 (set var (1+ val))))
139 value))
141 ;;;###tramp-autoload
142 (defmacro with-file-property (vec file property &rest body)
143 "Check in Tramp cache for PROPERTY, otherwise execute BODY and set cache.
144 FILE must be a local file name on a connection identified via VEC."
145 `(if (file-name-absolute-p ,file)
146 (let ((value (tramp-get-file-property ,vec ,file ,property 'undef)))
147 (when (eq value 'undef)
148 ;; We cannot pass @body as parameter to
149 ;; `tramp-set-file-property' because it mangles our
150 ;; debug messages.
151 (setq value (progn ,@body))
152 (tramp-set-file-property ,vec ,file ,property value))
153 value)
154 ,@body))
156 ;;;###tramp-autoload
157 (put 'with-file-property 'lisp-indent-function 3)
158 (put 'with-file-property 'edebug-form-spec t)
159 (tramp-compat-font-lock-add-keywords
160 'emacs-lisp-mode '("\\<with-file-property\\>"))
162 ;;;###tramp-autoload
163 (defun tramp-flush-file-property (vec file)
164 "Remove all properties of FILE in the cache context of VEC."
165 ;; Remove file property of symlinks.
166 (let ((truename (tramp-get-file-property vec file "file-truename" nil)))
167 (when (and (stringp truename)
168 (not (string-equal file truename)))
169 (tramp-flush-file-property vec truename)))
170 ;; Unify localname.
171 (setq vec (copy-sequence vec))
172 (aset vec 3 (tramp-run-real-handler 'directory-file-name (list file)))
173 (tramp-message vec 8 "%s" file)
174 (remhash vec tramp-cache-data))
176 ;;;###tramp-autoload
177 (defun tramp-flush-directory-property (vec directory)
178 "Remove all properties of DIRECTORY in the cache context of VEC.
179 Remove also properties of all files in subdirectories."
180 (let ((directory (tramp-run-real-handler
181 'directory-file-name (list directory))))
182 (tramp-message vec 8 "%s" directory)
183 (maphash
184 (lambda (key value)
185 (when (and (stringp (tramp-file-name-localname key))
186 (string-match directory (tramp-file-name-localname key)))
187 (remhash key tramp-cache-data)))
188 tramp-cache-data)))
190 ;; Reverting or killing a buffer should also flush file properties.
191 ;; They could have been changed outside Tramp. In eshell, "ls" would
192 ;; not show proper directory contents when a file has been copied or
193 ;; deleted before.
194 (defun tramp-flush-file-function ()
195 "Flush all Tramp cache properties from `buffer-file-name'."
196 (let ((bfn (if (stringp (buffer-file-name))
197 (buffer-file-name)
198 default-directory)))
199 (when (tramp-tramp-file-p bfn)
200 (with-parsed-tramp-file-name bfn nil
201 (tramp-flush-file-property v localname)))))
203 (add-hook 'before-revert-hook 'tramp-flush-file-function)
204 (add-hook 'eshell-pre-command-hook 'tramp-flush-file-function)
205 (add-hook 'kill-buffer-hook 'tramp-flush-file-function)
206 (add-hook 'tramp-cache-unload-hook
207 (lambda ()
208 (remove-hook 'before-revert-hook
209 'tramp-flush-file-function)
210 (remove-hook 'eshell-pre-command-hook
211 'tramp-flush-file-function)
212 (remove-hook 'kill-buffer-hook
213 'tramp-flush-file-function)))
215 ;;; -- Properties --
217 ;;;###tramp-autoload
218 (defun tramp-get-connection-property (key property default)
219 "Get the named PROPERTY for the connection.
220 KEY identifies the connection, it is either a process or a vector.
221 If the value is not set for the connection, returns DEFAULT."
222 ;; Unify key by removing localname from vector. Work with a copy in
223 ;; order to avoid side effects.
224 (when (vectorp key)
225 (setq key (copy-sequence key))
226 (aset key 3 nil))
227 (let* ((hash (gethash key tramp-cache-data))
228 (value (if (hash-table-p hash)
229 (gethash property hash default)
230 default)))
231 (tramp-message key 7 "%s %s" property value)
232 value))
234 ;;;###tramp-autoload
235 (defun tramp-set-connection-property (key property value)
236 "Set the named PROPERTY of a connection to VALUE.
237 KEY identifies the connection, it is either a process or a vector.
238 PROPERTY is set persistent when KEY is a vector."
239 ;; Unify key by removing localname from vector. Work with a copy in
240 ;; order to avoid side effects.
241 (when (vectorp key)
242 (setq key (copy-sequence key))
243 (aset key 3 nil))
244 (let ((hash (or (gethash key tramp-cache-data)
245 (puthash key (make-hash-table :test 'equal)
246 tramp-cache-data))))
247 (puthash property value hash)
248 (setq tramp-cache-data-changed t)
249 (tramp-message key 7 "%s %s" property value)
250 value))
252 ;;;###tramp-autoload
253 (defmacro with-connection-property (key property &rest body)
254 "Check in Tramp for property PROPERTY, otherwise executes BODY and set."
255 `(let ((value (tramp-get-connection-property ,key ,property 'undef)))
256 (when (eq value 'undef)
257 ;; We cannot pass ,@body as parameter to
258 ;; `tramp-set-connection-property' because it mangles our debug
259 ;; messages.
260 (setq value (progn ,@body))
261 (tramp-set-connection-property ,key ,property value))
262 value))
264 ;;;###tramp-autoload
265 (put 'with-connection-property 'lisp-indent-function 2)
266 (put 'with-connection-property 'edebug-form-spec t)
267 (tramp-compat-font-lock-add-keywords
268 'emacs-lisp-mode '("\\<with-connection-property\\>"))
270 ;;;###tramp-autoload
271 (defun tramp-flush-connection-property (key)
272 "Remove all properties identified by KEY.
273 KEY identifies the connection, it is either a process or a vector."
274 ;; Unify key by removing localname from vector. Work with a copy in
275 ;; order to avoid side effects.
276 (when (vectorp key)
277 (setq key (copy-sequence key))
278 (aset key 3 nil))
279 (tramp-message
280 key 7 "%s %s" key
281 (let ((hash (gethash key tramp-cache-data))
282 properties)
283 (if (hash-table-p hash)
284 (maphash
285 (lambda (x y) (add-to-list 'properties x 'append))
286 (gethash key tramp-cache-data)))
287 properties))
288 (setq tramp-cache-data-changed t)
289 (remhash key tramp-cache-data))
291 ;;;###tramp-autoload
292 (defun tramp-cache-print (table)
293 "Print hash table TABLE."
294 (when (hash-table-p table)
295 (let (result)
296 (maphash
297 (lambda (key value)
298 (let ((tmp (format
299 "(%s %s)"
300 (if (processp key)
301 (prin1-to-string (prin1-to-string key))
302 (prin1-to-string key))
303 (if (hash-table-p value)
304 (tramp-cache-print value)
305 (if (bufferp value)
306 (prin1-to-string (prin1-to-string value))
307 (prin1-to-string value))))))
308 (setq result (if result (concat result " " tmp) tmp))))
309 table)
310 result)))
312 ;;;###tramp-autoload
313 (defun tramp-list-connections ()
314 "Return a list of all known connection vectors according to `tramp-cache'."
315 (let (result)
316 (maphash
317 (lambda (key value)
318 (when (and (vectorp key) (null (aref key 3)))
319 (add-to-list 'result key)))
320 tramp-cache-data)
321 result))
323 (defun tramp-dump-connection-properties ()
324 "Write persistent connection properties into file `tramp-persistency-file-name'."
325 ;; We shouldn't fail, otherwise (X)Emacs might not be able to be closed.
326 (ignore-errors
327 (when (and (hash-table-p tramp-cache-data)
328 (not (zerop (hash-table-count tramp-cache-data)))
329 tramp-cache-data-changed
330 (stringp tramp-persistency-file-name))
331 (let ((cache (copy-hash-table tramp-cache-data))
332 print-length print-level)
333 ;; Remove temporary data. If there is the key "login-as", we
334 ;; don't save either, because all other properties might
335 ;; depend on the login name, and we want to give the
336 ;; possibility to use another login name later on.
337 (maphash
338 (lambda (key value)
339 (if (and (vectorp key)
340 (not (tramp-file-name-localname key))
341 (not (gethash "login-as" value)))
342 (progn
343 (remhash "process-name" value)
344 (remhash "process-buffer" value)
345 (remhash "first-password-request" value))
346 (remhash key cache)))
347 cache)
348 ;; Dump it.
349 (with-temp-buffer
350 (insert
351 ";; -*- emacs-lisp -*-"
352 ;; `time-stamp-string' might not exist in all (X)Emacs flavors.
353 (condition-case nil
354 (progn
355 (format
356 " <%s %s>\n"
357 (time-stamp-string "%02y/%02m/%02d %02H:%02M:%02S")
358 tramp-persistency-file-name))
359 (error "\n"))
360 ";; Tramp connection history. Don't change this file.\n"
361 ";; You can delete it, forcing Tramp to reapply the checks.\n\n"
362 (with-output-to-string
363 (pp (read (format "(%s)" (tramp-cache-print cache))))))
364 (write-region
365 (point-min) (point-max) tramp-persistency-file-name))))))
367 (unless noninteractive
368 (add-hook 'kill-emacs-hook 'tramp-dump-connection-properties))
369 (add-hook 'tramp-cache-unload-hook
370 (lambda ()
371 (remove-hook 'kill-emacs-hook
372 'tramp-dump-connection-properties)))
374 ;;;###tramp-autoload
375 (defun tramp-parse-connection-properties (method)
376 "Return a list of (user host) tuples allowed to access for METHOD.
377 This function is added always in `tramp-get-completion-function'
378 for all methods. Resulting data are derived from connection history."
379 (let (res)
380 (maphash
381 (lambda (key value)
382 (if (and (vectorp key)
383 (string-equal method (tramp-file-name-method key))
384 (not (tramp-file-name-localname key)))
385 (push (list (tramp-file-name-user key)
386 (tramp-file-name-host key))
387 res)))
388 tramp-cache-data)
389 res))
391 ;; Read persistent connection history.
392 (when (and (stringp tramp-persistency-file-name)
393 (zerop (hash-table-count tramp-cache-data))
394 ;; When "emacs -Q" has been called, both variables are nil.
395 ;; We do not load the persistency file then, in order to
396 ;; have a clean test environment.
397 (or (and (boundp 'init-file-user) (symbol-value 'init-file-user))
398 (and (boundp 'site-run-file) (symbol-value 'site-run-file))))
399 (condition-case err
400 (with-temp-buffer
401 (insert-file-contents tramp-persistency-file-name)
402 (let ((list (read (current-buffer)))
403 element key item)
404 (while (setq element (pop list))
405 (setq key (pop element))
406 (while (setq item (pop element))
407 (tramp-set-connection-property key (pop item) (car item)))))
408 (setq tramp-cache-data-changed nil))
409 (file-error
410 ;; Most likely because the file doesn't exist yet. No message.
411 (clrhash tramp-cache-data))
412 (error
413 ;; File is corrupted.
414 (message "Tramp persistency file '%s' is corrupted: %s"
415 tramp-persistency-file-name (error-message-string err))
416 (clrhash tramp-cache-data))))
418 (add-hook 'tramp-unload-hook
419 (lambda ()
420 (unload-feature 'tramp-cache 'force)))
422 (provide 'tramp-cache)
424 ;;; tramp-cache.el ends here