]> code.delx.au - gnu-emacs/blob - lisp/net/tramp-cache.el
Add 2012 to FSF copyright years for Emacs files (do not merge to trunk)
[gnu-emacs] / lisp / net / tramp-cache.el
1 ;;; tramp-cache.el --- file information caching for Tramp
2
3 ;; Copyright (C) 2000, 2005, 2006, 2007, 2008, 2009,
4 ;; 2010, 2011, 2012 Free Software Foundation, Inc.
5
6 ;; Author: Daniel Pittman <daniel@inanna.danann.net>
7 ;; Michael Albinus <michael.albinus@gmx.de>
8 ;; Keywords: comm, processes
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 ;; Pacify byte-compiler.
53 (eval-when-compile
54 (require 'cl)
55 (autoload 'tramp-message "tramp")
56 (autoload 'tramp-tramp-file-p "tramp")
57 ;; We cannot autoload macro `with-parsed-tramp-file-name', it
58 ;; results in problems of byte-compiled code.
59 (autoload 'tramp-dissect-file-name "tramp")
60 (autoload 'tramp-file-name-method "tramp")
61 (autoload 'tramp-file-name-user "tramp")
62 (autoload 'tramp-file-name-host "tramp")
63 (autoload 'tramp-file-name-localname "tramp")
64 (autoload 'tramp-run-real-handler "tramp")
65 (autoload 'tramp-time-less-p "tramp")
66 (autoload 'time-stamp-string "time-stamp"))
67
68 ;;; -- Cache --
69
70 (defvar tramp-cache-data (make-hash-table :test 'equal)
71 "Hash table for remote files properties.")
72
73 (defvar tramp-cache-inhibit-cache nil
74 "Inhibit cache read access, when `t'.
75 `nil' means to accept cache entries unconditionally. If the
76 value is a timestamp (as returned by `current-time'), cache
77 entries are not used when they have been written before this
78 time.")
79
80 (defcustom tramp-persistency-file-name
81 (cond
82 ;; GNU Emacs.
83 ((and (boundp 'user-emacs-directory)
84 (stringp (symbol-value 'user-emacs-directory))
85 (file-directory-p (symbol-value 'user-emacs-directory)))
86 (expand-file-name "tramp" (symbol-value 'user-emacs-directory)))
87 ((and (not (featurep 'xemacs)) (file-directory-p "~/.emacs.d/"))
88 "~/.emacs.d/tramp")
89 ;; XEmacs.
90 ((and (boundp 'user-init-directory)
91 (stringp (symbol-value 'user-init-directory))
92 (file-directory-p (symbol-value 'user-init-directory)))
93 (expand-file-name "tramp" (symbol-value 'user-init-directory)))
94 ((and (featurep 'xemacs) (file-directory-p "~/.xemacs/"))
95 "~/.xemacs/tramp")
96 ;; For users without `~/.emacs.d/' or `~/.xemacs/'.
97 (t "~/.tramp"))
98 "File which keeps connection history for Tramp connections."
99 :group 'tramp
100 :type 'file)
101
102 (defvar tramp-cache-data-changed nil
103 "Whether persistent cache data have been changed.")
104
105 (defun tramp-get-file-property (vec file property default)
106 "Get the PROPERTY of FILE from the cache context of VEC.
107 Returns DEFAULT if not set."
108 ;; Unify localname.
109 (setq vec (copy-sequence vec))
110 (aset vec 3 (tramp-run-real-handler 'directory-file-name (list file)))
111 (let* ((hash (or (gethash vec tramp-cache-data)
112 (puthash vec (make-hash-table :test 'equal)
113 tramp-cache-data)))
114 (value (when (hash-table-p hash) (gethash property hash))))
115 (if
116 ;; We take the value only if there is any, and
117 ;; `tramp-cache-inhibit-cache' indicates that it is still
118 ;; valid. Otherwise, DEFAULT is set.
119 (and (consp value)
120 (or (null tramp-cache-inhibit-cache)
121 (and (consp tramp-cache-inhibit-cache)
122 (tramp-time-less-p
123 tramp-cache-inhibit-cache (car value)))))
124 (setq value (cdr value))
125 (setq value default))
126
127 (if (consp tramp-cache-inhibit-cache)
128 (tramp-message vec 1 "%s %s %s" file property value))
129 (tramp-message vec 8 "%s %s %s" file property value)
130 value))
131
132 (defun tramp-set-file-property (vec file property value)
133 "Set the PROPERTY of FILE to VALUE, in the cache context of VEC.
134 Returns VALUE."
135 ;; Unify localname.
136 (setq vec (copy-sequence vec))
137 (aset vec 3 (tramp-run-real-handler 'directory-file-name (list file)))
138 (let ((hash (or (gethash vec tramp-cache-data)
139 (puthash vec (make-hash-table :test 'equal)
140 tramp-cache-data))))
141 ;; We put the timestamp there.
142 (puthash property (cons (current-time) value) hash)
143 (tramp-message vec 8 "%s %s %s" file property value)
144 value))
145
146 (defun tramp-flush-file-property (vec file)
147 "Remove all properties of FILE in the cache context of VEC."
148 ;; Unify localname.
149 (setq vec (copy-sequence vec))
150 (aset vec 3 (tramp-run-real-handler 'directory-file-name (list file)))
151 (tramp-message vec 8 "%s" file)
152 (remhash vec tramp-cache-data))
153
154 (defun tramp-flush-directory-property (vec directory)
155 "Remove all properties of DIRECTORY in the cache context of VEC.
156 Remove also properties of all files in subdirectories."
157 (let ((directory (tramp-run-real-handler
158 'directory-file-name (list directory))))
159 (tramp-message vec 8 "%s" directory)
160 (maphash
161 '(lambda (key value)
162 (when (and (stringp (tramp-file-name-localname key))
163 (string-match directory (tramp-file-name-localname key)))
164 (remhash key tramp-cache-data)))
165 tramp-cache-data)))
166
167 ;; Reverting or killing a buffer should also flush file properties.
168 ;; They could have been changed outside Tramp. In eshell, "ls" would
169 ;; not show proper directory contents when a file has been copied or
170 ;; deleted before.
171 (defun tramp-flush-file-function ()
172 "Flush all Tramp cache properties from `buffer-file-name'."
173 (let ((bfn (if (stringp (buffer-file-name))
174 (buffer-file-name)
175 default-directory)))
176 (when (tramp-tramp-file-p bfn)
177 (let* ((v (tramp-dissect-file-name bfn))
178 (localname (tramp-file-name-localname v)))
179 (tramp-flush-file-property v localname)))))
180
181 (add-hook 'before-revert-hook 'tramp-flush-file-function)
182 (add-hook 'eshell-pre-command-hook 'tramp-flush-file-function)
183 (add-hook 'kill-buffer-hook 'tramp-flush-file-function)
184 (add-hook 'tramp-cache-unload-hook
185 '(lambda ()
186 (remove-hook 'before-revert-hook
187 'tramp-flush-file-function)
188 (remove-hook 'eshell-pre-command-hook
189 'tramp-flush-file-function)
190 (remove-hook 'kill-buffer-hook
191 'tramp-flush-file-function)))
192
193 ;;; -- Properties --
194
195 (defun tramp-get-connection-property (key property default)
196 "Get the named PROPERTY for the connection.
197 KEY identifies the connection, it is either a process or a vector.
198 If the value is not set for the connection, returns DEFAULT."
199 ;; Unify key by removing localname from vector. Work with a copy in
200 ;; order to avoid side effects.
201 (when (vectorp key)
202 (setq key (copy-sequence key))
203 (aset key 3 nil))
204 (let* ((hash (gethash key tramp-cache-data))
205 (value (if (hash-table-p hash)
206 (gethash property hash default)
207 default)))
208 (tramp-message key 7 "%s %s" property value)
209 value))
210
211 (defun tramp-set-connection-property (key property value)
212 "Set the named PROPERTY of a connection to VALUE.
213 KEY identifies the connection, it is either a process or a vector.
214 PROPERTY is set persistent when KEY is a vector."
215 ;; Unify key by removing localname from vector. Work with a copy in
216 ;; order to avoid side effects.
217 (when (vectorp key)
218 (setq key (copy-sequence key))
219 (aset key 3 nil))
220 (let ((hash (or (gethash key tramp-cache-data)
221 (puthash key (make-hash-table :test 'equal)
222 tramp-cache-data))))
223 (puthash property value hash)
224 (setq tramp-cache-data-changed t)
225 ;; This function is called also during initialization of
226 ;; tramp-cache.el. `tramp-messageĀ“ is not defined yet at this
227 ;; time, so we ignore the corresponding error.
228 (condition-case nil
229 (tramp-message key 7 "%s %s" property value)
230 (error nil))
231 value))
232
233 (defun tramp-flush-connection-property (key)
234 "Remove all properties identified by KEY.
235 KEY identifies the connection, it is either a process or a vector."
236 ;; Unify key by removing localname from vector. Work with a copy in
237 ;; order to avoid side effects.
238 (when (vectorp key)
239 (setq key (copy-sequence key))
240 (aset key 3 nil))
241 (tramp-message
242 key 7 "%s %s" key
243 (let ((hash (gethash key tramp-cache-data))
244 properties)
245 (if (hash-table-p hash)
246 (maphash
247 (lambda (x y) (add-to-list 'properties x 'append))
248 (gethash key tramp-cache-data)))
249 properties))
250 (setq tramp-cache-data-changed t)
251 (remhash key tramp-cache-data))
252
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 (defun tramp-list-connections ()
274 "Return a list of all known connection vectors according to `tramp-cache'."
275 (let (result)
276 (maphash
277 '(lambda (key value)
278 (when (and (vectorp key) (null (aref key 3)))
279 (add-to-list 'result key)))
280 tramp-cache-data)
281 result))
282
283 (defun tramp-dump-connection-properties ()
284 "Write persistent connection properties into file `tramp-persistency-file-name'."
285 ;; We shouldn't fail, otherwise (X)Emacs might not be able to be closed.
286 (condition-case nil
287 (when (and (hash-table-p tramp-cache-data)
288 (not (zerop (hash-table-count tramp-cache-data)))
289 tramp-cache-data-changed
290 (stringp tramp-persistency-file-name))
291 (let ((cache (copy-hash-table tramp-cache-data)))
292 ;; Remove temporary data.
293 (maphash
294 '(lambda (key value)
295 (if (and (vectorp key) (not (tramp-file-name-localname key)))
296 (progn
297 (remhash "process-name" value)
298 (remhash "process-buffer" value)
299 (remhash "first-password-request" value))
300 (remhash key cache)))
301 cache)
302 ;; Dump it.
303 (with-temp-buffer
304 (insert
305 ";; -*- emacs-lisp -*-"
306 ;; `time-stamp-string' might not exist in all (X)Emacs flavors.
307 (condition-case nil
308 (progn
309 (format
310 " <%s %s>\n"
311 (time-stamp-string "%02y/%02m/%02d %02H:%02M:%02S")
312 tramp-persistency-file-name))
313 (error "\n"))
314 ";; Tramp connection history. Don't change this file.\n"
315 ";; You can delete it, forcing Tramp to reapply the checks.\n\n"
316 (with-output-to-string
317 (pp (read (format "(%s)" (tramp-cache-print cache))))))
318 (write-region
319 (point-min) (point-max) tramp-persistency-file-name))))
320 (error nil)))
321
322 (add-hook 'kill-emacs-hook 'tramp-dump-connection-properties)
323 (add-hook 'tramp-cache-unload-hook
324 '(lambda ()
325 (remove-hook 'kill-emacs-hook
326 'tramp-dump-connection-properties)))
327
328 (defun tramp-parse-connection-properties (method)
329 "Return a list of (user host) tuples allowed to access for METHOD.
330 This function is added always in `tramp-get-completion-function'
331 for all methods. Resulting data are derived from connection history."
332 (let (res)
333 (maphash
334 '(lambda (key value)
335 (if (and (vectorp key)
336 (string-equal method (tramp-file-name-method key))
337 (not (tramp-file-name-localname key)))
338 (push (list (tramp-file-name-user key)
339 (tramp-file-name-host key))
340 res)))
341 tramp-cache-data)
342 res))
343
344 ;; Read persistent connection history.
345 (when (and (stringp tramp-persistency-file-name)
346 (zerop (hash-table-count tramp-cache-data)))
347 (condition-case err
348 (with-temp-buffer
349 (insert-file-contents tramp-persistency-file-name)
350 (let ((list (read (current-buffer)))
351 element key item)
352 (while (setq element (pop list))
353 (setq key (pop element))
354 (while (setq item (pop element))
355 (tramp-set-connection-property key (pop item) (car item)))))
356 (setq tramp-cache-data-changed nil))
357 (file-error
358 ;; Most likely because the file doesn't exist yet. No message.
359 (clrhash tramp-cache-data))
360 (error
361 ;; File is corrupted.
362 (message "Tramp persistency file '%s' is corrupted: %s"
363 tramp-persistency-file-name (error-message-string err))
364 (clrhash tramp-cache-data))))
365
366 (provide 'tramp-cache)
367
368 ;; arch-tag: ee1739b7-7628-408c-9b96-d11a74b05d26
369 ;;; tramp-cache.el ends here