paths.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. paths to configuration files
  3. Copyright (C) 2010-2023
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2010.
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software: you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, either version 3 of the License,
  11. or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <config.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <errno.h>
  23. #include "lib/global.h"
  24. #include "lib/fileloc.h"
  25. #include "lib/vfs/vfs.h"
  26. #include "lib/util.h" /* unix_error_string() */
  27. #include "lib/mcconfig.h"
  28. /*** global variables ****************************************************************************/
  29. /*** file scope macro definitions ****************************************************************/
  30. /*** file scope type declarations ****************************************************************/
  31. /*** file scope variables ************************************************************************/
  32. static gboolean xdg_vars_initialized = FALSE;
  33. static char *mc_config_str = NULL;
  34. static char *mc_cache_str = NULL;
  35. static char *mc_data_str = NULL;
  36. static gboolean config_dir_present = FALSE;
  37. static const struct
  38. {
  39. char **basedir;
  40. const char *filename;
  41. } mc_config_files_reference[] =
  42. {
  43. /* *INDENT-OFF* */
  44. /* config */
  45. { &mc_config_str, MC_CONFIG_FILE },
  46. { &mc_config_str, MC_FHL_INI_FILE },
  47. { &mc_config_str, MC_HOTLIST_FILE },
  48. { &mc_config_str, GLOBAL_KEYMAP_FILE },
  49. { &mc_config_str, MC_USERMENU_FILE },
  50. { &mc_config_str, EDIT_HOME_MENU },
  51. { &mc_config_str, MC_PANELS_FILE },
  52. /* User should move this file with applying some changes in file */
  53. { &mc_config_str, MC_EXT_FILE },
  54. { &mc_config_str, MC_EXT_OLD_FILE },
  55. /* data */
  56. { &mc_data_str, MC_SKINS_DIR },
  57. { &mc_data_str, FISH_PREFIX },
  58. { &mc_data_str, MC_ASHRC_FILE },
  59. { &mc_data_str, MC_BASHRC_FILE },
  60. { &mc_data_str, MC_INPUTRC_FILE },
  61. { &mc_data_str, MC_ZSHRC_FILE },
  62. { &mc_data_str, MC_EXTFS_DIR },
  63. { &mc_data_str, MC_HISTORY_FILE },
  64. { &mc_data_str, MC_FILEPOS_FILE },
  65. { &mc_data_str, EDIT_SYNTAX_FILE },
  66. { &mc_data_str, EDIT_HOME_CLIP_FILE },
  67. { &mc_data_str, MC_MACRO_FILE },
  68. /* cache */
  69. { &mc_cache_str, "mc.log" },
  70. { &mc_cache_str, MC_TREESTORE_FILE },
  71. { &mc_cache_str, EDIT_HOME_TEMP_FILE },
  72. { &mc_cache_str, EDIT_HOME_BLOCK_FILE },
  73. { NULL, NULL }
  74. /* *INDENT-ON* */
  75. };
  76. /* --------------------------------------------------------------------------------------------- */
  77. /*** file scope functions *********************************************************************** */
  78. /* --------------------------------------------------------------------------------------------- */
  79. static void
  80. mc_config_mkdir (const char *directory_name, GError ** mcerror)
  81. {
  82. mc_return_if_error (mcerror);
  83. if ((!g_file_test (directory_name, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) &&
  84. (g_mkdir_with_parents (directory_name, 0700) != 0))
  85. mc_propagate_error (mcerror, 0, _("Cannot create %s directory"), directory_name);
  86. }
  87. /* --------------------------------------------------------------------------------------------- */
  88. static char *
  89. mc_config_init_one_config_path (const char *path_base, const char *subdir, GError ** mcerror)
  90. {
  91. char *full_path;
  92. mc_return_val_if_error (mcerror, FALSE);
  93. full_path = g_build_filename (path_base, subdir, (char *) NULL);
  94. if (g_file_test (full_path, G_FILE_TEST_EXISTS))
  95. {
  96. if (g_file_test (full_path, G_FILE_TEST_IS_DIR))
  97. config_dir_present = TRUE;
  98. else
  99. {
  100. fprintf (stderr, "%s %s\n", _("FATAL: not a directory:"), full_path);
  101. exit (EXIT_FAILURE);
  102. }
  103. }
  104. mc_config_mkdir (full_path, mcerror);
  105. if (mcerror != NULL && *mcerror != NULL)
  106. MC_PTR_FREE (full_path);
  107. return full_path;
  108. }
  109. /* --------------------------------------------------------------------------------------------- */
  110. /*** public functions ****************************************************************************/
  111. /* --------------------------------------------------------------------------------------------- */
  112. void
  113. mc_config_init_config_paths (GError ** mcerror)
  114. {
  115. const char *profile_root;
  116. char *dir;
  117. mc_return_if_error (mcerror);
  118. if (xdg_vars_initialized)
  119. return;
  120. profile_root = mc_get_profile_root ();
  121. if (strcmp (profile_root, mc_config_get_home_dir ()) != 0)
  122. {
  123. /*
  124. * The user overrode the default profile root.
  125. *
  126. * In this case we can't use GLib's g_get_user_{config,cache,data}_dir()
  127. * as these functions use the user's home dir as the root.
  128. */
  129. dir = g_build_filename (profile_root, ".config", (char *) NULL);
  130. mc_config_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
  131. g_free (dir);
  132. dir = g_build_filename (profile_root, ".cache", (char *) NULL);
  133. mc_cache_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
  134. g_free (dir);
  135. dir = g_build_filename (profile_root, ".local", "share", (char *) NULL);
  136. mc_data_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
  137. g_free (dir);
  138. }
  139. else
  140. {
  141. mc_config_str =
  142. mc_config_init_one_config_path (g_get_user_config_dir (), MC_USERCONF_DIR, mcerror);
  143. mc_cache_str =
  144. mc_config_init_one_config_path (g_get_user_cache_dir (), MC_USERCONF_DIR, mcerror);
  145. mc_data_str =
  146. mc_config_init_one_config_path (g_get_user_data_dir (), MC_USERCONF_DIR, mcerror);
  147. }
  148. xdg_vars_initialized = TRUE;
  149. }
  150. /* --------------------------------------------------------------------------------------------- */
  151. void
  152. mc_config_deinit_config_paths (void)
  153. {
  154. if (!xdg_vars_initialized)
  155. return;
  156. g_free (mc_config_str);
  157. g_free (mc_cache_str);
  158. g_free (mc_data_str);
  159. g_free (mc_global.share_data_dir);
  160. g_free (mc_global.sysconfig_dir);
  161. xdg_vars_initialized = FALSE;
  162. }
  163. /* --------------------------------------------------------------------------------------------- */
  164. const char *
  165. mc_config_get_data_path (void)
  166. {
  167. if (!xdg_vars_initialized)
  168. mc_config_init_config_paths (NULL);
  169. return (const char *) mc_data_str;
  170. }
  171. /* --------------------------------------------------------------------------------------------- */
  172. const char *
  173. mc_config_get_cache_path (void)
  174. {
  175. if (!xdg_vars_initialized)
  176. mc_config_init_config_paths (NULL);
  177. return (const char *) mc_cache_str;
  178. }
  179. /* --------------------------------------------------------------------------------------------- */
  180. const char *
  181. mc_config_get_home_dir (void)
  182. {
  183. static const char *homedir = NULL;
  184. if (homedir == NULL)
  185. {
  186. /* Prior to GLib 2.36, g_get_home_dir() ignores $HOME, which is why
  187. * we read it ourselves. As that function's documentation explains,
  188. * using $HOME is good for compatibility with other programs and
  189. * for running from test frameworks. */
  190. homedir = g_getenv ("HOME");
  191. if (homedir == NULL || *homedir == '\0')
  192. homedir = g_get_home_dir ();
  193. }
  194. return homedir;
  195. }
  196. /* --------------------------------------------------------------------------------------------- */
  197. const char *
  198. mc_config_get_path (void)
  199. {
  200. if (!xdg_vars_initialized)
  201. mc_config_init_config_paths (NULL);
  202. return (const char *) mc_config_str;
  203. }
  204. /* --------------------------------------------------------------------------------------------- */
  205. /**
  206. * Get full path to config file by short name.
  207. *
  208. * @param config_name short name
  209. * @return full path to config file
  210. */
  211. char *
  212. mc_config_get_full_path (const char *config_name)
  213. {
  214. size_t rule_index;
  215. if (config_name == NULL)
  216. return NULL;
  217. if (!xdg_vars_initialized)
  218. mc_config_init_config_paths (NULL);
  219. for (rule_index = 0; mc_config_files_reference[rule_index].filename != NULL; rule_index++)
  220. if (strcmp (config_name, mc_config_files_reference[rule_index].filename) == 0)
  221. return g_build_filename (*mc_config_files_reference[rule_index].basedir,
  222. mc_config_files_reference[rule_index].filename, (char *) NULL);
  223. return NULL;
  224. }
  225. /* --------------------------------------------------------------------------------------------- */
  226. /**
  227. * Get full path to config file by short name.
  228. *
  229. * @param config_name short name
  230. * @return object with full path to config file
  231. */
  232. vfs_path_t *
  233. mc_config_get_full_vpath (const char *config_name)
  234. {
  235. vfs_path_t *ret_vpath;
  236. char *str_path;
  237. str_path = mc_config_get_full_path (config_name);
  238. ret_vpath = vfs_path_from_str (str_path);
  239. g_free (str_path);
  240. return ret_vpath;
  241. }
  242. /* --------------------------------------------------------------------------------------------- */