e4fca46ce2d3fc5edb7215550f717a13f97d674e
[bpt/emacs.git] / lisp / net / tramp-cache.el
1 ;;; tramp-cache.el --- file information caching for Tramp
2
3 ;; Copyright (C) 2000, 2005-2012 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 nile, depending on the file existence, or
42 ;; "file-attributes" caches the result of the function
43 ;; `file-attributes'.
44 ;;
45 ;; - The key is a process. This are temporary properties related to
46 ;; an open connection. Examples: "scripts" keeps shell script
47 ;; definitions already sent to the remote shell, "last-cmd-time" is
48 ;; the time stamp a command has been sent to the remote process.
49
50 ;;; Code:
51
52 (require 'tramp)
53 (autoload 'time-stamp-string "time-stamp")
54
55 ;;; -- Cache --
56
57 ;;;###tramp-autoload
58 (defvar tramp-cache-data (make-hash-table :test 'equal)
59 "Hash table for remote files properties.")
60
61 (defcustom tramp-persistency-file-name
62 (cond
63 ;; GNU Emacs.
64 ((and (fboundp 'locate-user-emacs-file))
65 (expand-file-name (tramp-compat-funcall 'locate-user-emacs-file "tramp")))
66 ((and (boundp 'user-emacs-directory)
67 (stringp (symbol-value 'user-emacs-directory))
68 (file-directory-p (symbol-value 'user-emacs-directory)))
69 (expand-file-name "tramp" (symbol-value 'user-emacs-directory)))
70 ((and (not (featurep 'xemacs)) (file-directory-p "~/.emacs.d/"))
71 "~/.emacs.d/tramp")
72 ;; XEmacs.
73 ((and (boundp 'user-init-directory)
74 (stringp (symbol-value 'user-init-directory))
75 (file-directory-p (symbol-value 'user-init-directory)))
76 (expand-file-name "tramp" (symbol-value 'user-init-directory)))
77 ((and (featurep 'xemacs) (file-directory-p "~/.xemacs/"))
78 "~/.xemacs/tramp")
79 ;; For users without `~/.emacs.d/' or `~/.xemacs/'.
80 (t "~/.tramp"))
81 "File which keeps connection history for Tramp connections."
82 :group 'tramp
83 :type 'file)
84
85 (defvar tramp-cache-data-changed nil
86 "Whether persistent cache data have been changed.")
87
88 ;;;###tramp-autoload
89 (defun tramp-get-file-property (vec file property default)
90 "Get the PROPERTY of FILE from the cache context of VEC.
91 Returns DEFAULT if not set."
92 ;; Unify localname.
93 (setq vec (copy-sequence vec))
94 (aset vec 3 (tramp-run-real-handler 'directory-file-name (list file)))
95 (let* ((hash (or (gethash vec tramp-cache-data)
96 (puthash vec (make-hash-table :test 'equal)
97 tramp-cache-data)))
98 (value (when (hash-table-p hash) (gethash property hash))))
99 (if
100 ;; We take the value only if there is any, and
101 ;; `remote-file-name-inhibit-cache' indicates that it is still
102 ;; valid. Otherwise, DEFAULT is set.
103 (and (consp value)
104 (or (null remote-file-name-inhibit-cache)
105 (and (integerp remote-file-name-inhibit-cache)
106 (<=
107 (tramp-time-diff (current-time) (car value))
108 remote-file-name-inhibit-cache))
109 (and (consp remote-file-name-inhibit-cache)
110 (tramp-time-less-p
111 remote-file-name-inhibit-cache (car value)))))
112 (setq value (cdr value))
113 (setq value default))
114
115 (tramp-message vec 8 "%s %s %s" file property value)
116 (when (>= tramp-verbose 10)
117 (let* ((var (intern (concat "tramp-cache-get-count-" property)))
118 (val (or (ignore-errors (symbol-value var)) 0)))
119 (set var (1+ val))))
120 value))
121
122 ;;;###tramp-autoload
123 (defun tramp-set-file-property (vec file property value)
124 "Set the PROPERTY of FILE to VALUE, in the cache context of VEC.
125 Returns VALUE."
126 ;; Unify localname.
127 (setq vec (copy-sequence vec))
128 (aset vec 3 (tramp-run-real-handler 'directory-file-name (list file)))
129 (let ((hash (or (gethash vec tramp-cache-data)
130 (puthash vec (make-hash-table :test 'equal)
131 tramp-cache-data))))
132 ;; We put the timestamp there.
133 (puthash property (cons (current-time) value) hash)
134 (tramp-message vec 8 "%s %s %s" file property value)
135 (when (>= tramp-verbose 10)
136 (let* ((var (intern (concat "tramp-cache-set-count-" property)))
137 (val (or (ignore-errors (symbol-value var)) 0)))
138 (set var (1+ val))))
139 value))
140
141 ;;;###tramp-autoload
142 (defun tramp-flush-file-property (vec file)
143 "Remove all properties of FILE in the cache context of VEC."
144 ;; Remove file property of symlinks.
145 (let ((truename (tramp-get-file-property vec file "file-truename" nil)))
146 (when (and (stringp truename)
147 (not (string-equal file truename)))
148 (tramp-flush-file-property vec truename)))
149 ;; Unify localname.
150 (setq vec (copy-sequence vec))
151 (aset vec 3 (tramp-run-real-handler 'directory-file-name (list file)))
152 (tramp-message vec 8 "%s" file)
153 (remhash vec tramp-cache-data))
154
155 ;;;###tramp-autoload
156 (defun tramp-flush-directory-property (vec directory)
157 "Remove all properties of DIRECTORY in the cache context of VEC.
158 Remove also properties of all files in subdirectories."
159 (let ((directory (tramp-run-real-handler
160 'directory-file-name (list directory))))
161 (tramp-message vec 8 "%s" directory)
162 (maphash
163 (lambda (key value)
164 (when (and (stringp (tramp-file-name-localname key))
165 (string-match directory (tramp-file-name-localname key)))
166 (remhash key tramp-cache-data)))
167 tramp-cache-data)))
168
169 ;; Reverting or killing a buffer should also flush file properties.
170 ;; They could have been changed outside Tramp. In eshell, "ls" would
171 ;; not show proper directory contents when a file has been copied or
172 ;; deleted before.
173 (defun tramp-flush-file-function ()
174 "Flush all Tramp cache properties from `buffer-file-name'."
175 (let ((bfn (if (stringp (buffer-file-name))
176 (buffer-file-name)
177 default-directory)))
178 (when (tramp-tramp-file-p bfn)
179 (with-parsed-tramp-file-name bfn nil
180 (tramp-flush-file-property v localname)))))
181
182 (add-hook 'before-revert-hook 'tramp-flush-file-function)
183 (add-hook 'eshell-pre-command-hook 'tramp-flush-file-function)
184 (add-hook 'kill-buffer-hook 'tramp-flush-file-function)
185 (add-hook 'tramp-cache-unload-hook
186 (lambda ()
187 (remove-hook 'before-revert-hook
188 'tramp-flush-file-function)
189 (remove-hook 'eshell-pre-command-hook
190 'tramp-flush-file-function)
191 (remove-hook 'kill-buffer-hook
192 'tramp-flush-file-function)))
193
194 ;;; -- Properties --
195
196 ;;;###tramp-autoload
197 (defun tramp-get-connection-property (key property default)
198 "Get the named PROPERTY for the connection.
199 KEY identifies the connection, it is either a process or a vector.
200 If the value is not set for the connection, returns DEFAULT."
201 ;; Unify key by removing localname from vector. Work with a copy in
202 ;; order to avoid side effects.
203 (when (vectorp key)
204 (setq key (copy-sequence key))
205 (aset key 3 nil))
206 (let* ((hash (gethash key tramp-cache-data))
207 (value (if (hash-table-p hash)
208 (gethash property hash default)
209 default)))
210 (tramp-message key 7 "%s %s" property value)
211 value))
212
213 ;;;###tramp-autoload
214 (defun tramp-set-connection-property (key property value)
215 "Set the named PROPERTY of a connection to VALUE.
216 KEY identifies the connection, it is either a process or a vector.
217 PROPERTY is set persistent when KEY is a vector."
218 ;; Unify key by removing localname from vector. Work with a copy in
219 ;; order to avoid side effects.
220 (when (vectorp key)
221 (setq key (copy-sequence key))
222 (aset key 3 nil))
223 (let ((hash (or (gethash key tramp-cache-data)
224 (puthash key (make-hash-table :test 'equal)
225 tramp-cache-data))))
226 (puthash property value hash)
227 (setq tramp-cache-data-changed t)
228 (tramp-message key 7 "%s %s" property value)
229 value))
230
231 ;;;###tramp-autoload
232 (defun tramp-flush-connection-property (key)
233 "Remove all properties identified by KEY.
234 KEY identifies the connection, it is either a process or a vector."
235 ;; Unify key by removing localname from vector. Work with a copy in
236 ;; order to avoid side effects.
237 (when (vectorp key)
238 (setq key (copy-sequence key))
239 (aset key 3 nil))
240 (tramp-message
241 key 7 "%s %s" key
242 (let ((hash (gethash key tramp-cache-data))
243 properties)
244 (if (hash-table-p hash)
245 (maphash
246 (lambda (x y) (add-to-list 'properties x 'append))
247 (gethash key tramp-cache-data)))
248 properties))
249 (setq tramp-cache-data-changed t)
250 (remhash key tramp-cache-data))
251
252 ;;;###tramp-autoload
253 (defun tramp-cache-print (table)
254 "Print hash table TABLE."
255 (when (hash-table-p table)
256 (let (result)
257 (maphash
258 (lambda (key value)
259 (let ((tmp (format
260 "(%s %s)"
261 (if (processp key)
262 (prin1-to-string (prin1-to-string key))
263 (prin1-to-string key))
264 (if (hash-table-p value)
265 (tramp-cache-print value)
266 (if (bufferp value)
267 (prin1-to-string (prin1-to-string value))
268 (prin1-to-string value))))))
269 (setq result (if result (concat result " " tmp) tmp))))
270 table)
271 result)))
272
273 ;;;###tramp-autoload
274 (defun tramp-list-connections ()
275 "Return a list of all known connection vectors according to `tramp-cache'."
276 (let (result)
277 (maphash
278 (lambda (key value)
279 (when (and (vectorp key) (null (aref key 3)))
280 (add-to-list 'result key)))
281 tramp-cache-data)
282 result))
283
284 (defun tramp-dump-connection-properties ()
285 "Write persistent connection properties into file `tramp-persistency-file-name'."
286 ;; We shouldn't fail, otherwise (X)Emacs might not be able to be closed.
287 (ignore-errors
288 (when (and (hash-table-p tramp-cache-data)
289 (not (zerop (hash-table-count tramp-cache-data)))
290 tramp-cache-data-changed
291 (stringp tramp-persistency-file-name))
292 (let ((cache (copy-hash-table tramp-cache-data))
293 print-length print-level)
294 ;; Remove temporary data. If there is the key "login-as", we
295 ;; don't save either, because all other properties might
296 ;; depend on the login name, and we want to give the
297 ;; possibility to use another login name later on.
298 (maphash
299 (lambda (key value)
300 (if (and (vectorp key)
301 (not (tramp-file-name-localname key))
302 (not (gethash "login-as" value)))
303 (progn
304 (remhash "process-name" value)
305 (remhash "process-buffer" value)
306 (remhash "first-password-request" value))
307 (remhash key cache)))
308 cache)
309 ;; Dump it.
310 (with-temp-buffer
311 (insert
312 ";; -*- emacs-lisp -*-"
313 ;; `time-stamp-string' might not exist in all (X)Emacs flavors.
314 (condition-case nil
315 (progn
316 (format
317 " <%s %s>\n"
318 (time-stamp-string "%02y/%02m/%02d %02H:%02M:%02S")
319 tramp-persistency-file-name))
320 (error "\n"))
321 ";; Tramp connection history. Don't change this file.\n"
322 ";; You can delete it, forcing Tramp to reapply the checks.\n\n"
323 (with-output-to-string
324 (pp (read (format "(%s)" (tramp-cache-print cache))))))
325 (write-region
326 (point-min) (point-max) tramp-persistency-file-name))))))
327
328 (unless noninteractive
329 (add-hook 'kill-emacs-hook 'tramp-dump-connection-properties))
330 (add-hook 'tramp-cache-unload-hook
331 (lambda ()
332 (remove-hook 'kill-emacs-hook
333 'tramp-dump-connection-properties)))
334
335 ;;;###tramp-autoload
336 (defun tramp-parse-connection-properties (method)
337 "Return a list of (user host) tuples allowed to access for METHOD.
338 This function is added always in `tramp-get-completion-function'
339 for all methods. Resulting data are derived from connection history."
340 (let (res)
341 (maphash
342 (lambda (key value)
343 (if (and (vectorp key)
344 (string-equal method (tramp-file-name-method key))
345 (not (tramp-file-name-localname key)))
346 (push (list (tramp-file-name-user key)
347 (tramp-file-name-host key))
348 res)))
349 tramp-cache-data)
350 res))
351
352 ;; Read persistent connection history.
353 (when (and (stringp tramp-persistency-file-name)
354 (zerop (hash-table-count tramp-cache-data))
355 ;; When "emacs -Q" has been called, both variables are nil.
356 ;; We do not load the persistency file then, in order to
357 ;; have a clean test environment.
358 (or (and (boundp 'init-file-user) (symbol-value 'init-file-user))
359 (and (boundp 'site-run-file) (symbol-value 'site-run-file))))
360 (condition-case err
361 (with-temp-buffer
362 (insert-file-contents tramp-persistency-file-name)
363 (let ((list (read (current-buffer)))
364 element key item)
365 (while (setq element (pop list))
366 (setq key (pop element))
367 (while (setq item (pop element))
368 (tramp-set-connection-property key (pop item) (car item)))))
369 (setq tramp-cache-data-changed nil))
370 (file-error
371 ;; Most likely because the file doesn't exist yet. No message.
372 (clrhash tramp-cache-data))
373 (error
374 ;; File is corrupted.
375 (message "Tramp persistency file '%s' is corrupted: %s"
376 tramp-persistency-file-name (error-message-string err))
377 (clrhash tramp-cache-data))))
378
379 (add-hook 'tramp-unload-hook
380 (lambda ()
381 (unload-feature 'tramp-cache 'force)))
382
383 (provide 'tramp-cache)
384
385 ;;; tramp-cache.el ends here