paths.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. paths to configuration files
  3. Copyright (C) 2010-2025
  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 "lib/global.h"
  23. #include "lib/fileloc.h"
  24. #include "lib/vfs/vfs.h"
  25. #include "lib/util.h" /* unix_error_string() */
  26. #include "lib/mcconfig.h"
  27. /*** global variables ****************************************************************************/
  28. /*** file scope macro definitions ****************************************************************/
  29. /*** file scope type declarations ****************************************************************/
  30. /*** forward declarations (file scope functions) *************************************************/
  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. /* *INDENT-OFF* */
  43. /* config */
  44. { &mc_config_str, MC_CONFIG_FILE },
  45. { &mc_config_str, MC_FHL_INI_FILE },
  46. { &mc_config_str, MC_HOTLIST_FILE },
  47. { &mc_config_str, GLOBAL_KEYMAP_FILE },
  48. { &mc_config_str, MC_USERMENU_FILE },
  49. { &mc_config_str, EDIT_HOME_MENU },
  50. { &mc_config_str, MC_PANELS_FILE },
  51. /* User should move this file with applying some changes in file */
  52. { &mc_config_str, MC_EXT_FILE },
  53. { &mc_config_str, MC_EXT_OLD_FILE },
  54. /* data */
  55. { &mc_data_str, MC_SKINS_DIR },
  56. { &mc_data_str, VFS_SHELL_PREFIX },
  57. { &mc_data_str, MC_ASHRC_FILE },
  58. { &mc_data_str, MC_KSHRC_FILE },
  59. { &mc_data_str, MC_MKSHRC_FILE },
  60. { &mc_data_str, MC_BASHRC_FILE },
  61. { &mc_data_str, MC_INPUTRC_FILE },
  62. { &mc_data_str, MC_ZSHRC_FILE },
  63. { &mc_data_str, MC_EXTFS_DIR },
  64. { &mc_data_str, MC_HISTORY_FILE },
  65. { &mc_data_str, MC_FILEPOS_FILE },
  66. { &mc_data_str, EDIT_SYNTAX_FILE },
  67. { &mc_data_str, EDIT_HOME_CLIP_FILE },
  68. { &mc_data_str, MC_MACRO_FILE },
  69. /* cache */
  70. { &mc_cache_str, "mc.log" },
  71. { &mc_cache_str, MC_TREESTORE_FILE },
  72. { &mc_cache_str, EDIT_HOME_TEMP_FILE },
  73. { &mc_cache_str, EDIT_HOME_BLOCK_FILE },
  74. { NULL, NULL }
  75. /* *INDENT-ON* */
  76. };
  77. /* --------------------------------------------------------------------------------------------- */
  78. /*** file scope functions *********************************************************************** */
  79. /* --------------------------------------------------------------------------------------------- */
  80. static void
  81. mc_config_mkdir (const char *directory_name, GError **mcerror)
  82. {
  83. mc_return_if_error (mcerror);
  84. if ((!g_file_test (directory_name, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) &&
  85. (g_mkdir_with_parents (directory_name, 0700) != 0))
  86. mc_propagate_error (mcerror, 0, _("Cannot create %s directory"), directory_name);
  87. }
  88. /* --------------------------------------------------------------------------------------------- */
  89. static char *
  90. mc_config_init_one_config_path (const char *path_base, const char *subdir, GError **mcerror)
  91. {
  92. char *full_path;
  93. mc_return_val_if_error (mcerror, FALSE);
  94. full_path = g_build_filename (path_base, subdir, (char *) NULL);
  95. if (g_file_test (full_path, G_FILE_TEST_EXISTS))
  96. {
  97. if (g_file_test (full_path, G_FILE_TEST_IS_DIR))
  98. config_dir_present = TRUE;
  99. else
  100. {
  101. fprintf (stderr, "%s %s\n", _("FATAL: not a directory:"), full_path);
  102. exit (EXIT_FAILURE);
  103. }
  104. }
  105. mc_config_mkdir (full_path, mcerror);
  106. if (mcerror != NULL && *mcerror != NULL)
  107. MC_PTR_FREE (full_path);
  108. return full_path;
  109. }
  110. /* --------------------------------------------------------------------------------------------- */
  111. /*** public functions ****************************************************************************/
  112. /* --------------------------------------------------------------------------------------------- */
  113. void
  114. mc_config_init_config_paths (GError **mcerror)
  115. {
  116. const char *profile_root;
  117. char *dir;
  118. mc_return_if_error (mcerror);
  119. if (xdg_vars_initialized)
  120. return;
  121. profile_root = mc_get_profile_root ();
  122. if (strcmp (profile_root, mc_config_get_home_dir ()) != 0)
  123. {
  124. /*
  125. * The user overrode the default profile root.
  126. *
  127. * In this case we can't use GLib's g_get_user_{config,cache,data}_dir()
  128. * as these functions use the user's home dir as the root.
  129. */
  130. dir = g_build_filename (profile_root, ".config", (char *) NULL);
  131. mc_config_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
  132. g_free (dir);
  133. dir = g_build_filename (profile_root, ".cache", (char *) NULL);
  134. mc_cache_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
  135. g_free (dir);
  136. dir = g_build_filename (profile_root, ".local", "share", (char *) NULL);
  137. mc_data_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
  138. g_free (dir);
  139. }
  140. else
  141. {
  142. mc_config_str =
  143. mc_config_init_one_config_path (g_get_user_config_dir (), MC_USERCONF_DIR, mcerror);
  144. mc_cache_str =
  145. mc_config_init_one_config_path (g_get_user_cache_dir (), MC_USERCONF_DIR, mcerror);
  146. mc_data_str =
  147. mc_config_init_one_config_path (g_get_user_data_dir (), MC_USERCONF_DIR, mcerror);
  148. }
  149. xdg_vars_initialized = TRUE;
  150. }
  151. /* --------------------------------------------------------------------------------------------- */
  152. void
  153. mc_config_deinit_config_paths (void)
  154. {
  155. if (!xdg_vars_initialized)
  156. return;
  157. g_free (mc_config_str);
  158. g_free (mc_cache_str);
  159. g_free (mc_data_str);
  160. g_free (mc_global.share_data_dir);
  161. g_free (mc_global.sysconfig_dir);
  162. xdg_vars_initialized = FALSE;
  163. }
  164. /* --------------------------------------------------------------------------------------------- */
  165. const char *
  166. mc_config_get_data_path (void)
  167. {
  168. if (!xdg_vars_initialized)
  169. mc_config_init_config_paths (NULL);
  170. return (const char *) mc_data_str;
  171. }
  172. /* --------------------------------------------------------------------------------------------- */
  173. const char *
  174. mc_config_get_cache_path (void)
  175. {
  176. if (!xdg_vars_initialized)
  177. mc_config_init_config_paths (NULL);
  178. return (const char *) mc_cache_str;
  179. }
  180. /* --------------------------------------------------------------------------------------------- */
  181. const char *
  182. mc_config_get_home_dir (void)
  183. {
  184. static const char *homedir = NULL;
  185. if (homedir == NULL)
  186. {
  187. /* Prior to GLib 2.36, g_get_home_dir() ignores $HOME, which is why
  188. * we read it ourselves. As that function's documentation explains,
  189. * using $HOME is good for compatibility with other programs and
  190. * for running from test frameworks. */
  191. homedir = g_getenv ("HOME");
  192. if (homedir == NULL || *homedir == '\0')
  193. homedir = g_get_home_dir ();
  194. }
  195. return homedir;
  196. }
  197. /* --------------------------------------------------------------------------------------------- */
  198. const char *
  199. mc_config_get_path (void)
  200. {
  201. if (!xdg_vars_initialized)
  202. mc_config_init_config_paths (NULL);
  203. return (const char *) mc_config_str;
  204. }
  205. /* --------------------------------------------------------------------------------------------- */
  206. /**
  207. * Get full path to config file by short name.
  208. *
  209. * @param config_name short name
  210. * @return full path to config file
  211. */
  212. char *
  213. mc_config_get_full_path (const char *config_name)
  214. {
  215. size_t rule_index;
  216. if (config_name == NULL)
  217. return NULL;
  218. if (!xdg_vars_initialized)
  219. mc_config_init_config_paths (NULL);
  220. for (rule_index = 0; mc_config_files_reference[rule_index].filename != NULL; rule_index++)
  221. if (strcmp (config_name, mc_config_files_reference[rule_index].filename) == 0)
  222. return g_build_filename (*mc_config_files_reference[rule_index].basedir,
  223. mc_config_files_reference[rule_index].filename, (char *) NULL);
  224. return NULL;
  225. }
  226. /* --------------------------------------------------------------------------------------------- */
  227. /**
  228. * Get full path to config file by short name.
  229. *
  230. * @param config_name short name
  231. * @return object with full path to config file
  232. */
  233. vfs_path_t *
  234. mc_config_get_full_vpath (const char *config_name)
  235. {
  236. vfs_path_t *ret_vpath;
  237. char *str_path;
  238. str_path = mc_config_get_full_path (config_name);
  239. ret_vpath = vfs_path_from_str (str_path);
  240. g_free (str_path);
  241. return ret_vpath;
  242. }
  243. /* --------------------------------------------------------------------------------------------- */