summaryrefslogtreecommitdiff
path: root/guix/build/utils.scm
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2020-09-19 14:11:47 +0200
committerLudovic Courtès <ludo@gnu.org>2020-09-19 17:28:47 +0200
commitd845e3af7c410d70d240590d80ed31b8f169c266 (patch)
tree0222aae5386f553f237fd06d1d01ae246c42cee5 /guix/build/utils.scm
parenta08cb3ca951cf9ebdea5258e6c34e2664877c363 (diff)
downloadguix-patches-d845e3af7c410d70d240590d80ed31b8f169c266.tar
guix-patches-d845e3af7c410d70d240590d80ed31b8f169c266.tar.gz
utils: Add 'call-with-temporary-output-file'.
* guix/utils.scm: Re-export 'call-with-temporary-output-file'. (call-with-temporary-output-file): Move to... * guix/build/utils.scm (call-with-temporary-output-file): ... here.
Diffstat (limited to 'guix/build/utils.scm')
-rw-r--r--guix/build/utils.scm17
1 files changed, 17 insertions, 0 deletions
diff --git a/guix/build/utils.scm b/guix/build/utils.scm
index e872cfffd3..afcb71fae3 100644
--- a/guix/build/utils.scm
+++ b/guix/build/utils.scm
@@ -53,6 +53,7 @@
directory-exists?
executable-file?
symbolic-link?
+ call-with-temporary-output-file
call-with-ascii-input-file
elf-file?
ar-file?
@@ -198,6 +199,22 @@ introduce the version part."
"Return #t if FILE is a symbolic link (aka. \"symlink\".)"
(eq? (stat:type (lstat file)) 'symlink))
+(define (call-with-temporary-output-file proc)
+ "Call PROC with a name of a temporary file and open output port to that
+file; close the file and delete it when leaving the dynamic extent of this
+call."
+ (let* ((directory (or (getenv "TMPDIR") "/tmp"))
+ (template (string-append directory "/guix-file.XXXXXX"))
+ (out (mkstemp! template)))
+ (dynamic-wind
+ (lambda ()
+ #t)
+ (lambda ()
+ (proc template out))
+ (lambda ()
+ (false-if-exception (close out))
+ (false-if-exception (delete-file template))))))
+
(define (call-with-ascii-input-file file proc)
"Open FILE as an ASCII or binary file, and pass the resulting port to
PROC. FILE is closed when PROC's dynamic extent is left. Return the