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