Subject: Re: FILE_ATTRIBUTE_TEMPORARY ?
From: Brian Inglis <[email-masked]>
To: [email-masked]
Date: Wed, 6 Jan 2021 10:16:28 -0700
List-Id: General Cygwin discussions and problem reports <cygwin.cygwin.com>

On 2021-01-06 02:44, Mark Geisert wrote:
> Hi Noel,
> 
> Noel Grandin via Cygwin wrote:
>> Hi
>>
>> Does the cygwin
>>      tempfile()
>> API use the Win32
>>      FILE_ATTRIBUTE_TEMPORARY
>> flag on the files it creates?
>>
>> Asking because that makes a fairly significant performance difference when 
>> creating very short lived temp files.
> 
> Good question!  If one specifies O_TMPFILE as one of the flags on an open() 
> call, Cygwin does set that Win32 attribute as desired.  Unfortunately, Cygwin's 
> tmpfile() is supplied by newlib and it does not specify O_TMPFILE on its 
> underlying open() call.
> 
> That looks like a bug to me, unless I'm missing something subtle there; I'll 
> submit a patch and find out.

O_TMPFILE is only implemented under winsup/cygwin, and only defined for newlib 
under Cygwin:

https://sourceware.org/git/?p=newlib-cygwin.git;a=blob;f=newlib/libc/include/sys/_default_fcntl.h

see:

https://sourceware.org/git/?p=newlib-cygwin.git;a=commitdiff;h=0aa99373c1d01b19a7f8ba53e8c7749358480f3e

	https://cygwin.com/pipermail/cygwin/2018-February/236018.html

and:

	https://cygwin.com/pipermail/cygwin/2018-February/235869.html

https://sourceware.org/git/?p=newlib-cygwin.git;a=commitdiff;h=7ae73be14194ccadc9a7557be33c3940ca4667cb

You should allow linkat to reverse the applied attributes to make the file 
permanent, which may be taken care of in the patch above to 
fhandler_disk_file::link;
from Linux open(2) see

	https://man7.org/linux/man-pages/man2/open.2.html:

"O_TMPFILE must be specified with one of O_RDWR or O_WRONLY and,
optionally, O_EXCL. If O_EXCL is not specified, then linkat(2) can be
used to link the temporary file into the filesystem, making it
permanent, using code like the following:

	char path[PATH_MAX];
	fd = open("/path/to/dir", O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR);

	/* File I/O on 'fd'... */

	snprintf(path, PATH_MAX, "/proc/self/fd/%d", fd);
	linkat(AT_FDCWD, path, AT_FDCWD, "/path/for/file",
					AT_SYMLINK_FOLLOW);
"

although the implementation may not currently handle O_EXCL.

-- 
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada

This email may be disturbing to some readers as it contains
too much technical detail. Reader discretion is advised.
[Data in binary units and prefixes, physical quantities in SI.]
--
Problem reports:      https://cygwin.com/problems.html
FAQ:                  https://cygwin.com/faq/
Documentation:        https://cygwin.com/docs.html
Unsubscribe info:     https://cygwin.com/ml/#unsubscribe-simple