nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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