2009-11-04 Felix Zielcke <fzielcke@z-51.de>
* util//grub-mkconfig_lib.in (bindir): New variable. (grub_mkrelpath): Likewise. Properly set path variable. Use ${grub_mkrelpath} instead of calling it directly. 2009-11-02 Felix Zielcke <fzielcke@z-51.de> * util/probe.c (probe): Make the file path relative to its root. Change a info message to use the GRUB path. Enable again the check if we can read the file with GRUB facilities. 2009-11-01 Felix Zielcke <fzielcke@z-51.de> * util/grub-mkrelpath.c: New file. * conf/common.rmk (bin_UTILITIES): Add grub-mkrelpath. (grub_mkrelpath_SOURCES): New variable. * include/grub/util/misc.h: New function prototype. * util/misc.c (make_system_path_relative_to_its_root): New function. * util/grub-mkconfig_lib.in (make_system_path_relative_to_its_root): Use grub-mkrelpath.
This commit is contained in:
parent
c926e1d585
commit
50ceeb7cb5
7 changed files with 219 additions and 50 deletions
81
util/misc.c
81
util/misc.c
|
@ -18,10 +18,12 @@
|
|||
|
||||
#include <config.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <setjmp.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
@ -449,3 +451,82 @@ fail:
|
|||
}
|
||||
|
||||
#endif /* __MINGW32__ */
|
||||
|
||||
/* This function never prints trailing slashes (so that its output
|
||||
can be appended a slash unconditionally). */
|
||||
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;
|
||||
|
||||
/* canonicalize. */
|
||||
p = realpath (path, NULL);
|
||||
|
||||
if (p == 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 = strdup (p);
|
||||
free (p);
|
||||
|
||||
if (stat (buf, &st) < 0)
|
||||
grub_util_error ("can not stat %s: %s", buf, strerror (errno));
|
||||
|
||||
buf2 = strdup (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 ("can not stat %s: %s", buf, strerror (errno));
|
||||
|
||||
/* buf is another filesystem; we found it. */
|
||||
if (st.st_dev != num)
|
||||
break;
|
||||
|
||||
offset = p - buf;
|
||||
/* offset == 1 means root directory. */
|
||||
if (offset == 1)
|
||||
{
|
||||
free (buf);
|
||||
len = strlen (buf2);
|
||||
while (buf2[len - 1] == '/' && len > 1)
|
||||
{
|
||||
buf2[len - 1] = '\0';
|
||||
len--;
|
||||
}
|
||||
return buf2;
|
||||
}
|
||||
}
|
||||
free (buf);
|
||||
buf3 = strdup (buf2 + offset);
|
||||
free (buf2);
|
||||
|
||||
len = strlen (buf3);
|
||||
while (buf2[len - 1] == '/' && len > 1)
|
||||
{
|
||||
buf2[len - 1] = '\0';
|
||||
len--;
|
||||
}
|
||||
|
||||
return buf3;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue