]> 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 vector.
227 If the value is not set for the connection, returns DEFAULT."
228 ;; Unify key by removing localname and hop from vector. Work with a
229 ;; copy in order to avoid side effects.
230 (when (vectorp key)
231 (setq key (copy-sequence key))
232 (aset key 3 nil)
233 (aset key 4 nil))
234 (let* ((hash (tramp-get-hash-table key))
235 (value (if (hash-table-p hash)
236 (gethash property hash default)
237 default)))
238 (tramp-message key 7 "%s %s" property value)
239 value))
240
241 ;;;###tramp-autoload
242 (defun tramp-set-connection-property (key property value)
243 "Set the named PROPERTY of a connection to VALUE.
244 KEY identifies the connection, it is either a process or a vector.
245 PROPERTY is set persistent when KEY is a vector."
246 ;; Unify key by removing localname and hop from vector. Work with a
247 ;; copy in order to avoid side effects.
248 (when (vectorp key)
249 (setq key (copy-sequence key))
250 (aset key 3 nil)
251 (aset key 4 nil))
252 (let ((hash (tramp-get-hash-table key)))
253 (puthash property value hash)
254 (setq tramp-cache-data-changed t)
255 (tramp-message key 7 "%s %s" property value)
256 value))
257
258 ;;;###tramp-autoload
259 (defun tramp-connection-property-p (key property)
260 "Check whether named PROPERTY of a connection is defined.
261 KEY identifies the connection, it is either a process or a vector."
262 (not (eq (tramp-get-connection-property key property 'undef) 'undef)))
263
264 ;;;###tramp-autoload
265 (defun tramp-flush-connection-property (key)
266 "Remove all properties identified by KEY.
267 KEY identifies the connection, it is either a process or a vector."
268 ;; Unify key by removing localname and hop from vector. Work with a
269 ;; copy in order to avoid side effects.
270 (when (vectorp key)
271 (setq key (copy-sequence key))
272 (aset key 3 nil)
273 (aset key 4 nil))
274 (tramp-message
275 key 7 "%s %s" key
276 (let ((hash (gethash key tramp-cache-data))
277 properties)
278 (when (hash-table-p hash)
279 (maphash (lambda (x _y) (add-to-list 'properties x 'append)) hash))
280 properties))
281 (setq tramp-cache-data-changed t)
282 (remhash key tramp-cache-data))
283
284 ;;;###tramp-autoload
285 (defun tramp-cache-print (table)
286 "Print hash table TABLE."
287 (when (hash-table-p table)
288 (let (result)
289 (maphash
290 (lambda (key value)
291 ;; Remove text properties from KEY and VALUE.
292 (when (vectorp key)
293 (dotimes (i (length key))
294 (when (stringp (aref key i))
295 (aset key i (substring-no-properties (aref key i))))))
296 (when (stringp key)
297 (setq key (substring-no-properties key)))
298 (when (stringp value)
299 (setq value (substring-no-properties value)))
300 ;; Dump.
301 (let ((tmp (format
302 "(%s %s)"
303 (if (processp key)
304 (prin1-to-string (prin1-to-string key))
305 (prin1-to-string key))
306 (if (hash-table-p value)
307 (tramp-cache-print value)
308 (if (bufferp value)
309 (prin1-to-string (prin1-to-string value))
310 (prin1-to-string value))))))
311 (setq result (if result (concat result " " tmp) tmp))))
312 table)
313 result)))
314
315 ;;;###tramp-autoload
316 (defun tramp-list-connections ()
317 "Return a list of all known connection vectors according to `tramp-cache'."
318 (let (result)
319 (maphash
320 (lambda (key _value)
321 (when (and (vectorp key) (null (aref key 3)))
322 (add-to-list 'result key)))
323 tramp-cache-data)
324 result))
325
326 (defun tramp-dump-connection-properties ()
327 "Write persistent connection properties into file `tramp-persistency-file-name'."
328 ;; We shouldn't fail, otherwise (X)Emacs might not be able to be closed.
329 (ignore-errors
330 (when (and (hash-table-p tramp-cache-data)
331 (not (zerop (hash-table-count tramp-cache-data)))
332 tramp-cache-data-changed
333 (stringp tramp-persistency-file-name))
334 (let ((cache (copy-hash-table tramp-cache-data))
335 print-length print-level)
336 ;; Remove temporary data. If there is the key "login-as", we
337 ;; don't save either, because all other properties might
338 ;; depend on the login name, and we want to give the
339 ;; possibility to use another login name later on.
340 (maphash
341 (lambda (key value)
342 (if (and (vectorp key)
343 (not (tramp-file-name-localname key))
344 (not (gethash "login-as" value)))
345 (progn
346 (remhash "process-name" value)
347 (remhash "process-buffer" value)
348 (remhash "first-password-request" value))
349 (remhash key cache)))
350 cache)
351 ;; Dump it.
352 (with-temp-file tramp-persistency-file-name
353 (insert
354 ";; -*- emacs-lisp -*-"
355 ;; `time-stamp-string' might not exist in all (X)Emacs flavors.
356 (condition-case nil
357 (progn
358 (format
359 " <%s %s>\n"
360 (time-stamp-string "%02y/%02m/%02d %02H:%02M:%02S")
361 tramp-persistency-file-name))
362 (error "\n"))
363 ";; Tramp connection history. Don't change this file.\n"
364 ";; You can delete it, forcing Tramp to reapply the checks.\n\n"
365 (with-output-to-string
366 (pp (read (format "(%s)" (tramp-cache-print cache)))))))))))
367
368 (unless noninteractive
369 (add-hook 'kill-emacs-hook 'tramp-dump-connection-properties))
370 (add-hook 'tramp-cache-unload-hook
371 (lambda ()
372 (remove-hook 'kill-emacs-hook
373 'tramp-dump-connection-properties)))
374
375 ;;;###tramp-autoload
376 (defun tramp-parse-connection-properties (method)
377 "Return a list of (user host) tuples allowed to access for METHOD.
378 This function is added always in `tramp-get-completion-function'
379 for all methods. Resulting data are derived from connection history."
380 (let (res)
381 (maphash
382 (lambda (key _value)
383 (if (and (vectorp key)
384 (string-equal method (tramp-file-name-method key))
385 (not (tramp-file-name-localname key)))
386 (push (list (tramp-file-name-user key)
387 (tramp-file-name-host key))
388 res)))
389 tramp-cache-data)
390 res))
391
392 ;; Read persistent connection history.
393 (when (and (stringp tramp-persistency-file-name)
394 (zerop (hash-table-count tramp-cache-data))
395 ;; When "emacs -Q" has been called, both variables are nil.
396 ;; We do not load the persistency file then, in order to
397 ;; have a clean test environment.
398 (or init-file-user
399 site-run-file))
400 (condition-case err
401 (with-temp-buffer
402 (insert-file-contents tramp-persistency-file-name)
403 (let ((list (read (current-buffer)))
404 (tramp-verbose 0)
405 element key item)
406 (while (setq element (pop list))
407 (setq key (pop element))
408 (while (setq item (pop element))
409 ;; We set only values which are not contained in
410 ;; `tramp-connection-properties'. The cache is
411 ;; initialized properly by side effect.
412 (unless (tramp-connection-property-p key (car item))
413 (tramp-set-connection-property key (pop item) (car item))))))
414 (setq tramp-cache-data-changed nil))
415 (file-error
416 ;; Most likely because the file doesn't exist yet. No message.
417 (clrhash tramp-cache-data))
418 (error
419 ;; File is corrupted.
420 (message "Tramp persistency file `%s' is corrupted: %s"
421 tramp-persistency-file-name (error-message-string err))
422 (clrhash tramp-cache-data))))
423
424 (add-hook 'tramp-unload-hook
425 (lambda ()
426 (unload-feature 'tramp-cache 'force)))
427
428 (provide 'tramp-cache)
429
430 ;;; tramp-cache.el ends here