Subject: Re: bug#37884: 27.0.50; Cannot write to a file in VirtualBox shared directory
From: Paul Eggert <[email-masked]>
To: Eli Zaretskii <[email-masked]>, Robert Pluim <[email-masked]>
Cc: [email-masked], [email-masked], [email-masked]
Date: Wed, 30 Oct 2019 18:41:10 -0700
List-Id: <debbugs-submit.debbugs.gnu.org>

On 10/30/19 9:25 AM, Eli Zaretskii wrote:

> Paul, do you
> see any downsides to the last change posted in this discussion?  IOW,
> could there be some situations where it could do any harm?

That change involves two extra system calls (one of which doesn't check 
the return value?!). In contrast, Bernardo's suggestion is simpler, 
involves no extra system calls (or machine instructions, for that 
matter), and has been tested. So I installed the attached patch to 
implement and document the workaround, and am optimistically closing the 
bug report.

The underlying problem is a significant bug in Virtual Box atop Windows 
10 Enterprise, a bug that surely breaks other applications (and could 
well break other parts of Emacs) that assume one can unlink a readonly 
file. Apparently the bug has been known for some time and a fix is 
available but the VirtualBox folks haven't gotten around to installing 
the fix:

https://www.virtualbox.org/ticket/4890
https://www.virtualbox.org/ticket/16463

If you're a VirtualBox user, I suggest that you ping the developers 
about fixing this, as one can't reasonably expect every application 
developer to work around their bug.
From 3bbe6e7c42fb22c80ce1e239768679193dfe5bdf Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Wed, 30 Oct 2019 18:15:14 -0700
Subject: [PATCH] Fix bug with lock file on nonstandard filesystem
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* src/filelock.c (create_lock_file):
Don’t disable user write access to the lock file (Bug#37884).
---
 src/filelock.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/src/filelock.c b/src/filelock.c
index ff25d6475d..2077287345 100644
--- a/src/filelock.c
+++ b/src/filelock.c
@@ -402,9 +402,15 @@ create_lock_file (char *lfname, char *lock_info_str, bool force)
 	  ptrdiff_t lock_info_len;
 	  lock_info_len = strlen (lock_info_str);
 	  err = 0;
+
+	  /* Make the lock file readable to others, so that others' sessions
+	     can read it.  Even though nobody should write to the lock file,
+	     keep it user-writable to work around problems on nonstandard file
+	     systems that prohibit unlinking readonly files (Bug#37884).  */
 	  if (emacs_write (fd, lock_info_str, lock_info_len) != lock_info_len
-	      || fchmod (fd, S_IRUSR | S_IRGRP | S_IROTH) != 0)
+	      || fchmod (fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) != 0)
 	    err = errno;
+
 	  /* There is no need to call fsync here, as the contents of
 	     the lock file need not survive system crashes.  */
 	  if (emacs_close (fd) != 0)
-- 
2.23.0