grub-devel.gnu.org archive mirror
 help / color / mirror / Atom feed
From: Javier Martinez Canillas <javierm@redhat.com>
To: grub-devel@gnu.org
Cc: Peter Jones <pjones@redhat.com>, Adam Jackson <ajax@redhat.com>,
	Javier Martinez Canillas <javierm@redhat.com>
Subject: [PATCH 2/2] Make editenv chase symlinks including those across devices.
Date: Fri, 18 Oct 2019 14:42:21 +0200	[thread overview]
Message-ID: <20191018124221.15934-2-javierm@redhat.com> (raw)
In-Reply-To: <20191018124221.15934-1-javierm@redhat.com>

From: Peter Jones <pjones@redhat.com>

This lets us make /boot/grub2/grubenv a symlink to
/boot/efi/EFI/fedora/grubenv even though they're different mount points,
which allows /usr/bin/grub2-editenv to be the same across platforms
(i.e. UEFI vs BIOS).

Signed-off-by: Peter Jones <pjones@redhat.com>
Reviewed-by: Adam Jackson <ajax@redhat.com>
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
---

 Makefile.util.def | 11 +++++++++++
 util/editenv.c    | 46 ++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 55 insertions(+), 2 deletions(-)

diff --git a/Makefile.util.def b/Makefile.util.def
index 969d32f0097..733a397cb2b 100644
--- a/Makefile.util.def
+++ b/Makefile.util.def
@@ -240,8 +240,19 @@ program = {
 
   common = util/grub-editenv.c;
   common = util/editenv.c;
+  common = util/grub-install-common.c;
   common = grub-core/osdep/init.c;
+  common = grub-core/osdep/compress.c;
+  extra_dist = grub-core/osdep/unix/compress.c;
+  extra_dist = grub-core/osdep/basic/compress.c;
+  common = util/mkimage.c;
+  common = util/grub-mkimage32.c;
+  common = util/grub-mkimage64.c;
+  common = grub-core/osdep/config.c;
+  common = util/config.c;
+  common = util/resolve.c;
 
+  ldadd = '$(LIBLZMA)';
   ldadd = libgrubmods.a;
   ldadd = libgrubgcry.a;
   ldadd = libgrubkern.a;
diff --git a/util/editenv.c b/util/editenv.c
index eb2d0c03a98..e61dc1283a4 100644
--- a/util/editenv.c
+++ b/util/editenv.c
@@ -37,6 +37,7 @@ grub_util_create_envblk_file (const char *name)
   FILE *fp;
   char *buf;
   char *namenew;
+  char *rename_target = xstrdup(name);
 
   buf = xmalloc (DEFAULT_ENVBLK_SIZE);
 
@@ -60,7 +61,48 @@ grub_util_create_envblk_file (const char *name)
   free (buf);
   fclose (fp);
 
-  if (grub_util_rename (namenew, name) < 0)
-    grub_util_error (_("cannot rename the file %s to %s"), namenew, name);
+  ssize_t size = 1;
+  while (1)
+    {
+      char *linkbuf;
+      ssize_t retsize;
+
+      linkbuf = xmalloc(size+1);
+      retsize = grub_util_readlink (rename_target, linkbuf, size);
+      if (retsize < 0 && (errno == ENOENT || errno == EINVAL))
+	{
+	  free (linkbuf);
+	  break;
+	}
+      else if (retsize < 0)
+	{
+	  grub_util_error (_("cannot rename the file %s to %s: %m"), namenew, name);
+	  free (linkbuf);
+	  free (namenew);
+	  return;
+	}
+      else if (retsize == size)
+	{
+	  free(linkbuf);
+	  size += 128;
+	  continue;
+	}
+
+      free (rename_target);
+      linkbuf[retsize] = '\0';
+      rename_target = linkbuf;
+    }
+
+  int rc = grub_util_rename (namenew, rename_target);
+  if (rc < 0 && errno == EXDEV)
+    {
+      rc = grub_install_copy_file (namenew, rename_target, 1);
+      grub_util_unlink (namenew);
+    }
+
+  if (rc < 0)
+    grub_util_error (_("cannot rename the file %s to %s: %m"), namenew, name);
+
   free (namenew);
+  free (rename_target);
 }
-- 
2.21.0



  reply	other threads:[~2019-10-18 12:42 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-18 12:42 [PATCH 1/2] Add grub_util_readlink() Javier Martinez Canillas
2019-10-18 12:42 ` Javier Martinez Canillas [this message]
2019-10-23  9:42   ` [PATCH 2/2] Make editenv chase symlinks including those across devices Daniel Kiper
2019-10-28 13:09     ` Javier Martinez Canillas
2019-10-23  9:19 ` [PATCH 1/2] Add grub_util_readlink() Daniel Kiper
2019-10-28 13:04   ` Javier Martinez Canillas
  -- strict thread matches above, loose matches on Subject: below --
2014-09-04 15:36 [PATCH 0/2] Allow editenv to follow symlinks to find/make grubenv Peter Jones
2014-09-04 15:36 ` [PATCH 2/2] Make editenv chase symlinks including those across devices Peter Jones
2014-09-21 16:15   ` Vladimir 'φ-coder/phcoder' Serbinenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191018124221.15934-2-javierm@redhat.com \
    --to=javierm@redhat.com \
    --cc=ajax@redhat.com \
    --cc=grub-devel@gnu.org \
    --cc=pjones@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).