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

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