nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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