nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0146-Make-editenv-chase-symlinks-including-those-across-d.patch

f725e3
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
f725e3
From: Peter Jones <pjones@redhat.com>
f725e3
Date: Wed, 3 Sep 2014 10:38:00 -0400
f725e3
Subject: [PATCH] Make editenv chase symlinks including those across devices.
f725e3
f725e3
This lets us make /boot/grub2/grubenv a symlink to
f725e3
/boot/efi/EFI/fedora/grubenv even though they're different mount points,
f725e3
which allows /usr/bin/grub2-editenv to be the same across platforms
f725e3
(i.e. UEFI vs BIOS).
f725e3
f725e3
Signed-off-by: Peter Jones <pjones@redhat.com>
f725e3
Reviewed-by: Adam Jackson <ajax@redhat.com>
f725e3
---
f725e3
 Makefile.util.def |  9 +++++++++
f725e3
 util/editenv.c    | 46 ++++++++++++++++++++++++++++++++++++++++++++--
f725e3
 2 files changed, 53 insertions(+), 2 deletions(-)
f725e3
f725e3
diff --git a/Makefile.util.def b/Makefile.util.def
f725e3
index 8f40e78332a..87029a10dfb 100644
f725e3
--- a/Makefile.util.def
f725e3
+++ b/Makefile.util.def
f725e3
@@ -228,8 +228,17 @@ program = {
f725e3
 
f725e3
   common = util/grub-editenv.c;
f725e3
   common = util/editenv.c;
f725e3
+  common = util/grub-install-common.c;
f725e3
   common = grub-core/osdep/init.c;
f725e3
+  common = grub-core/osdep/compress.c;
f725e3
+  extra_dist = grub-core/osdep/unix/compress.c;
f725e3
+  extra_dist = grub-core/osdep/basic/compress.c;
f725e3
+  common = util/mkimage.c;
f725e3
+  common = grub-core/osdep/config.c;
f725e3
+  common = util/config.c;
f725e3
+  common = util/resolve.c;
f725e3
 
f725e3
+  ldadd = '$(LIBLZMA)';
f725e3
   ldadd = libgrubmods.a;
f725e3
   ldadd = libgrubgcry.a;
f725e3
   ldadd = libgrubkern.a;
f725e3
diff --git a/util/editenv.c b/util/editenv.c
f725e3
index c6f8d2298c3..d8d1dad6ab9 100644
f725e3
--- a/util/editenv.c
f725e3
+++ b/util/editenv.c
f725e3
@@ -37,6 +37,7 @@ grub_util_create_envblk_file (const char *name)
f725e3
   FILE *fp;
f725e3
   char *buf;
f725e3
   char *namenew;
f725e3
+  char *rename_target = xstrdup(name);
f725e3
 
f725e3
   buf = xmalloc (DEFAULT_ENVBLK_SIZE);
f725e3
 
f725e3
@@ -59,7 +60,48 @@ grub_util_create_envblk_file (const char *name)
f725e3
   free (buf);
f725e3
   fclose (fp);
f725e3
 
f725e3
-  if (grub_util_rename (namenew, name) < 0)
f725e3
-    grub_util_error (_("cannot rename the file %s to %s"), namenew, name);
f725e3
+  ssize_t size = 1;
f725e3
+  while (1)
f725e3
+    {
f725e3
+      char *linkbuf;
f725e3
+      ssize_t retsize;
f725e3
+
f725e3
+      linkbuf = xmalloc(size+1);
f725e3
+      retsize = grub_util_readlink (rename_target, linkbuf, size);
f725e3
+      if (retsize < 0 && (errno == ENOENT || errno == EINVAL))
f725e3
+	{
f725e3
+	  free (linkbuf);
f725e3
+	  break;
f725e3
+	}
f725e3
+      else if (retsize < 0)
f725e3
+	{
f725e3
+	  grub_util_error (_("cannot rename the file %s to %s: %m"), namenew, name);
f725e3
+	  free (linkbuf);
f725e3
+	  free (namenew);
f725e3
+	  return;
f725e3
+	}
f725e3
+      else if (retsize == size)
f725e3
+	{
f725e3
+	  free(linkbuf);
f725e3
+	  size += 128;
f725e3
+	  continue;
f725e3
+	}
f725e3
+
f725e3
+      free (rename_target);
f725e3
+      linkbuf[retsize] = '\0';
f725e3
+      rename_target = linkbuf;
f725e3
+    }
f725e3
+
f725e3
+  int rc = grub_util_rename (namenew, rename_target);
f725e3
+  if (rc < 0 && errno == EXDEV)
f725e3
+    {
f725e3
+      rc = grub_install_copy_file (namenew, rename_target, 1);
f725e3
+      grub_util_unlink (namenew);
f725e3
+    }
f725e3
+
f725e3
+  if (rc < 0)
f725e3
+    grub_util_error (_("cannot rename the file %s to %s: %m"), namenew, name);
f725e3
+
f725e3
   free (namenew);
f725e3
+  free (rename_target);
f725e3
 }