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