Subject: bug#17706: 24.4.50; locked file error in rmail-insert-inbox-text

From: Ken Olum <[email-masked]>
Date: Thu, 26 Jun 2014 15:52:22 -0400

Here is a patch to mark the buffer modified (which locks the file) in
rmail-get-new-mail-1 before calling rmail-insert-inbox-text, and mark it
unmodified if rmail-insert-inbox-text returns (or is aborted) without
modifying the buffer.

                                        Ken

=== modified file 'lisp/mail/rmail.el'
--- lisp/mail/rmail.el	2014-05-21 16:58:19 +0000
+++ lisp/mail/rmail.el	2014-06-26 19:18:00 +0000
@@ -1816,9 +1816,20 @@
 	;; Read in the contents of the inbox files, renaming them as
 	;; necessary, and adding to the list of files to delete
 	;; eventually.
-	(if file-name
-	    (rmail-insert-inbox-text files nil)
-	  (setq delete-files (rmail-insert-inbox-text files t)))
+	(unwind-protect
+	    (progn
+	      ;;Set modified now to lock the file, so that we don't encounter
+	      ;;locking problems later in the middle of reading the mail.
+	      (set-buffer-modified-p t)
+	      (if file-name
+		  (rmail-insert-inbox-text files nil)
+		(setq delete-files (rmail-insert-inbox-text files t))))
+	  ;;If there was no new mail, or we aborted before actually trying
+	  ;;to get any, mark buffer unmodified.  Otherwise the buffer is
+	  ;;correctly marked modified and the file locked until we save
+	  ;; out the new mail.
+	  (if (= (point-min) (point-max)))
+	      (set-buffer-modified-p nil))
 	;; Scan the new text and convert each message to
 	;; Rmail/mbox format.
 	(goto-char (point-min))
@@ -1968,11 +1979,6 @@
     size))
 
 (defun rmail-insert-inbox-text (files renamep)
-  ;; Detect a locked file now, so that we avoid moving mail
-  ;; out of the real inbox file.  (That could scare people.)
-  (or (memq (file-locked-p buffer-file-name) '(nil t))
-      (error "RMAIL file %s is locked"
-	     (file-name-nondirectory buffer-file-name)))
   (let (file tofile delete-files movemail popmail got-password password)
     (while files
       ;; Handle remote mailbox names specially; don't expand as filenames

=== modified file 'lisp/ChangeLog'
--- lisp/ChangeLog	2014-06-26 19:46:02 +0000
+++ lisp/ChangeLog	2014-06-26 19:46:30 +0000
@@ -1,3 +1,10 @@
+2014-06-26  Ken Olum  <[email-masked]>
+
+	* mail/rmail.el (rmail-get-new-mail-1): Lock file before
+	calling rmail-insert-inbox-text.  Unlock if
+	rmail-insert-inbox-text doesn't modify buffer.
+	(rmail-insert-inbox-text): Remove lock-checking code
+
 2014-06-26  Leo Liu  <[email-masked]>
 
 	* skeleton.el (skeleton-end-hook): Default to nil and move the