Merge from emacs--devo--0
[bpt/emacs.git] / lisp / net / tramp-cache.el
1 ;;; -*- mode: Emacs-Lisp; coding: iso-2022-7bit; -*-
2 ;;; tramp-cache.el --- file information caching for Tramp
3
4 ;; Copyright (C) 2000, 2005, 2006, 2007 by 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; see the file COPYING. If not, see
24 ;; <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
28 ;; An implementation of information caching for remote files.
29
30 ;; Each connection, identified by a vector [method user host
31 ;; localname] or by a process, has a unique cache. We distinguish 3
32 ;; kind of caches, depending on the key:
33 ;;
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'.
39 ;;
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 nile, depending on the file existence, or
43 ;; "file-attributes" caches the result of the function
44 ;; `file-attributes'.
45 ;;
46 ;; - The key is a process. This are temporary properties related to
47 ;; an open connection. Examples: "scripts" keeps shell script
48 ;; definitions already sent to the remote shell, "last-cmd-time" is
49 ;; the time stamp a command has been sent to the remote process.
50
51 ;;; Code:
52
53 ;; Pacify byte-compiler.
54 (eval-when-compile
55 (require 'cl)
56 (autoload 'tramp-message "tramp")
57 (autoload 'tramp-tramp-file-p "tramp")
58 ;; We cannot autoload macro `with-parsed-tramp-file-name', it
59 ;; results in problems of byte-compiled code.
60 (autoload 'tramp-dissect-file-name "tramp")
61 (autoload 'tramp-file-name-method "tramp")
62 (autoload 'tramp-file-name-user "tramp")
63 (autoload 'tramp-file-name-host "tramp")
64 (autoload 'tramp-file-name-localname "tramp")
65 (autoload 'time-stamp-string "time-stamp"))
66
67 ;;; -- Cache --
68
69 (defvar tramp-cache-data (make-hash-table :test 'equal)
70 "Hash table for remote files properties.")
71
72 (defcustom tramp-persistency-file-name
73 (cond
74 ;; GNU Emacs.
75 ((and (boundp 'user-emacs-directory)
76 (stringp (symbol-value 'user-emacs-directory))
77 (file-directory-p (symbol-value 'user-emacs-directory)))
78 (expand-file-name "tramp" (symbol-value 'user-emacs-directory)))
79 ((and (not (featurep 'xemacs)) (file-directory-p "~/.emacs.d/"))
80 "~/.emacs.d/tramp")
81 ;; XEmacs.
82 ((and (boundp 'user-init-directory)
83 (stringp (symbol-value 'user-init-directory))
84 (file-directory-p (symbol-value 'user-init-directory)))
85 (expand-file-name "tramp" (symbol-value 'user-init-directory)))
86 ((and (featurep 'xemacs) (file-directory-p "~/.xemacs/"))
87 "~/.xemacs/tramp")
88 ;; For users without `~/.emacs.d/' or `~/.xemacs/'.
89 (t "~/.tramp"))
90 "File which keeps connection history for Tramp connections."
91 :group 'tramp
92 :type 'file)
93
94 (defun tramp-get-file-property (vec file property default)
95 "Get the PROPERTY of FILE from the cache context of VEC.
96 Returns DEFAULT if not set."
97 ;; Unify localname.
98 (setq vec (copy-sequence vec))
99 (aset vec 3 (directory-file-name file))
100 (let* ((hash (or (gethash vec tramp-cache-data)
101 (puthash vec (make-hash-table :test 'equal)
102 tramp-cache-data)))
103 (value (if (hash-table-p hash)
104 (gethash property hash default)
105 default)))
106 (tramp-message vec 8 "%s %s %s" file property value)
107 value))
108
109 (defun tramp-set-file-property (vec file property value)
110 "Set the PROPERTY of FILE to VALUE, in the cache context of VEC.
111 Returns VALUE."
112 ;; Unify localname.
113 (setq vec (copy-sequence vec))
114 (aset vec 3 (directory-file-name file))
115 (let ((hash (or (gethash vec tramp-cache-data)
116 (puthash vec (make-hash-table :test 'equal)
117 tramp-cache-data))))
118 (puthash property value hash)
119 (tramp-message vec 8 "%s %s %s" file property value)
120 value))
121
122 (defun tramp-flush-file-property (vec file)
123 "Remove all properties of FILE in the cache context of VEC."
124 ;; Unify localname.
125 (setq vec (copy-sequence vec))
126 (aset vec 3 (directory-file-name file))
127 (tramp-message vec 8 "%s" file)
128 (remhash vec tramp-cache-data))
129
130 (defun tramp-flush-directory-property (vec directory)
131 "Remove all properties of DIRECTORY in the cache context of VEC.
132 Remove also properties of all files in subdirectories."
133 (let ((directory (directory-file-name directory)))
134 (tramp-message vec 8 "%s" directory)
135 (maphash
136 '(lambda (key value)
137 (when (and (stringp key)
138 (string-match directory (tramp-file-name-localname key)))
139 (remhash key tramp-cache-data)))
140 tramp-cache-data)))
141
142 (defun tramp-cache-print (table)
143 "Prints hash table TABLE."
144 (when (hash-table-p table)
145 (let (result tmp)
146 (maphash
147 '(lambda (key value)
148 (setq tmp (format
149 "(%s %s)"
150 (if (processp key)
151 (prin1-to-string (prin1-to-string key))
152 (prin1-to-string key))
153 (if (hash-table-p value)
154 (tramp-cache-print value)
155 (if (bufferp value)
156 (prin1-to-string (prin1-to-string value))
157 (prin1-to-string value))))
158 result (if result (concat result " " tmp) tmp)))
159 table)
160 result)))
161
162 ;; Reverting or killing a buffer should also flush file properties.
163 ;; They could have been changed outside Tramp.
164 (defun tramp-flush-file-function ()
165 "Flush all Tramp cache properties from buffer-file-name."
166 (let ((bfn (buffer-file-name)))
167 (when (and (stringp bfn) (tramp-tramp-file-p bfn))
168 (let* ((v (tramp-dissect-file-name bfn))
169 (localname (tramp-file-name-localname v)))
170 (tramp-flush-file-property v localname)))))
171
172 (add-hook 'before-revert-hook 'tramp-flush-file-function)
173 (add-hook 'kill-buffer-hook 'tramp-flush-file-function)
174 (add-hook 'tramp-cache-unload-hook
175 '(lambda ()
176 (remove-hook 'before-revert-hook
177 'tramp-flush-file-function)
178 (remove-hook 'kill-buffer-hook
179 'tramp-flush-file-function)))
180
181 ;;; -- Properties --
182
183 (defun tramp-get-connection-property (key property default)
184 "Get the named PROPERTY for the connection.
185 KEY identifies the connection, it is either a process or a vector.
186 If the value is not set for the connection, returns DEFAULT."
187 ;; Unify key by removing localname from vector. Work with a copy in
188 ;; order to avoid side effects.
189 (when (vectorp key)
190 (setq key (copy-sequence key))
191 (aset key 3 nil))
192 (let* ((hash (gethash key tramp-cache-data))
193 (value (if (hash-table-p hash)
194 (gethash property hash default)
195 default)))
196 (tramp-message key 7 "%s %s" property value)
197 value))
198
199 (defun tramp-set-connection-property (key property value)
200 "Set the named PROPERTY of a connection to VALUE.
201 KEY identifies the connection, it is either a process or a vector.
202 PROPERTY is set persistent when KEY is a vector."
203 ;; Unify key by removing localname from vector. Work with a copy in
204 ;; order to avoid side effects.
205 (when (vectorp key)
206 (setq key (copy-sequence key))
207 (aset key 3 nil))
208 (let ((hash (or (gethash key tramp-cache-data)
209 (puthash key (make-hash-table :test 'equal)
210 tramp-cache-data))))
211 (puthash property value hash)
212 ;; This function is called also during initialization of
213 ;; tramp-cache.el. `tramp-messageĀ“ is not defined yet at this
214 ;; time, so we ignore the corresponding error.
215 (condition-case nil
216 (tramp-message key 7 "%s %s" property value)
217 (error nil))
218 value))
219
220 (defun tramp-flush-connection-property (key event)
221 "Remove all properties identified by KEY.
222 KEY identifies the connection, it is either a process or a
223 vector. EVENT is not used, it is just applied because this
224 function is intended to run also as process sentinel."
225 ;; Unify key by removing localname from vector. Work with a copy in
226 ;; order to avoid side effects.
227 (when (vectorp key)
228 (setq key (copy-sequence key))
229 (aset key 3 nil))
230 ; (tramp-message key 7 "%s" event)
231 (remhash key tramp-cache-data))
232
233 (defun tramp-dump-connection-properties ()
234 "Writes persistent connection properties into file
235 `tramp-persistency-file-name'."
236 ;; We shouldn't fail, otherwise (X)Emacs might not be able to be closed.
237 (condition-case nil
238 (when (and (hash-table-p tramp-cache-data)
239 (not (zerop (hash-table-count tramp-cache-data)))
240 (stringp tramp-persistency-file-name))
241 (let ((cache (copy-hash-table tramp-cache-data)))
242 ;; Remove temporary data.
243 (maphash
244 '(lambda (key value)
245 (if (and (vectorp key) (not (tramp-file-name-localname key)))
246 (progn
247 (remhash "process-name" value)
248 (remhash "process-buffer" value))
249 (remhash key cache)))
250 cache)
251 ;; Dump it.
252 (with-temp-buffer
253 (insert
254 ";; -*- emacs-lisp -*-"
255 ;; `time-stamp-string' might not exist in all (X)Emacs flavors.
256 (condition-case nil
257 (progn
258 (format
259 " <%s %s>\n"
260 (time-stamp-string "%02y/%02m/%02d %02H:%02M:%02S")
261 tramp-persistency-file-name))
262 (error "\n"))
263 ";; Tramp connection history. Don't change this file.\n"
264 ";; You can delete it, forcing Tramp to reapply the checks.\n\n"
265 (with-output-to-string
266 (pp (read (format "(%s)" (tramp-cache-print cache))))))
267 (write-region
268 (point-min) (point-max) tramp-persistency-file-name))))
269 (error nil)))
270
271 (add-hook 'kill-emacs-hook 'tramp-dump-connection-properties)
272 (add-hook 'tramp-cache-unload-hook
273 '(lambda ()
274 (remove-hook 'kill-emacs-hook
275 'tramp-dump-connection-properties)))
276
277 (defun tramp-parse-connection-properties (method)
278 "Return a list of (user host) tuples allowed to access for METHOD.
279 This function is added always in `tramp-get-completion-function'
280 for all methods. Resulting data are derived from connection
281 history."
282 (let (res)
283 (maphash
284 '(lambda (key value)
285 (if (and (vectorp key)
286 (string-equal method (tramp-file-name-method key))
287 (not (tramp-file-name-localname key)))
288 (push (list (tramp-file-name-user key)
289 (tramp-file-name-host key))
290 res)))
291 tramp-cache-data)
292 res))
293
294 ;; Read persistent connection history. Applied with
295 ;; `load-in-progress', because it shall be evaluated only once.
296 (when load-in-progress
297 (condition-case err
298 (with-temp-buffer
299 (insert-file-contents tramp-persistency-file-name)
300 (let ((list (read (current-buffer)))
301 element key item)
302 (while (setq element (pop list))
303 (setq key (pop element))
304 (while (setq item (pop element))
305 (tramp-set-connection-property key (pop item) (car item))))))
306 (file-error
307 ;; Most likely because the file doesn't exist yet. No message.
308 (clrhash tramp-cache-data))
309 (error
310 ;; File is corrupted.
311 (message "%s" (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