gnus-article-html: Decode contents by charset.
[emacs.git] / lisp / net / tramp-cache.el
blob9c8ab4cb017d781637375f0aa0e3715a18d2cbc9
1 ;;; tramp-cache.el --- file information caching for Tramp
3 ;; Copyright (C) 2000, 2005, 2006, 2007, 2008, 2009,
4 ;; 2010 Free Software Foundation, Inc.
6 ;; Author: Daniel Pittman <daniel@inanna.danann.net>
7 ;; Michael Albinus <michael.albinus@gmx.de>
8 ;; Keywords: comm, processes
9 ;; Package: tramp
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; An implementation of information caching for remote files.
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:
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'.
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'.
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.
51 ;;; Code:
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 'tramp-run-real-handler "tramp")
66 (autoload 'tramp-time-less-p "tramp")
67 (autoload 'time-stamp-string "time-stamp"))
69 ;;; -- Cache --
71 (defvar tramp-cache-data (make-hash-table :test 'equal)
72 "Hash table for remote files properties.")
74 (defvar tramp-cache-inhibit-cache nil
75 "Inhibit cache read access, when `t'.
76 `nil' means to accept cache entries unconditionally. If the
77 value is a timestamp (as returned by `current-time'), cache
78 entries are not used when they have been written before this
79 time.")
81 (defcustom tramp-persistency-file-name
82 (cond
83 ;; GNU Emacs.
84 ((and (boundp 'user-emacs-directory)
85 (stringp (symbol-value 'user-emacs-directory))
86 (file-directory-p (symbol-value 'user-emacs-directory)))
87 (expand-file-name "tramp" (symbol-value 'user-emacs-directory)))
88 ((and (not (featurep 'xemacs)) (file-directory-p "~/.emacs.d/"))
89 "~/.emacs.d/tramp")
90 ;; XEmacs.
91 ((and (boundp 'user-init-directory)
92 (stringp (symbol-value 'user-init-directory))
93 (file-directory-p (symbol-value 'user-init-directory)))
94 (expand-file-name "tramp" (symbol-value 'user-init-directory)))
95 ((and (featurep 'xemacs) (file-directory-p "~/.xemacs/"))
96 "~/.xemacs/tramp")
97 ;; For users without `~/.emacs.d/' or `~/.xemacs/'.
98 (t "~/.tramp"))
99 "File which keeps connection history for Tramp connections."
100 :group 'tramp
101 :type 'file)
103 (defvar tramp-cache-data-changed nil
104 "Whether persistent cache data have been changed.")
106 (defun tramp-get-file-property (vec file property default)
107 "Get the PROPERTY of FILE from the cache context of VEC.
108 Returns DEFAULT if not set."
109 ;; Unify localname.
110 (setq vec (copy-sequence vec))
111 (aset vec 3 (tramp-run-real-handler 'directory-file-name (list file)))
112 (let* ((hash (or (gethash vec tramp-cache-data)
113 (puthash vec (make-hash-table :test 'equal)
114 tramp-cache-data)))
115 (value (when (hash-table-p hash) (gethash property hash))))
117 ;; We take the value only if there is any, and
118 ;; `tramp-cache-inhibit-cache' indicates that it is still
119 ;; valid. Otherwise, DEFAULT is set.
120 (and (consp value)
121 (or (null tramp-cache-inhibit-cache)
122 (and (consp tramp-cache-inhibit-cache)
123 (tramp-time-less-p
124 tramp-cache-inhibit-cache (car value)))))
125 (setq value (cdr value))
126 (setq value default))
128 (if (consp tramp-cache-inhibit-cache)
129 (tramp-message vec 1 "%s %s %s" file property value))
130 (tramp-message vec 8 "%s %s %s" file property value)
131 value))
133 (defun tramp-set-file-property (vec file property value)
134 "Set the PROPERTY of FILE to VALUE, in the cache context of VEC.
135 Returns VALUE."
136 ;; Unify localname.
137 (setq vec (copy-sequence vec))
138 (aset vec 3 (tramp-run-real-handler 'directory-file-name (list file)))
139 (let ((hash (or (gethash vec tramp-cache-data)
140 (puthash vec (make-hash-table :test 'equal)
141 tramp-cache-data))))
142 ;; We put the timestamp there.
143 (puthash property (cons (current-time) value) hash)
144 (tramp-message vec 8 "%s %s %s" file property value)
145 value))
147 (defun tramp-flush-file-property (vec file)
148 "Remove all properties of FILE in the cache context of VEC."
149 ;; Unify localname.
150 (setq vec (copy-sequence vec))
151 (aset vec 3 (tramp-run-real-handler 'directory-file-name (list file)))
152 (tramp-message vec 8 "%s" file)
153 (remhash vec tramp-cache-data))
155 (defun tramp-flush-directory-property (vec directory)
156 "Remove all properties of DIRECTORY in the cache context of VEC.
157 Remove also properties of all files in subdirectories."
158 (let ((directory (tramp-run-real-handler
159 'directory-file-name (list directory))))
160 (tramp-message vec 8 "%s" directory)
161 (maphash
162 '(lambda (key value)
163 (when (and (stringp (tramp-file-name-localname key))
164 (string-match directory (tramp-file-name-localname key)))
165 (remhash key tramp-cache-data)))
166 tramp-cache-data)))
168 ;; Reverting or killing a buffer should also flush file properties.
169 ;; They could have been changed outside Tramp. In eshell, "ls" would
170 ;; not show proper directory contents when a file has been copied or
171 ;; deleted before.
172 (defun tramp-flush-file-function ()
173 "Flush all Tramp cache properties from `buffer-file-name'."
174 (let ((bfn (if (stringp (buffer-file-name))
175 (buffer-file-name)
176 default-directory)))
177 (when (tramp-tramp-file-p bfn)
178 (let* ((v (tramp-dissect-file-name bfn))
179 (localname (tramp-file-name-localname v)))
180 (tramp-flush-file-property v localname)))))
182 (add-hook 'before-revert-hook 'tramp-flush-file-function)
183 (add-hook 'eshell-pre-command-hook 'tramp-flush-file-function)
184 (add-hook 'kill-buffer-hook 'tramp-flush-file-function)
185 (add-hook 'tramp-cache-unload-hook
186 '(lambda ()
187 (remove-hook 'before-revert-hook
188 'tramp-flush-file-function)
189 (remove-hook 'eshell-pre-command-hook
190 'tramp-flush-file-function)
191 (remove-hook 'kill-buffer-hook
192 'tramp-flush-file-function)))
194 ;;; -- Properties --
196 (defun tramp-get-connection-property (key property default)
197 "Get the named PROPERTY for the connection.
198 KEY identifies the connection, it is either a process or a vector.
199 If the value is not set for the connection, returns DEFAULT."
200 ;; Unify key by removing localname from vector. Work with a copy in
201 ;; order to avoid side effects.
202 (when (vectorp key)
203 (setq key (copy-sequence key))
204 (aset key 3 nil))
205 (let* ((hash (gethash key tramp-cache-data))
206 (value (if (hash-table-p hash)
207 (gethash property hash default)
208 default)))
209 (tramp-message key 7 "%s %s" property value)
210 value))
212 (defun tramp-set-connection-property (key property value)
213 "Set the named PROPERTY of a connection to VALUE.
214 KEY identifies the connection, it is either a process or a vector.
215 PROPERTY is set persistent when KEY is a vector."
216 ;; Unify key by removing localname from vector. Work with a copy in
217 ;; order to avoid side effects.
218 (when (vectorp key)
219 (setq key (copy-sequence key))
220 (aset key 3 nil))
221 (let ((hash (or (gethash key tramp-cache-data)
222 (puthash key (make-hash-table :test 'equal)
223 tramp-cache-data))))
224 (puthash property value hash)
225 (setq tramp-cache-data-changed t)
226 ;; This function is called also during initialization of
227 ;; tramp-cache.el. `tramp-messageĀ“ is not defined yet at this
228 ;; time, so we ignore the corresponding error.
229 (condition-case nil
230 (tramp-message key 7 "%s %s" property value)
231 (error nil))
232 value))
234 (defun tramp-flush-connection-property (key)
235 "Remove all properties identified by KEY.
236 KEY identifies the connection, it is either a process or a vector."
237 ;; Unify key by removing localname from vector. Work with a copy in
238 ;; order to avoid side effects.
239 (when (vectorp key)
240 (setq key (copy-sequence key))
241 (aset key 3 nil))
242 (tramp-message
243 key 7 "%s %s" key
244 (let ((hash (gethash key tramp-cache-data))
245 properties)
246 (if (hash-table-p hash)
247 (maphash
248 (lambda (x y) (add-to-list 'properties x 'append))
249 (gethash key tramp-cache-data)))
250 properties))
251 (setq tramp-cache-data-changed t)
252 (remhash key tramp-cache-data))
254 (defun tramp-cache-print (table)
255 "Print hash table TABLE."
256 (when (hash-table-p table)
257 (let (result)
258 (maphash
259 '(lambda (key value)
260 (let ((tmp (format
261 "(%s %s)"
262 (if (processp key)
263 (prin1-to-string (prin1-to-string key))
264 (prin1-to-string key))
265 (if (hash-table-p value)
266 (tramp-cache-print value)
267 (if (bufferp value)
268 (prin1-to-string (prin1-to-string value))
269 (prin1-to-string value))))))
270 (setq result (if result (concat result " " tmp) tmp))))
271 table)
272 result)))
274 (defun tramp-list-connections ()
275 "Return a list of all known connection vectors according to `tramp-cache'."
276 (let (result)
277 (maphash
278 '(lambda (key value)
279 (when (and (vectorp key) (null (aref key 3)))
280 (add-to-list 'result key)))
281 tramp-cache-data)
282 result))
284 (defun tramp-dump-connection-properties ()
285 "Write persistent connection properties into file `tramp-persistency-file-name'."
286 ;; We shouldn't fail, otherwise (X)Emacs might not be able to be closed.
287 (condition-case nil
288 (when (and (hash-table-p tramp-cache-data)
289 (not (zerop (hash-table-count tramp-cache-data)))
290 tramp-cache-data-changed
291 (stringp tramp-persistency-file-name))
292 (let ((cache (copy-hash-table tramp-cache-data)))
293 ;; Remove temporary data.
294 (maphash
295 '(lambda (key value)
296 (if (and (vectorp key) (not (tramp-file-name-localname key)))
297 (progn
298 (remhash "process-name" value)
299 (remhash "process-buffer" value)
300 (remhash "first-password-request" value))
301 (remhash key cache)))
302 cache)
303 ;; Dump it.
304 (with-temp-buffer
305 (insert
306 ";; -*- emacs-lisp -*-"
307 ;; `time-stamp-string' might not exist in all (X)Emacs flavors.
308 (condition-case nil
309 (progn
310 (format
311 " <%s %s>\n"
312 (time-stamp-string "%02y/%02m/%02d %02H:%02M:%02S")
313 tramp-persistency-file-name))
314 (error "\n"))
315 ";; Tramp connection history. Don't change this file.\n"
316 ";; You can delete it, forcing Tramp to reapply the checks.\n\n"
317 (with-output-to-string
318 (pp (read (format "(%s)" (tramp-cache-print cache))))))
319 (write-region
320 (point-min) (point-max) tramp-persistency-file-name))))
321 (error nil)))
323 (add-hook 'kill-emacs-hook 'tramp-dump-connection-properties)
324 (add-hook 'tramp-cache-unload-hook
325 '(lambda ()
326 (remove-hook 'kill-emacs-hook
327 'tramp-dump-connection-properties)))
329 (defun tramp-parse-connection-properties (method)
330 "Return a list of (user host) tuples allowed to access for METHOD.
331 This function is added always in `tramp-get-completion-function'
332 for all methods. Resulting data are derived from connection history."
333 (let (res)
334 (maphash
335 '(lambda (key value)
336 (if (and (vectorp key)
337 (string-equal method (tramp-file-name-method key))
338 (not (tramp-file-name-localname key)))
339 (push (list (tramp-file-name-user key)
340 (tramp-file-name-host key))
341 res)))
342 tramp-cache-data)
343 res))
345 ;; Read persistent connection history.
346 (when (and (stringp tramp-persistency-file-name)
347 (zerop (hash-table-count tramp-cache-data)))
348 (condition-case err
349 (with-temp-buffer
350 (insert-file-contents tramp-persistency-file-name)
351 (let ((list (read (current-buffer)))
352 element key item)
353 (while (setq element (pop list))
354 (setq key (pop element))
355 (while (setq item (pop element))
356 (tramp-set-connection-property key (pop item) (car item)))))
357 (setq tramp-cache-data-changed nil))
358 (file-error
359 ;; Most likely because the file doesn't exist yet. No message.
360 (clrhash tramp-cache-data))
361 (error
362 ;; File is corrupted.
363 (message "Tramp persistency file '%s' is corrupted: %s"
364 tramp-persistency-file-name (error-message-string err))
365 (clrhash tramp-cache-data))))
367 (provide 'tramp-cache)
369 ;; arch-tag: ee1739b7-7628-408c-9b96-d11a74b05d26
370 ;;; tramp-cache.el ends here