]> code.delx.au - gnu-emacs/blob - lisp/net/tramp-cache.el
Merge from emacs--rel--22
[gnu-emacs] / lisp / net / tramp-cache.el
1 ;;; tramp-cache.el --- file information caching for Tramp
2
3 ;; Copyright (C) 2000, 2005, 2006, 2007 by 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
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, see
23 ;; <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 'time-stamp-string "time-stamp"))
65
66 ;;; -- Cache --
67
68 (defvar tramp-cache-data (make-hash-table :test 'equal)
69 "Hash table for remote files properties.")
70
71 (defcustom tramp-persistency-file-name
72 (cond
73 ;; GNU Emacs.
74 ((and (boundp 'user-emacs-directory)
75 (stringp (symbol-value 'user-emacs-directory))
76 (file-directory-p (symbol-value 'user-emacs-directory)))
77 (expand-file-name "tramp" (symbol-value 'user-emacs-directory)))
78 ((and (not (featurep 'xemacs)) (file-directory-p "~/.emacs.d/"))
79 "~/.emacs.d/tramp")
80 ;; XEmacs.
81 ((and (boundp 'user-init-directory)
82 (stringp (symbol-value 'user-init-directory))
83 (file-directory-p (symbol-value 'user-init-directory)))
84 (expand-file-name "tramp" (symbol-value 'user-init-directory)))
85 ((and (featurep 'xemacs) (file-directory-p "~/.xemacs/"))
86 "~/.xemacs/tramp")
87 ;; For users without `~/.emacs.d/' or `~/.xemacs/'.
88 (t "~/.tramp"))
89 "File which keeps connection history for Tramp connections."
90 :group 'tramp
91 :type 'file)
92
93 (defun tramp-get-file-property (vec file property default)
94 "Get the PROPERTY of FILE from the cache context of VEC.
95 Returns DEFAULT if not set."
96 ;; Unify localname.
97 (setq vec (copy-sequence vec))
98 (aset vec 3 (directory-file-name file))
99 (let* ((hash (or (gethash vec tramp-cache-data)
100 (puthash vec (make-hash-table :test 'equal)
101 tramp-cache-data)))
102 (value (if (hash-table-p hash)
103 (gethash property hash default)
104 default)))
105 (tramp-message vec 8 "%s %s %s" file property value)
106 value))
107
108 (defun tramp-set-file-property (vec file property value)
109 "Set the PROPERTY of FILE to VALUE, in the cache context of VEC.
110 Returns VALUE."
111 ;; Unify localname.
112 (setq vec (copy-sequence vec))
113 (aset vec 3 (directory-file-name file))
114 (let ((hash (or (gethash vec tramp-cache-data)
115 (puthash vec (make-hash-table :test 'equal)
116 tramp-cache-data))))
117 (puthash property value hash)
118 (tramp-message vec 8 "%s %s %s" file property value)
119 value))
120
121 (defun tramp-flush-file-property (vec file)
122 "Remove all properties of FILE in the cache context of VEC."
123 ;; Unify localname.
124 (setq vec (copy-sequence vec))
125 (aset vec 3 (directory-file-name file))
126 (tramp-message vec 8 "%s" file)
127 (remhash vec tramp-cache-data))
128
129 (defun tramp-flush-directory-property (vec directory)
130 "Remove all properties of DIRECTORY in the cache context of VEC.
131 Remove also properties of all files in subdirectories."
132 (let ((directory (directory-file-name directory)))
133 (tramp-message vec 8 "%s" directory)
134 (maphash
135 '(lambda (key value)
136 (when (and (stringp key)
137 (string-match directory (tramp-file-name-localname key)))
138 (remhash key tramp-cache-data)))
139 tramp-cache-data)))
140
141 (defun tramp-cache-print (table)
142 "Prints hash table TABLE."
143 (when (hash-table-p table)
144 (let (result)
145 (maphash
146 '(lambda (key value)
147 (let ((tmp (format
148 "(%s %s)"
149 (if (processp key)
150 (prin1-to-string (prin1-to-string key))
151 (prin1-to-string key))
152 (if (hash-table-p value)
153 (tramp-cache-print value)
154 (if (bufferp value)
155 (prin1-to-string (prin1-to-string value))
156 (prin1-to-string value))))))
157 (setq result (if result (concat result " " tmp) tmp))))
158 table)
159 result)))
160
161 ;; Reverting or killing a buffer should also flush file properties.
162 ;; They could have been changed outside Tramp.
163 (defun tramp-flush-file-function ()
164 "Flush all Tramp cache properties from buffer-file-name."
165 (let ((bfn (buffer-file-name)))
166 (when (and (stringp bfn) (tramp-tramp-file-p bfn))
167 (let* ((v (tramp-dissect-file-name bfn))
168 (localname (tramp-file-name-localname v)))
169 (tramp-flush-file-property v localname)))))
170
171 (add-hook 'before-revert-hook 'tramp-flush-file-function)
172 (add-hook 'kill-buffer-hook 'tramp-flush-file-function)
173 (add-hook 'tramp-cache-unload-hook
174 '(lambda ()
175 (remove-hook 'before-revert-hook
176 'tramp-flush-file-function)
177 (remove-hook 'kill-buffer-hook
178 'tramp-flush-file-function)))
179
180 ;;; -- Properties --
181
182 (defun tramp-get-connection-property (key property default)
183 "Get the named PROPERTY for the connection.
184 KEY identifies the connection, it is either a process or a vector.
185 If the value is not set for the connection, returns DEFAULT."
186 ;; Unify key by removing localname from vector. Work with a copy in
187 ;; order to avoid side effects.
188 (when (vectorp key)
189 (setq key (copy-sequence key))
190 (aset key 3 nil))
191 (let* ((hash (gethash key tramp-cache-data))
192 (value (if (hash-table-p hash)
193 (gethash property hash default)
194 default)))
195 (tramp-message key 7 "%s %s" property value)
196 value))
197
198 (defun tramp-set-connection-property (key property value)
199 "Set the named PROPERTY of a connection to VALUE.
200 KEY identifies the connection, it is either a process or a vector.
201 PROPERTY is set persistent when KEY is a vector."
202 ;; Unify key by removing localname from vector. Work with a copy in
203 ;; order to avoid side effects.
204 (when (vectorp key)
205 (setq key (copy-sequence key))
206 (aset key 3 nil))
207 (let ((hash (or (gethash key tramp-cache-data)
208 (puthash key (make-hash-table :test 'equal)
209 tramp-cache-data))))
210 (puthash property value hash)
211 ;; This function is called also during initialization of
212 ;; tramp-cache.el. `tramp-messageĀ“ is not defined yet at this
213 ;; time, so we ignore the corresponding error.
214 (condition-case nil
215 (tramp-message key 7 "%s %s" property value)
216 (error nil))
217 value))
218
219 (defun tramp-flush-connection-property (key event)
220 "Remove all properties identified by KEY.
221 KEY identifies the connection, it is either a process or a
222 vector. EVENT is not used, it is just applied because this
223 function is intended to run also as process sentinel."
224 ;; Unify key by removing localname from vector. Work with a copy in
225 ;; order to avoid side effects.
226 (when (vectorp key)
227 (setq key (copy-sequence key))
228 (aset key 3 nil))
229 ; (tramp-message key 7 "%s" event)
230 (remhash key tramp-cache-data))
231
232 (defun tramp-dump-connection-properties ()
233 "Writes persistent connection properties into file
234 `tramp-persistency-file-name'."
235 ;; We shouldn't fail, otherwise (X)Emacs might not be able to be closed.
236 (condition-case nil
237 (when (and (hash-table-p tramp-cache-data)
238 (not (zerop (hash-table-count tramp-cache-data)))
239 (stringp tramp-persistency-file-name))
240 (let ((cache (copy-hash-table tramp-cache-data)))
241 ;; Remove temporary data.
242 (maphash
243 '(lambda (key value)
244 (if (and (vectorp key) (not (tramp-file-name-localname key)))
245 (progn
246 (remhash "process-name" value)
247 (remhash "process-buffer" value))
248 (remhash key cache)))
249 cache)
250 ;; Dump it.
251 (with-temp-buffer
252 (insert
253 ";; -*- emacs-lisp -*-"
254 ;; `time-stamp-string' might not exist in all (X)Emacs flavors.
255 (condition-case nil
256 (progn
257 (format
258 " <%s %s>\n"
259 (time-stamp-string "%02y/%02m/%02d %02H:%02M:%02S")
260 tramp-persistency-file-name))
261 (error "\n"))
262 ";; Tramp connection history. Don't change this file.\n"
263 ";; You can delete it, forcing Tramp to reapply the checks.\n\n"
264 (with-output-to-string
265 (pp (read (format "(%s)" (tramp-cache-print cache))))))
266 (write-region
267 (point-min) (point-max) tramp-persistency-file-name))))
268 (error nil)))
269
270 (add-hook 'kill-emacs-hook 'tramp-dump-connection-properties)
271 (add-hook 'tramp-cache-unload-hook
272 '(lambda ()
273 (remove-hook 'kill-emacs-hook
274 'tramp-dump-connection-properties)))
275
276 (defun tramp-parse-connection-properties (method)
277 "Return a list of (user host) tuples allowed to access for METHOD.
278 This function is added always in `tramp-get-completion-function'
279 for all methods. Resulting data are derived from connection
280 history."
281 (let (res)
282 (maphash
283 '(lambda (key value)
284 (if (and (vectorp key)
285 (string-equal method (tramp-file-name-method key))
286 (not (tramp-file-name-localname key)))
287 (push (list (tramp-file-name-user key)
288 (tramp-file-name-host key))
289 res)))
290 tramp-cache-data)
291 res))
292
293 ;; Read persistent connection history.
294 (when (and (stringp tramp-persistency-file-name)
295 (zerop (hash-table-count tramp-cache-data)))
296 (condition-case err
297 (with-temp-buffer
298 (insert-file-contents tramp-persistency-file-name)
299 (let ((list (read (current-buffer)))
300 element key item)
301 (while (setq element (pop list))
302 (setq key (pop element))
303 (while (setq item (pop element))
304 (tramp-set-connection-property key (pop item) (car item))))))
305 (file-error
306 ;; Most likely because the file doesn't exist yet. No message.
307 (clrhash tramp-cache-data))
308 (error
309 ;; File is corrupted.
310 (message "Tramp persistency file '%s' is corrupted: %s"
311 tramp-persistency-file-name (error-message-string err))
312 (clrhash tramp-cache-data))))
313
314 (provide 'tramp-cache)
315
316 ;; arch-tag: ee1739b7-7628-408c-9b96-d11a74b05d26
317 ;;; tramp-cache.el ends here