grub-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] Write grub-mkimage output in a safer way


From: Colin Watson
Subject: [PATCH] Write grub-mkimage output in a safer way
Date: Wed, 11 Dec 2013 11:47:40 +0000
User-agent: Mutt/1.5.21 (2010-09-15)

* 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  <address@hidden>
+
+       * 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  <address@hidden>
 
        * 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 via email to

[Prev in Thread] Current Thread [Next in Thread]