From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.71) id 1VqiGf-0002bW-LG for mharc-grub-devel@gnu.org; Wed, 11 Dec 2013 06:47:53 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55832) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VqiGb-0002aZ-Vw for grub-devel@gnu.org; Wed, 11 Dec 2013 06:47:51 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VqiGW-0004Hc-QN for grub-devel@gnu.org; Wed, 11 Dec 2013 06:47:49 -0500 Received: from v6.chiark.greenend.org.uk ([2001:ba8:1e3::]:56695 helo=chiark.greenend.org.uk) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VqiGW-0004HM-L1 for grub-devel@gnu.org; Wed, 11 Dec 2013 06:47:44 -0500 Received: from [172.20.153.9] (helo=riva.pelham.vpn.ucam.org) by chiark.greenend.org.uk (Debian Exim 4.72 #1) with esmtps (return-path cjwatson@ubuntu.com) id 1VqiGV-0004bX-29 for grub-devel@gnu.org; Wed, 11 Dec 2013 11:47:43 +0000 Received: from ns1.pelham.vpn.ucam.org ([172.20.153.2] helo=riva.ucam.org) by riva.pelham.vpn.ucam.org with esmtps (TLS1.2:DHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.80) (envelope-from ) id 1VqiGT-0002kz-R4 for grub-devel@gnu.org; Wed, 11 Dec 2013 11:47:41 +0000 Date: Wed, 11 Dec 2013 11:47:40 +0000 From: Colin Watson To: grub-devel@gnu.org Subject: [PATCH] Write grub-mkimage output in a safer way Message-ID: <20131211114740.GL5883@riva.ucam.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2001:ba8:1e3:: X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list Reply-To: The development of GNU GRUB List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 11 Dec 2013 11:47:51 -0000 * util/grub-mkimage.c (main): If asked to write to an output file rather than stdout, open FILE.new and rename it into place at the end. This is safer in the event of a failure in the middle. --- ChangeLog | 6 ++++++ util/grub-mkimage.c | 12 ++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 37184be..4304182 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2013-12-11 Colin Watson + + * util/grub-mkimage.c (main): If asked to write to an output file + rather than stdout, open FILE.new and rename it into place at the + end. This is safer in the event of a failure in the middle. + 2013-12-11 Vladimir Serbinenko * grub-core/kern/ia64/dl.c (grub_arch_dl_relocate_symbols): Add checks diff --git a/util/grub-mkimage.c b/util/grub-mkimage.c index 0243741..a5b40b3 100644 --- a/util/grub-mkimage.c +++ b/util/grub-mkimage.c @@ -232,6 +232,7 @@ main (int argc, char *argv[]) { FILE *fp = stdout; struct arguments arguments; + char *output_new = NULL; grub_util_host_init (&argc, &argv); @@ -260,7 +261,8 @@ main (int argc, char *argv[]) if (arguments.output) { - fp = grub_util_fopen (arguments.output, "wb"); + output_new = xasprintf ("%s.new", arguments.output); + fp = grub_util_fopen (output_new, "wb"); if (! fp) grub_util_error (_("cannot open `%s': %s"), arguments.output, strerror (errno)); @@ -292,7 +294,13 @@ main (int argc, char *argv[]) free (arguments.dir); if (arguments.output) - free (arguments.output); + { + if (grub_util_rename (output_new, arguments.output) < 0) + grub_util_error (_("cannot rename the file %s to %s"), + output_new, arguments.output); + free (output_new); + free (arguments.output); + } return 0; } -- 1.8.4.4