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