grub-devel.gnu.org archive mirror
 help / color / mirror / Atom feed
From: Colin Watson <cjwatson@ubuntu.com>
To: grub-devel@gnu.org
Subject: [PATCH] Write grub-mkimage output in a safer way
Date: Wed, 11 Dec 2013 11:47:40 +0000	[thread overview]
Message-ID: <20131211114740.GL5883@riva.ucam.org> (raw)

* 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  <cjwatson@ubuntu.com>
+
+	* 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  <phcoder@gmail.com>
 
 	* 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


                 reply	other threads:[~2013-12-11 11:47 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20131211114740.GL5883@riva.ucam.org \
    --to=cjwatson@ubuntu.com \
    --cc=grub-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).