Subject: Re: [PATCH] wrapper: simplify xmkstemp()

From: Junio C Hamano <[email-masked]>
Date: Mon, 17 Nov 2025 13:52:53 -0800

René Scharfe <[email-masked]> writes:

> Call xmkstemp_mode() instead of duplicating its error handling code.
> This switches the implementation from the system's mkstemp(3) to our own
> git_mkstemp_mode(), which works just as well.
>
> Signed-off-by: René Scharfe <[email-masked]>
> ---
>  wrapper.c | 19 +------------------
>  1 file changed, 1 insertion(+), 18 deletions(-)
>
> diff --git a/wrapper.c b/wrapper.c
> index 3d507d4204..d5976b3e7e 100644
> --- a/wrapper.c
> +++ b/wrapper.c
> @@ -421,24 +421,7 @@ FILE *fopen_or_warn(const char *path, const char *mode)
>  
>  int xmkstemp(char *filename_template)
>  {
> -	int fd;
> -	char origtemplate[PATH_MAX];
> -	strlcpy(origtemplate, filename_template, sizeof(origtemplate));
> -
> -	fd = mkstemp(filename_template);
> -	if (fd < 0) {
> -		int saved_errno = errno;
> -		const char *nonrelative_template;
> -
> -		if (strlen(filename_template) != strlen(origtemplate))
> -			filename_template = origtemplate;
> -
> -		nonrelative_template = absolute_path(filename_template);
> -		errno = saved_errno;
> -		die_errno("Unable to create temporary file '%s'",
> -			nonrelative_template);
> -	}
> -	return fd;
> +	return xmkstemp_mode(filename_template, 0600);
>  }

A patch that loses lines is nice.  My curiosity wonders what the
strlen() comparison in the original was about, but let's not waste
our brain cycles to code that we no longer use ;-).  xmkstemp_mode()
checks if our git_mkstemp_mode() cleared the template[0] as a sign
to restore the origtemplate, and uses the template that was munged
by git_mkstemp_mode() and used to attempt opening it, which seems
very sensible.

Will queue.  Thanks.