paths.c 9.4 KB

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