master d5eab4b2d4c: Improve Tramp backup handling

Michael Albinus via Mailing list for Emacs changes <[email protected]>
Newsgroups gmane.emacs.diffs
Message-ID <[email protected]>
branch: master
commit d5eab4b2d4cb187eb39efbba21780ba754d01816
Author: Michael Albinus <[email protected]>
Commit: Michael Albinus <[email protected]>

    Improve Tramp backup handling
    
    * doc/misc/tramp.texi (Auto-save File Lock and Backup): Adapt example.
    
    * lisp/net/tramp.el (tramp-handle-find-backup-file-name):
    Use connection-local value of `make-backup-files'.
    
    * test/lisp/net/tramp-tests.el (tramp-test38-find-backup-file-name):
    Extend test.
---
 doc/misc/tramp.texi          | 32 +++++++++++++-------
 lisp/net/tramp.el            | 70 +++++++++++++++++++++++---------------------
 test/lisp/net/tramp-tests.el | 20 ++++++++++++-
 3 files changed, 76 insertions(+), 46 deletions(-)

diff --git a/doc/misc/tramp.texi b/doc/misc/tramp.texi
index 7bee92a94d5..cc51f048d28 100644
--- a/doc/misc/tramp.texi
+++ b/doc/misc/tramp.texi
@@ -3380,13 +3380,23 @@ of the secretfile is now owned by the user logged in from
 When @code{backup-directory-alist} is @code{nil} (the default), such
 problems do not occur.
 
-To ``turn off'' the backup feature for remote files and stop
-@value{tramp} from saving to the backup directory, use this:
+@vindex make-backup-files
+In case you do not want to use backup files for remote files, use
+connection-local variable @code{make-backup-files}
+@ifinfo
+(@pxref{Connection Variables, , , emacs})
+@end ifinfo
+like this:
 
 @lisp
 @group
-(add-to-list 'backup-directory-alist
-             (cons tramp-file-name-regexp nil))
+(connection-local-set-profile-variables
+ 'no-remote-backup-files '((make-backup-files . nil)))
+@end group
+
+@group
+(connection-local-set-profiles
+ '(:application tramp) 'no-remote-backup-files)
 @end group
 @end lisp
 
@@ -3396,13 +3406,13 @@ Disabling backups can be targeted to just the @option{su} and
 
 @lisp
 @group
-(setq backup-enable-predicate
-      (lambda (name)
-        (and (normal-backup-enable-predicate name)
-             (not
-              (let ((method (file-remote-p name 'method)))
-                (when (stringp method)
-                  (member method '("su" "sudo"))))))))
+(connection-local-set-profiles
+ '(:application tramp :protocol "su") 'no-remote-backup-files)
+@end group
+
+@group
+(connection-local-set-profiles
+ '(:application tramp :protocol "sudo") 'no-remote-backup-files)
 @end group
 @end lisp
 
diff --git a/lisp/net/tramp.el b/lisp/net/tramp.el
index 5219435cbd2..477366c6848 100644
--- a/lisp/net/tramp.el
+++ b/lisp/net/tramp.el
@@ -4792,40 +4792,42 @@ existing) are returned."
 
 (defun tramp-handle-find-backup-file-name (filename)
   "Like `find-backup-file-name' for Tramp files."
-  (with-parsed-tramp-file-name filename nil
-    (let ((backup-directory-alist
-	   (if tramp-backup-directory-alist
-	       (mapcar
-		(lambda (x)
-		  (cons
-		   (car x)
-		   (if (and (stringp (cdr x))
-			    (file-name-absolute-p (cdr x))
-			    (not (tramp-tramp-file-p (cdr x))))
-		       (tramp-make-tramp-file-name v (cdr x))
-		     (cdr x))))
-		tramp-backup-directory-alist)
-	     backup-directory-alist))
-	  result)
-      (prog1 ;; Run plain `find-backup-file-name'.
-	  (setq result
-		(tramp-run-real-handler
-		 #'find-backup-file-name (list filename)))
-        ;; Protect against security hole.
-	(when (and (not tramp-allow-unsafe-temporary-files)
-		   (not backup-inhibited)
-		   (file-in-directory-p (car result) temporary-file-directory)
-		   (= (or (file-attribute-user-id
-			   (file-attributes filename 'integer))
-			  tramp-unknown-id-integer)
-		      tramp-root-id-integer)
-		   (not (with-tramp-connection-property
-			    (tramp-get-process v) "unsafe-temporary-file"
-			  (yes-or-no-p
-			   (concat
-			    "Backup file on local temporary directory, "
-			    "do you want to continue?")))))
-	  (tramp-error v 'file-error "Unsafe backup file name"))))))
+  (when-let* ((default-directory (file-name-directory filename))
+	      ((tramp-compat-connection-local-value make-backup-files)))
+    (with-parsed-tramp-file-name filename nil
+      (let ((backup-directory-alist
+	     (if tramp-backup-directory-alist
+		 (mapcar
+		  (lambda (x)
+		    (cons
+		     (car x)
+		     (if (and (stringp (cdr x))
+			      (file-name-absolute-p (cdr x))
+			      (not (tramp-tramp-file-p (cdr x))))
+			 (tramp-make-tramp-file-name v (cdr x))
+		       (cdr x))))
+		  tramp-backup-directory-alist)
+	       backup-directory-alist))
+	    result)
+	(prog1 ;; Run plain `find-backup-file-name'.
+	    (setq result
+		  (tramp-run-real-handler
+		   #'find-backup-file-name (list filename)))
+          ;; Protect against security hole.
+	  (when (and (not tramp-allow-unsafe-temporary-files)
+		     (not backup-inhibited)
+		     (file-in-directory-p (car result) temporary-file-directory)
+		     (= (or (file-attribute-user-id
+			     (file-attributes filename 'integer))
+			    tramp-unknown-id-integer)
+			tramp-root-id-integer)
+		     (not (with-tramp-connection-property
+			      (tramp-get-process v) "unsafe-temporary-file"
+			    (yes-or-no-p
+			     (concat
+			      "Backup file on local temporary directory, "
+			      "do you want to continue?")))))
+	    (tramp-error v 'file-error "Unsafe backup file name")))))))
 
 (defun tramp-handle-insert-directory
     (filename switches &optional wildcard full-directory-p)
diff --git a/test/lisp/net/tramp-tests.el b/test/lisp/net/tramp-tests.el
index 6badf064efe..f4f061d2efb 100644
--- a/test/lisp/net/tramp-tests.el
+++ b/test/lisp/net/tramp-tests.el
@@ -7288,7 +7288,25 @@ INPUT, if non-nil, is a string sent to the process."
 
 	;; Cleanup.
 	(ignore-errors (delete-file tmp-name1))
-	(tramp-cleanup-connection tramp-test-vec 'keep-debug 'keep-password)))))
+	(tramp-cleanup-connection tramp-test-vec 'keep-debug 'keep-password))
+
+      ;; Check connection-local `make-backup-files'.
+      (let ((clpa connection-local-profile-alist)
+	    (clca connection-local-criteria-alist))
+	(connection-local-set-profile-variables
+	 'no-remote-backup-files '((make-backup-files . nil)))
+	(connection-local-set-profiles
+	 `(:application tramp
+	   :protocol ,(file-remote-p default-directory 'method)
+	   :user ,(file-remote-p default-directory 'user)
+	   :machine ,(file-remote-p default-directory 'host))
+	 'no-remote-backup-files)
+
+	(should-not (find-backup-file-name tmp-name1))
+
+	(custom-set-variables
+	 `(connection-local-profile-alist ',clpa now)
+	 `(connection-local-criteria-alist ',clca now))))))
 
 (ert-deftest tramp-test39-make-lock-file-name ()
   "Check `make-lock-file-name', `lock-file', `unlock-file' and `file-locked-p'."
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.