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

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