From: Felix Zielcke <fzielcke@z-51.de>
To: The development of GRUB 2 <grub-devel@gnu.org>
Subject: [PATH] grub-mkrelpath
Date: Fri, 28 Aug 2009 14:53:12 +0200 [thread overview]
Message-ID: <1251463992.2547.24.camel@fz.local> (raw)
[-- Attachment #1: Type: text/plain, Size: 435 bytes --]
Here's a patch which implements grub-mkrelpath based on my
make_system_path_relative_to_its_root patch to fix the problem with
grub-setup and blocklists, if the path to core.img given to grub-setup
isn't already relative to the root.
I wasn't sure if I should place the function now in
util/grub-mkrelpath.c or some other file. That depends if we're going to
use it now in grub-setup or not.
--
Felix Zielcke
Proud Debian Maintainer
[-- Attachment #2: grub-mkrelpath.diff --]
[-- Type: text/x-patch, Size: 7076 bytes --]
2009-08-28 Felix Zielcke <fzielcke@z-51.de>
* conf/common.rmk (bin_UTILITIES): Add grub-mkrelpath.
(grub_mkrelpath_SOURCES): New variable.
* configure.ac (AC_CHECK_FUNCS): Add realpath.
* util/grub-mkconfig_lib.in (make_system_path_relative_to_its_root):
Use grub-mkrelpath.
* util/grub-mkrelpath.c: New file.
diff --git a/conf/common.rmk b/conf/common.rmk
index 6d76746..9a2dc4c 100644
--- a/conf/common.rmk
+++ b/conf/common.rmk
@@ -55,6 +55,10 @@ grub_mkfont_CFLAGS = $(freetype_cflags)
grub_mkfont_LDFLAGS = $(freetype_libs)
endif
+# For grub-mkrelpath.
+bin_UTILITIES += grub-mkrelpath
+grub_mkrelpath_SOURCES = util/grub-mkrelpath.c util/misc.c
+
# For the parser.
grub_script.tab.c grub_script.tab.h: script/sh/parser.y
$(YACC) -d -p grub_script_yy -b grub_script $(srcdir)/script/sh/parser.y
diff --git a/configure.ac b/configure.ac
index 549b35c..ee5ebb1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -172,7 +172,7 @@ if test x$grub_cv_apple_cc = xyes ; then
fi
# Check for functions.
-AC_CHECK_FUNCS(posix_memalign memalign asprintf)
+AC_CHECK_FUNCS(posix_memalign memalign asprintf realpath)
#
# Check for target programs.
diff --git a/util/grub-mkconfig_lib.in b/util/grub-mkconfig_lib.in
index 2385b08..c8d2736 100644
--- a/util/grub-mkconfig_lib.in
+++ b/util/grub-mkconfig_lib.in
@@ -32,49 +32,7 @@ grub_warn ()
make_system_path_relative_to_its_root ()
{
- path=$1
- # abort if file doesn't exist
- if test -e $path ; then : ;else
- return 1
- fi
-
- # canonicalize
- if path=`readlink -f $path` ; then : ; else
- return 1
- fi
-
- # if not a directory, climb up to the directory containing it
- if test -d $path ; then
- dir=$path
- else
- dir=`echo $path | sed -e "s,/[^/]*$,,g"`
- fi
-
- num=`stat -c %d $dir`
-
- # this loop sets $dir to the root directory of the filesystem we're inspecting
- while : ; do
- parent=`readlink -f $dir/..`
- if [ "x`stat -c %d $parent`" = "x$num" ] ; then : ; else
- # $parent is another filesystem; we found it.
- break
- fi
- if [ "x$dir" = "x/" ] ; then
- # / is our root.
- break
- fi
- dir=$parent
- done
-
- # This function never prints trailing slashes (so that its output can be
- # appended a slash unconditionally). Each slash in $dir is considered a
- # preceding slash, and therefore the root directory is an empty string.
- if [ "$dir" = "/" ] ; then
- dir=""
- fi
-
- # XXX: This fails if $dir contains ','.
- path=`echo "$path" | sed -e "s,^$dir,,g"` || return 1
+ path = "`grub-mkrelpath $1`"
case "`uname 2>/dev/null`" in
CYGWIN*)
diff --git a/util/grub-mkrelpath.c b/util/grub-mkrelpath.c
index e69de29..7c9c678 100644
--- a/util/grub-mkrelpath.c
+++ b/util/grub-mkrelpath.c
@@ -0,0 +1,194 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2009 Free Software Foundation, Inc.
+ *
+ * GRUB is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GRUB is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <grub/util/misc.h>
+#include <errno.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <stdint.h>
+#include <string.h>
+#include <getopt.h>
+
+static struct option options[] =
+ {
+ {"help", no_argument, 0, 'h'},
+ {"version", no_argument, 0, 'V'},
+ };
+
+char *
+make_system_path_relative_to_its_root (const char *path)
+{
+ struct stat st;
+ char *p, *buf, *buf2, *buf3;
+ uintptr_t offset = 0;
+ dev_t num;
+ size_t len;
+
+#ifdef HAVE_REALPATH
+ p = realpath (path, NULL);
+
+ if (buf == NULL)
+ {
+ if (errno != EINVAL)
+ grub_util_error ("failed to get realpath of %s", path);
+ else
+ grub_util_error ("realpath not supporting (path, NULL)");
+ }
+ len = strlen (p) + 1;
+ buf = xmalloc (len);
+ buf2 = xmalloc (len);
+ strcpy (buf, p);
+ free (p);
+#else /* ! HAVE_REALPATH */
+ grub_util_warn ("grub-mkrelpath might not work on your OS correctly.");
+ if (*path != '/')
+ {
+ len = 1024;
+ buf = xmalloc (len);
+ do
+ {
+ p = getcwd (buf, len);
+ if (p == NULL)
+ {
+ if (errno != ERANGE)
+ grub_util_error ("can not get current working directory");
+ else
+ len *= 2;
+ buf = xrealloc (buf, len);
+ }
+ } while (p == NULL);
+ buf = xmalloc (strlen (path) + len + 1);
+ strcat (buf, "/");
+ strcat (buf, path);
+ }
+ else
+ {
+ buf = xmalloc (strlen (path) + 1);
+ strcpy (buf, path);
+ }
+#endif /* ! HAVE_REALPATH */
+ buf2 = xmalloc (strlen (buf) + 1);
+ strcpy (buf2, buf);
+ if (stat (buf, &st) < 0)
+ grub_util_error ("can not stat %s", p);
+
+ num = st.st_dev;
+ while (1)
+ {
+ p = strrchr (buf, '/');
+ if (p == NULL)
+ grub_util_error ("FIXME no / in buf");
+ if (p != buf)
+ *p = 0;
+ else
+ *++p = 0;
+
+ if (stat (buf, &st) < 0)
+ grub_util_error ("can not stat %s", buf);
+
+ if (st.st_dev != num)
+ break;
+
+ offset = p - buf;
+ if (offset == 1)
+ {
+ free (buf);
+ return buf2;
+ }
+ }
+ free (buf);
+ buf3 = strdup (buf2 + offset);
+ free (buf2);
+ return buf3;
+}
+
+static void
+usage (int status)
+{
+ if (status)
+ fprintf (stderr, "Try ``grub-mkrelpath --help'' for more information.\n");
+ else
+ printf ("\
+Usage: grub-mkrelpath [OPTIONS] PATH\n\
+\n\
+Make a system path relative to it's root.\n\
+\n\
+Options:\n\
+ -h, --help display this message and exit\n\
+ -V, --version print version information and exit\n\
+\n\
+Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
+
+ exit (status);
+}
+
+int
+main (int argc, char *argv[])
+{
+ char *argument, *relpath;
+
+ progname = "grub-mkrelpath";
+
+ /* Check for options. */
+ while (1)
+ {
+ int c = getopt_long (argc, argv, "hV", options, 0);
+
+ if (c == -1)
+ break;
+ else
+ switch (c)
+ {
+ case 'h':
+ usage (0);
+ break;
+
+ case 'V':
+ printf ("%s (%s) %s\n", progname, PACKAGE_NAME, PACKAGE_VERSION);
+ return 0;
+
+ default:
+ usage (1);
+ break;
+ }
+ }
+
+ if (optind >= argc)
+ {
+ fprintf (stderr, "No path is specified.\n");
+ usage (1);
+ }
+
+ if (optind + 1 != argc)
+ {
+ fprintf (stderr, "Unknown extra argument `%s'.\n", argv[optind + 1]);
+ usage (1);
+ }
+
+ argument = argv[optind];
+
+ relpath = make_system_path_relative_to_its_root (argument);
+ printf ("%s\n",relpath);
+ free (relpath);
+
+ return 0;
+}
next reply other threads:[~2009-08-28 12:53 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-08-28 12:53 Felix Zielcke [this message]
2009-08-28 16:28 ` [PATH] grub-mkrelpath Robert Millan
2009-08-28 17:58 ` Felix Zielcke
2009-08-28 23:55 ` Robert Millan
2009-08-29 0:58 ` Vladimir 'phcoder' Serbinenko
2009-08-29 7:51 ` Felix Zielcke
2009-11-01 15:39 ` Felix Zielcke
2009-11-01 22:04 ` Robert Millan
2009-11-02 8:54 ` Felix Zielcke
2009-11-02 13:45 ` Robert Millan
2009-11-02 15:41 ` Felix Zielcke
2009-11-03 10:21 ` rubisher
2009-11-03 10:33 ` Felix Zielcke
2009-11-03 13:51 ` rubisher
2009-11-03 14:09 ` Felix Zielcke
2009-11-04 9:55 ` rubisher
2009-11-04 11:07 ` Felix Zielcke
2009-11-04 14:59 ` rubisher
2009-11-04 15:55 ` Felix Zielcke
2009-11-11 22:43 ` Felix Zielcke
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=1251463992.2547.24.camel@fz.local \
--to=fzielcke@z-51.de \
--cc=grub-devel@gnu.org \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.