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

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