1 ;;; tramp-cache.el --- file information caching for Tramp
3 ;; Copyright (C) 2000, 2005, 2006, 2007, 2008, 2009,
4 ;; 2010, 2011 Free Software Foundation, Inc.
6 ;; Author: Daniel Pittman <daniel@inanna.danann.net>
7 ;; Michael Albinus <michael.albinus@gmx.de>
8 ;; Keywords: comm, processes
10 ;; This file is part of GNU Emacs.
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.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; An implementation of information caching for remote files.
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:
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'.
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
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.
52 ;; Pacify byte-compiler.
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 'tramp-run-real-handler
"tramp")
65 (autoload 'tramp-time-less-p
"tramp")
66 (autoload 'time-stamp-string
"time-stamp"))
70 (defvar tramp-cache-data
(make-hash-table :test
'equal
)
71 "Hash table for remote files properties.")
73 (defvar tramp-cache-inhibit-cache nil
74 "Inhibit cache read access, when `t'.
75 `nil' means to accept cache entries unconditionally. If the
76 value is a timestamp (as returned by `current-time'), cache
77 entries are not used when they have been written before this
80 (defcustom tramp-persistency-file-name
83 ((and (boundp 'user-emacs-directory
)
84 (stringp (symbol-value 'user-emacs-directory
))
85 (file-directory-p (symbol-value 'user-emacs-directory
)))
86 (expand-file-name "tramp" (symbol-value 'user-emacs-directory
)))
87 ((and (not (featurep 'xemacs
)) (file-directory-p "~/.emacs.d/"))
90 ((and (boundp 'user-init-directory
)
91 (stringp (symbol-value 'user-init-directory
))
92 (file-directory-p (symbol-value 'user-init-directory
)))
93 (expand-file-name "tramp" (symbol-value 'user-init-directory
)))
94 ((and (featurep 'xemacs
) (file-directory-p "~/.xemacs/"))
96 ;; For users without `~/.emacs.d/' or `~/.xemacs/'.
98 "File which keeps connection history for Tramp connections."
102 (defvar tramp-cache-data-changed nil
103 "Whether persistent cache data have been changed.")
105 (defun tramp-get-file-property (vec file property default
)
106 "Get the PROPERTY of FILE from the cache context of VEC.
107 Returns DEFAULT if not set."
109 (setq vec
(copy-sequence vec
))
110 (aset vec
3 (tramp-run-real-handler 'directory-file-name
(list file
)))
111 (let* ((hash (or (gethash vec tramp-cache-data
)
112 (puthash vec
(make-hash-table :test
'equal
)
114 (value (when (hash-table-p hash
) (gethash property hash
))))
116 ;; We take the value only if there is any, and
117 ;; `tramp-cache-inhibit-cache' indicates that it is still
118 ;; valid. Otherwise, DEFAULT is set.
120 (or (null tramp-cache-inhibit-cache
)
121 (and (consp tramp-cache-inhibit-cache
)
123 tramp-cache-inhibit-cache
(car value
)))))
124 (setq value
(cdr value
))
125 (setq value default
))
127 (if (consp tramp-cache-inhibit-cache
)
128 (tramp-message vec
1 "%s %s %s" file property value
))
129 (tramp-message vec
8 "%s %s %s" file property value
)
132 (defun tramp-set-file-property (vec file property value
)
133 "Set the PROPERTY of FILE to VALUE, in the cache context of VEC.
136 (setq vec
(copy-sequence vec
))
137 (aset vec
3 (tramp-run-real-handler 'directory-file-name
(list file
)))
138 (let ((hash (or (gethash vec tramp-cache-data
)
139 (puthash vec
(make-hash-table :test
'equal
)
141 ;; We put the timestamp there.
142 (puthash property
(cons (current-time) value
) hash
)
143 (tramp-message vec
8 "%s %s %s" file property value
)
146 (defun tramp-flush-file-property (vec file
)
147 "Remove all properties of FILE in the cache context of VEC."
149 (setq vec
(copy-sequence vec
))
150 (aset vec
3 (tramp-run-real-handler 'directory-file-name
(list file
)))
151 (tramp-message vec
8 "%s" file
)
152 (remhash vec tramp-cache-data
))
154 (defun tramp-flush-directory-property (vec directory
)
155 "Remove all properties of DIRECTORY in the cache context of VEC.
156 Remove also properties of all files in subdirectories."
157 (let ((directory (tramp-run-real-handler
158 'directory-file-name
(list directory
))))
159 (tramp-message vec
8 "%s" directory
)
162 (when (and (stringp (tramp-file-name-localname key
))
163 (string-match directory
(tramp-file-name-localname key
)))
164 (remhash key tramp-cache-data
)))
167 ;; Reverting or killing a buffer should also flush file properties.
168 ;; They could have been changed outside Tramp. In eshell, "ls" would
169 ;; not show proper directory contents when a file has been copied or
171 (defun tramp-flush-file-function ()
172 "Flush all Tramp cache properties from `buffer-file-name'."
173 (let ((bfn (if (stringp (buffer-file-name))
176 (when (tramp-tramp-file-p bfn
)
177 (let* ((v (tramp-dissect-file-name bfn
))
178 (localname (tramp-file-name-localname v
)))
179 (tramp-flush-file-property v localname
)))))
181 (add-hook 'before-revert-hook
'tramp-flush-file-function
)
182 (add-hook 'eshell-pre-command-hook
'tramp-flush-file-function
)
183 (add-hook 'kill-buffer-hook
'tramp-flush-file-function
)
184 (add-hook 'tramp-cache-unload-hook
186 (remove-hook 'before-revert-hook
187 'tramp-flush-file-function
)
188 (remove-hook 'eshell-pre-command-hook
189 'tramp-flush-file-function
)
190 (remove-hook 'kill-buffer-hook
191 'tramp-flush-file-function
)))
195 (defun tramp-get-connection-property (key property default
)
196 "Get the named PROPERTY for the connection.
197 KEY identifies the connection, it is either a process or a vector.
198 If the value is not set for the connection, returns DEFAULT."
199 ;; Unify key by removing localname from vector. Work with a copy in
200 ;; order to avoid side effects.
202 (setq key
(copy-sequence key
))
204 (let* ((hash (gethash key tramp-cache-data
))
205 (value (if (hash-table-p hash
)
206 (gethash property hash default
)
208 (tramp-message key
7 "%s %s" property value
)
211 (defun tramp-set-connection-property (key property value
)
212 "Set the named PROPERTY of a connection to VALUE.
213 KEY identifies the connection, it is either a process or a vector.
214 PROPERTY is set persistent when KEY is a vector."
215 ;; Unify key by removing localname from vector. Work with a copy in
216 ;; order to avoid side effects.
218 (setq key
(copy-sequence key
))
220 (let ((hash (or (gethash key tramp-cache-data
)
221 (puthash key
(make-hash-table :test
'equal
)
223 (puthash property value hash
)
224 (setq tramp-cache-data-changed t
)
225 ;; This function is called also during initialization of
226 ;; tramp-cache.el. `tramp-messageĀ“ is not defined yet at this
227 ;; time, so we ignore the corresponding error.
229 (tramp-message key
7 "%s %s" property value
)
233 (defun tramp-flush-connection-property (key)
234 "Remove all properties identified by KEY.
235 KEY identifies the connection, it is either a process or a vector."
236 ;; Unify key by removing localname from vector. Work with a copy in
237 ;; order to avoid side effects.
239 (setq key
(copy-sequence key
))
243 (let ((hash (gethash key tramp-cache-data
))
245 (if (hash-table-p hash
)
247 (lambda (x y
) (add-to-list 'properties x
'append
))
248 (gethash key tramp-cache-data
)))
250 (setq tramp-cache-data-changed t
)
251 (remhash key tramp-cache-data
))
253 (defun tramp-cache-print (table)
254 "Print hash table TABLE."
255 (when (hash-table-p table
)
262 (prin1-to-string (prin1-to-string key
))
263 (prin1-to-string key
))
264 (if (hash-table-p value
)
265 (tramp-cache-print value
)
267 (prin1-to-string (prin1-to-string value
))
268 (prin1-to-string value
))))))
269 (setq result
(if result
(concat result
" " tmp
) tmp
))))
273 (defun tramp-list-connections ()
274 "Return a list of all known connection vectors according to `tramp-cache'."
278 (when (and (vectorp key
) (null (aref key
3)))
279 (add-to-list 'result key
)))
283 (defun tramp-dump-connection-properties ()
284 "Write persistent connection properties into file `tramp-persistency-file-name'."
285 ;; We shouldn't fail, otherwise (X)Emacs might not be able to be closed.
287 (when (and (hash-table-p tramp-cache-data
)
288 (not (zerop (hash-table-count tramp-cache-data
)))
289 tramp-cache-data-changed
290 (stringp tramp-persistency-file-name
))
291 (let ((cache (copy-hash-table tramp-cache-data
)))
292 ;; Remove temporary data.
295 (if (and (vectorp key
) (not (tramp-file-name-localname key
)))
297 (remhash "process-name" value
)
298 (remhash "process-buffer" value
)
299 (remhash "first-password-request" value
))
300 (remhash key cache
)))
305 ";; -*- emacs-lisp -*-"
306 ;; `time-stamp-string' might not exist in all (X)Emacs flavors.
311 (time-stamp-string "%02y/%02m/%02d %02H:%02M:%02S")
312 tramp-persistency-file-name
))
314 ";; Tramp connection history. Don't change this file.\n"
315 ";; You can delete it, forcing Tramp to reapply the checks.\n\n"
316 (with-output-to-string
317 (pp (read (format "(%s)" (tramp-cache-print cache
))))))
319 (point-min) (point-max) tramp-persistency-file-name
))))
322 (add-hook 'kill-emacs-hook
'tramp-dump-connection-properties
)
323 (add-hook 'tramp-cache-unload-hook
325 (remove-hook 'kill-emacs-hook
326 'tramp-dump-connection-properties
)))
328 (defun tramp-parse-connection-properties (method)
329 "Return a list of (user host) tuples allowed to access for METHOD.
330 This function is added always in `tramp-get-completion-function'
331 for all methods. Resulting data are derived from connection history."
335 (if (and (vectorp key
)
336 (string-equal method
(tramp-file-name-method key
))
337 (not (tramp-file-name-localname key
)))
338 (push (list (tramp-file-name-user key
)
339 (tramp-file-name-host key
))
344 ;; Read persistent connection history.
345 (when (and (stringp tramp-persistency-file-name
)
346 (zerop (hash-table-count tramp-cache-data
)))
349 (insert-file-contents tramp-persistency-file-name
)
350 (let ((list (read (current-buffer)))
352 (while (setq element
(pop list
))
353 (setq key
(pop element
))
354 (while (setq item
(pop element
))
355 (tramp-set-connection-property key
(pop item
) (car item
)))))
356 (setq tramp-cache-data-changed nil
))
358 ;; Most likely because the file doesn't exist yet. No message.
359 (clrhash tramp-cache-data
))
361 ;; File is corrupted.
362 (message "Tramp persistency file '%s' is corrupted: %s"
363 tramp-persistency-file-name
(error-message-string err
))
364 (clrhash tramp-cache-data
))))
366 (provide 'tramp-cache
)
368 ;; arch-tag: ee1739b7-7628-408c-9b96-d11a74b05d26
369 ;;; tramp-cache.el ends here