Merge branch 'master' into comment-cache
[emacs.git] / lisp / net / tramp-cache.el
blob5205eceacffa999c25fa2fd2d14fe6432b3cc168
1 ;;; tramp-cache.el --- file information caching for Tramp
3 ;; Copyright (C) 2000, 2005-2017 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 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
45 ;; variable is set.
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.
52 ;;; Code:
54 (require 'tramp)
55 (autoload 'time-stamp-string "time-stamp")
57 ;;; -- Cache --
59 ;;;###tramp-autoload
60 (defvar tramp-cache-data (make-hash-table :test 'equal)
61 "Hash table for remote files properties.")
63 ;;;###tramp-autoload
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'. For more
70 details see the info pages."
71 :group 'tramp
72 :version "24.4"
73 :type '(repeat (list (choice :tag "File Name regexp" regexp (const nil))
74 (choice :tag " Property" string)
75 (choice :tag " Value" sexp)))
76 :require 'tramp)
78 ;;;###tramp-autoload
79 (defcustom tramp-persistency-file-name
80 (expand-file-name (locate-user-emacs-file "tramp"))
81 "File which keeps connection history for Tramp connections."
82 :group 'tramp
83 :type 'file
84 :require 'tramp)
86 (defvar tramp-cache-data-changed nil
87 "Whether persistent cache data have been changed.")
89 (defun tramp-get-hash-table (key)
90 "Returns the hash table for KEY.
91 If it doesn't exist yet, it is created and initialized with
92 matching entries of `tramp-connection-properties'."
93 (or (gethash key tramp-cache-data)
94 (let ((hash
95 (puthash key (make-hash-table :test 'equal) tramp-cache-data)))
96 (when (vectorp key)
97 (dolist (elt tramp-connection-properties)
98 (when (string-match
99 (or (nth 0 elt) "")
100 (tramp-make-tramp-file-name
101 (aref key 0) (aref key 1) (aref key 2) nil))
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 vector.
110 (setq file (tramp-compat-file-name-unquote file))
111 (setq key (copy-sequence key))
112 (aset key 3 (tramp-run-real-handler 'directory-file-name (list file)))
113 (aset key 4 nil)
114 (let* ((hash (tramp-get-hash-table key))
115 (value (when (hash-table-p hash) (gethash property hash))))
117 ;; 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 (and (boundp var) (symbol-value var)) 0)))
136 (set var (1+ val))))
137 value))
139 ;;;###tramp-autoload
140 (defun tramp-set-file-property (key file property value)
141 "Set the PROPERTY of FILE to VALUE, in the cache context of KEY.
142 Returns VALUE."
143 ;; Unify localname. Remove hop from vector.
144 (setq file (tramp-compat-file-name-unquote file))
145 (setq key (copy-sequence key))
146 (aset key 3 (tramp-run-real-handler 'directory-file-name (list file)))
147 (aset key 4 nil)
148 (let ((hash (tramp-get-hash-table key)))
149 ;; We put the timestamp there.
150 (puthash property (cons (current-time) value) hash)
151 (tramp-message key 8 "%s %s %s" file property value)
152 (when (>= tramp-verbose 10)
153 (let* ((var (intern (concat "tramp-cache-set-count-" property)))
154 (val (or (and (boundp var) (symbol-value var)) 0)))
155 (set var (1+ val))))
156 value))
158 ;;;###tramp-autoload
159 (defun tramp-flush-file-property (key file)
160 "Remove all properties of FILE in the cache context of KEY."
161 (let* ((file (tramp-run-real-handler
162 'directory-file-name (list file)))
163 (truename (tramp-get-file-property key file "file-truename" nil)))
164 ;; Unify localname. Remove hop from vector.
165 (setq file (tramp-compat-file-name-unquote file))
166 (setq key (copy-sequence key))
167 (aset key 3 file)
168 (aset key 4 nil)
169 (tramp-message key 8 "%s" file)
170 (remhash key tramp-cache-data)
171 ;; Remove file properties of symlinks.
172 (when (and (stringp truename)
173 (not (string-equal file (directory-file-name truename))))
174 (tramp-flush-file-property key truename))))
176 ;;;###tramp-autoload
177 (defun tramp-flush-directory-property (key directory)
178 "Remove all properties of DIRECTORY in the cache context of KEY.
179 Remove also properties of all files in subdirectories."
180 (setq directory (tramp-compat-file-name-unquote directory))
181 (let* ((directory (tramp-run-real-handler
182 'directory-file-name (list directory)))
183 (truename (tramp-get-file-property key directory "file-truename" nil)))
184 (tramp-message key 8 "%s" directory)
185 (maphash
186 (lambda (key _value)
187 (when (and (stringp (tramp-file-name-localname key))
188 (string-match (regexp-quote directory)
189 (tramp-file-name-localname key)))
190 (remhash key tramp-cache-data)))
191 tramp-cache-data)
192 ;; Remove file properties of symlinks.
193 (when (and (stringp truename)
194 (not (string-equal directory (directory-file-name truename))))
195 (tramp-flush-directory-property key truename))))
197 ;; Reverting or killing a buffer should also flush file properties.
198 ;; They could have been changed outside Tramp. In eshell, "ls" would
199 ;; not show proper directory contents when a file has been copied or
200 ;; deleted before. We must apply `save-match-data', because it would
201 ;; corrupt other packages otherwise (reported from org).
202 (defun tramp-flush-file-function ()
203 "Flush all Tramp cache properties from `buffer-file-name'.
204 This is suppressed for temporary buffers."
205 (save-match-data
206 (unless (or (null (buffer-name))
207 (string-match "^\\( \\|\\*\\)" (buffer-name)))
208 (let ((bfn (if (stringp (buffer-file-name))
209 (buffer-file-name)
210 default-directory))
211 (tramp-verbose 0))
212 (when (tramp-tramp-file-p bfn)
213 (with-parsed-tramp-file-name bfn nil
214 (tramp-flush-file-property v localname)))))))
216 (add-hook 'before-revert-hook 'tramp-flush-file-function)
217 (add-hook 'eshell-pre-command-hook 'tramp-flush-file-function)
218 (add-hook 'kill-buffer-hook 'tramp-flush-file-function)
219 (add-hook 'tramp-cache-unload-hook
220 (lambda ()
221 (remove-hook 'before-revert-hook
222 'tramp-flush-file-function)
223 (remove-hook 'eshell-pre-command-hook
224 'tramp-flush-file-function)
225 (remove-hook 'kill-buffer-hook
226 'tramp-flush-file-function)))
228 ;;; -- Properties --
230 ;;;###tramp-autoload
231 (defun tramp-get-connection-property (key property default)
232 "Get the named PROPERTY for the connection.
233 KEY identifies the connection, it is either a process or a
234 vector. A special case is nil, which is used to cache connection
235 properties of the local machine. If the value is not set for the
236 connection, returns DEFAULT."
237 ;; Unify key by removing localname and hop from vector. Work with a
238 ;; copy in order to avoid side effects.
239 (when (vectorp key)
240 (setq key (copy-sequence key))
241 (aset key 3 nil)
242 (aset key 4 nil))
243 (let* ((hash (tramp-get-hash-table key))
244 (value
245 ;; If the key is an auxiliary process object, check whether
246 ;; the process is still alive.
247 (if (and (processp key) (not (tramp-compat-process-live-p key)))
248 default
249 (if (hash-table-p hash)
250 (gethash property hash default)
251 default))))
252 (tramp-message key 7 "%s %s" property value)
253 value))
255 ;;;###tramp-autoload
256 (defun tramp-set-connection-property (key property value)
257 "Set the named PROPERTY of a connection to VALUE.
258 KEY identifies the connection, it is either a process or a
259 vector. A special case is nil, which is used to cache connection
260 properties of the local machine. PROPERTY is set persistent when
261 KEY is a vector."
262 ;; Unify key by removing localname and hop from vector. Work with a
263 ;; copy in order to avoid side effects.
264 (when (vectorp key)
265 (setq key (copy-sequence key))
266 (aset key 3 nil)
267 (aset key 4 nil))
268 (let ((hash (tramp-get-hash-table key)))
269 (puthash property value hash)
270 (setq tramp-cache-data-changed t)
271 (tramp-message key 7 "%s %s" property value)
272 value))
274 ;;;###tramp-autoload
275 (defun tramp-connection-property-p (key property)
276 "Check whether named PROPERTY of a connection is defined.
277 KEY identifies the connection, it is either a process or a
278 vector. A special case is nil, which is used to cache connection
279 properties of the local machine."
280 (not (eq (tramp-get-connection-property key property 'undef) 'undef)))
282 ;;;###tramp-autoload
283 (defun tramp-flush-connection-property (key)
284 "Remove all properties identified by KEY.
285 KEY identifies the connection, it is either a process or a
286 vector. A special case is nil, which is used to cache connection
287 properties of the local machine."
288 ;; Unify key by removing localname and hop from vector. Work with a
289 ;; copy in order to avoid side effects.
290 (when (vectorp key)
291 (setq key (copy-sequence key))
292 (aset key 3 nil)
293 (aset key 4 nil))
294 (tramp-message
295 key 7 "%s %s" key
296 (let ((hash (gethash key tramp-cache-data))
297 properties)
298 (when (hash-table-p hash)
299 (maphash (lambda (x _y) (add-to-list 'properties x 'append)) hash))
300 properties))
301 (setq tramp-cache-data-changed t)
302 (remhash key tramp-cache-data))
304 ;;;###tramp-autoload
305 (defun tramp-cache-print (table)
306 "Print hash table TABLE."
307 (when (hash-table-p table)
308 (let (result)
309 (maphash
310 (lambda (key value)
311 ;; Remove text properties from KEY and VALUE.
312 (when (vectorp key)
313 (dotimes (i (length key))
314 (when (stringp (aref key i))
315 (aset key i (substring-no-properties (aref key i))))))
316 (when (stringp key)
317 (setq key (substring-no-properties key)))
318 (when (stringp value)
319 (setq value (substring-no-properties value)))
320 ;; Dump.
321 (let ((tmp (format
322 "(%s %s)"
323 (if (processp key)
324 (prin1-to-string (prin1-to-string key))
325 (prin1-to-string key))
326 (if (hash-table-p value)
327 (tramp-cache-print value)
328 (if (bufferp value)
329 (prin1-to-string (prin1-to-string value))
330 (prin1-to-string value))))))
331 (setq result (if result (concat result " " tmp) tmp))))
332 table)
333 result)))
335 ;;;###tramp-autoload
336 (defun tramp-list-connections ()
337 "Return a list of all known connection vectors according to `tramp-cache'."
338 (let (result tramp-verbose)
339 (maphash
340 (lambda (key _value)
341 (when (and (vectorp key) (null (aref key 3))
342 (tramp-connection-property-p key "process-buffer"))
343 (add-to-list 'result key)))
344 tramp-cache-data)
345 result))
347 (defun tramp-dump-connection-properties ()
348 "Write persistent connection properties into file `tramp-persistency-file-name'."
349 ;; We shouldn't fail, otherwise Emacs might not be able to be closed.
350 (ignore-errors
351 (when (and (hash-table-p tramp-cache-data)
352 (not (zerop (hash-table-count tramp-cache-data)))
353 tramp-cache-data-changed
354 (stringp tramp-persistency-file-name))
355 (let ((cache (copy-hash-table tramp-cache-data))
356 print-length print-level)
357 ;; Remove temporary data. If there is the key "login-as", we
358 ;; don't save either, because all other properties might
359 ;; depend on the login name, and we want to give the
360 ;; possibility to use another login name later on.
361 (maphash
362 (lambda (key value)
363 (if (and (vectorp key)
364 (not (tramp-file-name-localname key))
365 (not (gethash "login-as" value)))
366 (progn
367 (remhash "process-name" value)
368 (remhash "process-buffer" value)
369 (remhash "first-password-request" value))
370 (remhash key cache)))
371 cache)
372 ;; Dump it.
373 (with-temp-file tramp-persistency-file-name
374 (insert
375 ";; -*- emacs-lisp -*-"
376 ;; `time-stamp-string' might not exist in all Emacs flavors.
377 (condition-case nil
378 (progn
379 (format
380 " <%s %s>\n"
381 (time-stamp-string "%02y/%02m/%02d %02H:%02M:%02S")
382 tramp-persistency-file-name))
383 (error "\n"))
384 ";; Tramp connection history. Don't change this file.\n"
385 ";; You can delete it, forcing Tramp to reapply the checks.\n\n"
386 (with-output-to-string
387 (pp (read (format "(%s)" (tramp-cache-print cache)))))))))))
389 (unless noninteractive
390 (add-hook 'kill-emacs-hook 'tramp-dump-connection-properties))
391 (add-hook 'tramp-cache-unload-hook
392 (lambda ()
393 (remove-hook 'kill-emacs-hook
394 'tramp-dump-connection-properties)))
396 ;;;###tramp-autoload
397 (defun tramp-parse-connection-properties (method)
398 "Return a list of (user host) tuples allowed to access for METHOD.
399 This function is added always in `tramp-get-completion-function'
400 for all methods. Resulting data are derived from connection history."
401 (let (res)
402 (maphash
403 (lambda (key _value)
404 (if (and (vectorp key)
405 (string-equal method (tramp-file-name-method key))
406 (not (tramp-file-name-localname key)))
407 (push (list (tramp-file-name-user key)
408 (tramp-file-name-host key))
409 res)))
410 tramp-cache-data)
411 res))
413 ;; Read persistent connection history.
414 (when (and (stringp tramp-persistency-file-name)
415 (zerop (hash-table-count tramp-cache-data))
416 ;; When "emacs -Q" has been called, both variables are nil.
417 ;; We do not load the persistency file then, in order to
418 ;; have a clean test environment.
419 (or init-file-user
420 site-run-file))
421 (condition-case err
422 (with-temp-buffer
423 (insert-file-contents tramp-persistency-file-name)
424 (let ((list (read (current-buffer)))
425 (tramp-verbose 0)
426 element key item)
427 (while (setq element (pop list))
428 (setq key (pop element))
429 (while (setq item (pop element))
430 ;; We set only values which are not contained in
431 ;; `tramp-connection-properties'. The cache is
432 ;; initialized properly by side effect.
433 (unless (tramp-connection-property-p key (car item))
434 (tramp-set-connection-property key (pop item) (car item))))))
435 (setq tramp-cache-data-changed nil))
436 (file-error
437 ;; Most likely because the file doesn't exist yet. No message.
438 (clrhash tramp-cache-data))
439 (error
440 ;; File is corrupted.
441 (message "Tramp persistency file `%s' is corrupted: %s"
442 tramp-persistency-file-name (error-message-string err))
443 (clrhash tramp-cache-data))))
445 (add-hook 'tramp-unload-hook
446 (lambda ()
447 (unload-feature 'tramp-cache 'force)))
449 (provide 'tramp-cache)
451 ;;; tramp-cache.el ends here