1 ;;; tramp-cache.el --- file information caching for Tramp
3 ;; Copyright (C) 2000, 2005, 2006, 2007, 2008,
4 ;; 2009 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 'time-stamp-string
"time-stamp"))
69 (defvar tramp-cache-data
(make-hash-table :test
'equal
)
70 "Hash table for remote files properties.")
72 (defcustom tramp-persistency-file-name
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/"))
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/"))
88 ;; For users without `~/.emacs.d/' or `~/.xemacs/'.
90 "File which keeps connection history for Tramp connections."
94 (defvar tramp-cache-data-changed nil
95 "Whether persistent cache data have been changed.")
97 (defun tramp-get-file-property (vec file property default
)
98 "Get the PROPERTY of FILE from the cache context of VEC.
99 Returns DEFAULT if not set."
101 (setq vec
(copy-sequence vec
))
102 (aset vec
3 (tramp-run-real-handler 'directory-file-name
(list file
)))
103 (let* ((hash (or (gethash vec tramp-cache-data
)
104 (puthash vec
(make-hash-table :test
'equal
)
106 (value (if (hash-table-p hash
)
107 (gethash property hash default
)
109 (tramp-message vec
8 "%s %s %s" file property value
)
112 (defun tramp-set-file-property (vec file property value
)
113 "Set the PROPERTY of FILE to VALUE, in the cache context of VEC.
116 (setq vec
(copy-sequence vec
))
117 (aset vec
3 (tramp-run-real-handler 'directory-file-name
(list file
)))
118 (let ((hash (or (gethash vec tramp-cache-data
)
119 (puthash vec
(make-hash-table :test
'equal
)
121 (puthash property value hash
)
122 (tramp-message vec
8 "%s %s %s" file property value
)
125 (defun tramp-flush-file-property (vec file
)
126 "Remove all properties of FILE in the cache context of VEC."
128 (setq vec
(copy-sequence vec
))
129 (aset vec
3 (tramp-run-real-handler 'directory-file-name
(list file
)))
130 (tramp-message vec
8 "%s" file
)
131 (remhash vec tramp-cache-data
))
133 (defun tramp-flush-directory-property (vec directory
)
134 "Remove all properties of DIRECTORY in the cache context of VEC.
135 Remove also properties of all files in subdirectories."
136 (let ((directory (tramp-run-real-handler
137 'directory-file-name
(list directory
))))
138 (tramp-message vec
8 "%s" directory
)
141 (when (and (stringp key
)
142 (string-match directory
(tramp-file-name-localname key
)))
143 (remhash key tramp-cache-data
)))
146 ;; Reverting or killing a buffer should also flush file properties.
147 ;; They could have been changed outside Tramp. In eshell, "ls" would
148 ;; not show proper directory contents when a file has been copied or
150 (defun tramp-flush-file-function ()
151 "Flush all Tramp cache properties from `buffer-file-name'."
152 (let ((bfn (if (stringp (buffer-file-name))
155 (when (tramp-tramp-file-p bfn
)
156 (let* ((v (tramp-dissect-file-name bfn
))
157 (localname (tramp-file-name-localname v
)))
158 (tramp-flush-file-property v localname
)))))
160 (add-hook 'before-revert-hook
'tramp-flush-file-function
)
161 (add-hook 'eshell-pre-command-hook
'tramp-flush-file-function
)
162 (add-hook 'kill-buffer-hook
'tramp-flush-file-function
)
163 (add-hook 'tramp-cache-unload-hook
165 (remove-hook 'before-revert-hook
166 'tramp-flush-file-function
)
167 (remove-hook 'eshell-pre-command-hook
168 'tramp-flush-file-function
)
169 (remove-hook 'kill-buffer-hook
170 'tramp-flush-file-function
)))
174 (defun tramp-get-connection-property (key property default
)
175 "Get the named PROPERTY for the connection.
176 KEY identifies the connection, it is either a process or a vector.
177 If the value is not set for the connection, returns DEFAULT."
178 ;; Unify key by removing localname from vector. Work with a copy in
179 ;; order to avoid side effects.
181 (setq key
(copy-sequence key
))
183 (let* ((hash (gethash key tramp-cache-data
))
184 (value (if (hash-table-p hash
)
185 (gethash property hash default
)
187 (tramp-message key
7 "%s %s" property value
)
190 (defun tramp-set-connection-property (key property value
)
191 "Set the named PROPERTY of a connection to VALUE.
192 KEY identifies the connection, it is either a process or a vector.
193 PROPERTY is set persistent when KEY is a vector."
194 ;; Unify key by removing localname from vector. Work with a copy in
195 ;; order to avoid side effects.
197 (setq key
(copy-sequence key
))
199 (let ((hash (or (gethash key tramp-cache-data
)
200 (puthash key
(make-hash-table :test
'equal
)
202 (puthash property value hash
)
203 (setq tramp-cache-data-changed t
)
204 ;; This function is called also during initialization of
205 ;; tramp-cache.el. `tramp-messageĀ“ is not defined yet at this
206 ;; time, so we ignore the corresponding error.
208 (tramp-message key
7 "%s %s" property value
)
212 (defun tramp-flush-connection-property (key)
213 "Remove all properties identified by KEY.
214 KEY identifies the connection, it is either a process or 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 (setq tramp-cache-data-changed t
)
221 (remhash key tramp-cache-data
))
223 (defun tramp-cache-print (table)
224 "Print hash table TABLE."
225 (when (hash-table-p table
)
232 (prin1-to-string (prin1-to-string key
))
233 (prin1-to-string key
))
234 (if (hash-table-p value
)
235 (tramp-cache-print value
)
237 (prin1-to-string (prin1-to-string value
))
238 (prin1-to-string value
))))))
239 (setq result
(if result
(concat result
" " tmp
) tmp
))))
243 (defun tramp-list-connections ()
244 "Return a list of all known connection vectors according to `tramp-cache'."
248 (when (and (vectorp key
) (null (aref key
3)))
249 (add-to-list 'result key
)))
253 (defun tramp-dump-connection-properties ()
254 "Write persistent connection properties into file `tramp-persistency-file-name'."
255 ;; We shouldn't fail, otherwise (X)Emacs might not be able to be closed.
257 (when (and (hash-table-p tramp-cache-data
)
258 (not (zerop (hash-table-count tramp-cache-data
)))
259 tramp-cache-data-changed
260 (stringp tramp-persistency-file-name
))
261 (let ((cache (copy-hash-table tramp-cache-data
)))
262 ;; Remove temporary data.
265 (if (and (vectorp key
) (not (tramp-file-name-localname key
)))
267 (remhash "process-name" value
)
268 (remhash "process-buffer" value
))
269 (remhash key cache
)))
274 ";; -*- emacs-lisp -*-"
275 ;; `time-stamp-string' might not exist in all (X)Emacs flavors.
280 (time-stamp-string "%02y/%02m/%02d %02H:%02M:%02S")
281 tramp-persistency-file-name
))
283 ";; Tramp connection history. Don't change this file.\n"
284 ";; You can delete it, forcing Tramp to reapply the checks.\n\n"
285 (with-output-to-string
286 (pp (read (format "(%s)" (tramp-cache-print cache
))))))
288 (point-min) (point-max) tramp-persistency-file-name
))))
291 (add-hook 'kill-emacs-hook
'tramp-dump-connection-properties
)
292 (add-hook 'tramp-cache-unload-hook
294 (remove-hook 'kill-emacs-hook
295 'tramp-dump-connection-properties
)))
297 (defun tramp-parse-connection-properties (method)
298 "Return a list of (user host) tuples allowed to access for METHOD.
299 This function is added always in `tramp-get-completion-function'
300 for all methods. Resulting data are derived from connection history."
304 (if (and (vectorp key
)
305 (string-equal method
(tramp-file-name-method key
))
306 (not (tramp-file-name-localname key
)))
307 (push (list (tramp-file-name-user key
)
308 (tramp-file-name-host key
))
313 ;; Read persistent connection history.
314 (when (and (stringp tramp-persistency-file-name
)
315 (zerop (hash-table-count tramp-cache-data
)))
318 (insert-file-contents tramp-persistency-file-name
)
319 (let ((list (read (current-buffer)))
321 (while (setq element
(pop list
))
322 (setq key
(pop element
))
323 (while (setq item
(pop element
))
324 (tramp-set-connection-property key
(pop item
) (car item
)))))
325 (setq tramp-cache-data-changed nil
))
327 ;; Most likely because the file doesn't exist yet. No message.
328 (clrhash tramp-cache-data
))
330 ;; File is corrupted.
331 (message "Tramp persistency file '%s' is corrupted: %s"
332 tramp-persistency-file-name
(error-message-string err
))
333 (clrhash tramp-cache-data
))))
335 (provide 'tramp-cache
)
337 ;; arch-tag: ee1739b7-7628-408c-9b96-d11a74b05d26
338 ;;; tramp-cache.el ends here