Update copyright year to 2015
[emacs.git] / lisp / net / tramp-cache.el
blobfb9d5e84c940e4857f52bbe64a9011cd6bff51b0
1 ;;; tramp-cache.el --- file information caching for Tramp
3 ;; Copyright (C) 2000, 2005-2015 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'."
70 :group 'tramp
71 :version "24.4"
72 :type '(repeat (list (choice :tag "File Name regexp" regexp (const nil))
73 (choice :tag " Property" string)
74 (choice :tag " Value" sexp))))
76 (defcustom tramp-persistency-file-name
77 (cond
78 ;; GNU Emacs.
79 ((and (fboundp 'locate-user-emacs-file))
80 (expand-file-name (tramp-compat-funcall 'locate-user-emacs-file "tramp")))
81 ((and (boundp 'user-emacs-directory)
82 (stringp (symbol-value 'user-emacs-directory))
83 (file-directory-p (symbol-value 'user-emacs-directory)))
84 (expand-file-name "tramp" (symbol-value 'user-emacs-directory)))
85 ((and (not (featurep 'xemacs)) (file-directory-p "~/.emacs.d/"))
86 "~/.emacs.d/tramp")
87 ;; XEmacs.
88 ((and (boundp 'user-init-directory)
89 (stringp (symbol-value 'user-init-directory))
90 (file-directory-p (symbol-value 'user-init-directory)))
91 (expand-file-name "tramp" (symbol-value 'user-init-directory)))
92 ((and (featurep 'xemacs) (file-directory-p "~/.xemacs/"))
93 "~/.xemacs/tramp")
94 ;; For users without `~/.emacs.d/' or `~/.xemacs/'.
95 (t "~/.tramp"))
96 "File which keeps connection history for Tramp connections."
97 :group 'tramp
98 :type 'file)
100 (defvar tramp-cache-data-changed nil
101 "Whether persistent cache data have been changed.")
103 (defun tramp-get-hash-table (key)
104 "Returns the hash table for KEY.
105 If it doesn't exist yet, it is created and initialized with
106 matching entries of `tramp-connection-properties'."
107 (or (gethash key tramp-cache-data)
108 (let ((hash
109 (puthash key (make-hash-table :test 'equal) tramp-cache-data)))
110 (when (vectorp key)
111 (dolist (elt tramp-connection-properties)
112 (when (string-match
113 (or (nth 0 elt) "")
114 (tramp-make-tramp-file-name
115 (aref key 0) (aref key 1) (aref key 2) nil))
116 (tramp-set-connection-property key (nth 1 elt) (nth 2 elt)))))
117 hash)))
119 ;;;###tramp-autoload
120 (defun tramp-get-file-property (key file property default)
121 "Get the PROPERTY of FILE from the cache context of KEY.
122 Returns DEFAULT if not set."
123 ;; Unify localname.
124 (setq key (copy-sequence key))
125 (aset key 3 (tramp-run-real-handler 'directory-file-name (list file)))
126 (let* ((hash (tramp-get-hash-table key))
127 (value (when (hash-table-p hash) (gethash property hash))))
129 ;; We take the value only if there is any, and
130 ;; `remote-file-name-inhibit-cache' indicates that it is still
131 ;; valid. Otherwise, DEFAULT is set.
132 (and (consp value)
133 (or (null remote-file-name-inhibit-cache)
134 (and (integerp remote-file-name-inhibit-cache)
136 (tramp-time-diff (current-time) (car value))
137 remote-file-name-inhibit-cache))
138 (and (consp remote-file-name-inhibit-cache)
139 (time-less-p
140 remote-file-name-inhibit-cache (car value)))))
141 (setq value (cdr value))
142 (setq value default))
144 (tramp-message key 8 "%s %s %s" file property value)
145 (when (>= tramp-verbose 10)
146 (let* ((var (intern (concat "tramp-cache-get-count-" property)))
147 (val (or (and (boundp var) (symbol-value var)) 0)))
148 (set var (1+ val))))
149 value))
151 ;;;###tramp-autoload
152 (defun tramp-set-file-property (key file property value)
153 "Set the PROPERTY of FILE to VALUE, in the cache context of KEY.
154 Returns VALUE."
155 ;; Unify localname.
156 (setq key (copy-sequence key))
157 (aset key 3 (tramp-run-real-handler 'directory-file-name (list file)))
158 (let ((hash (tramp-get-hash-table key)))
159 ;; We put the timestamp there.
160 (puthash property (cons (current-time) value) hash)
161 (tramp-message key 8 "%s %s %s" file property value)
162 (when (>= tramp-verbose 10)
163 (let* ((var (intern (concat "tramp-cache-set-count-" property)))
164 (val (or (and (boundp var) (symbol-value var)) 0)))
165 (set var (1+ val))))
166 value))
168 ;;;###tramp-autoload
169 (defun tramp-flush-file-property (key file)
170 "Remove all properties of FILE in the cache context of KEY."
171 ;; Remove file properties of symlinks.
172 (let ((truename (tramp-get-file-property key file "file-truename" nil)))
173 (when (and (stringp truename)
174 (not (string-equal file truename)))
175 (tramp-flush-file-property key truename)))
176 ;; Unify localname.
177 (setq key (copy-sequence key))
178 (aset key 3 (tramp-run-real-handler 'directory-file-name (list file)))
179 (tramp-message key 8 "%s" file)
180 (remhash key tramp-cache-data))
182 ;;;###tramp-autoload
183 (defun tramp-flush-directory-property (key directory)
184 "Remove all properties of DIRECTORY in the cache context of KEY.
185 Remove also properties of all files in subdirectories."
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 ;; Remove file properties of symlinks.
190 (when (and (stringp truename)
191 (not (string-equal directory truename)))
192 (tramp-flush-directory-property key truename))
193 (tramp-message key 8 "%s" directory)
194 (maphash
195 (lambda (key _value)
196 (when (and (stringp (tramp-file-name-localname key))
197 (string-match directory (tramp-file-name-localname key)))
198 (remhash key tramp-cache-data)))
199 tramp-cache-data)))
201 ;; Reverting or killing a buffer should also flush file properties.
202 ;; They could have been changed outside Tramp. In eshell, "ls" would
203 ;; not show proper directory contents when a file has been copied or
204 ;; deleted before. We must apply `save-match-data', because it would
205 ;; corrupt other packages otherwise (reported from org).
206 (defun tramp-flush-file-function ()
207 "Flush all Tramp cache properties from `buffer-file-name'.
208 This is suppressed for temporary buffers."
209 (save-match-data
210 (unless (or (null (buffer-name))
211 (string-match "^\\( \\|\\*\\)" (buffer-name)))
212 (let ((bfn (if (stringp (buffer-file-name))
213 (buffer-file-name)
214 default-directory))
215 (tramp-verbose 0))
216 (when (tramp-tramp-file-p bfn)
217 (with-parsed-tramp-file-name bfn nil
218 (tramp-flush-file-property v localname)))))))
220 (add-hook 'before-revert-hook 'tramp-flush-file-function)
221 (add-hook 'eshell-pre-command-hook 'tramp-flush-file-function)
222 (add-hook 'kill-buffer-hook 'tramp-flush-file-function)
223 (add-hook 'tramp-cache-unload-hook
224 (lambda ()
225 (remove-hook 'before-revert-hook
226 'tramp-flush-file-function)
227 (remove-hook 'eshell-pre-command-hook
228 'tramp-flush-file-function)
229 (remove-hook 'kill-buffer-hook
230 'tramp-flush-file-function)))
232 ;;; -- Properties --
234 ;;;###tramp-autoload
235 (defun tramp-get-connection-property (key property default)
236 "Get the named PROPERTY for the connection.
237 KEY identifies the connection, it is either a process or a vector.
238 If the value is not set for the connection, returns DEFAULT."
239 ;; Unify key by removing localname from vector. Work with a copy in
240 ;; order to avoid side effects.
241 (when (vectorp key)
242 (setq key (copy-sequence key))
243 (aset key 3 nil))
244 (let* ((hash (tramp-get-hash-table key))
245 (value (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 vector.
255 PROPERTY is set persistent when KEY is a vector."
256 ;; Unify key by removing localname from vector. Work with a copy in
257 ;; order to avoid side effects.
258 (when (vectorp key)
259 (setq key (copy-sequence key))
260 (aset key 3 nil))
261 (let ((hash (tramp-get-hash-table key)))
262 (puthash property value hash)
263 (setq tramp-cache-data-changed t)
264 (tramp-message key 7 "%s %s" property value)
265 value))
267 ;;;###tramp-autoload
268 (defun tramp-connection-property-p (key property)
269 "Check whether named PROPERTY of a connection is defined.
270 KEY identifies the connection, it is either a process or a vector."
271 (not (eq (tramp-get-connection-property key property 'undef) 'undef)))
273 ;;;###tramp-autoload
274 (defun tramp-flush-connection-property (key)
275 "Remove all properties identified by KEY.
276 KEY identifies the connection, it is either a process or a vector."
277 ;; Unify key by removing localname from vector. Work with a copy in
278 ;; order to avoid side effects.
279 (when (vectorp key)
280 (setq key (copy-sequence key))
281 (aset key 3 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))
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 ;; `substring-no-properties' does not exist in XEmacs.
301 (when (functionp 'substring-no-properties)
302 (when (vectorp key)
303 (dotimes (i (length key))
304 (when (stringp (aref key i))
305 (aset key i
306 (tramp-compat-funcall
307 'substring-no-properties (aref key i))))))
308 (when (stringp key)
309 (setq key (tramp-compat-funcall 'substring-no-properties key)))
310 (when (stringp value)
311 (setq value
312 (tramp-compat-funcall 'substring-no-properties value))))
313 ;; Dump.
314 (let ((tmp (format
315 "(%s %s)"
316 (if (processp key)
317 (prin1-to-string (prin1-to-string key))
318 (prin1-to-string key))
319 (if (hash-table-p value)
320 (tramp-cache-print value)
321 (if (bufferp value)
322 (prin1-to-string (prin1-to-string value))
323 (prin1-to-string value))))))
324 (setq result (if result (concat result " " tmp) tmp))))
325 table)
326 result)))
328 ;;;###tramp-autoload
329 (defun tramp-list-connections ()
330 "Return a list of all known connection vectors according to `tramp-cache'."
331 (let (result)
332 (maphash
333 (lambda (key _value)
334 (when (and (vectorp key) (null (aref key 3)))
335 (add-to-list 'result key)))
336 tramp-cache-data)
337 result))
339 (defun tramp-dump-connection-properties ()
340 "Write persistent connection properties into file `tramp-persistency-file-name'."
341 ;; We shouldn't fail, otherwise (X)Emacs might not be able to be closed.
342 (ignore-errors
343 (when (and (hash-table-p tramp-cache-data)
344 (not (zerop (hash-table-count tramp-cache-data)))
345 tramp-cache-data-changed
346 (stringp tramp-persistency-file-name))
347 (let ((cache (copy-hash-table tramp-cache-data))
348 print-length print-level)
349 ;; Remove temporary data. If there is the key "login-as", we
350 ;; don't save either, because all other properties might
351 ;; depend on the login name, and we want to give the
352 ;; possibility to use another login name later on.
353 (maphash
354 (lambda (key value)
355 (if (and (vectorp key)
356 (not (tramp-file-name-localname key))
357 (not (gethash "login-as" value)))
358 (progn
359 (remhash "process-name" value)
360 (remhash "process-buffer" value)
361 (remhash "first-password-request" value))
362 (remhash key cache)))
363 cache)
364 ;; Dump it.
365 (with-temp-buffer
366 (insert
367 ";; -*- emacs-lisp -*-"
368 ;; `time-stamp-string' might not exist in all (X)Emacs flavors.
369 (condition-case nil
370 (progn
371 (format
372 " <%s %s>\n"
373 (time-stamp-string "%02y/%02m/%02d %02H:%02M:%02S")
374 tramp-persistency-file-name))
375 (error "\n"))
376 ";; Tramp connection history. Don't change this file.\n"
377 ";; You can delete it, forcing Tramp to reapply the checks.\n\n"
378 (with-output-to-string
379 (pp (read (format "(%s)" (tramp-cache-print cache))))))
380 (write-region
381 (point-min) (point-max) tramp-persistency-file-name))))))
383 (unless noninteractive
384 (add-hook 'kill-emacs-hook 'tramp-dump-connection-properties))
385 (add-hook 'tramp-cache-unload-hook
386 (lambda ()
387 (remove-hook 'kill-emacs-hook
388 'tramp-dump-connection-properties)))
390 ;;;###tramp-autoload
391 (defun tramp-parse-connection-properties (method)
392 "Return a list of (user host) tuples allowed to access for METHOD.
393 This function is added always in `tramp-get-completion-function'
394 for all methods. Resulting data are derived from connection history."
395 (let (res)
396 (maphash
397 (lambda (key _value)
398 (if (and (vectorp key)
399 (string-equal method (tramp-file-name-method key))
400 (not (tramp-file-name-localname key)))
401 (push (list (tramp-file-name-user key)
402 (tramp-file-name-host key))
403 res)))
404 tramp-cache-data)
405 res))
407 ;; Read persistent connection history.
408 (when (and (stringp tramp-persistency-file-name)
409 (zerop (hash-table-count tramp-cache-data))
410 ;; When "emacs -Q" has been called, both variables are nil.
411 ;; We do not load the persistency file then, in order to
412 ;; have a clean test environment.
413 (or (and (boundp 'init-file-user) (symbol-value 'init-file-user))
414 (and (boundp 'site-run-file) (symbol-value 'site-run-file))))
415 (condition-case err
416 (with-temp-buffer
417 (insert-file-contents tramp-persistency-file-name)
418 (let ((list (read (current-buffer)))
419 (tramp-verbose 0)
420 element key item)
421 (while (setq element (pop list))
422 (setq key (pop element))
423 (while (setq item (pop element))
424 ;; We set only values which are not contained in
425 ;; `tramp-connection-properties'. The cache is
426 ;; initialized properly by side effect.
427 (unless (tramp-connection-property-p key (car item))
428 (tramp-set-connection-property key (pop item) (car item))))))
429 (setq tramp-cache-data-changed nil))
430 (file-error
431 ;; Most likely because the file doesn't exist yet. No message.
432 (clrhash tramp-cache-data))
433 (error
434 ;; File is corrupted.
435 (message "Tramp persistency file '%s' is corrupted: %s"
436 tramp-persistency-file-name (error-message-string err))
437 (clrhash tramp-cache-data))))
439 (add-hook 'tramp-unload-hook
440 (lambda ()
441 (unload-feature 'tramp-cache 'force)))
443 (provide 'tramp-cache)
445 ;;; tramp-cache.el ends here