From e6d4e05cbda8445b86bccaa26b8604620cbe6412 Mon Sep 17 00:00:00 2001 From: Andy Wingo Date: Sat, 9 Aug 2008 14:11:35 +0200 Subject: [PATCH] don't truncate .go files, do an atomic rename to prevent SIGBUS * module/system/base/compile.scm (call-with-output-file/atomic): New proc, outputs to a tempfile then does an atomic rename. Prevents SIGBUS if a compiled file is truncated and rewritten, as the file's objcode is mmap'd in. (compile-file): Use the new helper. --- module/system/base/compile.scm | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/module/system/base/compile.scm b/module/system/base/compile.scm index 8a08c5a25..98de7d1d0 100644 --- a/module/system/base/compile.scm +++ b/module/system/base/compile.scm @@ -58,6 +58,18 @@ (define (scheme) (lookup-language 'scheme)) +(define (call-with-output-file/atomic filename proc) + (let* ((template (string-append filename ".XXXXXX")) + (tmp (mkstemp! template))) + (catch #t + (lambda () + (with-output-to-port tmp + (lambda () (proc (current-output-port)))) + (rename-file template filename)) + (lambda args + (delete-file template) + (apply throw args))))) + (define (compile-file file . opts) (let ((comp (compiled-file-name file)) (scheme (scheme))) @@ -65,7 +77,7 @@ (lambda () (call-with-compile-error-catch (lambda () - (call-with-output-file comp + (call-with-output-file/atomic comp (lambda (port) (let* ((source (read-file-in file scheme)) (objcode (apply compile-in source (current-module)