Subject: bug#22795: 25.0.91; Can't write read only file on w32

From: "Ota, Takaaki" <[email-masked]>
Date: Fri, 26 Feb 2016 11:26:05 -0800

I think this is something to do with my mingw.  I cannot remember when
I updated mingw last time.  Here is the comparison between trace in
emacs-24.5 and emacs-25.0.91.  The difference is in the open system
call.  Both pass the same set of parameters to open but emacs-24.5
gets 3 and emacs-25.0.91 gets -1.  Both emacs were built using mingw
but I cannot guarantee they are the same version of mingw.  Can you
think of any other reason than they were built with different mingw to
explain the difference of the open() behavior?

-Tak

================================ emacs-25.0.91
[New Thread 10664.0x2714]
4707      bool file_locked = 0;
(gdb) 
4801      if (open_and_close_file && !auto_saving)
(gdb) 
4803          lock_file (lockname);
(gdb) 
4804          file_locked = 1;
(gdb) 
4807      encoded_filename = ENCODE_FILE (filename);
(gdb) 
4808      fn = SSDATA (encoded_filename);
(gdb) 
4810      open_flags |= EQ (mustbenew, Qexcl) ? O_EXCL : !NILP (append) ? 0 : O_TRUNC;
(gdb) 
4811      if (NUMBERP (append))
(gdb) 
4810      open_flags |= EQ (mustbenew, Qexcl) ? O_EXCL : !NILP (append) ? 0 : O_TRUNC;
(gdb) 
4821      if (open_and_close_file)
(gdb) 
4823          desc = emacs_open (fn, open_flags, mode);
(gdb) s
emacs_open (file=file@entry=0x829692c "c:/d/ota/memo", 
    oflags=oflags@entry=33537, mode=mode@entry=384) at sysdep.c:2260
2260    {
(gdb) n
2263        oflags |= O_BINARY;
(gdb) 
2264      oflags |= O_CLOEXEC;
(gdb) 
2265      while ((fd = open (file, oflags, mode)) < 0 && errno == EINTR)
(gdb) p file
$10 = 0x829692c "c:/d/ota/memo"
(gdb) p oflags
$11 = 33665
(gdb) p mode
$12 = 384
(gdb) n
2270    }
(gdb) p fd
$13 = -1

================================ emacs-24.5
[New Thread 3264.0xa38]
4708      bool file_locked = 0;
(gdb) 
4809      if (open_and_close_file && !auto_saving)
(gdb) 
4811          lock_file (lockname);
(gdb) 
4812          file_locked = 1;
(gdb) 
4816      encoded_filename = ENCODE_FILE (filename);
(gdb) 
4817      fn = SSDATA (encoded_filename);
(gdb) 
4819      open_flags |= EQ (mustbenew, Qexcl) ? O_EXCL : !NILP (append) ? 0 : O_TRUNC;
(gdb) 
4820      if (NUMBERP (append))
(gdb) 
4823        open_flags |= O_APPEND;
(gdb) 
4830      if (open_and_close_file)
(gdb) 
4832          desc = emacs_open (fn, open_flags, mode);
(gdb) s
emacs_open (file=file@entry=0x5d8aec8 "c:/d/ota/memo", 
    oflags=oflags@entry=33537, mode=mode@entry=384) at sysdep.c:2143
2143    {
(gdb) n
2145      oflags |= O_CLOEXEC;
(gdb) 
2146      while ((fd = open (file, oflags, mode)) < 0 && errno == EINTR)
(gdb) p file
$1 = 0x5d8aec8 "c:/d/ota/memo"
(gdb) p oflags
$2 = 33665
(gdb) p mode
$3 = 384
(gdb) n
[New Thread 3264.0x4f0]
[New Thread 3264.0x12a0]
2151    }
(gdb) p fd
$4 = 3

Thu, 25 Feb 2016 20:09:34 +0200: Eli Zaretskii <[email-masked]> wrote:

> > Date: Thu, 25 Feb 2016 09:08:38 -0800
> > CC: <[email-masked]>
> > From: "Ota, Takaaki" <[email-masked]>
> > 
> > Yes, I own and have full permission of the directory.  I noticed one
> > thing when I tried other than C: drive.  An external USB drive with
> > NTFS file system exhibits the same problem as C: drive.  A flash drive
> > with FAT32 doesn't show the same problem.  Have you tried with NTFS?
> 
> I tried only with NTFS.
> 
> > I tried "emacs -Q" (actually "./runemacs -Q") and the result is the
> > same.  On FAT32 the writing succeeds.  On NTFS the writing fails by
> > Permission denied error.
> 
> Very strange.  I guess the only way forward is for you to step with
> Edebug through save-buffer and its subroutines in files.el, and tell
> what fails there and why.  Can you do that?  (Since this works on
> FAT32, I suspect some issue with NT security, although 24.5 also used
> ACLs...)
> 
> Thanks.
>