Subject: Re: bug#13079: 24.3.50; Emacs cannot create subprocess

From: Li Zhai <[email-masked]>
Date: Thu, 6 Dec 2012 16:18:10 +0800

I've tried another method, using the flag FILE_FLAG_DELETE_ON_CLOSE
while open the files.

Create a file by passed the flag FILE_FLAG_DELETE_ON_CLOSE to
CreateFile, if all the handles are closed, the file will be deleted
automatically. I wrote a very simple program to test that feature. It
seems works.

#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include <stdio.h>
#include <windows.h>

void main( void )
{
   HANDLE fh1, fh2;
   char buf[100];
   DWORD dwReadSize;
   DWORD dwWriteSize;

   fh1 = CreateFile("temp.txt", GENERIC_READ | GENERIC_WRITE,
                    FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
                    NULL, CREATE_ALWAYS,
                    FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, NULL);

   WriteFile(fh1, "hello", 5, &dwWriteSize, NULL);

   fh2 = CreateFile("temp.txt", GENERIC_READ | GENERIC_WRITE,
                    FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
                    NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

   if (fh2 == INVALID_HANDLE_VALUE)
       perror( "Open failed on fh2" );

   printf("Close the first file handle...\n");
   CloseHandle(fh1);

   printf("Read file...\n");
   ReadFile(fh2, buf, 5, &dwReadSize, NULL);
   buf[5] = 0;
   printf("File content:\n %s\n", buf);

   _getch();

   printf("Close the second file handle...The file should be deleted\n");
   CloseHandle(fh2);
}

But this method has some limit. Firstly, all the files must
create/open by Win32 API, the codes are totally unportable. Secondly,
the file must be opened with a flag FILE_SHARE_DELETE, or the file
will not be deleted.

And I don't know how to integrate the Win32 API with the emacs codes.
Emacs codes use POSIX functions(_open, _close, _read...), and the
delete_on_close feature required WIN32 API. I've tried using
_O_TEMPORARY flags with _open function, but not succeeded.

On Thu, Dec 6, 2012 at 11:54 AM, Eli Zaretskii <[email-masked]> wrote:
>> Date: Thu, 6 Dec 2012 09:58:19 +0800
>> From: Li Zhai <[email-masked]>
>> Cc: [email-masked]
>>
>> I got it. The maximize number of calling mktemp is 42 if we don't
>> remove the temporary em* files.
>
> Yes.
>
>> I found a very interesting discuss about the limit of mktemp on windows.
>>
>> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47439
>>
>> They implement a new mktemp function to walk around the limit on the
>> number of concurrently open files.
>>
>> Can we implement our mktemp functions instead of using microsoft's stuff?
>
> But that will just push the limit farther.  It will not fix the
> underlying fundamental problem of not deleting temporary files in this
> case.  Since Emacs is typically run for long periods of time, sooner
> or later the problem will strike anyway.