Write grub-mkimage output in a safer way
PATCH 1/1 Write grub-mkimage output in a safer way
* 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(-)
ChangeLog +6 -0
@@ -1,3 +1,9 @@ +2013-12-11 Colin Watson <[email protected]> + + * 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 <[email protected]> * grub-core/kern/ia64/dl.c (grub_arch_dl_relocate_symbols): Add checks
util/grub-mkimage.c +10 -2
@@ -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 ; }
Signature · 1 line
1.8.4.4