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