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.
This commit is contained in:
Vladimir 'phcoder' Serbinenko 2013-10-19 16:21:08 +02:00
parent 31de274d29
commit a4012f0764
9 changed files with 358 additions and 235 deletions

View File

@ -1,3 +1,9 @@
2013-10-19 Vladimir Serbinenko <phcoder@gmail.com>
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 <phcoder@gmail.com>
* grub-core/osdep/aros/hostdisk.c (grub_util_is_directory):

View File

@ -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;

View File

@ -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)
{

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include <config-util.h>
#include <config.h>
#include <grub/util/misc.h>
#include <grub/mm.h>
#include <grub/misc.h>
#include <grub/emu/misc.h>
#include <grub/emu/hostdisk.h>
#include <grub/emu/getroot.h>
#include <string.h>
#include <dos/dos.h>
#include <dos/filesystem.h>
#include <dos/exall.h>
#include <proto/dos.h>
#include <proto/exec.h>
#include <devices/trackdisk.h>
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;
}

View File

@ -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

View File

@ -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)
{

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include <config-util.h>
#include <config.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <grub/util/misc.h>
#include <grub/emu/hostdisk.h>
#include <grub/emu/getroot.h>
#include <grub/mm.h>
/* 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;
}

View File

@ -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)
{

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include <config-util.h>
#include <config.h>
#include <grub/types.h>
#include <grub/util/misc.h>
#include <grub/mm.h>
#include <grub/misc.h>
#include <grub/emu/misc.h>
#include <grub/emu/hostdisk.h>
#include <grub/emu/getroot.h>
#include <grub/charset.h>
#include <grub/util/windows.h>
#include <windows.h>
#include <winioctl.h>
#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;
}