Rework fixing Bug#24432
[emacs.git] / lisp / net / tramp-cache.el
blob531044fddfda695e15b6880b5c5f6a0483b34f52
1 ;;; tramp-cache.el --- file information caching for Tramp
3 ;; Copyright (C) 2000, 2005-2016 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 key (copy-sequence key))
111 (aset key 3 (tramp-run-real-handler 'directory-file-name (list file)))
112 (aset key 4 nil)
113 (let* ((hash (tramp-get-hash-table key))
114 (value (when (hash-table-p hash) (gethash property hash))))
116 ;; We take the value only if there is any, and
117 ;; `remote-file-name-inhibit-cache' indicates that it is still
118 ;; valid. Otherwise, DEFAULT is set.
119 (and (consp value)
120 (or (null remote-file-name-inhibit-cache)
121 (and (integerp remote-file-name-inhibit-cache)
123 (tramp-time-diff (current-time) (car value))
124 remote-file-name-inhibit-cache))
125 (and (consp remote-file-name-inhibit-cache)
126 (time-less-p
127 remote-file-name-inhibit-cache (car value)))))
128 (setq value (cdr value))
129 (setq value default))
131 (tramp-message key 8 "%s %s %s" file property value)
132 (when (>= tramp-verbose 10)
133 (let* ((var (intern (concat "tramp-cache-get-count-" property)))
134 (val (or (and (boundp var) (symbol-value var)) 0)))
135 (set var (1+ val))))
136 value))
138 ;;;###tramp-autoload
139 (defun tramp-set-file-property (key file property value)
140 "Set the PROPERTY of FILE to VALUE, in the cache context of KEY.
141 Returns VALUE."
142 ;; Unify localname. Remove hop from vector.
143 (setq key (copy-sequence key))
144 (aset key 3 (tramp-run-real-handler 'directory-file-name (list file)))
145 (aset key 4 nil)
146 (let ((hash (tramp-get-hash-table key)))
147 ;; We put the timestamp there.
148 (puthash property (cons (current-time) value) hash)
149 (tramp-message key 8 "%s %s %s" file property value)
150 (when (>= tramp-verbose 10)
151 (let* ((var (intern (concat "tramp-cache-set-count-" property)))
152 (val (or (and (boundp var) (symbol-value var)) 0)))
153 (set var (1+ val))))
154 value))
156 ;;;###tramp-autoload
157 (defun tramp-flush-file-property (key file)
158 "Remove all properties of FILE in the cache context of KEY."
159 (let* ((file (tramp-run-real-handler
160 'directory-file-name (list file)))
161 (truename (tramp-get-file-property key file "file-truename" nil)))
162 ;; Remove file properties of symlinks.
163 (when (and (stringp truename)
164 (not (string-equal file (directory-file-name truename))))
165 (tramp-flush-file-property key truename))
166 ;; Unify localname. Remove hop from vector.
167 (setq key (copy-sequence key))
168 (aset key 3 file)
169 (aset key 4 nil)
170 (tramp-message key 8 "%s" file)
171 (remhash key tramp-cache-data)))
173 ;;;###tramp-autoload
174 (defun tramp-flush-directory-property (key directory)
175 "Remove all properties of DIRECTORY in the cache context of KEY.
176 Remove also properties of all files in subdirectories."
177 (let* ((directory (tramp-run-real-handler
178 'directory-file-name (list directory)))
179 (truename (tramp-get-file-property key directory "file-truename" nil)))
180 ;; Remove file properties of symlinks.
181 (when (and (stringp truename)
182 (not (string-equal directory (directory-file-name truename))))
183 (tramp-flush-directory-property key truename))
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)))
193 ;; Reverting or killing a buffer should also flush file properties.
194 ;; They could have been changed outside Tramp. In eshell, "ls" would
195 ;; not show proper directory contents when a file has been copied or
196 ;; deleted before. We must apply `save-match-data', because it would
197 ;; corrupt other packages otherwise (reported from org).
198 (defun tramp-flush-file-function ()
199 "Flush all Tramp cache properties from `buffer-file-name'.
200 This is suppressed for temporary buffers."
201 (save-match-data
202 (unless (or (null (buffer-name))
203 (string-match "^\\( \\|\\*\\)" (buffer-name)))
204 (let ((bfn (if (stringp (buffer-file-name))
205 (buffer-file-name)
206 default-directory))
207 (tramp-verbose 0))
208 (when (tramp-tramp-file-p bfn)
209 (with-parsed-tramp-file-name bfn nil
210 (tramp-flush-file-property v localname)))))))
212 (add-hook 'before-revert-hook 'tramp-flush-file-function)
213 (add-hook 'eshell-pre-command-hook 'tramp-flush-file-function)
214 (add-hook 'kill-buffer-hook 'tramp-flush-file-function)
215 (add-hook 'tramp-cache-unload-hook
216 (lambda ()
217 (remove-hook 'before-revert-hook
218 'tramp-flush-file-function)
219 (remove-hook 'eshell-pre-command-hook
220 'tramp-flush-file-function)
221 (remove-hook 'kill-buffer-hook
222 'tramp-flush-file-function)))
224 ;;; -- Properties --
226 ;;;###tramp-autoload
227 (defun tramp-get-connection-property (key property default)
228 "Get the named PROPERTY for the connection.
229 KEY identifies the connection, it is either a process or a
230 vector. A special case is nil, which is used to cache connection
231 properties of the local machine. If the value is not set for the
232 connection, returns DEFAULT."
233 ;; Unify key by removing localname and hop from vector. Work with a
234 ;; copy in order to avoid side effects.
235 (when (vectorp key)
236 (setq key (copy-sequence key))
237 (aset key 3 nil)
238 (aset key 4 nil))
239 (let* ((hash (tramp-get-hash-table key))
240 (value
241 ;; If the key is an auxiliary process object, check whether
242 ;; the process is still alive.
243 (if (and (processp key) (not (tramp-compat-process-live-p key)))
244 default
245 (if (hash-table-p hash)
246 (gethash property hash default)
247 default))))
248 (tramp-message key 7 "%s %s" property value)
249 value))
251 ;;;###tramp-autoload
252 (defun tramp-set-connection-property (key property value)
253 "Set the named PROPERTY of a connection to VALUE.
254 KEY identifies the connection, it is either a process or a
255 vector. A special case is nil, which is used to cache connection
256 properties of the local machine. PROPERTY is set persistent when
257 KEY is a vector."
258 ;; Unify key by removing localname and hop from vector. Work with a
259 ;; copy in order to avoid side effects.
260 (when (vectorp key)
261 (setq key (copy-sequence key))
262 (aset key 3 nil)
263 (aset key 4 nil))
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)
268 value))
270 ;;;###tramp-autoload
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
274 vector. A special case is nil, which is used to cache connection
275 properties of the local machine."
276 (not (eq (tramp-get-connection-property key property 'undef) 'undef)))
278 ;;;###tramp-autoload
279 (defun tramp-flush-connection-property (key)
280 "Remove all properties identified by KEY.
281 KEY identifies the connection, it is either a process or a
282 vector. A special case is nil, which is used to cache connection
283 properties of the local machine."
284 ;; Unify key by removing localname and hop from vector. Work with a
285 ;; copy in order to avoid side effects.
286 (when (vectorp key)
287 (setq key (copy-sequence key))
288 (aset key 3 nil)
289 (aset key 4 nil))
290 (tramp-message
291 key 7 "%s %s" key
292 (let ((hash (gethash key tramp-cache-data))
293 properties)
294 (when (hash-table-p hash)
295 (maphash (lambda (x _y) (add-to-list 'properties x 'append)) hash))
296 properties))
297 (setq tramp-cache-data-changed t)
298 (remhash key tramp-cache-data))
300 ;;;###tramp-autoload
301 (defun tramp-cache-print (table)
302 "Print hash table TABLE."
303 (when (hash-table-p table)
304 (let (result)
305 (maphash
306 (lambda (key value)
307 ;; Remove text properties from KEY and VALUE.
308 (when (vectorp key)
309 (dotimes (i (length key))
310 (when (stringp (aref key i))
311 (aset key i (substring-no-properties (aref key i))))))
312 (when (stringp key)
313 (setq key (substring-no-properties key)))
314 (when (stringp value)
315 (setq value (substring-no-properties value)))
316 ;; Dump.
317 (let ((tmp (format
318 "(%s %s)"
319 (if (processp key)
320 (prin1-to-string (prin1-to-string key))
321 (prin1-to-string key))
322 (if (hash-table-p value)
323 (tramp-cache-print value)
324 (if (bufferp value)
325 (prin1-to-string (prin1-to-string value))
326 (prin1-to-string value))))))
327 (setq result (if result (concat result " " tmp) tmp))))
328 table)
329 result)))
331 ;;;###tramp-autoload
332 (defun tramp-list-connections ()
333 "Return a list of all known connection vectors according to `tramp-cache'."
334 (let (result tramp-verbose)
335 (maphash
336 (lambda (key _value)
337 (when (and (vectorp key) (null (aref key 3))
338 (tramp-connection-property-p key "process-buffer"))
339 (add-to-list 'result key)))
340 tramp-cache-data)
341 result))
343 (defun tramp-dump-connection-properties ()
344 "Write persistent connection properties into file `tramp-persistency-file-name'."
345 ;; We shouldn't fail, otherwise Emacs might not be able to be closed.
346 (ignore-errors
347 (when (and (hash-table-p tramp-cache-data)
348 (not (zerop (hash-table-count tramp-cache-data)))
349 tramp-cache-data-changed
350 (stringp tramp-persistency-file-name))
351 (let ((cache (copy-hash-table tramp-cache-data))
352 print-length print-level)
353 ;; Remove temporary data. If there is the key "login-as", we
354 ;; don't save either, because all other properties might
355 ;; depend on the login name, and we want to give the
356 ;; possibility to use another login name later on.
357 (maphash
358 (lambda (key value)
359 (if (and (vectorp key)
360 (not (tramp-file-name-localname key))
361 (not (gethash "login-as" value)))
362 (progn
363 (remhash "process-name" value)
364 (remhash "process-buffer" value)
365 (remhash "first-password-request" value))
366 (remhash key cache)))
367 cache)
368 ;; Dump it.
369 (with-temp-file tramp-persistency-file-name
370 (insert
371 ";; -*- emacs-lisp -*-"
372 ;; `time-stamp-string' might not exist in all Emacs flavors.
373 (condition-case nil
374 (progn
375 (format
376 " <%s %s>\n"
377 (time-stamp-string "%02y/%02m/%02d %02H:%02M:%02S")
378 tramp-persistency-file-name))
379 (error "\n"))
380 ";; Tramp connection history. Don't change this file.\n"
381 ";; You can delete it, forcing Tramp to reapply the checks.\n\n"
382 (with-output-to-string
383 (pp (read (format "(%s)" (tramp-cache-print cache)))))))))))
385 (unless noninteractive
386 (add-hook 'kill-emacs-hook 'tramp-dump-connection-properties))
387 (add-hook 'tramp-cache-unload-hook
388 (lambda ()
389 (remove-hook 'kill-emacs-hook
390 'tramp-dump-connection-properties)))
392 ;;;###tramp-autoload
393 (defun tramp-parse-connection-properties (method)
394 "Return a list of (user host) tuples allowed to access for METHOD.
395 This function is added always in `tramp-get-completion-function'
396 for all methods. Resulting data are derived from connection history."
397 (let (res)
398 (maphash
399 (lambda (key _value)
400 (if (and (vectorp key)
401 (string-equal method (tramp-file-name-method key))
402 (not (tramp-file-name-localname key)))
403 (push (list (tramp-file-name-user key)
404 (tramp-file-name-host key))
405 res)))
406 tramp-cache-data)
407 res))
409 ;; Read persistent connection history.
410 (when (and (stringp tramp-persistency-file-name)
411 (zerop (hash-table-count tramp-cache-data))
412 ;; When "emacs -Q" has been called, both variables are nil.
413 ;; We do not load the persistency file then, in order to
414 ;; have a clean test environment.
415 (or init-file-user
416 site-run-file))
417 (condition-case err
418 (with-temp-buffer
419 (insert-file-contents tramp-persistency-file-name)
420 (let ((list (read (current-buffer)))
421 (tramp-verbose 0)
422 element key item)
423 (while (setq element (pop list))
424 (setq key (pop element))
425 (while (setq item (pop element))
426 ;; We set only values which are not contained in
427 ;; `tramp-connection-properties'. The cache is
428 ;; initialized properly by side effect.
429 (unless (tramp-connection-property-p key (car item))
430 (tramp-set-connection-property key (pop item) (car item))))))
431 (setq tramp-cache-data-changed nil))
432 (file-error
433 ;; Most likely because the file doesn't exist yet. No message.
434 (clrhash tramp-cache-data))
435 (error
436 ;; File is corrupted.
437 (message "Tramp persistency file `%s' is corrupted: %s"
438 tramp-persistency-file-name (error-message-string err))
439 (clrhash tramp-cache-data))))
441 (add-hook 'tramp-unload-hook
442 (lambda ()
443 (unload-feature 'tramp-cache 'force)))
445 (provide 'tramp-cache)
447 ;;; tramp-cache.el ends here