(gnus-blocked-images): Clarify privacy implications
[emacs.git] / lisp / net / tramp-cache.el
blob97c687598f2288b1dc704f1e1693a11d2169923c
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
9 ;; Package: tramp
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/>.
26 ;;; Commentary:
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
46 ;; variable is set.
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.
53 ;;; Code:
55 (require 'tramp)
56 (autoload 'time-stamp-string "time-stamp")
58 ;;; -- Cache --
60 ;;;###tramp-autoload
61 (defvar tramp-cache-data (make-hash-table :test 'equal)
62 "Hash table for remote files properties.")
64 ;;;###tramp-autoload
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."
72 :group 'tramp
73 :version "24.4"
74 :type '(repeat (list (choice :tag "File Name regexp" regexp (const nil))
75 (choice :tag " Property" string)
76 (choice :tag " Value" sexp)))
77 :require 'tramp)
79 ;;;###tramp-autoload
80 (defcustom tramp-persistency-file-name
81 (expand-file-name (locate-user-emacs-file "tramp"))
82 "File which keeps connection history for Tramp connections."
83 :group 'tramp
84 :type 'file
85 :require 'tramp)
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)
95 (let ((hash
96 (puthash key (make-hash-table :test 'equal) tramp-cache-data)))
97 (when (tramp-file-name-p key)
98 (dolist (elt tramp-connection-properties)
99 (when (string-match
100 (or (nth 0 elt) "")
101 (tramp-make-tramp-file-name key 'noloc 'nohop))
102 (tramp-set-connection-property key (nth 1 elt) (nth 2 elt)))))
103 hash)))
105 ;;;###tramp-autoload
106 (defun tramp-get-file-property (key file property default)
107 "Get the PROPERTY of FILE from the cache context of KEY.
108 Returns DEFAULT if not set."
109 ;; Unify localname. Remove hop from `tramp-file-name' structure.
110 (setq file (tramp-compat-file-name-unquote file)
111 key (copy-tramp-file-name key))
112 (setf (tramp-file-name-localname key)
113 (tramp-run-real-handler 'directory-file-name (list file))
114 (tramp-file-name-hop key) nil)
115 (let* ((hash (tramp-get-hash-table key))
116 (value (when (hash-table-p hash) (gethash property hash))))
117 (if ;; We take the value only if there is any, and
118 ;; `remote-file-name-inhibit-cache' indicates that it is still
119 ;; valid. Otherwise, DEFAULT is set.
120 (and (consp value)
121 (or (null remote-file-name-inhibit-cache)
122 (and (integerp remote-file-name-inhibit-cache)
124 (tramp-time-diff (current-time) (car value))
125 remote-file-name-inhibit-cache))
126 (and (consp remote-file-name-inhibit-cache)
127 (time-less-p
128 remote-file-name-inhibit-cache (car value)))))
129 (setq value (cdr value))
130 (setq value default))
132 (tramp-message key 8 "%s %s %s" file property value)
133 (when (>= tramp-verbose 10)
134 (let* ((var (intern (concat "tramp-cache-get-count-" property)))
135 (val (or (bound-and-true-p var)
136 (progn
137 (add-hook 'tramp-cache-unload-hook
138 (lambda () (makunbound var)))
139 0))))
140 (set var (1+ val))))
141 value))
143 ;;;###tramp-autoload
144 (defun tramp-set-file-property (key file property value)
145 "Set the PROPERTY of FILE to VALUE, in the cache context of KEY.
146 Returns VALUE."
147 ;; Unify localname. Remove hop from `tramp-file-name' structure.
148 (setq file (tramp-compat-file-name-unquote file)
149 key (copy-tramp-file-name key))
150 (setf (tramp-file-name-localname key)
151 (tramp-run-real-handler 'directory-file-name (list file))
152 (tramp-file-name-hop key) nil)
153 (let ((hash (tramp-get-hash-table key)))
154 ;; We put the timestamp there.
155 (puthash property (cons (current-time) value) hash)
156 (tramp-message key 8 "%s %s %s" file property value)
157 (when (>= tramp-verbose 10)
158 (let* ((var (intern (concat "tramp-cache-set-count-" property)))
159 (val (or (bound-and-true-p var)
160 (progn
161 (add-hook 'tramp-cache-unload-hook
162 (lambda () (makunbound var)))
163 0))))
164 (set var (1+ val))))
165 value))
167 ;;;###tramp-autoload
168 (defun tramp-flush-file-property (key file property)
169 "Remove PROPERTY of FILE in the cache context of KEY."
170 ;; Unify localname. Remove hop from `tramp-file-name' structure.
171 (setq file (tramp-compat-file-name-unquote file)
172 key (copy-tramp-file-name key))
173 (setf (tramp-file-name-localname key)
174 (tramp-run-real-handler 'directory-file-name (list file))
175 (tramp-file-name-hop key) nil)
176 (remhash property (tramp-get-hash-table key))
177 (tramp-message key 8 "%s %s" file property)
178 (when (>= tramp-verbose 10)
179 (let ((var (intern (concat "tramp-cache-set-count-" property))))
180 (makunbound var))))
182 ;;;###tramp-autoload
183 (defun tramp-flush-file-properties (key file)
184 "Remove all properties of FILE in the cache context of KEY."
185 (let* ((file (tramp-run-real-handler
186 'directory-file-name (list file)))
187 (truename (tramp-get-file-property key file "file-truename" nil)))
188 ;; Unify localname. Remove hop from `tramp-file-name' structure.
189 (setq file (tramp-compat-file-name-unquote file)
190 key (copy-tramp-file-name key))
191 (setf (tramp-file-name-localname key) file
192 (tramp-file-name-hop key) nil)
193 (tramp-message key 8 "%s" file)
194 (remhash key tramp-cache-data)
195 ;; Remove file properties of symlinks.
196 (when (and (stringp truename)
197 (not (string-equal file (directory-file-name truename))))
198 (tramp-flush-file-properties key truename))))
200 ;;;###tramp-autoload
201 (defun tramp-flush-directory-properties (key directory)
202 "Remove all properties of DIRECTORY in the cache context of KEY.
203 Remove also properties of all files in subdirectories."
204 (setq directory (tramp-compat-file-name-unquote directory))
205 (let* ((directory (tramp-run-real-handler
206 'directory-file-name (list directory)))
207 (truename (tramp-get-file-property key directory "file-truename" nil)))
208 (tramp-message key 8 "%s" directory)
209 (maphash
210 (lambda (key _value)
211 (when (and (tramp-file-name-p key)
212 (stringp (tramp-file-name-localname key))
213 (string-match (regexp-quote directory)
214 (tramp-file-name-localname key)))
215 (remhash key tramp-cache-data)))
216 tramp-cache-data)
217 ;; Remove file properties of symlinks.
218 (when (and (stringp truename)
219 (not (string-equal directory (directory-file-name truename))))
220 (tramp-flush-directory-properties key truename))))
222 ;; Reverting or killing a buffer should also flush file properties.
223 ;; They could have been changed outside Tramp. In eshell, "ls" would
224 ;; not show proper directory contents when a file has been copied or
225 ;; deleted before. We must apply `save-match-data', because it would
226 ;; corrupt other packages otherwise (reported from org).
227 (defun tramp-flush-file-function ()
228 "Flush all Tramp cache properties from `buffer-file-name'.
229 This is suppressed for temporary buffers."
230 (save-match-data
231 (unless (or (null (buffer-name))
232 (string-match "^\\( \\|\\*\\)" (buffer-name)))
233 (let ((bfn (if (stringp (buffer-file-name))
234 (buffer-file-name)
235 default-directory))
236 (tramp-verbose 0))
237 (when (tramp-tramp-file-p bfn)
238 (with-parsed-tramp-file-name bfn nil
239 (tramp-flush-file-properties v localname)))))))
241 (add-hook 'before-revert-hook 'tramp-flush-file-function)
242 (add-hook 'eshell-pre-command-hook 'tramp-flush-file-function)
243 (add-hook 'kill-buffer-hook 'tramp-flush-file-function)
244 (add-hook 'tramp-cache-unload-hook
245 (lambda ()
246 (remove-hook 'before-revert-hook
247 'tramp-flush-file-function)
248 (remove-hook 'eshell-pre-command-hook
249 'tramp-flush-file-function)
250 (remove-hook 'kill-buffer-hook
251 'tramp-flush-file-function)))
253 ;;; -- Properties --
255 ;;;###tramp-autoload
256 (defun tramp-get-connection-property (key property default)
257 "Get the named PROPERTY for the connection.
258 KEY identifies the connection, it is either a process or a
259 `tramp-file-name' structure. A special case is nil, which is
260 used to cache connection properties of the local machine. If the
261 value is not set for the connection, returns DEFAULT."
262 ;; Unify key by removing localname and hop from `tramp-file-name'
263 ;; structure. Work with a copy in order to avoid side effects.
264 (when (tramp-file-name-p key)
265 (setq key (copy-tramp-file-name key))
266 (setf (tramp-file-name-localname key) nil
267 (tramp-file-name-hop key) nil))
268 (let* ((hash (tramp-get-hash-table key))
269 (value
270 ;; If the key is an auxiliary process object, check whether
271 ;; the process is still alive.
272 (if (and (processp key) (not (process-live-p key)))
273 default
274 (if (hash-table-p hash)
275 (gethash property hash default)
276 default))))
277 (tramp-message key 7 "%s %s" property value)
278 value))
280 ;;;###tramp-autoload
281 (defun tramp-set-connection-property (key property value)
282 "Set the named PROPERTY of a connection to VALUE.
283 KEY identifies the connection, it is either a process or a
284 `tramp-file-name' structure. A special case is nil, which is
285 used to cache connection properties of the local machine.
286 PROPERTY is set persistent when KEY is a `tramp-file-name' structure."
287 ;; Unify key by removing localname and hop from `tramp-file-name'
288 ;; structure. Work with a copy in order to avoid side effects.
289 (when (tramp-file-name-p key)
290 (setq key (copy-tramp-file-name key))
291 (setf (tramp-file-name-localname key) nil
292 (tramp-file-name-hop key) nil))
293 (let ((hash (tramp-get-hash-table key)))
294 (puthash property value hash)
295 (setq tramp-cache-data-changed t)
296 (tramp-message key 7 "%s %s" property value)
297 value))
299 ;;;###tramp-autoload
300 (defun tramp-connection-property-p (key property)
301 "Check whether named PROPERTY of a connection is defined.
302 KEY identifies the connection, it is either a process or a
303 `tramp-file-name' structure. A special case is nil, which is
304 used to cache connection properties of the local machine."
305 (not (eq (tramp-get-connection-property key property 'undef) 'undef)))
307 ;;;###tramp-autoload
308 (defun tramp-flush-connection-property (key property)
309 "Remove the named PROPERTY of a connection identified by KEY.
310 KEY identifies the connection, it is either a process or a
311 `tramp-file-name' structure. A special case is nil, which is
312 used to cache connection properties of the local machine.
313 PROPERTY is set persistent when KEY is a `tramp-file-name' structure."
314 ;; Unify key by removing localname and hop from `tramp-file-name'
315 ;; structure. Work with a copy in order to avoid side effects.
316 (when (tramp-file-name-p key)
317 (setq key (copy-tramp-file-name key))
318 (setf (tramp-file-name-localname key) nil
319 (tramp-file-name-hop key) nil))
320 (remhash property (tramp-get-hash-table key))
321 (setq tramp-cache-data-changed t)
322 (tramp-message key 7 "%s" property))
324 ;;;###tramp-autoload
325 (defun tramp-flush-connection-properties (key)
326 "Remove all properties identified by KEY.
327 KEY identifies the connection, it is either a process or a
328 `tramp-file-name' structure. A special case is nil, which is
329 used to cache connection properties of the local machine."
330 ;; Unify key by removing localname and hop from `tramp-file-name'
331 ;; structure. Work with a copy in order to avoid side effects.
332 (when (tramp-file-name-p key)
333 (setq key (copy-tramp-file-name key))
334 (setf (tramp-file-name-localname key) nil
335 (tramp-file-name-hop key) nil))
336 (tramp-message
337 key 7 "%s %s" key
338 (let ((hash (gethash key tramp-cache-data))
339 properties)
340 (when (hash-table-p hash)
341 (maphash (lambda (x _y) (add-to-list 'properties x 'append)) hash))
342 properties))
343 (setq tramp-cache-data-changed t)
344 (remhash key tramp-cache-data))
346 ;;;###tramp-autoload
347 (defun tramp-cache-print (table)
348 "Print hash table TABLE."
349 (when (hash-table-p table)
350 (let (result)
351 (maphash
352 (lambda (key value)
353 ;; Remove text properties from KEY and VALUE.
354 ;; `cl-struct-slot-*' functions exist since Emacs 25 only; we
355 ;; ignore errors.
356 (when (tramp-file-name-p key)
357 ;; (dolist
358 ;; (slot
359 ;; (mapcar 'car (cdr (cl-struct-slot-info 'tramp-file-name))))
360 ;; (when (stringp (cl-struct-slot-value 'tramp-file-name slot key))
361 ;; (setf (cl-struct-slot-value 'tramp-file-name slot key)
362 ;; (substring-no-properties
363 ;; (cl-struct-slot-value 'tramp-file-name slot key))))))
364 (dotimes (i (length key))
365 (when (stringp (elt key i))
366 (setf (elt key i) (substring-no-properties (elt key i))))))
367 (when (stringp key)
368 (setq key (substring-no-properties key)))
369 (when (stringp value)
370 (setq value (substring-no-properties value)))
371 ;; Dump.
372 (let ((tmp (format
373 "(%s %s)"
374 (if (processp key)
375 (prin1-to-string (prin1-to-string key))
376 (prin1-to-string key))
377 (if (hash-table-p value)
378 (tramp-cache-print value)
379 (if (bufferp value)
380 (prin1-to-string (prin1-to-string value))
381 (prin1-to-string value))))))
382 (setq result (if result (concat result " " tmp) tmp))))
383 table)
384 result)))
386 ;;;###tramp-autoload
387 (defun tramp-list-connections ()
388 "Return all known `tramp-file-name' structs according to `tramp-cache'."
389 (let (result tramp-verbose)
390 (maphash
391 (lambda (key _value)
392 (when (and (tramp-file-name-p key)
393 (null (tramp-file-name-localname key))
394 (tramp-connection-property-p key "process-buffer"))
395 (add-to-list 'result key)))
396 tramp-cache-data)
397 result))
399 (defun tramp-dump-connection-properties ()
400 "Write persistent connection properties into file `tramp-persistency-file-name'."
401 ;; We shouldn't fail, otherwise Emacs might not be able to be closed.
402 (ignore-errors
403 (when (and (hash-table-p tramp-cache-data)
404 (not (zerop (hash-table-count tramp-cache-data)))
405 tramp-cache-data-changed
406 (stringp tramp-persistency-file-name))
407 (let ((cache (copy-hash-table tramp-cache-data))
408 print-length print-level)
409 ;; Remove temporary data. If there is the key "login-as", we
410 ;; don't save either, because all other properties might
411 ;; depend on the login name, and we want to give the
412 ;; possibility to use another login name later on. Key
413 ;; "started" exists for the "ftp" method only, which must be
414 ;; be kept persistent.
415 (maphash
416 (lambda (key value)
417 (if (and (tramp-file-name-p key) value
418 (not (string-equal
419 (tramp-file-name-method key) tramp-archive-method))
420 (not (tramp-file-name-localname key))
421 (not (gethash "login-as" value))
422 (not (gethash "started" value)))
423 (progn
424 (remhash "process-name" value)
425 (remhash "process-buffer" value)
426 (remhash "first-password-request" value))
427 (remhash key cache)))
428 cache)
429 ;; Dump it.
430 (with-temp-file tramp-persistency-file-name
431 (insert
432 ";; -*- emacs-lisp -*-"
433 ;; `time-stamp-string' might not exist in all Emacs flavors.
434 (condition-case nil
435 (progn
436 (format
437 " <%s %s>\n"
438 (time-stamp-string "%02y/%02m/%02d %02H:%02M:%02S")
439 tramp-persistency-file-name))
440 (error "\n"))
441 ";; Tramp connection history. Don't change this file.\n"
442 ";; You can delete it, forcing Tramp to reapply the checks.\n\n"
443 (with-output-to-string
444 (pp (read (format "(%s)" (tramp-cache-print cache)))))))))))
446 (unless noninteractive
447 (add-hook 'kill-emacs-hook 'tramp-dump-connection-properties))
448 (add-hook 'tramp-cache-unload-hook
449 (lambda ()
450 (remove-hook 'kill-emacs-hook
451 'tramp-dump-connection-properties)))
453 ;;;###tramp-autoload
454 (defun tramp-parse-connection-properties (method)
455 "Return a list of (user host) tuples allowed to access for METHOD.
456 This function is added always in `tramp-get-completion-function'
457 for all methods. Resulting data are derived from connection history."
458 (let (res)
459 (maphash
460 (lambda (key _value)
461 (if (and (tramp-file-name-p key)
462 (string-equal method (tramp-file-name-method key))
463 (not (tramp-file-name-localname key)))
464 (push (list (tramp-file-name-user key)
465 (tramp-file-name-host key))
466 res)))
467 tramp-cache-data)
468 res))
470 ;; When "emacs -Q" has been called, both variables are nil. We do not
471 ;; load the persistency file then, in order to have a clean test environment.
472 ;;;###tramp-autoload
473 (defvar tramp-cache-read-persistent-data (or init-file-user site-run-file)
474 "Whether to read persistent data at startup time.")
476 ;; Read persistent connection history.
477 (when (and (stringp tramp-persistency-file-name)
478 (zerop (hash-table-count tramp-cache-data))
479 tramp-cache-read-persistent-data)
480 (condition-case err
481 (with-temp-buffer
482 (insert-file-contents tramp-persistency-file-name)
483 (let ((list (read (current-buffer)))
484 (tramp-verbose 0)
485 element key item)
486 (while (setq element (pop list))
487 (setq key (pop element))
488 (when (tramp-file-name-p key)
489 (while (setq item (pop element))
490 ;; We set only values which are not contained in
491 ;; `tramp-connection-properties'. The cache is
492 ;; initialized properly by side effect.
493 (unless (tramp-connection-property-p key (car item))
494 (tramp-set-connection-property key (pop item) (car item)))))))
495 (setq tramp-cache-data-changed nil))
496 (file-error
497 ;; Most likely because the file doesn't exist yet. No message.
498 (clrhash tramp-cache-data))
499 (error
500 ;; File is corrupted.
501 (message "Tramp persistency file `%s' is corrupted: %s"
502 tramp-persistency-file-name (error-message-string err))
503 (clrhash tramp-cache-data))))
505 (add-hook 'tramp-unload-hook
506 (lambda ()
507 (unload-feature 'tramp-cache 'force)))
509 (provide 'tramp-cache)
511 ;;; tramp-cache.el ends here