]> code.delx.au - gnu-emacs/blobdiff - lisp/net/tramp-cache.el
; Merge from origin/emacs-25
[gnu-emacs] / lisp / net / tramp-cache.el
index e4fca46ce2d3fc5edb7215550f717a13f97d674e..92f66f414aefbb2fa900ad49f92502f81b7b1dd2 100644 (file)
@@ -1,6 +1,6 @@
 ;;; tramp-cache.el --- file information caching for Tramp
 
-;; Copyright (C) 2000, 2005-2012 Free Software Foundation, Inc.
+;; Copyright (C) 2000, 2005-2016 Free Software Foundation, Inc.
 
 ;; Author: Daniel Pittman <daniel@inanna.danann.net>
 ;;         Michael Albinus <michael.albinus@gmx.de>
 ;;
 ;; - localname is a string.  This are temporary properties, which are
 ;;   related to the file localname is referring to.  Examples:
-;;   "file-exists-p" is t or nile, depending on the file existence, or
+;;   "file-exists-p" is t or nil, depending on the file existence, or
 ;;   "file-attributes" caches the result of the function
-;;  `file-attributes'.
+;;   `file-attributes'.  These entries have a timestamp, and they
+;;   expire after `remote-file-name-inhibit-cache' seconds if this
+;;   variable is set.
 ;;
 ;; - The key is a process.  This are temporary properties related to
 ;;   an open connection.  Examples: "scripts" keeps shell script
 (defvar tramp-cache-data (make-hash-table :test 'equal)
   "Hash table for remote files properties.")
 
+;;;###tramp-autoload
+(defcustom tramp-connection-properties nil
+  "List of static connection properties.
+Every entry has the form (REGEXP PROPERTY VALUE).  The regexp
+matches remote file names.  It can be nil.  PROPERTY is a string,
+and VALUE the corresponding value.  They are used, if there is no
+matching entry for PROPERTY in `tramp-cache-data'.  For more
+details see the info pages."
+  :group 'tramp
+  :version "24.4"
+  :type '(repeat (list (choice :tag "File Name regexp" regexp (const nil))
+                      (choice :tag "        Property" string)
+                      (choice :tag "           Value" sexp))))
+
 (defcustom tramp-persistency-file-name
-  (cond
-   ;; GNU Emacs.
-   ((and (fboundp 'locate-user-emacs-file))
-    (expand-file-name (tramp-compat-funcall 'locate-user-emacs-file "tramp")))
-   ((and (boundp 'user-emacs-directory)
-        (stringp (symbol-value 'user-emacs-directory))
-        (file-directory-p (symbol-value 'user-emacs-directory)))
-    (expand-file-name "tramp" (symbol-value 'user-emacs-directory)))
-   ((and (not (featurep 'xemacs)) (file-directory-p "~/.emacs.d/"))
-    "~/.emacs.d/tramp")
-   ;; XEmacs.
-   ((and (boundp 'user-init-directory)
-        (stringp (symbol-value 'user-init-directory))
-        (file-directory-p (symbol-value 'user-init-directory)))
-    (expand-file-name "tramp" (symbol-value 'user-init-directory)))
-   ((and (featurep 'xemacs) (file-directory-p "~/.xemacs/"))
-    "~/.xemacs/tramp")
-   ;; For users without `~/.emacs.d/' or `~/.xemacs/'.
-   (t "~/.tramp"))
+  (expand-file-name (locate-user-emacs-file "tramp"))
   "File which keeps connection history for Tramp connections."
   :group 'tramp
   :type 'file)
 (defvar tramp-cache-data-changed nil
   "Whether persistent cache data have been changed.")
 
+(defun tramp-get-hash-table (key)
+  "Returns the hash table for KEY.
+If it doesn't exist yet, it is created and initialized with
+matching entries of `tramp-connection-properties'."
+  (or (gethash key tramp-cache-data)
+      (let ((hash
+            (puthash key (make-hash-table :test 'equal) tramp-cache-data)))
+       (when (vectorp key)
+         (dolist (elt tramp-connection-properties)
+           (when (string-match
+                  (or (nth 0 elt) "")
+                  (tramp-make-tramp-file-name
+                   (aref key 0) (aref key 1) (aref key 2) nil))
+             (tramp-set-connection-property key (nth 1 elt) (nth 2 elt)))))
+       hash)))
+
 ;;;###tramp-autoload
-(defun tramp-get-file-property (vec file property default)
-  "Get the PROPERTY of FILE from the cache context of VEC.
+(defun tramp-get-file-property (key file property default)
+  "Get the PROPERTY of FILE from the cache context of KEY.
 Returns DEFAULT if not set."
-  ;; Unify localname.
-  (setq vec (copy-sequence vec))
-  (aset vec 3 (tramp-run-real-handler 'directory-file-name (list file)))
-  (let* ((hash (or (gethash vec tramp-cache-data)
-                  (puthash vec (make-hash-table :test 'equal)
-                           tramp-cache-data)))
+  ;; Unify localname.  Remove hop from vector.
+  (setq key (copy-sequence key))
+  (aset key 3 (tramp-run-real-handler 'directory-file-name (list file)))
+  (aset key 4 nil)
+  (let* ((hash (tramp-get-hash-table key))
         (value (when (hash-table-p hash) (gethash property hash))))
     (if
        ;; We take the value only if there is any, and
@@ -107,77 +120,91 @@ Returns DEFAULT if not set."
                       (tramp-time-diff (current-time) (car value))
                       remote-file-name-inhibit-cache))
                 (and (consp remote-file-name-inhibit-cache)
-                     (tramp-time-less-p
+                     (time-less-p
                       remote-file-name-inhibit-cache (car value)))))
        (setq value (cdr value))
       (setq value default))
 
-    (tramp-message vec 8 "%s %s %s" file property value)
+    (tramp-message key 8 "%s %s %s" file property value)
     (when (>= tramp-verbose 10)
       (let* ((var (intern (concat "tramp-cache-get-count-" property)))
-            (val (or (ignore-errors (symbol-value var)) 0)))
+            (val (or (and (boundp var) (symbol-value var)) 0)))
        (set var (1+ val))))
     value))
 
 ;;;###tramp-autoload
-(defun tramp-set-file-property (vec file property value)
-  "Set the PROPERTY of FILE to VALUE, in the cache context of VEC.
+(defun tramp-set-file-property (key file property value)
+  "Set the PROPERTY of FILE to VALUE, in the cache context of KEY.
 Returns VALUE."
-  ;; Unify localname.
-  (setq vec (copy-sequence vec))
-  (aset vec 3 (tramp-run-real-handler 'directory-file-name (list file)))
-  (let ((hash (or (gethash vec tramp-cache-data)
-                 (puthash vec (make-hash-table :test 'equal)
-                          tramp-cache-data))))
+  ;; Unify localname.  Remove hop from vector.
+  (setq key (copy-sequence key))
+  (aset key 3 (tramp-run-real-handler 'directory-file-name (list file)))
+  (aset key 4 nil)
+  (let ((hash (tramp-get-hash-table key)))
     ;; We put the timestamp there.
     (puthash property (cons (current-time) value) hash)
-    (tramp-message vec 8 "%s %s %s" file property value)
+    (tramp-message key 8 "%s %s %s" file property value)
     (when (>= tramp-verbose 10)
       (let* ((var (intern (concat "tramp-cache-set-count-" property)))
-            (val (or (ignore-errors (symbol-value var)) 0)))
+            (val (or (and (boundp var) (symbol-value var)) 0)))
        (set var (1+ val))))
     value))
 
 ;;;###tramp-autoload
-(defun tramp-flush-file-property (vec file)
-  "Remove all properties of FILE in the cache context of VEC."
-  ;; Remove file property of symlinks.
-  (let ((truename (tramp-get-file-property vec file "file-truename" nil)))
+(defun tramp-flush-file-property (key file)
+  "Remove all properties of FILE in the cache context of KEY."
+  (let* ((file (tramp-run-real-handler
+               'directory-file-name (list file)))
+        (truename (tramp-get-file-property key file "file-truename" nil)))
+    ;; Remove file properties of symlinks.
     (when (and (stringp truename)
-              (not (string-equal file truename)))
-      (tramp-flush-file-property vec truename)))
-  ;; Unify localname.
-  (setq vec (copy-sequence vec))
-  (aset vec 3 (tramp-run-real-handler 'directory-file-name (list file)))
-  (tramp-message vec 8 "%s" file)
-  (remhash vec tramp-cache-data))
+              (not (string-equal file (directory-file-name truename))))
+      (tramp-flush-file-property key truename))
+    ;; Unify localname.  Remove hop from vector.
+    (setq key (copy-sequence key))
+    (aset key 3 file)
+    (aset key 4 nil)
+    (tramp-message key 8 "%s" file)
+    (remhash key tramp-cache-data)))
 
 ;;;###tramp-autoload
-(defun tramp-flush-directory-property (vec directory)
-  "Remove all properties of DIRECTORY in the cache context of VEC.
+(defun tramp-flush-directory-property (key directory)
+  "Remove all properties of DIRECTORY in the cache context of KEY.
 Remove also properties of all files in subdirectories."
-  (let ((directory (tramp-run-real-handler
-                   'directory-file-name (list directory))))
-  (tramp-message vec 8 "%s" directory)
+  (let* ((directory (tramp-run-real-handler
+                   'directory-file-name (list directory)))
+        (truename (tramp-get-file-property key directory "file-truename" nil)))
+    ;; Remove file properties of symlinks.
+    (when (and (stringp truename)
+              (not (string-equal directory (directory-file-name truename))))
+      (tramp-flush-directory-property key truename))
+    (tramp-message key 8 "%s" directory)
     (maphash
-     (lambda (key value)
+     (lambda (key _value)
        (when (and (stringp (tramp-file-name-localname key))
-                 (string-match directory (tramp-file-name-localname key)))
+                 (string-match (regexp-quote directory)
+                               (tramp-file-name-localname key)))
         (remhash key tramp-cache-data)))
      tramp-cache-data)))
 
 ;; Reverting or killing a buffer should also flush file properties.
 ;; They could have been changed outside Tramp.  In eshell, "ls" would
 ;; not show proper directory contents when a file has been copied or
-;; deleted before.
+;; deleted before.  We must apply `save-match-data', because it would
+;; corrupt other packages otherwise (reported from org).
 (defun tramp-flush-file-function ()
-  "Flush all Tramp cache properties from `buffer-file-name'."
-  (let ((bfn (if (stringp (buffer-file-name))
-                (buffer-file-name)
-              default-directory)))
-    (when (tramp-tramp-file-p bfn)
-      (with-parsed-tramp-file-name bfn nil
-       (tramp-flush-file-property v localname)))))
+  "Flush all Tramp cache properties from `buffer-file-name'.
+This is suppressed for temporary buffers."
+  (save-match-data
+    (unless (or (null (buffer-name))
+               (string-match "^\\( \\|\\*\\)" (buffer-name)))
+      (let ((bfn (if (stringp (buffer-file-name))
+                    (buffer-file-name)
+                  default-directory))
+           (tramp-verbose 0))
+       (when (tramp-tramp-file-p bfn)
+         (with-parsed-tramp-file-name bfn nil
+           (tramp-flush-file-property v localname)))))))
 
 (add-hook 'before-revert-hook 'tramp-flush-file-function)
 (add-hook 'eshell-pre-command-hook 'tramp-flush-file-function)
@@ -198,12 +225,13 @@ Remove also properties of all files in subdirectories."
   "Get the named PROPERTY for the connection.
 KEY identifies the connection, it is either a process or a vector.
 If the value is not set for the connection, returns DEFAULT."
-  ;; Unify key by removing localname from vector.  Work with a copy in
-  ;; order to avoid side effects.
+  ;; Unify key by removing localname and hop from vector.  Work with a
+  ;; copy in order to avoid side effects.
   (when (vectorp key)
     (setq key (copy-sequence key))
-    (aset key 3 nil))
-  (let* ((hash (gethash key tramp-cache-data))
+    (aset key 3 nil)
+    (aset key 4 nil))
+  (let* ((hash (tramp-get-hash-table key))
         (value (if (hash-table-p hash)
                    (gethash property hash default)
                  default)))
@@ -215,36 +243,40 @@ If the value is not set for the connection, returns DEFAULT."
   "Set the named PROPERTY of a connection to VALUE.
 KEY identifies the connection, it is either a process or a vector.
 PROPERTY is set persistent when KEY is a vector."
-  ;; Unify key by removing localname from vector.  Work with a copy in
-  ;; order to avoid side effects.
+  ;; Unify key by removing localname and hop from vector.  Work with a
+  ;; copy in order to avoid side effects.
   (when (vectorp key)
     (setq key (copy-sequence key))
-    (aset key 3 nil))
-  (let ((hash (or (gethash key tramp-cache-data)
-                 (puthash key (make-hash-table :test 'equal)
-                          tramp-cache-data))))
+    (aset key 3 nil)
+    (aset key 4 nil))
+  (let ((hash (tramp-get-hash-table key)))
     (puthash property value hash)
     (setq tramp-cache-data-changed t)
     (tramp-message key 7 "%s %s" property value)
     value))
 
+;;;###tramp-autoload
+(defun tramp-connection-property-p (key property)
+  "Check whether named PROPERTY of a connection is defined.
+KEY identifies the connection, it is either a process or a vector."
+  (not (eq (tramp-get-connection-property key property 'undef) 'undef)))
+
 ;;;###tramp-autoload
 (defun tramp-flush-connection-property (key)
   "Remove all properties identified by KEY.
 KEY identifies the connection, it is either a process or a vector."
-  ;; Unify key by removing localname from vector.  Work with a copy in
-  ;; order to avoid side effects.
+  ;; Unify key by removing localname and hop from vector.  Work with a
+  ;; copy in order to avoid side effects.
   (when (vectorp key)
     (setq key (copy-sequence key))
-    (aset key 3 nil))
+    (aset key 3 nil)
+    (aset key 4 nil))
   (tramp-message
    key 7 "%s %s" key
    (let ((hash (gethash key tramp-cache-data))
         properties)
-     (if (hash-table-p hash)
-        (maphash
-         (lambda (x y) (add-to-list 'properties x 'append))
-         (gethash key tramp-cache-data)))
+     (when (hash-table-p hash)
+       (maphash (lambda (x _y) (add-to-list 'properties x 'append)) hash))
      properties))
   (setq tramp-cache-data-changed t)
   (remhash key tramp-cache-data))
@@ -256,6 +288,16 @@ KEY identifies the connection, it is either a process or a vector."
     (let (result)
       (maphash
        (lambda (key value)
+        ;; Remove text properties from KEY and VALUE.
+        (when (vectorp key)
+          (dotimes (i (length key))
+            (when (stringp (aref key i))
+              (aset key i (substring-no-properties (aref key i))))))
+        (when (stringp key)
+          (setq key (substring-no-properties key)))
+        (when (stringp value)
+          (setq value (substring-no-properties value)))
+        ;; Dump.
         (let ((tmp (format
                     "(%s %s)"
                     (if (processp key)
@@ -275,7 +317,7 @@ KEY identifies the connection, it is either a process or a vector."
   "Return a list of all known connection vectors according to `tramp-cache'."
     (let (result)
       (maphash
-       (lambda (key value)
+       (lambda (key _value)
         (when (and (vectorp key) (null (aref key 3)))
           (add-to-list 'result key)))
        tramp-cache-data)
@@ -307,7 +349,7 @@ KEY identifies the connection, it is either a process or a vector."
             (remhash key cache)))
         cache)
        ;; Dump it.
-       (with-temp-buffer
+       (with-temp-file tramp-persistency-file-name
          (insert
           ";; -*- emacs-lisp -*-"
           ;; `time-stamp-string' might not exist in all (X)Emacs flavors.
@@ -321,9 +363,7 @@ KEY identifies the connection, it is either a process or a vector."
           ";; Tramp connection history.  Don't change this file.\n"
           ";; You can delete it, forcing Tramp to reapply the checks.\n\n"
           (with-output-to-string
-            (pp (read (format "(%s)" (tramp-cache-print cache))))))
-         (write-region
-          (point-min) (point-max) tramp-persistency-file-name))))))
+            (pp (read (format "(%s)" (tramp-cache-print cache)))))))))))
 
 (unless noninteractive
   (add-hook 'kill-emacs-hook 'tramp-dump-connection-properties))
@@ -339,7 +379,7 @@ This function is added always in `tramp-get-completion-function'
 for all methods.  Resulting data are derived from connection history."
   (let (res)
     (maphash
-     (lambda (key value)
+     (lambda (key _value)
        (if (and (vectorp key)
                (string-equal method (tramp-file-name-method key))
                (not (tramp-file-name-localname key)))
@@ -355,24 +395,29 @@ for all methods.  Resulting data are derived from connection history."
           ;; When "emacs -Q" has been called, both variables are nil.
           ;; We do not load the persistency file then, in order to
           ;; have a clean test environment.
-          (or (and (boundp 'init-file-user) (symbol-value 'init-file-user))
-              (and (boundp 'site-run-file) (symbol-value 'site-run-file))))
+          (or init-file-user
+              site-run-file))
   (condition-case err
       (with-temp-buffer
        (insert-file-contents tramp-persistency-file-name)
        (let ((list (read (current-buffer)))
+             (tramp-verbose 0)
              element key item)
          (while (setq element (pop list))
            (setq key (pop element))
            (while (setq item (pop element))
-             (tramp-set-connection-property key (pop item) (car item)))))
+             ;; We set only values which are not contained in
+             ;; `tramp-connection-properties'.  The cache is
+             ;; initialized properly by side effect.
+             (unless (tramp-connection-property-p key (car item))
+               (tramp-set-connection-property key (pop item) (car item))))))
        (setq tramp-cache-data-changed nil))
     (file-error
      ;; Most likely because the file doesn't exist yet.  No message.
      (clrhash tramp-cache-data))
     (error
      ;; File is corrupted.
-     (message "Tramp persistency file '%s' is corrupted: %s"
+     (message "Tramp persistency file `%s' is corrupted: %s"
              tramp-persistency-file-name (error-message-string err))
      (clrhash tramp-cache-data))))