grub-devel.gnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Write grub-mkimage output in a safer way
@ 2013-12-11 11:47 Colin Watson
  0 siblings, 0 replies; only message in thread
From: Colin Watson @ 2013-12-11 11:47 UTC (permalink / raw)
  To: grub-devel

* 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


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2013-12-11 11:47 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-12-11 11:47 [PATCH] Write grub-mkimage output in a safer way Colin Watson

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).