pathstuff.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /* Path manipulation routines for GDB and gdbserver.
  2. Copyright (C) 1986-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "common-defs.h"
  15. #include "pathstuff.h"
  16. #include "host-defs.h"
  17. #include "filenames.h"
  18. #include "gdb_tilde_expand.h"
  19. #ifdef USE_WIN32API
  20. #include <windows.h>
  21. #endif
  22. /* See gdbsupport/pathstuff.h. */
  23. char *current_directory;
  24. /* See gdbsupport/pathstuff.h. */
  25. gdb::unique_xmalloc_ptr<char>
  26. gdb_realpath (const char *filename)
  27. {
  28. /* On most hosts, we rely on canonicalize_file_name to compute
  29. the FILENAME's realpath.
  30. But the situation is slightly more complex on Windows, due to some
  31. versions of GCC which were reported to generate paths where
  32. backlashes (the directory separator) were doubled. For instance:
  33. c:\\some\\double\\slashes\\dir
  34. ... instead of ...
  35. c:\some\double\slashes\dir
  36. Those double-slashes were getting in the way when comparing paths,
  37. for instance when trying to insert a breakpoint as follow:
  38. (gdb) b c:/some/double/slashes/dir/foo.c:4
  39. No source file named c:/some/double/slashes/dir/foo.c:4.
  40. (gdb) b c:\some\double\slashes\dir\foo.c:4
  41. No source file named c:\some\double\slashes\dir\foo.c:4.
  42. To prevent this from happening, we need this function to always
  43. strip those extra backslashes. While canonicalize_file_name does
  44. perform this simplification, it only works when the path is valid.
  45. Since the simplification would be useful even if the path is not
  46. valid (one can always set a breakpoint on a file, even if the file
  47. does not exist locally), we rely instead on GetFullPathName to
  48. perform the canonicalization. */
  49. #if defined (_WIN32)
  50. {
  51. char buf[MAX_PATH];
  52. DWORD len = GetFullPathName (filename, MAX_PATH, buf, NULL);
  53. /* The file system is case-insensitive but case-preserving.
  54. So it is important we do not lowercase the path. Otherwise,
  55. we might not be able to display the original casing in a given
  56. path. */
  57. if (len > 0 && len < MAX_PATH)
  58. return make_unique_xstrdup (buf);
  59. }
  60. #else
  61. {
  62. char *rp = canonicalize_file_name (filename);
  63. if (rp != NULL)
  64. return gdb::unique_xmalloc_ptr<char> (rp);
  65. }
  66. #endif
  67. /* This system is a lost cause, just dup the buffer. */
  68. return make_unique_xstrdup (filename);
  69. }
  70. /* See gdbsupport/pathstuff.h. */
  71. gdb::unique_xmalloc_ptr<char>
  72. gdb_realpath_keepfile (const char *filename)
  73. {
  74. const char *base_name = lbasename (filename);
  75. char *dir_name;
  76. char *result;
  77. /* Extract the basename of filename, and return immediately
  78. a copy of filename if it does not contain any directory prefix. */
  79. if (base_name == filename)
  80. return make_unique_xstrdup (filename);
  81. dir_name = (char *) alloca ((size_t) (base_name - filename + 2));
  82. /* Allocate enough space to store the dir_name + plus one extra
  83. character sometimes needed under Windows (see below), and
  84. then the closing \000 character. */
  85. strncpy (dir_name, filename, base_name - filename);
  86. dir_name[base_name - filename] = '\000';
  87. #ifdef HAVE_DOS_BASED_FILE_SYSTEM
  88. /* We need to be careful when filename is of the form 'd:foo', which
  89. is equivalent of d:./foo, which is totally different from d:/foo. */
  90. if (strlen (dir_name) == 2 && isalpha (dir_name[0]) && dir_name[1] == ':')
  91. {
  92. dir_name[2] = '.';
  93. dir_name[3] = '\000';
  94. }
  95. #endif
  96. /* Canonicalize the directory prefix, and build the resulting
  97. filename. If the dirname realpath already contains an ending
  98. directory separator, avoid doubling it. */
  99. gdb::unique_xmalloc_ptr<char> path_storage = gdb_realpath (dir_name);
  100. const char *real_path = path_storage.get ();
  101. if (IS_DIR_SEPARATOR (real_path[strlen (real_path) - 1]))
  102. result = concat (real_path, base_name, (char *) NULL);
  103. else
  104. result = concat (real_path, SLASH_STRING, base_name, (char *) NULL);
  105. return gdb::unique_xmalloc_ptr<char> (result);
  106. }
  107. /* See gdbsupport/pathstuff.h. */
  108. gdb::unique_xmalloc_ptr<char>
  109. gdb_abspath (const char *path)
  110. {
  111. gdb_assert (path != NULL && path[0] != '\0');
  112. if (path[0] == '~')
  113. return gdb_tilde_expand_up (path);
  114. if (IS_ABSOLUTE_PATH (path) || current_directory == NULL)
  115. return make_unique_xstrdup (path);
  116. /* Beware the // my son, the Emacs barfs, the botch that catch... */
  117. return gdb::unique_xmalloc_ptr<char>
  118. (concat (current_directory,
  119. IS_DIR_SEPARATOR (current_directory[strlen (current_directory) - 1])
  120. ? "" : SLASH_STRING,
  121. path, (char *) NULL));
  122. }
  123. /* See gdbsupport/pathstuff.h. */
  124. const char *
  125. child_path (const char *parent, const char *child)
  126. {
  127. /* The child path must start with the parent path. */
  128. size_t parent_len = strlen (parent);
  129. if (filename_ncmp (parent, child, parent_len) != 0)
  130. return NULL;
  131. /* The parent path must be a directory and the child must contain at
  132. least one component underneath the parent. */
  133. const char *child_component;
  134. if (parent_len > 0 && IS_DIR_SEPARATOR (parent[parent_len - 1]))
  135. {
  136. /* The parent path ends in a directory separator, so it is a
  137. directory. The first child component starts after the common
  138. prefix. */
  139. child_component = child + parent_len;
  140. }
  141. else
  142. {
  143. /* The parent path does not end in a directory separator. The
  144. first character in the child after the common prefix must be
  145. a directory separator.
  146. Note that CHILD must hold at least parent_len characters for
  147. filename_ncmp to return zero. If the character at parent_len
  148. is nul due to CHILD containing the same path as PARENT, the
  149. IS_DIR_SEPARATOR check will fail here. */
  150. if (!IS_DIR_SEPARATOR (child[parent_len]))
  151. return NULL;
  152. /* The first child component starts after the separator after the
  153. common prefix. */
  154. child_component = child + parent_len + 1;
  155. }
  156. /* The child must contain at least one non-separator character after
  157. the parent. */
  158. while (*child_component != '\0')
  159. {
  160. if (!IS_DIR_SEPARATOR (*child_component))
  161. return child_component;
  162. child_component++;
  163. }
  164. return NULL;
  165. }
  166. /* See gdbsupport/pathstuff.h. */
  167. bool
  168. contains_dir_separator (const char *path)
  169. {
  170. for (; *path != '\0'; path++)
  171. {
  172. if (IS_DIR_SEPARATOR (*path))
  173. return true;
  174. }
  175. return false;
  176. }
  177. /* See gdbsupport/pathstuff.h. */
  178. std::string
  179. get_standard_cache_dir ()
  180. {
  181. #ifdef __APPLE__
  182. #define HOME_CACHE_DIR "Library/Caches"
  183. #else
  184. #define HOME_CACHE_DIR ".cache"
  185. #endif
  186. #ifndef __APPLE__
  187. const char *xdg_cache_home = getenv ("XDG_CACHE_HOME");
  188. if (xdg_cache_home != NULL && xdg_cache_home[0] != '\0')
  189. {
  190. /* Make sure the path is absolute and tilde-expanded. */
  191. gdb::unique_xmalloc_ptr<char> abs (gdb_abspath (xdg_cache_home));
  192. return string_printf ("%s/gdb", abs.get ());
  193. }
  194. #endif
  195. const char *home = getenv ("HOME");
  196. if (home != NULL && home[0] != '\0')
  197. {
  198. /* Make sure the path is absolute and tilde-expanded. */
  199. gdb::unique_xmalloc_ptr<char> abs (gdb_abspath (home));
  200. return string_printf ("%s/" HOME_CACHE_DIR "/gdb", abs.get ());
  201. }
  202. #ifdef WIN32
  203. const char *win_home = getenv ("LOCALAPPDATA");
  204. if (win_home != NULL && win_home[0] != '\0')
  205. {
  206. /* Make sure the path is absolute and tilde-expanded. */
  207. gdb::unique_xmalloc_ptr<char> abs (gdb_abspath (win_home));
  208. return string_printf ("%s/gdb", abs.get ());
  209. }
  210. #endif
  211. return {};
  212. }
  213. /* See gdbsupport/pathstuff.h. */
  214. std::string
  215. get_standard_temp_dir ()
  216. {
  217. #ifdef WIN32
  218. const char *tmp = getenv ("TMP");
  219. if (tmp != nullptr)
  220. return tmp;
  221. tmp = getenv ("TEMP");
  222. if (tmp != nullptr)
  223. return tmp;
  224. error (_("Couldn't find temp dir path, both TMP and TEMP are unset."));
  225. #else
  226. const char *tmp = getenv ("TMPDIR");
  227. if (tmp != nullptr)
  228. return tmp;
  229. return "/tmp";
  230. #endif
  231. }
  232. /* See pathstuff.h. */
  233. std::string
  234. get_standard_config_dir ()
  235. {
  236. #ifdef __APPLE__
  237. #define HOME_CONFIG_DIR "Library/Preferences"
  238. #else
  239. #define HOME_CONFIG_DIR ".config"
  240. #endif
  241. #ifndef __APPLE__
  242. const char *xdg_config_home = getenv ("XDG_CONFIG_HOME");
  243. if (xdg_config_home != NULL && xdg_config_home[0] != '\0')
  244. {
  245. /* Make sure the path is absolute and tilde-expanded. */
  246. gdb::unique_xmalloc_ptr<char> abs (gdb_abspath (xdg_config_home));
  247. return string_printf ("%s/gdb", abs.get ());
  248. }
  249. #endif
  250. const char *home = getenv ("HOME");
  251. if (home != NULL && home[0] != '\0')
  252. {
  253. /* Make sure the path is absolute and tilde-expanded. */
  254. gdb::unique_xmalloc_ptr<char> abs (gdb_abspath (home));
  255. return string_printf ("%s/" HOME_CONFIG_DIR "/gdb", abs.get ());
  256. }
  257. return {};
  258. }
  259. /* See pathstuff.h. */
  260. std::string
  261. get_standard_config_filename (const char *filename)
  262. {
  263. std::string config_dir = get_standard_config_dir ();
  264. if (config_dir != "")
  265. {
  266. const char *tmp = (*filename == '.') ? (filename + 1) : filename;
  267. std::string path = config_dir + SLASH_STRING + std::string (tmp);
  268. return path;
  269. }
  270. return {};
  271. }
  272. /* See pathstuff.h. */
  273. std::string
  274. find_gdb_home_config_file (const char *name, struct stat *buf)
  275. {
  276. gdb_assert (name != nullptr);
  277. gdb_assert (*name != '\0');
  278. std::string config_dir_file = get_standard_config_filename (name);
  279. if (!config_dir_file.empty ())
  280. {
  281. if (stat (config_dir_file.c_str (), buf) == 0)
  282. return config_dir_file;
  283. }
  284. const char *homedir = getenv ("HOME");
  285. if (homedir != nullptr && homedir[0] != '\0')
  286. {
  287. /* Make sure the path is absolute and tilde-expanded. */
  288. gdb::unique_xmalloc_ptr<char> abs (gdb_abspath (homedir));
  289. std::string path = (std::string (abs.get ()) + SLASH_STRING
  290. + std::string (name));
  291. if (stat (path.c_str (), buf) == 0)
  292. return path;
  293. }
  294. return {};
  295. }
  296. /* See gdbsupport/pathstuff.h. */
  297. const char *
  298. get_shell ()
  299. {
  300. const char *ret = getenv ("SHELL");
  301. if (ret == NULL)
  302. ret = "/bin/sh";
  303. return ret;
  304. }
  305. /* See gdbsupport/pathstuff.h. */
  306. gdb::char_vector
  307. make_temp_filename (const std::string &f)
  308. {
  309. gdb::char_vector filename_temp (f.length () + 8);
  310. strcpy (filename_temp.data (), f.c_str ());
  311. strcat (filename_temp.data () + f.size (), "-XXXXXX");
  312. return filename_temp;
  313. }