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

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