From a4012f076498419d4133029820f27548860f5b7c Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 19 Oct 2013 16:21:08 +0200 Subject: [PATCH] Split make_system_path_relative_to_its_root into separate file relpath.c from getroot.c as it's common between unix and haiku but otherwise haiku doesn't use any functions from unix getroot.c. --- ChangeLog | 6 ++ Makefile.util.def | 4 + grub-core/osdep/aros/getroot.c | 39 ------- grub-core/osdep/aros/relpath.c | 75 +++++++++++++ grub-core/osdep/relpath.c | 7 ++ grub-core/osdep/unix/getroot.c | 136 ------------------------ grub-core/osdep/unix/relpath.c | 168 ++++++++++++++++++++++++++++++ grub-core/osdep/windows/getroot.c | 60 ----------- grub-core/osdep/windows/relpath.c | 98 +++++++++++++++++ 9 files changed, 358 insertions(+), 235 deletions(-) create mode 100644 grub-core/osdep/aros/relpath.c create mode 100644 grub-core/osdep/relpath.c create mode 100644 grub-core/osdep/unix/relpath.c create mode 100644 grub-core/osdep/windows/relpath.c diff --git a/ChangeLog b/ChangeLog index 80f9a8669..57f33c912 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2013-10-19 Vladimir Serbinenko + + Split make_system_path_relative_to_its_root into separate file + relpath.c from getroot.c as it's common between unix and haiku + but otherwise haiku doesn't use any functions from unix getroot.c. + 2013-10-19 Vladimir Serbinenko * grub-core/osdep/aros/hostdisk.c (grub_util_is_directory): diff --git a/Makefile.util.def b/Makefile.util.def index 225b82d13..efb0d8786 100644 --- a/Makefile.util.def +++ b/Makefile.util.def @@ -13,6 +13,10 @@ library = { common = grub-core/osdep/unix/getroot.c; common = grub-core/osdep/getroot.c; common = grub-core/osdep/devmapper/getroot.c; + common = grub-core/osdep/relpath.c; + extra_dist = grub-core/osdep/unix/relpath.c; + extra_dist = grub-core/osdep/aros/relpath.c; + extra_dist = grub-core/osdep/windows/relpath.c; common = grub-core/kern/emu/hostdisk.c; common = grub-core/osdep/devmapper/hostdisk.c; common = grub-core/osdep/hostdisk.c; diff --git a/grub-core/osdep/aros/getroot.c b/grub-core/osdep/aros/getroot.c index f1d66c1c3..a27df9eb9 100644 --- a/grub-core/osdep/aros/getroot.c +++ b/grub-core/osdep/aros/getroot.c @@ -130,45 +130,6 @@ grub_util_find_partition_start_os (const char *dev) * (grub_uint64_t) envec->de_SizeBlock) >> 7; } -char * -grub_make_system_path_relative_to_its_root (const char *path) -{ - char *p; - unsigned char *tmp; - char *ret; - BPTR lck; - - if (path[0] == '/' && path[1] == '/' && path[2] == ':') - return xstrdup (path); - - tmp = xmalloc (2048); - - lck = Lock ((const unsigned char *) path, SHARED_LOCK); - if (!lck || !NameFromLock (lck, tmp, 2040)) - { - free (tmp); - tmp = (unsigned char *) xstrdup (path); - } - if (lck) - UnLock (lck); - p = strchr ((char *) tmp, ':'); - if (!p) - return (char *) tmp; - if (p[1] == '/' || p[1] == '\0') - { - ret = xstrdup (p + 1); - } - else - { - ret = xmalloc (strlen (p + 1) + 2); - ret[0] = '/'; - strcpy (ret + 1, p + 1); - } - - free (tmp); - return ret; -} - char ** grub_guess_root_devices (const char *path) { diff --git a/grub-core/osdep/aros/relpath.c b/grub-core/osdep/aros/relpath.c new file mode 100644 index 000000000..9c2328d5f --- /dev/null +++ b/grub-core/osdep/aros/relpath.c @@ -0,0 +1,75 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2013 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 . + */ + +#include +#include + +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +char * +grub_make_system_path_relative_to_its_root (const char *path) +{ + char *p; + unsigned char *tmp; + char *ret; + BPTR lck; + + if (path[0] == '/' && path[1] == '/' && path[2] == ':') + return xstrdup (path); + + tmp = xmalloc (2048); + + lck = Lock ((const unsigned char *) path, SHARED_LOCK); + if (!lck || !NameFromLock (lck, tmp, 2040)) + { + free (tmp); + tmp = (unsigned char *) xstrdup (path); + } + if (lck) + UnLock (lck); + p = strchr ((char *) tmp, ':'); + if (!p) + return (char *) tmp; + if (p[1] == '/' || p[1] == '\0') + { + ret = xstrdup (p + 1); + } + else + { + ret = xmalloc (strlen (p + 1) + 2); + ret[0] = '/'; + strcpy (ret + 1, p + 1); + } + + free (tmp); + return ret; +} diff --git a/grub-core/osdep/relpath.c b/grub-core/osdep/relpath.c new file mode 100644 index 000000000..4e10d8ac1 --- /dev/null +++ b/grub-core/osdep/relpath.c @@ -0,0 +1,7 @@ +#if defined (__MINGW32__) || defined (__CYGWIN__) +#include "windows/relpath.c" +#elif defined (__AROS__) +#include "aros/relpath.c" +#else +#include "unix/relpath.c" +#endif diff --git a/grub-core/osdep/unix/getroot.c b/grub-core/osdep/unix/getroot.c index 2552e536e..410d58ddd 100644 --- a/grub-core/osdep/unix/getroot.c +++ b/grub-core/osdep/unix/getroot.c @@ -710,142 +710,6 @@ grub_find_zpool_from_dir (const char *dir, char **poolname, char **poolfs) *poolfs = xstrdup (""); } -/* This function never prints trailing slashes (so that its output - can be appended a slash unconditionally). */ -char * -grub_make_system_path_relative_to_its_root (const char *path) -{ - struct stat st; - char *p, *buf, *buf2, *buf3, *ret; - uintptr_t offset = 0; - dev_t num; - size_t len; - char *poolfs = NULL; - - /* canonicalize. */ - p = canonicalize_file_name (path); - if (p == NULL) - grub_util_error (_("failed to get canonical path of `%s'"), path); - - /* For ZFS sub-pool filesystems, could be extended to others (btrfs?). */ - { - char *dummy; - grub_find_zpool_from_dir (p, &dummy, &poolfs); - } - - len = strlen (p) + 1; - buf = xstrdup (p); - free (p); - - if (stat (buf, &st) < 0) - grub_util_error (_("cannot stat `%s': %s"), buf, strerror (errno)); - - buf2 = xstrdup (buf); - num = st.st_dev; - - /* This loop sets offset to the number of chars of the root - directory we're inspecting. */ - while (1) - { - p = strrchr (buf, '/'); - if (p == NULL) - /* This should never happen. */ - grub_util_error ("%s", - /* TRANSLATORS: canonical pathname is the - complete one e.g. /etc/fstab. It has - to contain `/' normally, if it doesn't - we're in trouble and throw this error. */ - _("no `/' in canonical filename")); - if (p != buf) - *p = 0; - else - *++p = 0; - - if (stat (buf, &st) < 0) - grub_util_error (_("cannot stat `%s': %s"), buf, strerror (errno)); - - /* buf is another filesystem; we found it. */ - if (st.st_dev != num) - { - /* offset == 0 means path given is the mount point. - This works around special-casing of "/" in Un*x. This function never - prints trailing slashes (so that its output can be appended a slash - unconditionally). Each slash in is considered a preceding slash, and - therefore the root directory is an empty string. */ - if (offset == 0) - { - free (buf); -#ifdef __linux__ - { - char *bind; - grub_free (grub_find_root_devices_from_mountinfo (buf2, &bind)); - if (bind && bind[0] && bind[1]) - { - buf3 = bind; - goto parsedir; - } - grub_free (bind); - } -#endif - free (buf2); - if (poolfs) - return xasprintf ("/%s/@", poolfs); - return xstrdup (""); - } - else - break; - } - - offset = p - buf; - /* offset == 1 means root directory. */ - if (offset == 1) - { - /* Include leading slash. */ - offset = 0; - break; - } - } - free (buf); - buf3 = xstrdup (buf2 + offset); - buf2[offset] = 0; -#ifdef __linux__ - { - char *bind; - grub_free (grub_find_root_devices_from_mountinfo (buf2, &bind)); - if (bind && bind[0] && bind[1]) - { - char *temp = buf3; - buf3 = grub_xasprintf ("%s%s%s", bind, buf3[0] == '/' ?"":"/", buf3); - grub_free (temp); - } - grub_free (bind); - } -#endif - - free (buf2); - -#ifdef __linux__ - parsedir: -#endif - /* Remove trailing slashes, return empty string if root directory. */ - len = strlen (buf3); - while (len > 0 && buf3[len - 1] == '/') - { - buf3[len - 1] = '\0'; - len--; - } - - if (poolfs) - { - ret = xasprintf ("/%s/@%s", poolfs, buf3); - free (buf3); - } - else - ret = buf3; - - return ret; -} - int grub_util_biosdisk_is_floppy (grub_disk_t disk) { diff --git a/grub-core/osdep/unix/relpath.c b/grub-core/osdep/unix/relpath.c new file mode 100644 index 000000000..8489a16d9 --- /dev/null +++ b/grub-core/osdep/unix/relpath.c @@ -0,0 +1,168 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 1999,2000,2001,2002,2003,2006,2007,2008,2009,2010,2011,2012,2013 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 . + */ + +#include +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +/* This function never prints trailing slashes (so that its output + can be appended a slash unconditionally). */ +char * +grub_make_system_path_relative_to_its_root (const char *path) +{ + struct stat st; + char *p, *buf, *buf2, *buf3, *ret; + uintptr_t offset = 0; + dev_t num; + size_t len; + char *poolfs = NULL; + + /* canonicalize. */ + p = canonicalize_file_name (path); + if (p == NULL) + grub_util_error (_("failed to get canonical path of `%s'"), path); + + /* For ZFS sub-pool filesystems, could be extended to others (btrfs?). */ + { + char *dummy; + grub_find_zpool_from_dir (p, &dummy, &poolfs); + } + + len = strlen (p) + 1; + buf = xstrdup (p); + free (p); + + if (stat (buf, &st) < 0) + grub_util_error (_("cannot stat `%s': %s"), buf, strerror (errno)); + + buf2 = xstrdup (buf); + num = st.st_dev; + + /* This loop sets offset to the number of chars of the root + directory we're inspecting. */ + while (1) + { + p = strrchr (buf, '/'); + if (p == NULL) + /* This should never happen. */ + grub_util_error ("%s", + /* TRANSLATORS: canonical pathname is the + complete one e.g. /etc/fstab. It has + to contain `/' normally, if it doesn't + we're in trouble and throw this error. */ + _("no `/' in canonical filename")); + if (p != buf) + *p = 0; + else + *++p = 0; + + if (stat (buf, &st) < 0) + grub_util_error (_("cannot stat `%s': %s"), buf, strerror (errno)); + + /* buf is another filesystem; we found it. */ + if (st.st_dev != num) + { + /* offset == 0 means path given is the mount point. + This works around special-casing of "/" in Un*x. This function never + prints trailing slashes (so that its output can be appended a slash + unconditionally). Each slash in is considered a preceding slash, and + therefore the root directory is an empty string. */ + if (offset == 0) + { + free (buf); +#ifdef __linux__ + { + char *bind; + grub_free (grub_find_root_devices_from_mountinfo (buf2, &bind)); + if (bind && bind[0] && bind[1]) + { + buf3 = bind; + goto parsedir; + } + grub_free (bind); + } +#endif + free (buf2); + if (poolfs) + return xasprintf ("/%s/@", poolfs); + return xstrdup (""); + } + else + break; + } + + offset = p - buf; + /* offset == 1 means root directory. */ + if (offset == 1) + { + /* Include leading slash. */ + offset = 0; + break; + } + } + free (buf); + buf3 = xstrdup (buf2 + offset); + buf2[offset] = 0; +#ifdef __linux__ + { + char *bind; + grub_free (grub_find_root_devices_from_mountinfo (buf2, &bind)); + if (bind && bind[0] && bind[1]) + { + char *temp = buf3; + buf3 = grub_xasprintf ("%s%s%s", bind, buf3[0] == '/' ?"":"/", buf3); + grub_free (temp); + } + grub_free (bind); + } +#endif + + free (buf2); + +#ifdef __linux__ + parsedir: +#endif + /* Remove trailing slashes, return empty string if root directory. */ + len = strlen (buf3); + while (len > 0 && buf3[len - 1] == '/') + { + buf3[len - 1] = '\0'; + len--; + } + + if (poolfs) + { + ret = xasprintf ("/%s/@%s", poolfs, buf3); + free (buf3); + } + else + ret = buf3; + + return ret; +} diff --git a/grub-core/osdep/windows/getroot.c b/grub-core/osdep/windows/getroot.c index d86d0774d..160e8c92c 100644 --- a/grub-core/osdep/windows/getroot.c +++ b/grub-core/osdep/windows/getroot.c @@ -51,10 +51,8 @@ #if SIZEOF_TCHAR == 1 #define tcsnicmp strncasecmp -#define tclen strlen #elif SIZEOF_TCHAR == 2 #define tcsnicmp wcsnicmp -#define tclen wcslen #endif char ** @@ -244,64 +242,6 @@ grub_util_find_partition_start_os (const char *os_dev) return exts.Extents[0].StartingOffset.QuadPart / 512; } -char * -grub_make_system_path_relative_to_its_root (const char *path) -{ - TCHAR *dirwindows, *mntpointwindows; - TCHAR *ptr; - TCHAR volumename[100]; - size_t mntpointwindows_sz; - size_t offset, flen; - TCHAR *ret; - char *cret; - - dirwindows = grub_util_get_windows_path (path); - if (!dirwindows) - return xstrdup (path); - - mntpointwindows_sz = strlen (path) * 2 + 1; - mntpointwindows = xmalloc ((mntpointwindows_sz + 1) * sizeof (mntpointwindows[0])); - - if (!GetVolumePathName (dirwindows, - mntpointwindows, - mntpointwindows_sz)) - { - offset = 0; - if (dirwindows[0] && dirwindows[1] == ':') - offset = 2; - } - offset = tclen (mntpointwindows); - free (mntpointwindows); - flen = tclen (dirwindows); - if (offset > flen) - { - offset = 0; - if (dirwindows[0] && dirwindows[1] == ':') - offset = 2; - } - ret = xmalloc (sizeof (ret[0]) * (flen - offset + 2)); - if (dirwindows[offset] != '\\' - && dirwindows[offset] != '/' - && dirwindows[offset]) - { - ret[0] = '\\'; - memcpy (ret + 1, dirwindows + offset, (flen - offset + 1) * sizeof (ret[0])); - } - else - memcpy (ret, dirwindows + offset, (flen - offset + 1) * sizeof (ret[0])); - - free (dirwindows); - - for (ptr = ret; *ptr; ptr++) - if (*ptr == '\\') - *ptr = '/'; - - cret = grub_util_tchar_to_utf8 (ret); - free (ret); - - return cret; -} - int grub_util_biosdisk_is_floppy (grub_disk_t disk) { diff --git a/grub-core/osdep/windows/relpath.c b/grub-core/osdep/windows/relpath.c new file mode 100644 index 000000000..731cd0ca6 --- /dev/null +++ b/grub-core/osdep/windows/relpath.c @@ -0,0 +1,98 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 1999,2000,2001,2002,2003,2006,2007,2008,2009,2010,2011,2012,2013 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 . + */ + +#include +#include + +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if SIZEOF_TCHAR == 1 +#define tclen strlen +#elif SIZEOF_TCHAR == 2 +#define tclen wcslen +#endif + +char * +grub_make_system_path_relative_to_its_root (const char *path) +{ + TCHAR *dirwindows, *mntpointwindows; + TCHAR *ptr; + TCHAR volumename[100]; + size_t mntpointwindows_sz; + size_t offset, flen; + TCHAR *ret; + char *cret; + + dirwindows = grub_util_get_windows_path (path); + if (!dirwindows) + return xstrdup (path); + + mntpointwindows_sz = strlen (path) * 2 + 1; + mntpointwindows = xmalloc ((mntpointwindows_sz + 1) * sizeof (mntpointwindows[0])); + + if (!GetVolumePathName (dirwindows, + mntpointwindows, + mntpointwindows_sz)) + { + offset = 0; + if (dirwindows[0] && dirwindows[1] == ':') + offset = 2; + } + offset = tclen (mntpointwindows); + free (mntpointwindows); + flen = tclen (dirwindows); + if (offset > flen) + { + offset = 0; + if (dirwindows[0] && dirwindows[1] == ':') + offset = 2; + } + ret = xmalloc (sizeof (ret[0]) * (flen - offset + 2)); + if (dirwindows[offset] != '\\' + && dirwindows[offset] != '/' + && dirwindows[offset]) + { + ret[0] = '\\'; + memcpy (ret + 1, dirwindows + offset, (flen - offset + 1) * sizeof (ret[0])); + } + else + memcpy (ret, dirwindows + offset, (flen - offset + 1) * sizeof (ret[0])); + + free (dirwindows); + + for (ptr = ret; *ptr; ptr++) + if (*ptr == '\\') + *ptr = '/'; + + cret = grub_util_tchar_to_utf8 (ret); + free (ret); + + return cret; +}