From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.71) id 1XPZ6E-0001uv-9q for mharc-grub-devel@gnu.org; Thu, 04 Sep 2014 11:37:26 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42729) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XPZ66-0001iC-Op for grub-devel@gnu.org; Thu, 04 Sep 2014 11:37:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XPZ5y-0001ya-Rq for grub-devel@gnu.org; Thu, 04 Sep 2014 11:37:18 -0400 Received: from mx1.redhat.com ([209.132.183.28]:31326) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XPZ5y-0001yE-Kq for grub-devel@gnu.org; Thu, 04 Sep 2014 11:37:10 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s84Fb9m1000576 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL) for ; Thu, 4 Sep 2014 11:37:10 -0400 Received: from fenchurch.internal.datastacks.com.com (ovpn-113-152.phx2.redhat.com [10.3.113.152]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s84Fb7ix014651; Thu, 4 Sep 2014 11:37:09 -0400 From: Peter Jones To: grub-devel@gnu.org Subject: [PATCH 2/2] Make editenv chase symlinks including those across devices. Date: Thu, 4 Sep 2014 11:36:55 -0400 Message-Id: <1409845015-16778-3-git-send-email-pjones@redhat.com> In-Reply-To: <1409845015-16778-1-git-send-email-pjones@redhat.com> References: <1409845015-16778-1-git-send-email-pjones@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list Reply-To: The development of GNU GRUB List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 04 Sep 2014 15:37:25 -0000 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 Reviewed-by: Adam Jackson --- Makefile.util.def | 9 +++++++++ util/editenv.c | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/Makefile.util.def b/Makefile.util.def index 8f40e78..87029a1 100644 --- a/Makefile.util.def +++ b/Makefile.util.def @@ -228,8 +228,17 @@ 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 = 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 c6f8d22..d8d1dad 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); @@ -59,7 +60,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); } -- 1.9.3