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