1 ;;; tramp-cache.el --- file information caching for Tramp -*- lexical-binding:t -*-
3 ;; Copyright (C) 2000, 2005-2018 Free Software Foundation, Inc.
5 ;; Author: Daniel Pittman <daniel@inanna.danann.net>
6 ;; Michael Albinus <michael.albinus@gmx.de>
7 ;; Maintainer: Michael Albinus <michael.albinus@gmx.de>
8 ;; Keywords: comm, processes
11 ;; This file is part of GNU Emacs.
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.
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.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
28 ;; An implementation of information caching for remote files.
30 ;; Each connection, identified by a `tramp-file-name' structure or by
31 ;; a process, has a unique cache. We distinguish 3 kind of caches,
32 ;; depending on the key:
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'.
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 nil, depending on the file existence, or
43 ;; "file-attributes" caches the result of the function
44 ;; `file-attributes'. These entries have a timestamp, and they
45 ;; expire after `remote-file-name-inhibit-cache' seconds if this
48 ;; - The key is a process. This are temporary properties related to
49 ;; an open connection. Examples: "scripts" keeps shell script
50 ;; definitions already sent to the remote shell, "last-cmd-time" is
51 ;; the time stamp a command has been sent to the remote process.
56 (autoload 'time-stamp-string
"time-stamp")
61 (defvar tramp-cache-data
(make-hash-table :test
'equal
)
62 "Hash table for remote files properties.")
65 (defcustom tramp-connection-properties nil
66 "List of static connection properties.
67 Every entry has the form (REGEXP PROPERTY VALUE). The regexp
68 matches remote file names. It can be nil. PROPERTY is a string,
69 and VALUE the corresponding value. They are used, if there is no
70 matching entry for PROPERTY in `tramp-cache-data'. For more
71 details see the info pages."
74 :type
'(repeat (list (choice :tag
"File Name regexp" regexp
(const nil
))
75 (choice :tag
" Property" string
)
76 (choice :tag
" Value" sexp
)))
80 (defcustom tramp-persistency-file-name
81 (expand-file-name (locate-user-emacs-file "tramp"))
82 "File which keeps connection history for Tramp connections."
87 (defvar tramp-cache-data-changed nil
88 "Whether persistent cache data have been changed.")
90 (defun tramp-get-hash-table (key)
91 "Returns the hash table for KEY.
92 If it doesn't exist yet, it is created and initialized with
93 matching entries of `tramp-connection-properties'."
94 (or (gethash key tramp-cache-data
)
96 (puthash key
(make-hash-table :test
'equal
) tramp-cache-data
)))
97 (when (tramp-file-name-p key
)
98 (dolist (elt tramp-connection-properties
)
101 (tramp-make-tramp-file-name
102 (tramp-file-name-method key
) (tramp-file-name-user key
)
103 (tramp-file-name-domain key
) (tramp-file-name-host key
)
104 (tramp-file-name-port key
) nil
))
105 (tramp-set-connection-property key
(nth 1 elt
) (nth 2 elt
)))))
109 (defun tramp-get-file-property (key file property default
)
110 "Get the PROPERTY of FILE from the cache context of KEY.
111 Returns DEFAULT if not set."
112 ;; Unify localname. Remove hop from `tramp-file-name' structure.
113 (setq file
(tramp-compat-file-name-unquote file
)
114 key
(copy-tramp-file-name key
))
115 (setf (tramp-file-name-localname key
)
116 (tramp-run-real-handler 'directory-file-name
(list file
))
117 (tramp-file-name-hop key
) nil
)
118 (let* ((hash (tramp-get-hash-table key
))
119 (value (when (hash-table-p hash
) (gethash property hash
))))
121 ;; We take the value only if there is any, and
122 ;; `remote-file-name-inhibit-cache' indicates that it is still
123 ;; valid. Otherwise, DEFAULT is set.
125 (or (null remote-file-name-inhibit-cache
)
126 (and (integerp remote-file-name-inhibit-cache
)
128 (tramp-time-diff (current-time) (car value
))
129 remote-file-name-inhibit-cache
))
130 (and (consp remote-file-name-inhibit-cache
)
132 remote-file-name-inhibit-cache
(car value
)))))
133 (setq value
(cdr value
))
134 (setq value default
))
136 (tramp-message key
8 "%s %s %s" file property value
)
137 (when (>= tramp-verbose
10)
138 (let* ((var (intern (concat "tramp-cache-get-count-" property
)))
139 (val (or (bound-and-true-p var
)
141 (add-hook 'tramp-cache-unload-hook
142 (lambda () (makunbound var
)))
148 (defun tramp-set-file-property (key file property value
)
149 "Set the PROPERTY of FILE to VALUE, in the cache context of KEY.
151 ;; Unify localname. Remove hop from `tramp-file-name' structure.
152 (setq file
(tramp-compat-file-name-unquote file
)
153 key
(copy-tramp-file-name key
))
154 (setf (tramp-file-name-localname key
)
155 (tramp-run-real-handler 'directory-file-name
(list file
))
156 (tramp-file-name-hop key
) nil
)
157 (let ((hash (tramp-get-hash-table key
)))
158 ;; We put the timestamp there.
159 (puthash property
(cons (current-time) value
) hash
)
160 (tramp-message key
8 "%s %s %s" file property value
)
161 (when (>= tramp-verbose
10)
162 (let* ((var (intern (concat "tramp-cache-set-count-" property
)))
163 (val (or (bound-and-true-p var
)
165 (add-hook 'tramp-cache-unload-hook
166 (lambda () (makunbound var
)))
172 (defun tramp-flush-file-property (key file
)
173 "Remove all properties of FILE in the cache context of KEY."
174 (let* ((file (tramp-run-real-handler
175 'directory-file-name
(list file
)))
176 (truename (tramp-get-file-property key file
"file-truename" nil
)))
177 ;; Unify localname. Remove hop from `tramp-file-name' structure.
178 (setq file
(tramp-compat-file-name-unquote file
)
179 key
(copy-tramp-file-name key
))
180 (setf (tramp-file-name-localname key
) file
181 (tramp-file-name-hop key
) nil
)
182 (tramp-message key
8 "%s" file
)
183 (remhash key tramp-cache-data
)
184 ;; Remove file properties of symlinks.
185 (when (and (stringp truename
)
186 (not (string-equal file
(directory-file-name truename
))))
187 (tramp-flush-file-property key truename
))))
190 (defun tramp-flush-directory-property (key directory
)
191 "Remove all properties of DIRECTORY in the cache context of KEY.
192 Remove also properties of all files in subdirectories."
193 (setq directory
(tramp-compat-file-name-unquote directory
))
194 (let* ((directory (tramp-run-real-handler
195 'directory-file-name
(list directory
)))
196 (truename (tramp-get-file-property key directory
"file-truename" nil
)))
197 (tramp-message key
8 "%s" directory
)
200 (when (and (tramp-file-name-p key
)
201 (stringp (tramp-file-name-localname key
))
202 (string-match (regexp-quote directory
)
203 (tramp-file-name-localname key
)))
204 (remhash key tramp-cache-data
)))
206 ;; Remove file properties of symlinks.
207 (when (and (stringp truename
)
208 (not (string-equal directory
(directory-file-name truename
))))
209 (tramp-flush-directory-property key truename
))))
211 ;; Reverting or killing a buffer should also flush file properties.
212 ;; They could have been changed outside Tramp. In eshell, "ls" would
213 ;; not show proper directory contents when a file has been copied or
214 ;; deleted before. We must apply `save-match-data', because it would
215 ;; corrupt other packages otherwise (reported from org).
216 (defun tramp-flush-file-function ()
217 "Flush all Tramp cache properties from `buffer-file-name'.
218 This is suppressed for temporary buffers."
220 (unless (or (null (buffer-name))
221 (string-match "^\\( \\|\\*\\)" (buffer-name)))
222 (let ((bfn (if (stringp (buffer-file-name))
226 (when (tramp-tramp-file-p bfn
)
227 (with-parsed-tramp-file-name bfn nil
228 (tramp-flush-file-property v localname
)))))))
230 (add-hook 'before-revert-hook
'tramp-flush-file-function
)
231 (add-hook 'eshell-pre-command-hook
'tramp-flush-file-function
)
232 (add-hook 'kill-buffer-hook
'tramp-flush-file-function
)
233 (add-hook 'tramp-cache-unload-hook
235 (remove-hook 'before-revert-hook
236 'tramp-flush-file-function
)
237 (remove-hook 'eshell-pre-command-hook
238 'tramp-flush-file-function
)
239 (remove-hook 'kill-buffer-hook
240 'tramp-flush-file-function
)))
245 (defun tramp-get-connection-property (key property default
)
246 "Get the named PROPERTY for the connection.
247 KEY identifies the connection, it is either a process or a
248 `tramp-file-name' structure. A special case is nil, which is
249 used to cache connection properties of the local machine. If the
250 value is not set for the connection, returns DEFAULT."
251 ;; Unify key by removing localname and hop from `tramp-file-name'
252 ;; structure. Work with a copy in order to avoid side effects.
253 (when (tramp-file-name-p key
)
254 (setq key
(copy-tramp-file-name key
))
255 (setf (tramp-file-name-localname key
) nil
256 (tramp-file-name-hop key
) nil
))
257 (let* ((hash (tramp-get-hash-table key
))
259 ;; If the key is an auxiliary process object, check whether
260 ;; the process is still alive.
261 (if (and (processp key
) (not (process-live-p key
)))
263 (if (hash-table-p hash
)
264 (gethash property hash default
)
266 (tramp-message key
7 "%s %s" property value
)
270 (defun tramp-set-connection-property (key property value
)
271 "Set the named PROPERTY of a connection to VALUE.
272 KEY identifies the connection, it is either a process or a
273 `tramp-file-name' structure. A special case is nil, which is
274 used to cache connection properties of the local machine.
275 PROPERTY is set persistent when KEY is a `tramp-file-name' structure."
276 ;; Unify key by removing localname and hop from `tramp-file-name'
277 ;; structure. Work with a copy in order to avoid side effects.
278 (when (tramp-file-name-p key
)
279 (setq key
(copy-tramp-file-name key
))
280 (setf (tramp-file-name-localname key
) nil
281 (tramp-file-name-hop key
) nil
))
282 (let ((hash (tramp-get-hash-table key
)))
283 (puthash property value hash
)
284 (setq tramp-cache-data-changed t
)
285 (tramp-message key
7 "%s %s" property value
)
289 (defun tramp-connection-property-p (key property
)
290 "Check whether named PROPERTY of a connection is defined.
291 KEY identifies the connection, it is either a process or a
292 `tramp-file-name' structure. A special case is nil, which is
293 used to cache connection properties of the local machine."
294 (not (eq (tramp-get-connection-property key property
'undef
) 'undef
)))
297 (defun tramp-flush-connection-property (key)
298 "Remove all properties identified by KEY.
299 KEY identifies the connection, it is either a process or a
300 `tramp-file-name' structure. A special case is nil, which is
301 used to cache connection properties of the local machine."
302 ;; Unify key by removing localname and hop from `tramp-file-name'
303 ;; structure. Work with a copy in order to avoid side effects.
304 (when (tramp-file-name-p key
)
305 (setq key
(copy-tramp-file-name key
))
306 (setf (tramp-file-name-localname key
) nil
307 (tramp-file-name-hop key
) nil
))
310 (let ((hash (gethash key tramp-cache-data
))
312 (when (hash-table-p hash
)
313 (maphash (lambda (x _y
) (add-to-list 'properties x
'append
)) hash
))
315 (setq tramp-cache-data-changed t
)
316 (remhash key tramp-cache-data
))
319 (defun tramp-cache-print (table)
320 "Print hash table TABLE."
321 (when (hash-table-p table
)
325 ;; Remove text properties from KEY and VALUE.
326 ;; `cl-struct-slot-*' functions exist since Emacs 25 only; we
328 (when (tramp-file-name-p key
)
331 ;; (mapcar 'car (cdr (cl-struct-slot-info 'tramp-file-name))))
332 ;; (when (stringp (cl-struct-slot-value 'tramp-file-name slot key))
333 ;; (setf (cl-struct-slot-value 'tramp-file-name slot key)
334 ;; (substring-no-properties
335 ;; (cl-struct-slot-value 'tramp-file-name slot key))))))
336 (dotimes (i (length key
))
337 (when (stringp (elt key i
))
338 (setf (elt key i
) (substring-no-properties (elt key i
))))))
340 (setq key
(substring-no-properties key
)))
341 (when (stringp value
)
342 (setq value
(substring-no-properties value
)))
347 (prin1-to-string (prin1-to-string key
))
348 (prin1-to-string key
))
349 (if (hash-table-p value
)
350 (tramp-cache-print value
)
352 (prin1-to-string (prin1-to-string value
))
353 (prin1-to-string value
))))))
354 (setq result
(if result
(concat result
" " tmp
) tmp
))))
359 (defun tramp-list-connections ()
360 "Return all known `tramp-file-name' structs according to `tramp-cache'."
361 (let (result tramp-verbose
)
364 (when (and (tramp-file-name-p key
)
365 (null (tramp-file-name-localname key
))
366 (tramp-connection-property-p key
"process-buffer"))
367 (add-to-list 'result key
)))
371 (defun tramp-dump-connection-properties ()
372 "Write persistent connection properties into file `tramp-persistency-file-name'."
373 ;; We shouldn't fail, otherwise Emacs might not be able to be closed.
375 (when (and (hash-table-p tramp-cache-data
)
376 (not (zerop (hash-table-count tramp-cache-data
)))
377 tramp-cache-data-changed
378 (stringp tramp-persistency-file-name
))
379 (let ((cache (copy-hash-table tramp-cache-data
))
380 print-length print-level
)
381 ;; Remove temporary data. If there is the key "login-as", we
382 ;; don't save either, because all other properties might
383 ;; depend on the login name, and we want to give the
384 ;; possibility to use another login name later on. Key
385 ;; "started" exists for the "ftp" method only, which must be
386 ;; be kept persistent.
389 (if (and (tramp-file-name-p key
) value
390 (not (tramp-file-name-localname key
))
391 (not (gethash "login-as" value
))
392 (not (gethash "started" value
)))
394 (remhash "process-name" value
)
395 (remhash "process-buffer" value
)
396 (remhash "first-password-request" value
))
397 (remhash key cache
)))
400 (with-temp-file tramp-persistency-file-name
402 ";; -*- emacs-lisp -*-"
403 ;; `time-stamp-string' might not exist in all Emacs flavors.
408 (time-stamp-string "%02y/%02m/%02d %02H:%02M:%02S")
409 tramp-persistency-file-name
))
411 ";; Tramp connection history. Don't change this file.\n"
412 ";; You can delete it, forcing Tramp to reapply the checks.\n\n"
413 (with-output-to-string
414 (pp (read (format "(%s)" (tramp-cache-print cache
)))))))))))
416 (unless noninteractive
417 (add-hook 'kill-emacs-hook
'tramp-dump-connection-properties
))
418 (add-hook 'tramp-cache-unload-hook
420 (remove-hook 'kill-emacs-hook
421 'tramp-dump-connection-properties
)))
424 (defun tramp-parse-connection-properties (method)
425 "Return a list of (user host) tuples allowed to access for METHOD.
426 This function is added always in `tramp-get-completion-function'
427 for all methods. Resulting data are derived from connection history."
431 (if (and (tramp-file-name-p key
)
432 (string-equal method
(tramp-file-name-method key
))
433 (not (tramp-file-name-localname key
)))
434 (push (list (tramp-file-name-user key
)
435 (tramp-file-name-host key
))
440 ;; When "emacs -Q" has been called, both variables are nil. We do not
441 ;; load the persistency file then, in order to have a clean test environment.
443 (defvar tramp-cache-read-persistent-data
(or init-file-user site-run-file
)
444 "Whether to read persistent data at startup time.")
446 ;; Read persistent connection history.
447 (when (and (stringp tramp-persistency-file-name
)
448 (zerop (hash-table-count tramp-cache-data
))
449 tramp-cache-read-persistent-data
)
452 (insert-file-contents tramp-persistency-file-name
)
453 (let ((list (read (current-buffer)))
456 (while (setq element
(pop list
))
457 (setq key
(pop element
))
458 (when (tramp-file-name-p key
)
459 (while (setq item
(pop element
))
460 ;; We set only values which are not contained in
461 ;; `tramp-connection-properties'. The cache is
462 ;; initialized properly by side effect.
463 (unless (tramp-connection-property-p key
(car item
))
464 (tramp-set-connection-property key
(pop item
) (car item
)))))))
465 (setq tramp-cache-data-changed nil
))
467 ;; Most likely because the file doesn't exist yet. No message.
468 (clrhash tramp-cache-data
))
470 ;; File is corrupted.
471 (message "Tramp persistency file `%s' is corrupted: %s"
472 tramp-persistency-file-name
(error-message-string err
))
473 (clrhash tramp-cache-data
))))
475 (add-hook 'tramp-unload-hook
477 (unload-feature 'tramp-cache
'force
)))
479 (provide 'tramp-cache
)
481 ;;; tramp-cache.el ends here