Switch to recommended form of GPLv3 permissions notice.
[bpt/emacs.git] / 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 (defun tramp-get-file-property (vec file property default)
93 "Get the PROPERTY of FILE from the cache context of VEC.
94 Returns DEFAULT if not set."
95 ;; Unify localname.
96 (setq vec (copy-sequence vec))
97 (aset vec 3 (tramp-run-real-handler 'directory-file-name (list file)))
98 (let* ((hash (or (gethash vec tramp-cache-data)
99 (puthash vec (make-hash-table :test 'equal)
100 tramp-cache-data)))
101 (value (if (hash-table-p hash)
102 (gethash property hash default)
103 default)))
104 (tramp-message vec 8 "%s %s %s" file property value)
105 value))
106
107 (defun tramp-set-file-property (vec file property value)
108 "Set the PROPERTY of FILE to VALUE, in the cache context of VEC.
109 Returns VALUE."
110 ;; Unify localname.
111 (setq vec (copy-sequence vec))
112 (aset vec 3 (tramp-run-real-handler 'directory-file-name (list file)))
113 (let ((hash (or (gethash vec tramp-cache-data)
114 (puthash vec (make-hash-table :test 'equal)
115 tramp-cache-data))))
116 (puthash property value hash)
117 (tramp-message vec 8 "%s %s %s" file property value)
118 value))
119
120 (defun tramp-flush-file-property (vec file)
121 "Remove all properties of FILE in the cache context of VEC."
122 ;; Unify localname.
123 (setq vec (copy-sequence vec))
124 (aset vec 3 (tramp-run-real-handler 'directory-file-name (list file)))
125 (tramp-message vec 8 "%s" file)
126 (remhash vec tramp-cache-data))
127
128 (defun tramp-flush-directory-property (vec directory)
129 "Remove all properties of DIRECTORY in the cache context of VEC.
130 Remove also properties of all files in subdirectories."
131 (let ((directory (tramp-run-real-handler
132 'directory-file-name (list 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 ;; Reverting or killing a buffer should also flush file properties.
142 ;; They could have been changed outside Tramp. In eshell, "ls" would
143 ;; not show proper directory contents when a file has been copied or
144 ;; deleted before.
145 (defun tramp-flush-file-function ()
146 "Flush all Tramp cache properties from buffer-file-name."
147 (let ((bfn (if (stringp (buffer-file-name))
148 (buffer-file-name)
149 default-directory)))
150 (when (tramp-tramp-file-p bfn)
151 (let* ((v (tramp-dissect-file-name bfn))
152 (localname (tramp-file-name-localname v)))
153 (tramp-flush-file-property v localname)))))
154
155 (add-hook 'before-revert-hook 'tramp-flush-file-function)
156 (add-hook 'eshell-pre-command-hook 'tramp-flush-file-function)
157 (add-hook 'kill-buffer-hook 'tramp-flush-file-function)
158 (add-hook 'tramp-cache-unload-hook
159 '(lambda ()
160 (remove-hook 'before-revert-hook
161 'tramp-flush-file-function)
162 (remove-hook 'eshell-pre-command-hook
163 'tramp-flush-file-function)
164 (remove-hook 'kill-buffer-hook
165 'tramp-flush-file-function)))
166
167 ;;; -- Properties --
168
169 (defun tramp-get-connection-property (key property default)
170 "Get the named PROPERTY for the connection.
171 KEY identifies the connection, it is either a process or a vector.
172 If the value is not set for the connection, returns DEFAULT."
173 ;; Unify key by removing localname from vector. Work with a copy in
174 ;; order to avoid side effects.
175 (when (vectorp key)
176 (setq key (copy-sequence key))
177 (aset key 3 nil))
178 (let* ((hash (gethash key tramp-cache-data))
179 (value (if (hash-table-p hash)
180 (gethash property hash default)
181 default)))
182 (tramp-message key 7 "%s %s" property value)
183 value))
184
185 (defun tramp-set-connection-property (key property value)
186 "Set the named PROPERTY of a connection to VALUE.
187 KEY identifies the connection, it is either a process or a vector.
188 PROPERTY is set persistent when KEY is a vector."
189 ;; Unify key by removing localname from vector. Work with a copy in
190 ;; order to avoid side effects.
191 (when (vectorp key)
192 (setq key (copy-sequence key))
193 (aset key 3 nil))
194 (let ((hash (or (gethash key tramp-cache-data)
195 (puthash key (make-hash-table :test 'equal)
196 tramp-cache-data))))
197 (puthash property value hash)
198 ;; This function is called also during initialization of
199 ;; tramp-cache.el. `tramp-messageĀ“ is not defined yet at this
200 ;; time, so we ignore the corresponding error.
201 (condition-case nil
202 (tramp-message key 7 "%s %s" property value)
203 (error nil))
204 value))
205
206 (defun tramp-flush-connection-property (key)
207 "Remove all properties identified by KEY.
208 KEY identifies the connection, it is either a process or a vector."
209 ;; Unify key by removing localname from vector. Work with a copy in
210 ;; order to avoid side effects.
211 (when (vectorp key)
212 (setq key (copy-sequence key))
213 (aset key 3 nil))
214 (remhash key tramp-cache-data))
215
216 (defun tramp-cache-print (table)
217 "Print hash table TABLE."
218 (when (hash-table-p table)
219 (let (result)
220 (maphash
221 '(lambda (key value)
222 (let ((tmp (format
223 "(%s %s)"
224 (if (processp key)
225 (prin1-to-string (prin1-to-string key))
226 (prin1-to-string key))
227 (if (hash-table-p value)
228 (tramp-cache-print value)
229 (if (bufferp value)
230 (prin1-to-string (prin1-to-string value))
231 (prin1-to-string value))))))
232 (setq result (if result (concat result " " tmp) tmp))))
233 table)
234 result)))
235
236 (defun tramp-list-connections ()
237 "Return a list of all known connection vectors according to `tramp-cache'."
238 (let (result)
239 (maphash
240 '(lambda (key value)
241 (when (and (vectorp key) (null (aref key 3)))
242 (add-to-list 'result key)))
243 tramp-cache-data)
244 result))
245
246 (defun tramp-dump-connection-properties ()
247 "Write persistent connection properties into file `tramp-persistency-file-name'."
248 ;; We shouldn't fail, otherwise (X)Emacs might not be able to be closed.
249 (condition-case nil
250 (when (and (hash-table-p tramp-cache-data)
251 (not (zerop (hash-table-count tramp-cache-data)))
252 (stringp tramp-persistency-file-name))
253 (let ((cache (copy-hash-table tramp-cache-data)))
254 ;; Remove temporary data.
255 (maphash
256 '(lambda (key value)
257 (if (and (vectorp key) (not (tramp-file-name-localname key)))
258 (progn
259 (remhash "process-name" value)
260 (remhash "process-buffer" value))
261 (remhash key cache)))
262 cache)
263 ;; Dump it.
264 (with-temp-buffer
265 (insert
266 ";; -*- emacs-lisp -*-"
267 ;; `time-stamp-string' might not exist in all (X)Emacs flavors.
268 (condition-case nil
269 (progn
270 (format
271 " <%s %s>\n"
272 (time-stamp-string "%02y/%02m/%02d %02H:%02M:%02S")
273 tramp-persistency-file-name))
274 (error "\n"))
275 ";; Tramp connection history. Don't change this file.\n"
276 ";; You can delete it, forcing Tramp to reapply the checks.\n\n"
277 (with-output-to-string
278 (pp (read (format "(%s)" (tramp-cache-print cache))))))
279 (write-region
280 (point-min) (point-max) tramp-persistency-file-name))))
281 (error nil)))
282
283 (add-hook 'kill-emacs-hook 'tramp-dump-connection-properties)
284 (add-hook 'tramp-cache-unload-hook
285 '(lambda ()
286 (remove-hook 'kill-emacs-hook
287 'tramp-dump-connection-properties)))
288
289 (defun tramp-parse-connection-properties (method)
290 "Return a list of (user host) tuples allowed to access for METHOD.
291 This function is added always in `tramp-get-completion-function'
292 for all methods. Resulting data are derived from connection
293 history."
294 (let (res)
295 (maphash
296 '(lambda (key value)
297 (if (and (vectorp key)
298 (string-equal method (tramp-file-name-method key))
299 (not (tramp-file-name-localname key)))
300 (push (list (tramp-file-name-user key)
301 (tramp-file-name-host key))
302 res)))
303 tramp-cache-data)
304 res))
305
306 ;; Read persistent connection history.
307 (when (and (stringp tramp-persistency-file-name)
308 (zerop (hash-table-count tramp-cache-data)))
309 (condition-case err
310 (with-temp-buffer
311 (insert-file-contents tramp-persistency-file-name)
312 (let ((list (read (current-buffer)))
313 element key item)
314 (while (setq element (pop list))
315 (setq key (pop element))
316 (while (setq item (pop element))
317 (tramp-set-connection-property key (pop item) (car item))))))
318 (file-error
319 ;; Most likely because the file doesn't exist yet. No message.
320 (clrhash tramp-cache-data))
321 (error
322 ;; File is corrupted.
323 (message "Tramp persistency file '%s' is corrupted: %s"
324 tramp-persistency-file-name (error-message-string err))
325 (clrhash tramp-cache-data))))
326
327 (provide 'tramp-cache)
328
329 ;; arch-tag: ee1739b7-7628-408c-9b96-d11a74b05d26
330 ;;; tramp-cache.el ends here