Spelling fixes.
[bpt/emacs.git] / lisp / net / tramp-cache.el
CommitLineData
00d6fd04
MA
1;;; tramp-cache.el --- file information caching for Tramp
2
ab422c4d 3;; Copyright (C) 2000, 2005-2013 Free Software Foundation, Inc.
00d6fd04
MA
4
5;; Author: Daniel Pittman <daniel@inanna.danann.net>
6;; Michael Albinus <michael.albinus@gmx.de>
7;; Keywords: comm, processes
bd78fa1d 8;; Package: tramp
00d6fd04
MA
9
10;; This file is part of GNU Emacs.
11
874a927a 12;; GNU Emacs is free software: you can redistribute it and/or modify
00d6fd04 13;; it under the terms of the GNU General Public License as published by
874a927a
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
00d6fd04
MA
16
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.
21
22;; You should have received a copy of the GNU General Public License
874a927a 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
00d6fd04
MA
24
25;;; Commentary:
26
27;; An implementation of information caching for remote files.
28
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:
32;;
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
2c68ca0e 36;; host when starting a Perl script. These properties are saved in
00d6fd04
MA
37;; the file `tramp-persistency-file-name'.
38;;
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
43;; `file-attributes'.
44;;
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.
49
50;;; Code:
51
0f34aa77
MA
52(require 'tramp)
53(autoload 'time-stamp-string "time-stamp")
00d6fd04
MA
54
55;;; -- Cache --
56
0f34aa77 57;;;###tramp-autoload
00d6fd04
MA
58(defvar tramp-cache-data (make-hash-table :test 'equal)
59 "Hash table for remote files properties.")
60
61addbc2
MA
61;;;###tramp-autoload
62(defcustom tramp-connection-properties nil
63 "List of static connection properties.
64Every entry has the form (REGEXP PROPERTY VALUE). The regexp
65matches remote file names. It can be nil. PROPERTY is a string,
66and VALUE the corresponding value. They are used, if there is no
67matching entry in for PROPERTY in `tramp-cache-data'."
68 :group 'tramp
69 :version "24.4"
70 :type '(repeat (list (choice :tag "File Name regexp" regexp (const nil))
71 (choice :tag " Property" string)
72 (choice :tag " Value" sexp))))
73
00d6fd04
MA
74(defcustom tramp-persistency-file-name
75 (cond
76 ;; GNU Emacs.
d68b0220
MA
77 ((and (fboundp 'locate-user-emacs-file))
78 (expand-file-name (tramp-compat-funcall 'locate-user-emacs-file "tramp")))
00d6fd04
MA
79 ((and (boundp 'user-emacs-directory)
80 (stringp (symbol-value 'user-emacs-directory))
81 (file-directory-p (symbol-value 'user-emacs-directory)))
82 (expand-file-name "tramp" (symbol-value 'user-emacs-directory)))
83 ((and (not (featurep 'xemacs)) (file-directory-p "~/.emacs.d/"))
84 "~/.emacs.d/tramp")
85 ;; XEmacs.
86 ((and (boundp 'user-init-directory)
87 (stringp (symbol-value 'user-init-directory))
88 (file-directory-p (symbol-value 'user-init-directory)))
89 (expand-file-name "tramp" (symbol-value 'user-init-directory)))
90 ((and (featurep 'xemacs) (file-directory-p "~/.xemacs/"))
91 "~/.xemacs/tramp")
92 ;; For users without `~/.emacs.d/' or `~/.xemacs/'.
93 (t "~/.tramp"))
94 "File which keeps connection history for Tramp connections."
95 :group 'tramp
96 :type 'file)
97
7c3404ec
MA
98(defvar tramp-cache-data-changed nil
99 "Whether persistent cache data have been changed.")
100
0f34aa77 101;;;###tramp-autoload
00d6fd04
MA
102(defun tramp-get-file-property (vec file property default)
103 "Get the PROPERTY of FILE from the cache context of VEC.
104Returns DEFAULT if not set."
105 ;; Unify localname.
106 (setq vec (copy-sequence vec))
87bdd2c7 107 (aset vec 3 (tramp-run-real-handler 'directory-file-name (list file)))
00d6fd04
MA
108 (let* ((hash (or (gethash vec tramp-cache-data)
109 (puthash vec (make-hash-table :test 'equal)
110 tramp-cache-data)))
d5b5c94a
MA
111 (value (when (hash-table-p hash) (gethash property hash))))
112 (if
113 ;; We take the value only if there is any, and
4bc3c53d 114 ;; `remote-file-name-inhibit-cache' indicates that it is still
d5b5c94a
MA
115 ;; valid. Otherwise, DEFAULT is set.
116 (and (consp value)
4bc3c53d
MA
117 (or (null remote-file-name-inhibit-cache)
118 (and (integerp remote-file-name-inhibit-cache)
119 (<=
120 (tramp-time-diff (current-time) (car value))
121 remote-file-name-inhibit-cache))
122 (and (consp remote-file-name-inhibit-cache)
d5b5c94a 123 (tramp-time-less-p
4bc3c53d 124 remote-file-name-inhibit-cache (car value)))))
d5b5c94a
MA
125 (setq value (cdr value))
126 (setq value default))
127
00d6fd04 128 (tramp-message vec 8 "%s %s %s" file property value)
4bc3c53d
MA
129 (when (>= tramp-verbose 10)
130 (let* ((var (intern (concat "tramp-cache-get-count-" property)))
131 (val (or (ignore-errors (symbol-value var)) 0)))
132 (set var (1+ val))))
00d6fd04
MA
133 value))
134
0f34aa77 135;;;###tramp-autoload
00d6fd04
MA
136(defun tramp-set-file-property (vec file property value)
137 "Set the PROPERTY of FILE to VALUE, in the cache context of VEC.
138Returns VALUE."
139 ;; Unify localname.
140 (setq vec (copy-sequence vec))
87bdd2c7 141 (aset vec 3 (tramp-run-real-handler 'directory-file-name (list file)))
00d6fd04
MA
142 (let ((hash (or (gethash vec tramp-cache-data)
143 (puthash vec (make-hash-table :test 'equal)
144 tramp-cache-data))))
d5b5c94a
MA
145 ;; We put the timestamp there.
146 (puthash property (cons (current-time) value) hash)
00d6fd04 147 (tramp-message vec 8 "%s %s %s" file property value)
4bc3c53d
MA
148 (when (>= tramp-verbose 10)
149 (let* ((var (intern (concat "tramp-cache-set-count-" property)))
150 (val (or (ignore-errors (symbol-value var)) 0)))
151 (set var (1+ val))))
00d6fd04
MA
152 value))
153
0f34aa77 154;;;###tramp-autoload
00d6fd04
MA
155(defun tramp-flush-file-property (vec file)
156 "Remove all properties of FILE in the cache context of VEC."
d0c8fc8a
MA
157 ;; Remove file property of symlinks.
158 (let ((truename (tramp-get-file-property vec file "file-truename" nil)))
159 (when (and (stringp truename)
160 (not (string-equal file truename)))
161 (tramp-flush-file-property vec truename)))
00d6fd04
MA
162 ;; Unify localname.
163 (setq vec (copy-sequence vec))
87bdd2c7 164 (aset vec 3 (tramp-run-real-handler 'directory-file-name (list file)))
00d6fd04
MA
165 (tramp-message vec 8 "%s" file)
166 (remhash vec tramp-cache-data))
167
0f34aa77 168;;;###tramp-autoload
00d6fd04
MA
169(defun tramp-flush-directory-property (vec directory)
170 "Remove all properties of DIRECTORY in the cache context of VEC.
171Remove also properties of all files in subdirectories."
87bdd2c7
MA
172 (let ((directory (tramp-run-real-handler
173 'directory-file-name (list directory))))
00d6fd04
MA
174 (tramp-message vec 8 "%s" directory)
175 (maphash
4f91a816 176 (lambda (key value)
065ec2c7
MA
177 (when (and (stringp (tramp-file-name-localname key))
178 (string-match directory (tramp-file-name-localname key)))
179 (remhash key tramp-cache-data)))
00d6fd04
MA
180 tramp-cache-data)))
181
00d6fd04 182;; Reverting or killing a buffer should also flush file properties.
a7580c1c
MA
183;; They could have been changed outside Tramp. In eshell, "ls" would
184;; not show proper directory contents when a file has been copied or
185;; deleted before.
00d6fd04 186(defun tramp-flush-file-function ()
06207091 187 "Flush all Tramp cache properties from `buffer-file-name'."
a7580c1c
MA
188 (let ((bfn (if (stringp (buffer-file-name))
189 (buffer-file-name)
190 default-directory)))
191 (when (tramp-tramp-file-p bfn)
0f34aa77 192 (with-parsed-tramp-file-name bfn nil
00d6fd04
MA
193 (tramp-flush-file-property v localname)))))
194
195(add-hook 'before-revert-hook 'tramp-flush-file-function)
a7580c1c 196(add-hook 'eshell-pre-command-hook 'tramp-flush-file-function)
00d6fd04
MA
197(add-hook 'kill-buffer-hook 'tramp-flush-file-function)
198(add-hook 'tramp-cache-unload-hook
4f91a816 199 (lambda ()
065ec2c7
MA
200 (remove-hook 'before-revert-hook
201 'tramp-flush-file-function)
202 (remove-hook 'eshell-pre-command-hook
203 'tramp-flush-file-function)
204 (remove-hook 'kill-buffer-hook
205 'tramp-flush-file-function)))
00d6fd04
MA
206
207;;; -- Properties --
208
0f34aa77 209;;;###tramp-autoload
00d6fd04
MA
210(defun tramp-get-connection-property (key property default)
211 "Get the named PROPERTY for the connection.
212KEY identifies the connection, it is either a process or a vector.
213If the value is not set for the connection, returns DEFAULT."
214 ;; Unify key by removing localname from vector. Work with a copy in
215 ;; order to avoid side effects.
216 (when (vectorp key)
217 (setq key (copy-sequence key))
218 (aset key 3 nil))
219 (let* ((hash (gethash key tramp-cache-data))
61addbc2
MA
220 (value
221 (catch 'result
222 (or
223 ;; Check for dynamic properties.
224 (and
225 (hash-table-p hash)
226 (maphash
227 (lambda (x y) (when (equal x property) (throw 'result y)))
228 hash))
229 ;; Check for static properties.
230 (and
231 (vectorp key)
232 (dolist (elt tramp-connection-properties)
233 (when (and (string-match
234 (or (nth 0 elt) "")
235 (tramp-make-tramp-file-name
236 (aref key 0) (aref key 1) (aref key 2) nil))
237 (string-equal (or (nth 1 elt) "") (or property "")))
238 (throw 'result (nth 2 elt)))))
239 ;; The default value.
240 default))))
00d6fd04
MA
241 (tramp-message key 7 "%s %s" property value)
242 value))
243
0f34aa77 244;;;###tramp-autoload
00d6fd04
MA
245(defun tramp-set-connection-property (key property value)
246 "Set the named PROPERTY of a connection to VALUE.
247KEY identifies the connection, it is either a process or a vector.
248PROPERTY is set persistent when KEY is a vector."
249 ;; Unify key by removing localname from vector. Work with a copy in
250 ;; order to avoid side effects.
251 (when (vectorp key)
252 (setq key (copy-sequence key))
253 (aset key 3 nil))
254 (let ((hash (or (gethash key tramp-cache-data)
255 (puthash key (make-hash-table :test 'equal)
a5509865 256 tramp-cache-data))))
00d6fd04 257 (puthash property value hash)
7c3404ec 258 (setq tramp-cache-data-changed t)
03c1ad43 259 (tramp-message key 7 "%s %s" property value)
00d6fd04
MA
260 value))
261
0f34aa77 262;;;###tramp-autoload
1a0b96d3 263(defun tramp-flush-connection-property (key)
00d6fd04 264 "Remove all properties identified by KEY.
1a0b96d3 265KEY identifies the connection, it is either a process or a vector."
00d6fd04
MA
266 ;; Unify key by removing localname from vector. Work with a copy in
267 ;; order to avoid side effects.
268 (when (vectorp key)
269 (setq key (copy-sequence key))
270 (aset key 3 nil))
e946faaf
MA
271 (tramp-message
272 key 7 "%s %s" key
674a9263
MA
273 (let ((hash (gethash key tramp-cache-data))
274 properties)
275 (if (hash-table-p hash)
276 (maphash
277 (lambda (x y) (add-to-list 'properties x 'append))
278 (gethash key tramp-cache-data)))
e946faaf 279 properties))
7c3404ec 280 (setq tramp-cache-data-changed t)
00d6fd04
MA
281 (remhash key tramp-cache-data))
282
0f34aa77 283;;;###tramp-autoload
726f0272 284(defun tramp-cache-print (table)
b08104a0 285 "Print hash table TABLE."
726f0272
MA
286 (when (hash-table-p table)
287 (let (result)
288 (maphash
4f91a816 289 (lambda (key value)
065ec2c7
MA
290 (let ((tmp (format
291 "(%s %s)"
292 (if (processp key)
293 (prin1-to-string (prin1-to-string key))
294 (prin1-to-string key))
295 (if (hash-table-p value)
296 (tramp-cache-print value)
297 (if (bufferp value)
298 (prin1-to-string (prin1-to-string value))
299 (prin1-to-string value))))))
300 (setq result (if result (concat result " " tmp) tmp))))
726f0272
MA
301 table)
302 result)))
303
0f34aa77 304;;;###tramp-autoload
b08104a0
MA
305(defun tramp-list-connections ()
306 "Return a list of all known connection vectors according to `tramp-cache'."
726f0272
MA
307 (let (result)
308 (maphash
4f91a816 309 (lambda (key value)
065ec2c7
MA
310 (when (and (vectorp key) (null (aref key 3)))
311 (add-to-list 'result key)))
726f0272
MA
312 tramp-cache-data)
313 result))
314
00d6fd04 315(defun tramp-dump-connection-properties ()
b08104a0 316 "Write persistent connection properties into file `tramp-persistency-file-name'."
00d6fd04 317 ;; We shouldn't fail, otherwise (X)Emacs might not be able to be closed.
03c1ad43
MA
318 (ignore-errors
319 (when (and (hash-table-p tramp-cache-data)
320 (not (zerop (hash-table-count tramp-cache-data)))
321 tramp-cache-data-changed
322 (stringp tramp-persistency-file-name))
2fe4b125
MA
323 (let ((cache (copy-hash-table tramp-cache-data))
324 print-length print-level)
a5509865
MA
325 ;; Remove temporary data. If there is the key "login-as", we
326 ;; don't save either, because all other properties might
327 ;; depend on the login name, and we want to give the
328 ;; possibility to use another login name later on.
03c1ad43 329 (maphash
4f91a816 330 (lambda (key value)
a5509865
MA
331 (if (and (vectorp key)
332 (not (tramp-file-name-localname key))
333 (not (gethash "login-as" value)))
065ec2c7
MA
334 (progn
335 (remhash "process-name" value)
336 (remhash "process-buffer" value)
337 (remhash "first-password-request" value))
338 (remhash key cache)))
03c1ad43
MA
339 cache)
340 ;; Dump it.
341 (with-temp-buffer
342 (insert
343 ";; -*- emacs-lisp -*-"
344 ;; `time-stamp-string' might not exist in all (X)Emacs flavors.
345 (condition-case nil
346 (progn
347 (format
348 " <%s %s>\n"
349 (time-stamp-string "%02y/%02m/%02d %02H:%02M:%02S")
350 tramp-persistency-file-name))
351 (error "\n"))
352 ";; Tramp connection history. Don't change this file.\n"
353 ";; You can delete it, forcing Tramp to reapply the checks.\n\n"
354 (with-output-to-string
355 (pp (read (format "(%s)" (tramp-cache-print cache))))))
356 (write-region
357 (point-min) (point-max) tramp-persistency-file-name))))))
00d6fd04 358
845fc5e5
JB
359(unless noninteractive
360 (add-hook 'kill-emacs-hook 'tramp-dump-connection-properties))
00d6fd04 361(add-hook 'tramp-cache-unload-hook
4f91a816 362 (lambda ()
065ec2c7
MA
363 (remove-hook 'kill-emacs-hook
364 'tramp-dump-connection-properties)))
00d6fd04 365
8fca3921 366;;;###tramp-autoload
00d6fd04
MA
367(defun tramp-parse-connection-properties (method)
368 "Return a list of (user host) tuples allowed to access for METHOD.
369This function is added always in `tramp-get-completion-function'
06207091 370for all methods. Resulting data are derived from connection history."
00d6fd04
MA
371 (let (res)
372 (maphash
4f91a816 373 (lambda (key value)
065ec2c7
MA
374 (if (and (vectorp key)
375 (string-equal method (tramp-file-name-method key))
376 (not (tramp-file-name-localname key)))
377 (push (list (tramp-file-name-user key)
378 (tramp-file-name-host key))
379 res)))
00d6fd04
MA
380 tramp-cache-data)
381 res))
382
27e813fe 383;; Read persistent connection history.
8a4438b6 384(when (and (stringp tramp-persistency-file-name)
065ec2c7
MA
385 (zerop (hash-table-count tramp-cache-data))
386 ;; When "emacs -Q" has been called, both variables are nil.
387 ;; We do not load the persistency file then, in order to
388 ;; have a clean test environment.
09388e76
MA
389 (or (and (boundp 'init-file-user) (symbol-value 'init-file-user))
390 (and (boundp 'site-run-file) (symbol-value 'site-run-file))))
00d6fd04
MA
391 (condition-case err
392 (with-temp-buffer
393 (insert-file-contents tramp-persistency-file-name)
394 (let ((list (read (current-buffer)))
395 element key item)
396 (while (setq element (pop list))
397 (setq key (pop element))
398 (while (setq item (pop element))
7c3404ec
MA
399 (tramp-set-connection-property key (pop item) (car item)))))
400 (setq tramp-cache-data-changed nil))
00d6fd04
MA
401 (file-error
402 ;; Most likely because the file doesn't exist yet. No message.
403 (clrhash tramp-cache-data))
404 (error
405 ;; File is corrupted.
8a4438b6
MA
406 (message "Tramp persistency file '%s' is corrupted: %s"
407 tramp-persistency-file-name (error-message-string err))
00d6fd04
MA
408 (clrhash tramp-cache-data))))
409
0f34aa77
MA
410(add-hook 'tramp-unload-hook
411 (lambda ()
412 (unload-feature 'tramp-cache 'force)))
413
00d6fd04
MA
414(provide 'tramp-cache)
415
416;;; tramp-cache.el ends here