relpath.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 1999,2000,2001,2002,2003,2006,2007,2008,2009,2010,2011,2012,2013 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <config-util.h>
  19. #include <config.h>
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <unistd.h>
  23. #include <stdint.h>
  24. #include <string.h>
  25. #include <errno.h>
  26. #include <grub/util/misc.h>
  27. #include <grub/emu/hostdisk.h>
  28. #include <grub/emu/getroot.h>
  29. #include <grub/mm.h>
  30. /* This function never prints trailing slashes (so that its output
  31. can be appended a slash unconditionally). */
  32. char *
  33. grub_make_system_path_relative_to_its_root (const char *path)
  34. {
  35. struct stat st;
  36. char *p, *buf, *buf2, *buf3, *ret;
  37. uintptr_t offset = 0;
  38. dev_t num;
  39. size_t len;
  40. char *poolfs = NULL;
  41. /* canonicalize. */
  42. p = grub_canonicalize_file_name (path);
  43. if (p == NULL)
  44. grub_util_error (_("failed to get canonical path of `%s'"), path);
  45. #ifdef __linux__
  46. ret = grub_make_system_path_relative_to_its_root_os (p);
  47. if (ret)
  48. {
  49. free (p);
  50. return ret;
  51. }
  52. #endif
  53. /* For ZFS sub-pool filesystems. */
  54. #ifndef __HAIKU__
  55. {
  56. char *dummy;
  57. grub_find_zpool_from_dir (p, &dummy, &poolfs);
  58. }
  59. #endif
  60. len = strlen (p) + 1;
  61. buf = xstrdup (p);
  62. free (p);
  63. if (stat (buf, &st) < 0)
  64. grub_util_error (_("cannot stat `%s': %s"), buf, strerror (errno));
  65. buf2 = xstrdup (buf);
  66. num = st.st_dev;
  67. /* This loop sets offset to the number of chars of the root
  68. directory we're inspecting. */
  69. while (1)
  70. {
  71. p = strrchr (buf, '/');
  72. if (p == NULL)
  73. /* This should never happen. */
  74. grub_util_error ("%s",
  75. /* TRANSLATORS: canonical pathname is the
  76. complete one e.g. /etc/fstab. It has
  77. to contain `/' normally, if it doesn't
  78. we're in trouble and throw this error. */
  79. _("no `/' in canonical filename"));
  80. if (p != buf)
  81. *p = 0;
  82. else
  83. *++p = 0;
  84. if (stat (buf, &st) < 0)
  85. grub_util_error (_("cannot stat `%s': %s"), buf, strerror (errno));
  86. /* buf is another filesystem; we found it. */
  87. if (st.st_dev != num)
  88. {
  89. /* offset == 0 means path given is the mount point.
  90. This works around special-casing of "/" in Un*x. This function never
  91. prints trailing slashes (so that its output can be appended a slash
  92. unconditionally). Each slash in is considered a preceding slash, and
  93. therefore the root directory is an empty string. */
  94. if (offset == 0)
  95. {
  96. free (buf);
  97. free (buf2);
  98. if (poolfs)
  99. return xasprintf ("/%s/@", poolfs);
  100. return xstrdup ("");
  101. }
  102. else
  103. break;
  104. }
  105. offset = p - buf;
  106. /* offset == 1 means root directory. */
  107. if (offset == 1)
  108. {
  109. /* Include leading slash. */
  110. offset = 0;
  111. break;
  112. }
  113. }
  114. free (buf);
  115. buf3 = xstrdup (buf2 + offset);
  116. buf2[offset] = 0;
  117. free (buf2);
  118. /* Remove trailing slashes, return empty string if root directory. */
  119. len = strlen (buf3);
  120. while (len > 0 && buf3[len - 1] == '/')
  121. {
  122. buf3[len - 1] = '\0';
  123. len--;
  124. }
  125. if (poolfs)
  126. {
  127. ret = xasprintf ("/%s/@%s", poolfs, buf3);
  128. free (buf3);
  129. }
  130. else
  131. ret = buf3;
  132. return ret;
  133. }