Subject: Re: bug#37884: 27.0.50; Cannot write to a file in VirtualBox shared directory

From: Bernardo <[email-masked]>
Date: Tue, 29 Oct 2019 19:50:15 +1100

Robert Pluim <[email-masked]> writes:

>>>>>> On Mon, 28 Oct 2019 20:23:22 +1100, Bernardo <[email-masked]> said:
>     Bernardo> $ git diff src/filelock.c
>     Bernardo> diff --git a/src/filelock.c b/src/filelock.c
>     Bernardo> index ff25d6475d..79eb8fa91e 100644
>     Bernardo> --- a/src/filelock.c
>     Bernardo> +++ b/src/filelock.c
>     Bernardo> @@ -403,7 +403,7 @@ create_lock_file (char *lfname, char *lock_info_str, bool force)
>     Bernardo>           lock_info_len = strlen (lock_info_str);
>     Bernardo>           err = 0;
>     Bernardo>           if (emacs_write (fd, lock_info_str, lock_info_len) != lock_info_len
>     Bernardo> -             || fchmod (fd, S_IRUSR | S_IRGRP | S_IROTH) != 0)
>     Bernardo> +             || fchmod (fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) != 0)
>     Bernardo>             err = errno;
>     Bernardo>           /* There is no need to call fsync here, as the contents of
>     Bernardo>              the lock file need not survive system crashes.  */
>
>     Bernardo> and Emacs is happy again, no problems with writing to files in
>     Bernardo> VirtualBox shared directory.
>
>     Bernardo> Please let me know if you want me to test anything else.
>
> I think Eli's suggestion was more like the below, which only calls
> fchmod if the unlink fails.
>
> diff --git a/src/filelock.c b/src/filelock.c
> index ff25d6475d..7fb14774b0 100644
> --- a/src/filelock.c
> +++ b/src/filelock.c
> @@ -732,6 +732,15 @@ unlock_file (Lisp_Object fn)
>    int err = current_lock_owner (0, lfname);
>    if (err == -2 && unlink (lfname) != 0 && errno != ENOENT)
>      err = errno;
> +  /* On certain filesystems the file must be writable for unlink to
> +     succeed (Bug#37784).  */
> +  if (errno == EPERM)
> +    {
> +      fchmod (fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
> +      errno = 0;
> +      if (unlink (lfname) != 0 && errno != ENOENT)
> +        err = errno;
> +    }
>    if (0 < err)
>      report_file_errno ("Unlocking file", filename, err);
>  

two changes in that code snippet were needed to get things working:
 * replace fchmod() with chmod() (a minor one)
 * more importantly, had to set the value of err to 0, otherwise
   it would end up with the same problem;
 
guess you'll have a closer look at the 2nd change and possibly come up
with a better solution

The code that works fine looks like this:

$ git diff
diff --git a/src/filelock.c b/src/filelock.c
index ff25d6475d..046a1b014f 100644
--- a/src/filelock.c
+++ b/src/filelock.c
@@ -732,6 +732,16 @@ unlock_file (Lisp_Object fn)
   int err = current_lock_owner (0, lfname);
   if (err == -2 && unlink (lfname) != 0 && errno != ENOENT)
     err = errno;
+  /* On certain filesystems the file must be writable for unlink to
+     succeed (Bug#37784).  */
+  if (errno == EPERM)
+    {
+      chmod (lfname, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+      err = 0;
+      errno = 0;
+      if (unlink (lfname) != 0 && errno != ENOENT)
+       err = errno;
+    }
   if (0 < err)
     report_file_errno ("Unlocking file", filename, err);