Subject: [bug#75588] [PATCH] guix: utils: Delete temporary output files.

From: Ludovic Courtès <[email-masked]>
Date: Thu, 16 Jan 2025 09:52:01 +0100

Hello,

Kierin Bell <[email-masked]> skribis:

> This patch fixes a bug that causes: 1) the procedure passed to
> 'call-with-temporary-output-file' to always be called with the file name
> template string "/guix-file.XXXXXX" rather than the name of the created
> temporary file; and 2) the temporary file to persist rather than being
> deleted.
>
> Unless I'm missing something, Guix is creating temporary files without
> deleting them and nobody is noticing.

[…]

> --- a/guix/build/utils.scm
> +++ b/guix/build/utils.scm
> @@ -287,15 +287,16 @@ (define (call-with-temporary-output-file proc)
>  call."
>    (let* ((directory (or (getenv "TMPDIR") "/tmp"))
>           (template  (string-append directory "/guix-file.XXXXXX"))
> -         (out       (mkstemp! template)))
> +         (out       (mkstemp! template))
> +         (filename (port-filename out)))
>      (dynamic-wind
>        (lambda ()
>          #t)
>        (lambda ()
> -        (proc template out))
> +        (proc filename out))
>        (lambda ()
>          (false-if-exception (close out))
> -        (false-if-exception (delete-file template))))))
> +        (false-if-exception (delete-file filename))))))

AFAICS the current code is fine because ‘template’ is modified by
‘mkstemp!’:

--8<---------------cut here---------------start------------->8---
scheme@(guile-user)> (define template (string-copy "/tmp/guix-example-XXXXXX"))
scheme@(guile-user)> (mkstemp! template)
$15 = #<input-output: /tmp/guix-example-uZ5z4s 13>
scheme@(guile-user)> template
$16 = "/tmp/guix-example-uZ5z4s"
--8<---------------cut here---------------end--------------->8---

All good?

Thanks,
Ludo’.