merge with mainline
This commit is contained in:
commit
81827e248c
69 changed files with 2217 additions and 592 deletions
|
@ -22,8 +22,10 @@
|
|||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __CYGWIN__
|
||||
# include <sys/fcntl.h>
|
||||
|
@ -83,103 +85,6 @@ xgetcwd (void)
|
|||
return path;
|
||||
}
|
||||
|
||||
#ifdef __CYGWIN__
|
||||
/* Convert POSIX path to Win32 path,
|
||||
remove drive letter, replace backslashes. */
|
||||
static char *
|
||||
get_win32_path (const char *path)
|
||||
{
|
||||
char winpath[PATH_MAX];
|
||||
cygwin_conv_to_full_win32_path (path, winpath);
|
||||
|
||||
int len = strlen (winpath);
|
||||
if (len > 2 && winpath[1] == ':')
|
||||
{
|
||||
len -= 2;
|
||||
memmove (winpath, winpath + 2, len + 1);
|
||||
}
|
||||
|
||||
int i;
|
||||
for (i = 0; i < len; i++)
|
||||
if (winpath[i] == '\\')
|
||||
winpath[i] = '/';
|
||||
return xstrdup (winpath);
|
||||
}
|
||||
#endif
|
||||
|
||||
char *
|
||||
grub_get_prefix (const char *dir)
|
||||
{
|
||||
char *saved_cwd;
|
||||
char *abs_dir, *prev_dir;
|
||||
char *prefix;
|
||||
struct stat st, prev_st;
|
||||
|
||||
/* Save the current directory. */
|
||||
saved_cwd = xgetcwd ();
|
||||
|
||||
if (chdir (dir) < 0)
|
||||
grub_util_error ("cannot change directory to `%s'", dir);
|
||||
|
||||
abs_dir = xgetcwd ();
|
||||
strip_extra_slashes (abs_dir);
|
||||
prev_dir = xstrdup (abs_dir);
|
||||
|
||||
if (stat (".", &prev_st) < 0)
|
||||
grub_util_error ("cannot stat `%s'", dir);
|
||||
|
||||
if (! S_ISDIR (prev_st.st_mode))
|
||||
grub_util_error ("`%s' is not a directory", dir);
|
||||
|
||||
while (1)
|
||||
{
|
||||
if (chdir ("..") < 0)
|
||||
grub_util_error ("cannot change directory to the parent");
|
||||
|
||||
if (stat (".", &st) < 0)
|
||||
grub_util_error ("cannot stat current directory");
|
||||
|
||||
if (! S_ISDIR (st.st_mode))
|
||||
grub_util_error ("current directory is not a directory???");
|
||||
|
||||
if (prev_st.st_dev != st.st_dev || prev_st.st_ino == st.st_ino)
|
||||
break;
|
||||
|
||||
free (prev_dir);
|
||||
prev_dir = xgetcwd ();
|
||||
prev_st = st;
|
||||
}
|
||||
|
||||
strip_extra_slashes (prev_dir);
|
||||
prefix = xmalloc (strlen (abs_dir) - strlen (prev_dir) + 2);
|
||||
prefix[0] = '/';
|
||||
strcpy (prefix + 1, abs_dir + strlen (prev_dir));
|
||||
strip_extra_slashes (prefix);
|
||||
|
||||
if (chdir (saved_cwd) < 0)
|
||||
grub_util_error ("cannot change directory to `%s'", dir);
|
||||
|
||||
#ifdef __CYGWIN__
|
||||
if (st.st_dev != (DEV_CYGDRIVE_MAJOR << 16))
|
||||
{
|
||||
/* Reached some mount point not below /cygdrive.
|
||||
GRUB does not know Cygwin's emulated mounts,
|
||||
convert to Win32 path. */
|
||||
grub_util_info ("Cygwin prefix = %s", prefix);
|
||||
char * wprefix = get_win32_path (prefix);
|
||||
free (prefix);
|
||||
prefix = wprefix;
|
||||
}
|
||||
#endif
|
||||
|
||||
free (saved_cwd);
|
||||
free (abs_dir);
|
||||
free (prev_dir);
|
||||
|
||||
grub_util_info ("prefix = %s", prefix);
|
||||
return prefix;
|
||||
}
|
||||
|
||||
#ifdef __MINGW32__
|
||||
|
||||
static char *
|
||||
|
@ -641,4 +546,3 @@ grub_util_check_char_device (const char *blk_dev)
|
|||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -246,7 +246,7 @@ main (int argc, char *argv[])
|
|||
if (strcmp (root_dev, "host") == 0)
|
||||
dir = xstrdup (dir);
|
||||
else
|
||||
dir = grub_get_prefix (dir);
|
||||
dir = grub_make_system_path_relative_to_its_root (dir);
|
||||
prefix = xmalloc (strlen (root_dev) + 2 + strlen (dir) + 1);
|
||||
sprintf (prefix, "(%s)%s", root_dev, dir);
|
||||
free (dir);
|
||||
|
|
103
kern/emu/misc.c
103
kern/emu/misc.c
|
@ -3,8 +3,12 @@
|
|||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <grub/mm.h>
|
||||
#include <grub/err.h>
|
||||
|
@ -171,3 +175,102 @@ grub_get_rtc (void)
|
|||
+ (((tv.tv_sec % GRUB_TICKS_PER_SECOND) * 1000000 + tv.tv_usec)
|
||||
* GRUB_TICKS_PER_SECOND / 1000000));
|
||||
}
|
||||
|
||||
|
||||
/* 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;
|
||||
uintptr_t offset = 0;
|
||||
dev_t num;
|
||||
size_t len;
|
||||
|
||||
/* canonicalize. */
|
||||
p = canonicalize_file_name (path);
|
||||
|
||||
if (p == NULL)
|
||||
grub_util_error ("failed to get canonical path of %s", path);
|
||||
|
||||
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 ("FIXME: no / in buf. (make_system_path_relative_to_its_root)");
|
||||
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);
|
||||
free (buf2);
|
||||
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);
|
||||
free (buf2);
|
||||
|
||||
#ifdef __CYGWIN__
|
||||
if (st.st_dev != (DEV_CYGDRIVE_MAJOR << 16))
|
||||
{
|
||||
/* Reached some mount point not below /cygdrive.
|
||||
GRUB does not know Cygwin's emulated mounts,
|
||||
convert to Win32 path. */
|
||||
grub_util_info ("Cygwin path = %s\n", buf3);
|
||||
char * temp = get_win32_path (buf3);
|
||||
free (buf3);
|
||||
buf3 = temp;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Remove trailing slashes, return empty string if root directory. */
|
||||
len = strlen (buf3);
|
||||
while (len > 0 && buf3[len - 1] == '/')
|
||||
{
|
||||
buf3[len - 1] = '\0';
|
||||
len--;
|
||||
}
|
||||
|
||||
return buf3;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue