1 ;;; tramp-cache.el --- file information caching for Tramp
3 ;; Copyright (C) 2000, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 ;; Author: Daniel Pittman <daniel@inanna.danann.net>
6 ;; Michael Albinus <michael.albinus@gmx.de>
7 ;; Keywords: comm, processes
9 ;; This file is part of GNU Emacs.
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, or (at your option)
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, see
23 ;; <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 'time-stamp-string
"time-stamp"))
68 (defvar tramp-cache-data
(make-hash-table :test
'equal
)
69 "Hash table for remote files properties.")
71 (defcustom tramp-persistency-file-name
74 ((and (boundp 'user-emacs-directory
)
75 (stringp (symbol-value 'user-emacs-directory
))
76 (file-directory-p (symbol-value 'user-emacs-directory
)))
77 (expand-file-name "tramp" (symbol-value 'user-emacs-directory
)))
78 ((and (not (featurep 'xemacs
)) (file-directory-p "~/.emacs.d/"))
81 ((and (boundp 'user-init-directory
)
82 (stringp (symbol-value 'user-init-directory
))
83 (file-directory-p (symbol-value 'user-init-directory
)))
84 (expand-file-name "tramp" (symbol-value 'user-init-directory
)))
85 ((and (featurep 'xemacs
) (file-directory-p "~/.xemacs/"))
87 ;; For users without `~/.emacs.d/' or `~/.xemacs/'.
89 "File which keeps connection history for Tramp connections."
93 (defun tramp-get-file-property (vec file property default
)
94 "Get the PROPERTY of FILE from the cache context of VEC.
95 Returns DEFAULT if not set."
97 (setq vec
(copy-sequence vec
))
98 (aset vec
3 (directory-file-name file
))
99 (let* ((hash (or (gethash vec tramp-cache-data
)
100 (puthash vec
(make-hash-table :test
'equal
)
102 (value (if (hash-table-p hash
)
103 (gethash property hash default
)
105 (tramp-message vec
8 "%s %s %s" file property value
)
108 (defun tramp-set-file-property (vec file property value
)
109 "Set the PROPERTY of FILE to VALUE, in the cache context of VEC.
112 (setq vec
(copy-sequence vec
))
113 (aset vec
3 (directory-file-name file
))
114 (let ((hash (or (gethash vec tramp-cache-data
)
115 (puthash vec
(make-hash-table :test
'equal
)
117 (puthash property value hash
)
118 (tramp-message vec
8 "%s %s %s" file property value
)
121 (defun tramp-flush-file-property (vec file
)
122 "Remove all properties of FILE in the cache context of VEC."
124 (setq vec
(copy-sequence vec
))
125 (aset vec
3 (directory-file-name file
))
126 (tramp-message vec
8 "%s" file
)
127 (remhash vec tramp-cache-data
))
129 (defun tramp-flush-directory-property (vec directory
)
130 "Remove all properties of DIRECTORY in the cache context of VEC.
131 Remove also properties of all files in subdirectories."
132 (let ((directory (directory-file-name directory
)))
133 (tramp-message vec
8 "%s" directory
)
136 (when (and (stringp key
)
137 (string-match directory
(tramp-file-name-localname key
)))
138 (remhash key tramp-cache-data
)))
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
145 (defun tramp-flush-file-function ()
146 "Flush all Tramp cache properties from buffer-file-name."
147 (let ((bfn (if (stringp (buffer-file-name))
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
)))))
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
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
)))
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.
176 (setq key
(copy-sequence key
))
178 (let* ((hash (gethash key tramp-cache-data
))
179 (value (if (hash-table-p hash
)
180 (gethash property hash default
)
182 (tramp-message key
7 "%s %s" property value
)
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.
192 (setq key
(copy-sequence key
))
194 (let ((hash (or (gethash key tramp-cache-data
)
195 (puthash key
(make-hash-table :test
'equal
)
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.
202 (tramp-message key
7 "%s %s" property value
)
206 (defun tramp-flush-connection-property (key event
)
207 "Remove all properties identified by KEY.
208 KEY identifies the connection, it is either a process or a
209 vector. EVENT is not used, it is just applied because this
210 function is intended to run also as process sentinel."
211 ;; Unify key by removing localname from vector. Work with a copy in
212 ;; order to avoid side effects.
214 (setq key
(copy-sequence key
))
216 ; (tramp-message key 7 "%s" event)
217 (remhash key tramp-cache-data
))
219 (defun tramp-cache-print (table)
220 "Print hash table TABLE."
221 (when (hash-table-p table
)
228 (prin1-to-string (prin1-to-string key
))
229 (prin1-to-string key
))
230 (if (hash-table-p value
)
231 (tramp-cache-print value
)
233 (prin1-to-string (prin1-to-string value
))
234 (prin1-to-string value
))))))
235 (setq result
(if result
(concat result
" " tmp
) tmp
))))
239 (defun tramp-list-connections ()
240 "Return a list of all known connection vectors according to `tramp-cache'."
244 (when (and (vectorp key
) (null (aref key
3)))
245 (add-to-list 'result key
)))
249 (defun tramp-dump-connection-properties ()
250 "Write persistent connection properties into file `tramp-persistency-file-name'."
251 ;; We shouldn't fail, otherwise (X)Emacs might not be able to be closed.
253 (when (and (hash-table-p tramp-cache-data
)
254 (not (zerop (hash-table-count tramp-cache-data
)))
255 (stringp tramp-persistency-file-name
))
256 (let ((cache (copy-hash-table tramp-cache-data
)))
257 ;; Remove temporary data.
260 (if (and (vectorp key
) (not (tramp-file-name-localname key
)))
262 (remhash "process-name" value
)
263 (remhash "process-buffer" value
))
264 (remhash key cache
)))
269 ";; -*- emacs-lisp -*-"
270 ;; `time-stamp-string' might not exist in all (X)Emacs flavors.
275 (time-stamp-string "%02y/%02m/%02d %02H:%02M:%02S")
276 tramp-persistency-file-name
))
278 ";; Tramp connection history. Don't change this file.\n"
279 ";; You can delete it, forcing Tramp to reapply the checks.\n\n"
280 (with-output-to-string
281 (pp (read (format "(%s)" (tramp-cache-print cache
))))))
283 (point-min) (point-max) tramp-persistency-file-name
))))
286 (add-hook 'kill-emacs-hook
'tramp-dump-connection-properties
)
287 (add-hook 'tramp-cache-unload-hook
289 (remove-hook 'kill-emacs-hook
290 'tramp-dump-connection-properties
)))
292 (defun tramp-parse-connection-properties (method)
293 "Return a list of (user host) tuples allowed to access for METHOD.
294 This function is added always in `tramp-get-completion-function'
295 for all methods. Resulting data are derived from connection
300 (if (and (vectorp key
)
301 (string-equal method
(tramp-file-name-method key
))
302 (not (tramp-file-name-localname key
)))
303 (push (list (tramp-file-name-user key
)
304 (tramp-file-name-host key
))
309 ;; Read persistent connection history.
310 (when (and (stringp tramp-persistency-file-name
)
311 (zerop (hash-table-count tramp-cache-data
)))
314 (insert-file-contents tramp-persistency-file-name
)
315 (let ((list (read (current-buffer)))
317 (while (setq element
(pop list
))
318 (setq key
(pop element
))
319 (while (setq item
(pop element
))
320 (tramp-set-connection-property key
(pop item
) (car item
))))))
322 ;; Most likely because the file doesn't exist yet. No message.
323 (clrhash tramp-cache-data
))
325 ;; File is corrupted.
326 (message "Tramp persistency file '%s' is corrupted: %s"
327 tramp-persistency-file-name
(error-message-string err
))
328 (clrhash tramp-cache-data
))))
330 (provide 'tramp-cache
)
332 ;; arch-tag: ee1739b7-7628-408c-9b96-d11a74b05d26
333 ;;; tramp-cache.el ends here