]> git.ipfire.org Git - thirdparty/grub.git/commitdiff
2009-10-01 Colin Watson <cjwatson@ubuntu.com>
authorcjwatson <cjwatson@localhost>
Thu, 1 Oct 2009 18:22:43 +0000 (18:22 +0000)
committercjwatson <cjwatson@localhost>
Thu, 1 Oct 2009 18:22:43 +0000 (18:22 +0000)
* util/grub-editenv.c (create_envblk_file): Write new block with a
.new suffix and then rename it into place, to ensure atomic
creation.

ChangeLog
util/grub-editenv.c

index 394470abc954debf6a4599d12eaf7e011ee2808f..b2d1a7085d958f079c01747d9961bb3c5249c252 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2009-10-01  Colin Watson  <cjwatson@ubuntu.com>
+
+       * util/grub-editenv.c (create_envblk_file): Write new block with a
+       .new suffix and then rename it into place, to ensure atomic
+       creation.
+
 2009-09-28  Robert Millan  <rmh.grub@aybabtu.com>
 
        Do not automatically install headers.
index 6d123405028020b3dfe5876a618b3fd5ab059072..a8dc137c5da37c14258cb000e05865c79aea1576 100644 (file)
@@ -95,25 +95,31 @@ create_envblk_file (const char *name)
 {
   FILE *fp;
   char *buf;
+  char *namenew;
 
   buf = malloc (DEFAULT_ENVBLK_SIZE);
   if (! buf)
     grub_util_error ("out of memory");
 
-  fp = fopen (name, "wb");
+  asprintf (&namenew, "%s.new", name);
+  fp = fopen (namenew, "wb");
   if (! fp)
-    grub_util_error ("cannot open the file %s", name);
+    grub_util_error ("cannot open the file %s", namenew);
 
   memcpy (buf, GRUB_ENVBLK_SIGNATURE, sizeof (GRUB_ENVBLK_SIGNATURE) - 1);
   memset (buf + sizeof (GRUB_ENVBLK_SIGNATURE) - 1, '#',
           DEFAULT_ENVBLK_SIZE - sizeof (GRUB_ENVBLK_SIGNATURE) + 1);
 
   if (fwrite (buf, 1, DEFAULT_ENVBLK_SIZE, fp) != DEFAULT_ENVBLK_SIZE)
-    grub_util_error ("cannot write to the file %s", name);
+    grub_util_error ("cannot write to the file %s", namenew);
 
   fsync (fileno (fp));
   free (buf);
   fclose (fp);
+
+  if (rename (namenew, name) < 0)
+    grub_util_error ("cannot rename the file %s to %s", namenew, name);
+  free (namenew);
 }
 
 static grub_envblk_t