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 of the License, or
14 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
26 ;; An implementation of information caching for remote files.
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:
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'.
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
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.
51 ;; Pacify byte-compiler.
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"))
67 (defvar tramp-cache-data
(make-hash-table :test
'equal
)
68 "Hash table for remote files properties.")
70 (defcustom tramp-persistency-file-name
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/"))
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/"))
86 ;; For users without `~/.emacs.d/' or `~/.xemacs/'.
88 "File which keeps connection history for Tramp connections."
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."
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
)
101 (value (if (hash-table-p hash
)
102 (gethash property hash default
)
104 (tramp-message vec
8 "%s %s %s" file property value
)
107 (defun tramp-set-file-property (vec file property value
)
108 "Set the PROPERTY of FILE to VALUE, in the cache context of VEC.
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
)
116 (puthash property value hash
)
117 (tramp-message vec
8 "%s %s %s" file property value
)
120 (defun tramp-flush-file-property (vec file
)
121 "Remove all properties of FILE in the cache context of VEC."
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
))
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
)
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)
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.
212 (setq key
(copy-sequence key
))
214 (remhash key tramp-cache-data
))
216 (defun tramp-cache-print (table)
217 "Print hash table TABLE."
218 (when (hash-table-p table
)
225 (prin1-to-string (prin1-to-string key
))
226 (prin1-to-string key
))
227 (if (hash-table-p value
)
228 (tramp-cache-print value
)
230 (prin1-to-string (prin1-to-string value
))
231 (prin1-to-string value
))))))
232 (setq result
(if result
(concat result
" " tmp
) tmp
))))
236 (defun tramp-list-connections ()
237 "Return a list of all known connection vectors according to `tramp-cache'."
241 (when (and (vectorp key
) (null (aref key
3)))
242 (add-to-list 'result key
)))
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.
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.
257 (if (and (vectorp key
) (not (tramp-file-name-localname key
)))
259 (remhash "process-name" value
)
260 (remhash "process-buffer" value
))
261 (remhash key cache
)))
266 ";; -*- emacs-lisp -*-"
267 ;; `time-stamp-string' might not exist in all (X)Emacs flavors.
272 (time-stamp-string "%02y/%02m/%02d %02H:%02M:%02S")
273 tramp-persistency-file-name
))
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
))))))
280 (point-min) (point-max) tramp-persistency-file-name
))))
283 (add-hook 'kill-emacs-hook
'tramp-dump-connection-properties
)
284 (add-hook 'tramp-cache-unload-hook
286 (remove-hook 'kill-emacs-hook
287 'tramp-dump-connection-properties
)))
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 history."
296 (if (and (vectorp key
)
297 (string-equal method
(tramp-file-name-method key
))
298 (not (tramp-file-name-localname key
)))
299 (push (list (tramp-file-name-user key
)
300 (tramp-file-name-host key
))
305 ;; Read persistent connection history.
306 (when (and (stringp tramp-persistency-file-name
)
307 (zerop (hash-table-count tramp-cache-data
)))
310 (insert-file-contents tramp-persistency-file-name
)
311 (let ((list (read (current-buffer)))
313 (while (setq element
(pop list
))
314 (setq key
(pop element
))
315 (while (setq item
(pop element
))
316 (tramp-set-connection-property key
(pop item
) (car item
))))))
318 ;; Most likely because the file doesn't exist yet. No message.
319 (clrhash tramp-cache-data
))
321 ;; File is corrupted.
322 (message "Tramp persistency file '%s' is corrupted: %s"
323 tramp-persistency-file-name
(error-message-string err
))
324 (clrhash tramp-cache-data
))))
326 (provide 'tramp-cache
)
328 ;; arch-tag: ee1739b7-7628-408c-9b96-d11a74b05d26
329 ;;; tramp-cache.el ends here