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

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