1 ;;; tramp-cache.el --- file information caching for Tramp
3 ;; Copyright (C) 2000, 2005-2015 Free Software Foundation, Inc.
5 ;; Author: Daniel Pittman <daniel@inanna.danann.net>
6 ;; Michael Albinus <michael.albinus@gmx.de>
7 ;; Keywords: comm, processes
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/>.
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 nil, depending on the file existence, or
42 ;; "file-attributes" caches the result of the function
43 ;; `file-attributes'. These entries have a timestamp, and they
44 ;; expire after `remote-file-name-inhibit-cache' seconds if this
47 ;; - The key is a process. This are temporary properties related to
48 ;; an open connection. Examples: "scripts" keeps shell script
49 ;; definitions already sent to the remote shell, "last-cmd-time" is
50 ;; the time stamp a command has been sent to the remote process.
55 (autoload 'time-stamp-string
"time-stamp")
60 (defvar tramp-cache-data
(make-hash-table :test
'equal
)
61 "Hash table for remote files properties.")
64 (defcustom tramp-connection-properties nil
65 "List of static connection properties.
66 Every entry has the form (REGEXP PROPERTY VALUE). The regexp
67 matches remote file names. It can be nil. PROPERTY is a string,
68 and VALUE the corresponding value. They are used, if there is no
69 matching entry for PROPERTY in `tramp-cache-data'."
72 :type
'(repeat (list (choice :tag
"File Name regexp" regexp
(const nil
))
73 (choice :tag
" Property" string
)
74 (choice :tag
" Value" sexp
))))
76 (defcustom tramp-persistency-file-name
79 ((and (fboundp 'locate-user-emacs-file
))
80 (expand-file-name (tramp-compat-funcall 'locate-user-emacs-file
"tramp")))
81 ((and (boundp 'user-emacs-directory
)
82 (stringp (symbol-value 'user-emacs-directory
))
83 (file-directory-p (symbol-value 'user-emacs-directory
)))
84 (expand-file-name "tramp" (symbol-value 'user-emacs-directory
)))
85 ((and (not (featurep 'xemacs
)) (file-directory-p "~/.emacs.d/"))
88 ((and (boundp 'user-init-directory
)
89 (stringp (symbol-value 'user-init-directory
))
90 (file-directory-p (symbol-value 'user-init-directory
)))
91 (expand-file-name "tramp" (symbol-value 'user-init-directory
)))
92 ((and (featurep 'xemacs
) (file-directory-p "~/.xemacs/"))
94 ;; For users without `~/.emacs.d/' or `~/.xemacs/'.
96 "File which keeps connection history for Tramp connections."
100 (defvar tramp-cache-data-changed nil
101 "Whether persistent cache data have been changed.")
103 (defun tramp-get-hash-table (key)
104 "Returns the hash table for KEY.
105 If it doesn't exist yet, it is created and initialized with
106 matching entries of `tramp-connection-properties'."
107 (or (gethash key tramp-cache-data
)
109 (puthash key
(make-hash-table :test
'equal
) tramp-cache-data
)))
111 (dolist (elt tramp-connection-properties
)
114 (tramp-make-tramp-file-name
115 (aref key
0) (aref key
1) (aref key
2) nil
))
116 (tramp-set-connection-property key
(nth 1 elt
) (nth 2 elt
)))))
120 (defun tramp-get-file-property (key file property default
)
121 "Get the PROPERTY of FILE from the cache context of KEY.
122 Returns DEFAULT if not set."
124 (setq key
(copy-sequence key
))
125 (aset key
3 (tramp-run-real-handler 'directory-file-name
(list file
)))
126 (let* ((hash (tramp-get-hash-table key
))
127 (value (when (hash-table-p hash
) (gethash property hash
))))
129 ;; We take the value only if there is any, and
130 ;; `remote-file-name-inhibit-cache' indicates that it is still
131 ;; valid. Otherwise, DEFAULT is set.
133 (or (null remote-file-name-inhibit-cache
)
134 (and (integerp remote-file-name-inhibit-cache
)
136 (tramp-time-diff (current-time) (car value
))
137 remote-file-name-inhibit-cache
))
138 (and (consp remote-file-name-inhibit-cache
)
140 remote-file-name-inhibit-cache
(car value
)))))
141 (setq value
(cdr value
))
142 (setq value default
))
144 (tramp-message key
8 "%s %s %s" file property value
)
145 (when (>= tramp-verbose
10)
146 (let* ((var (intern (concat "tramp-cache-get-count-" property
)))
147 (val (or (and (boundp var
) (symbol-value var
)) 0)))
152 (defun tramp-set-file-property (key file property value
)
153 "Set the PROPERTY of FILE to VALUE, in the cache context of KEY.
156 (setq key
(copy-sequence key
))
157 (aset key
3 (tramp-run-real-handler 'directory-file-name
(list file
)))
158 (let ((hash (tramp-get-hash-table key
)))
159 ;; We put the timestamp there.
160 (puthash property
(cons (current-time) value
) hash
)
161 (tramp-message key
8 "%s %s %s" file property value
)
162 (when (>= tramp-verbose
10)
163 (let* ((var (intern (concat "tramp-cache-set-count-" property
)))
164 (val (or (and (boundp var
) (symbol-value var
)) 0)))
169 (defun tramp-flush-file-property (key file
)
170 "Remove all properties of FILE in the cache context of KEY."
171 (let* ((file (tramp-run-real-handler
172 'directory-file-name
(list file
)))
173 (truename (tramp-get-file-property key file
"file-truename" nil
)))
174 ;; Remove file properties of symlinks.
175 (when (and (stringp truename
)
176 (not (string-equal file
(directory-file-name truename
))))
177 (tramp-flush-file-property key truename
))
179 (setq key
(copy-sequence key
))
181 (tramp-message key
8 "%s" file
)
182 (remhash key tramp-cache-data
)))
185 (defun tramp-flush-directory-property (key directory
)
186 "Remove all properties of DIRECTORY in the cache context of KEY.
187 Remove also properties of all files in subdirectories."
188 (let* ((directory (tramp-run-real-handler
189 'directory-file-name
(list directory
)))
190 (truename (tramp-get-file-property key directory
"file-truename" nil
)))
191 ;; Remove file properties of symlinks.
192 (when (and (stringp truename
)
193 (not (string-equal directory
(directory-file-name truename
))))
194 (tramp-flush-directory-property key truename
))
195 (tramp-message key
8 "%s" directory
)
198 (when (and (stringp (tramp-file-name-localname key
))
199 (string-match (regexp-quote directory
)
200 (tramp-file-name-localname key
)))
201 (remhash key tramp-cache-data
)))
204 ;; Reverting or killing a buffer should also flush file properties.
205 ;; They could have been changed outside Tramp. In eshell, "ls" would
206 ;; not show proper directory contents when a file has been copied or
207 ;; deleted before. We must apply `save-match-data', because it would
208 ;; corrupt other packages otherwise (reported from org).
209 (defun tramp-flush-file-function ()
210 "Flush all Tramp cache properties from `buffer-file-name'.
211 This is suppressed for temporary buffers."
213 (unless (or (null (buffer-name))
214 (string-match "^\\( \\|\\*\\)" (buffer-name)))
215 (let ((bfn (if (stringp (buffer-file-name))
219 (when (tramp-tramp-file-p bfn
)
220 (with-parsed-tramp-file-name bfn nil
221 (tramp-flush-file-property v localname
)))))))
223 (add-hook 'before-revert-hook
'tramp-flush-file-function
)
224 (add-hook 'eshell-pre-command-hook
'tramp-flush-file-function
)
225 (add-hook 'kill-buffer-hook
'tramp-flush-file-function
)
226 (add-hook 'tramp-cache-unload-hook
228 (remove-hook 'before-revert-hook
229 'tramp-flush-file-function
)
230 (remove-hook 'eshell-pre-command-hook
231 'tramp-flush-file-function
)
232 (remove-hook 'kill-buffer-hook
233 'tramp-flush-file-function
)))
238 (defun tramp-get-connection-property (key property default
)
239 "Get the named PROPERTY for the connection.
240 KEY identifies the connection, it is either a process or a vector.
241 If the value is not set for the connection, returns DEFAULT."
242 ;; Unify key by removing localname from vector. Work with a copy in
243 ;; order to avoid side effects.
245 (setq key
(copy-sequence key
))
247 (let* ((hash (tramp-get-hash-table key
))
248 (value (if (hash-table-p hash
)
249 (gethash property hash default
)
251 (tramp-message key
7 "%s %s" property value
)
255 (defun tramp-set-connection-property (key property value
)
256 "Set the named PROPERTY of a connection to VALUE.
257 KEY identifies the connection, it is either a process or a vector.
258 PROPERTY is set persistent when KEY is a vector."
259 ;; Unify key by removing localname from vector. Work with a copy in
260 ;; order to avoid side effects.
262 (setq key
(copy-sequence key
))
264 (let ((hash (tramp-get-hash-table key
)))
265 (puthash property value hash
)
266 (setq tramp-cache-data-changed t
)
267 (tramp-message key
7 "%s %s" property value
)
271 (defun tramp-connection-property-p (key property
)
272 "Check whether named PROPERTY of a connection is defined.
273 KEY identifies the connection, it is either a process or a vector."
274 (not (eq (tramp-get-connection-property key property
'undef
) 'undef
)))
277 (defun tramp-flush-connection-property (key)
278 "Remove all properties identified by KEY.
279 KEY identifies the connection, it is either a process or a vector."
280 ;; Unify key by removing localname from vector. Work with a copy in
281 ;; order to avoid side effects.
283 (setq key
(copy-sequence key
))
287 (let ((hash (gethash key tramp-cache-data
))
289 (when (hash-table-p hash
)
290 (maphash (lambda (x _y
) (add-to-list 'properties x
'append
)) hash
))
292 (setq tramp-cache-data-changed t
)
293 (remhash key tramp-cache-data
))
296 (defun tramp-cache-print (table)
297 "Print hash table TABLE."
298 (when (hash-table-p table
)
302 ;; Remove text properties from KEY and VALUE.
303 ;; `substring-no-properties' does not exist in XEmacs.
304 (when (functionp 'substring-no-properties
)
306 (dotimes (i (length key
))
307 (when (stringp (aref key i
))
309 (tramp-compat-funcall
310 'substring-no-properties
(aref key i
))))))
312 (setq key
(tramp-compat-funcall 'substring-no-properties key
)))
313 (when (stringp value
)
315 (tramp-compat-funcall 'substring-no-properties value
))))
320 (prin1-to-string (prin1-to-string key
))
321 (prin1-to-string key
))
322 (if (hash-table-p value
)
323 (tramp-cache-print value
)
325 (prin1-to-string (prin1-to-string value
))
326 (prin1-to-string value
))))))
327 (setq result
(if result
(concat result
" " tmp
) tmp
))))
332 (defun tramp-list-connections ()
333 "Return a list of all known connection vectors according to `tramp-cache'."
337 (when (and (vectorp key
) (null (aref key
3)))
338 (add-to-list 'result key
)))
342 (defun tramp-dump-connection-properties ()
343 "Write persistent connection properties into file `tramp-persistency-file-name'."
344 ;; We shouldn't fail, otherwise (X)Emacs might not be able to be closed.
346 (when (and (hash-table-p tramp-cache-data
)
347 (not (zerop (hash-table-count tramp-cache-data
)))
348 tramp-cache-data-changed
349 (stringp tramp-persistency-file-name
))
350 (let ((cache (copy-hash-table tramp-cache-data
))
351 print-length print-level
)
352 ;; Remove temporary data. If there is the key "login-as", we
353 ;; don't save either, because all other properties might
354 ;; depend on the login name, and we want to give the
355 ;; possibility to use another login name later on.
358 (if (and (vectorp key
)
359 (not (tramp-file-name-localname key
))
360 (not (gethash "login-as" value
)))
362 (remhash "process-name" value
)
363 (remhash "process-buffer" value
)
364 (remhash "first-password-request" value
))
365 (remhash key cache
)))
368 (with-temp-file tramp-persistency-file-name
370 ";; -*- emacs-lisp -*-"
371 ;; `time-stamp-string' might not exist in all (X)Emacs flavors.
376 (time-stamp-string "%02y/%02m/%02d %02H:%02M:%02S")
377 tramp-persistency-file-name
))
379 ";; Tramp connection history. Don't change this file.\n"
380 ";; You can delete it, forcing Tramp to reapply the checks.\n\n"
381 (with-output-to-string
382 (pp (read (format "(%s)" (tramp-cache-print cache
)))))))))))
384 (unless noninteractive
385 (add-hook 'kill-emacs-hook
'tramp-dump-connection-properties
))
386 (add-hook 'tramp-cache-unload-hook
388 (remove-hook 'kill-emacs-hook
389 'tramp-dump-connection-properties
)))
392 (defun tramp-parse-connection-properties (method)
393 "Return a list of (user host) tuples allowed to access for METHOD.
394 This function is added always in `tramp-get-completion-function'
395 for all methods. Resulting data are derived from connection history."
399 (if (and (vectorp key
)
400 (string-equal method
(tramp-file-name-method key
))
401 (not (tramp-file-name-localname key
)))
402 (push (list (tramp-file-name-user key
)
403 (tramp-file-name-host key
))
408 ;; Read persistent connection history.
409 (when (and (stringp tramp-persistency-file-name
)
410 (zerop (hash-table-count tramp-cache-data
))
411 ;; When "emacs -Q" has been called, both variables are nil.
412 ;; We do not load the persistency file then, in order to
413 ;; have a clean test environment.
414 (or (and (boundp 'init-file-user
) (symbol-value 'init-file-user
))
415 (and (boundp 'site-run-file
) (symbol-value 'site-run-file
))))
418 (insert-file-contents tramp-persistency-file-name
)
419 (let ((list (read (current-buffer)))
422 (while (setq element
(pop list
))
423 (setq key
(pop element
))
424 (while (setq item
(pop element
))
425 ;; We set only values which are not contained in
426 ;; `tramp-connection-properties'. The cache is
427 ;; initialized properly by side effect.
428 (unless (tramp-connection-property-p key
(car item
))
429 (tramp-set-connection-property key
(pop item
) (car item
))))))
430 (setq tramp-cache-data-changed nil
))
432 ;; Most likely because the file doesn't exist yet. No message.
433 (clrhash tramp-cache-data
))
435 ;; File is corrupted.
436 (message "Tramp persistency file '%s' is corrupted: %s"
437 tramp-persistency-file-name
(error-message-string err
))
438 (clrhash tramp-cache-data
))))
440 (add-hook 'tramp-unload-hook
442 (unload-feature 'tramp-cache
'force
)))
444 (provide 'tramp-cache
)
446 ;;; tramp-cache.el ends here