changeset 100922:2b84620022aa

* net/tramp.el (tramp-handle-load) (tramp-do-copy-or-rename-file-directly) (tramp-handle-file-local-copy, tramp-handle-insert-file-contents) (tramp-handle-write-region): Delete temporary file in case of error.
author Michael Albinus <michael.albinus@gmx.de>
date Mon, 05 Jan 2009 19:37:01 +0000
parents ae6c8a6fe386
children 7bf074994867
files lisp/net/tramp.el
diffstat 1 files changed, 73 insertions(+), 57 deletions(-) [+]
line wrap: on
line diff
--- a/lisp/net/tramp.el	Mon Jan 05 17:54:03 2009 +0000
+++ b/lisp/net/tramp.el	Mon Jan 05 19:37:01 2009 +0000
@@ -2263,8 +2263,9 @@
       (unless nomessage (tramp-message v 0 "Loading %s..." file))
       (let ((local-copy (file-local-copy file)))
 	;; MUST-SUFFIX doesn't exist on XEmacs, so let it default to nil.
-	(load local-copy noerror t t)
-	(delete-file local-copy))
+	(unwind-protect
+	    (load local-copy noerror t t)
+	  (delete-file local-copy)))
       (unless nomessage (tramp-message v 0 "Loading %s...done" file))
       t)))
 
@@ -3258,45 +3259,51 @@
 
 	   ;; We need a temporary file in between.
 	   (t
-	    ;; Create the temporary file.
-	    (let ((tmpfile (tramp-compat-make-temp-file localname1)))
-	      (cond
-	       (t1
-		(tramp-send-command
-		 v (format
-		    "%s %s %s" cmd
-		    (tramp-shell-quote-argument localname1)
-		    (tramp-shell-quote-argument tmpfile)))
-		;; We must change the ownership as remote user.
-		(tramp-set-file-uid-gid
-		 (concat prefix tmpfile)
-		 (tramp-get-local-uid 'integer)
-		 (tramp-get-local-gid 'integer)))
-	       (t2
-		(if (eq op 'copy)
-		    (tramp-compat-copy-file
-		     localname1 tmpfile ok-if-already-exists
-		     keep-date preserve-uid-gid)
-		  (tramp-run-real-handler
-		   'rename-file (list localname1 tmpfile ok-if-already-exists)))
-		;; We must change the ownership as local user.
-		(tramp-set-file-uid-gid
-		 tmpfile
-		 (tramp-get-remote-uid v 'integer)
-		 (tramp-get-remote-gid v 'integer))))
-
-	      ;; Move the temporary file to its destination.
-	      (cond
-	       (t2
-		(tramp-send-command
-		 v (format
-		    "mv -f %s %s"
-		    (tramp-shell-quote-argument tmpfile)
-		    (tramp-shell-quote-argument localname2))))
-	       (t1
-		(tramp-run-real-handler
-		 'rename-file
-		 (list tmpfile localname2 ok-if-already-exists))))))))))
+	    (condition-case err
+		;; Create the temporary file.
+		(let ((tmpfile (tramp-compat-make-temp-file localname1)))
+		  (cond
+		   (t1
+		    (tramp-send-command
+		     v (format
+			"%s %s %s" cmd
+			(tramp-shell-quote-argument localname1)
+			(tramp-shell-quote-argument tmpfile)))
+		    ;; We must change the ownership as remote user.
+		    (tramp-set-file-uid-gid
+		     (concat prefix tmpfile)
+		     (tramp-get-local-uid 'integer)
+		     (tramp-get-local-gid 'integer)))
+		   (t2
+		    (if (eq op 'copy)
+			(tramp-compat-copy-file
+			 localname1 tmpfile ok-if-already-exists
+			 keep-date preserve-uid-gid)
+		      (tramp-run-real-handler
+		       'rename-file
+		       (list localname1 tmpfile ok-if-already-exists)))
+		    ;; We must change the ownership as local user.
+		    (tramp-set-file-uid-gid
+		     tmpfile
+		     (tramp-get-remote-uid v 'integer)
+		     (tramp-get-remote-gid v 'integer))))
+
+		  ;; Move the temporary file to its destination.
+		  (cond
+		   (t2
+		    (tramp-send-command
+		     v (format
+			"mv -f %s %s"
+			(tramp-shell-quote-argument tmpfile)
+			(tramp-shell-quote-argument localname2))))
+		   (t1
+		    (tramp-run-real-handler
+		     'rename-file
+		     (list tmpfile localname2 ok-if-already-exists)))))
+
+	      ;; Error handling.
+	      (error (delete-file tmpfile)
+		     (signal (car err) (cdr err)))))))))
 
       ;; Set the time and mode. Mask possible errors.
       ;; Won't be applied for 'rename.
@@ -4051,8 +4058,9 @@
 	      (tramp-message
 	       v 5 "Decoding remote file %s with command %s..."
 	       filename loc-dec)
-	      (tramp-call-local-coding-command loc-dec tmpfile2 tmpfile)
-	      (delete-file tmpfile2)))
+	      (unwind-protect
+		  (tramp-call-local-coding-command loc-dec tmpfile2 tmpfile)
+		(delete-file tmpfile2))))
 	  (tramp-message v 5 "Decoding remote file %s...done" filename)
 	  ;; Set proper permissions.
 	  (set-file-modes tmpfile (file-modes filename))
@@ -4134,18 +4142,19 @@
 	    (tramp-message v 4 "Inserting local temp file `%s'..." local-copy)
 	    ;; We must ensure that `file-coding-system-alist' matches
 	    ;; `local-copy'.
-	    (let ((file-coding-system-alist
-		   (tramp-find-file-name-coding-system-alist
-		    filename local-copy)))
-	      (setq result
-		    (insert-file-contents local-copy nil beg end replace))
-	      ;; Now `last-coding-system-used' has right value.  Remember it.
-	      (when (boundp 'last-coding-system-used)
-		(setq coding-system-used
-		      (symbol-value 'last-coding-system-used))))
+	    (unwind-protect
+		(let ((file-coding-system-alist
+		       (tramp-find-file-name-coding-system-alist
+			filename local-copy)))
+		  (setq result
+			(insert-file-contents local-copy nil beg end replace))
+		  ;; Now `last-coding-system-used' has right value.  Remember it.
+		  (when (boundp 'last-coding-system-used)
+		    (setq coding-system-used
+			  (symbol-value 'last-coding-system-used))))
+	      (delete-file local-copy))
 	    (tramp-message
 	     v 4 "Inserting local temp file `%s'...done" local-copy)
-	    (delete-file local-copy)
 	    (when (boundp 'last-coding-system-used)
 	      (set 'last-coding-system-used coding-system-used))))
 
@@ -4333,9 +4342,13 @@
 	  ;; matches `tmpfile'.
 	  (let ((file-coding-system-alist
 		 (tramp-find-file-name-coding-system-alist filename tmpfile)))
-	    (tramp-run-real-handler
-	     'write-region
-	     (list start end tmpfile append 'no-message lockname confirm))
+	    (condition-case err
+		(tramp-run-real-handler
+		 'write-region
+		 (list start end tmpfile append 'no-message lockname confirm))
+	      (error (delete-file tmpfile)
+		     (signal (car err) (cdr err))))
+
 	    ;; Now, `last-coding-system-used' has the right value.  Remember it.
 	    (when (boundp 'last-coding-system-used)
 	      (setq coding-system-used
@@ -4360,7 +4373,10 @@
 		(and (tramp-method-out-of-band-p v)
 		     (> (- (or end (point-max)) (or start (point-min)))
 			tramp-copy-size-limit)))
-	    (rename-file tmpfile filename t))
+	    (condition-case err
+		(rename-file tmpfile filename t)
+	      (error (delete-file tmpfile)
+		     (signal (car err) (cdr err)))))
 
 	   ;; Use inline file transfer.
 	   (rem-dec