1 ;;; -*- mode: Emacs-Lisp; coding: iso-2022-7bit; -*-
2 ;;; tramp-cache.el --- file information caching for Tramp
4 ;; Copyright (C) 2000, 2005, 2006, 2007 by 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; see the file COPYING. If not, see
24 ;; <http://www.gnu.org/licenses/>.
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
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.
53 ;; Pacify byte-compiler.
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 '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 (defun tramp-get-file-property (vec file property default
)
95 "Get the PROPERTY of FILE from the cache context of VEC.
96 Returns DEFAULT if not set."
98 (setq vec
(copy-sequence vec
))
99 (aset vec
3 (directory-file-name file
))
100 (let* ((hash (or (gethash vec tramp-cache-data
)
101 (puthash vec
(make-hash-table :test
'equal
)
103 (value (if (hash-table-p hash
)
104 (gethash property hash default
)
106 (tramp-message vec
8 "%s %s %s" file property value
)
109 (defun tramp-set-file-property (vec file property value
)
110 "Set the PROPERTY of FILE to VALUE, in the cache context of VEC.
113 (setq vec
(copy-sequence vec
))
114 (aset vec
3 (directory-file-name file
))
115 (let ((hash (or (gethash vec tramp-cache-data
)
116 (puthash vec
(make-hash-table :test
'equal
)
118 (puthash property value hash
)
119 (tramp-message vec
8 "%s %s %s" file property value
)
122 (defun tramp-flush-file-property (vec file
)
123 "Remove all properties of FILE in the cache context of VEC."
125 (setq vec
(copy-sequence vec
))
126 (aset vec
3 (directory-file-name file
))
127 (tramp-message vec
8 "%s" file
)
128 (remhash vec tramp-cache-data
))
130 (defun tramp-flush-directory-property (vec directory
)
131 "Remove all properties of DIRECTORY in the cache context of VEC.
132 Remove also properties of all files in subdirectories."
133 (let ((directory (directory-file-name directory
)))
134 (tramp-message vec
8 "%s" directory
)
137 (when (and (stringp key
)
138 (string-match directory
(tramp-file-name-localname key
)))
139 (remhash key tramp-cache-data
)))
142 (defun tramp-cache-print (table)
143 "Prints hash table TABLE."
144 (when (hash-table-p table
)
151 (prin1-to-string (prin1-to-string key
))
152 (prin1-to-string key
))
153 (if (hash-table-p value
)
154 (tramp-cache-print value
)
156 (prin1-to-string (prin1-to-string value
))
157 (prin1-to-string value
))))
158 result
(if result
(concat result
" " tmp
) tmp
)))
162 ;; Reverting or killing a buffer should also flush file properties.
163 ;; They could have been changed outside Tramp.
164 (defun tramp-flush-file-function ()
165 "Flush all Tramp cache properties from buffer-file-name."
166 (let ((bfn (buffer-file-name)))
167 (when (and (stringp bfn
) (tramp-tramp-file-p bfn
))
168 (let* ((v (tramp-dissect-file-name bfn
))
169 (localname (tramp-file-name-localname v
)))
170 (tramp-flush-file-property v localname
)))))
172 (add-hook 'before-revert-hook
'tramp-flush-file-function
)
173 (add-hook 'kill-buffer-hook
'tramp-flush-file-function
)
174 (add-hook 'tramp-cache-unload-hook
176 (remove-hook 'before-revert-hook
177 'tramp-flush-file-function
)
178 (remove-hook 'kill-buffer-hook
179 'tramp-flush-file-function
)))
183 (defun tramp-get-connection-property (key property default
)
184 "Get the named PROPERTY for the connection.
185 KEY identifies the connection, it is either a process or a vector.
186 If the value is not set for the connection, returns DEFAULT."
187 ;; Unify key by removing localname from vector. Work with a copy in
188 ;; order to avoid side effects.
190 (setq key
(copy-sequence key
))
192 (let* ((hash (gethash key tramp-cache-data
))
193 (value (if (hash-table-p hash
)
194 (gethash property hash default
)
196 (tramp-message key
7 "%s %s" property value
)
199 (defun tramp-set-connection-property (key property value
)
200 "Set the named PROPERTY of a connection to VALUE.
201 KEY identifies the connection, it is either a process or a vector.
202 PROPERTY is set persistent when KEY is a vector."
203 ;; Unify key by removing localname from vector. Work with a copy in
204 ;; order to avoid side effects.
206 (setq key
(copy-sequence key
))
208 (let ((hash (or (gethash key tramp-cache-data
)
209 (puthash key
(make-hash-table :test
'equal
)
211 (puthash property value hash
)
212 ;; This function is called also during initialization of
213 ;; tramp-cache.el. `tramp-messageĀ“ is not defined yet at this
214 ;; time, so we ignore the corresponding error.
216 (tramp-message key
7 "%s %s" property value
)
220 (defun tramp-flush-connection-property (key event
)
221 "Remove all properties identified by KEY.
222 KEY identifies the connection, it is either a process or a
223 vector. EVENT is not used, it is just applied because this
224 function is intended to run also as process sentinel."
225 ;; Unify key by removing localname from vector. Work with a copy in
226 ;; order to avoid side effects.
228 (setq key
(copy-sequence key
))
230 ; (tramp-message key 7 "%s" event)
231 (remhash key tramp-cache-data
))
233 (defun tramp-dump-connection-properties ()
234 "Writes persistent connection properties into file
235 `tramp-persistency-file-name'."
236 ;; We shouldn't fail, otherwise (X)Emacs might not be able to be closed.
238 (when (and (hash-table-p tramp-cache-data
)
239 (not (zerop (hash-table-count tramp-cache-data
)))
240 (stringp tramp-persistency-file-name
))
241 (let ((cache (copy-hash-table tramp-cache-data
)))
242 ;; Remove temporary data.
245 (if (and (vectorp key
) (not (tramp-file-name-localname key
)))
247 (remhash "process-name" value
)
248 (remhash "process-buffer" value
))
249 (remhash key cache
)))
254 ";; -*- emacs-lisp -*-"
255 ;; `time-stamp-string' might not exist in all (X)Emacs flavors.
260 (time-stamp-string "%02y/%02m/%02d %02H:%02M:%02S")
261 tramp-persistency-file-name
))
263 ";; Tramp connection history. Don't change this file.\n"
264 ";; You can delete it, forcing Tramp to reapply the checks.\n\n"
265 (with-output-to-string
266 (pp (read (format "(%s)" (tramp-cache-print cache
))))))
268 (point-min) (point-max) tramp-persistency-file-name
))))
271 (add-hook 'kill-emacs-hook
'tramp-dump-connection-properties
)
272 (add-hook 'tramp-cache-unload-hook
274 (remove-hook 'kill-emacs-hook
275 'tramp-dump-connection-properties
)))
277 (defun tramp-parse-connection-properties (method)
278 "Return a list of (user host) tuples allowed to access for METHOD.
279 This function is added always in `tramp-get-completion-function'
280 for all methods. Resulting data are derived from connection
285 (if (and (vectorp key
)
286 (string-equal method
(tramp-file-name-method key
))
287 (not (tramp-file-name-localname key
)))
288 (push (list (tramp-file-name-user key
)
289 (tramp-file-name-host key
))
294 ;; Read persistent connection history. Applied with
295 ;; `load-in-progress', because it shall be evaluated only once.
296 (when load-in-progress
299 (insert-file-contents tramp-persistency-file-name
)
300 (let ((list (read (current-buffer)))
302 (while (setq element
(pop list
))
303 (setq key
(pop element
))
304 (while (setq item
(pop element
))
305 (tramp-set-connection-property key
(pop item
) (car item
))))))
307 ;; Most likely because the file doesn't exist yet. No message.
308 (clrhash tramp-cache-data
))
310 ;; File is corrupted.
311 (message "%s" (error-message-string err
))
312 (clrhash tramp-cache-data
))))
314 (provide 'tramp-cache
)
316 ;; arch-tag: ee1739b7-7628-408c-9b96-d11a74b05d26
317 ;;; tramp-cache.el ends here