paths.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*
  2. paths to configuration files
  3. Copyright (C) 2010-2019
  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. #define MC_OLD_USERCONF_DIR ".mc"
  31. /*** file scope type declarations ****************************************************************/
  32. /*** file scope variables ************************************************************************/
  33. static gboolean xdg_vars_initialized = FALSE;
  34. static char *mc_config_str = NULL;
  35. static char *mc_cache_str = NULL;
  36. static char *mc_data_str = NULL;
  37. static gboolean config_dir_present = FALSE;
  38. static const struct
  39. {
  40. const char *old_filename;
  41. char **new_basedir;
  42. const char *new_filename;
  43. } mc_config_files_reference[] =
  44. {
  45. /* *INDENT-OFF* */
  46. /* config */
  47. { "ini", &mc_config_str, MC_CONFIG_FILE},
  48. { "filehighlight.ini", &mc_config_str, MC_FHL_INI_FILE},
  49. { "hotlist", &mc_config_str, MC_HOTLIST_FILE},
  50. { "mc.keymap", &mc_config_str, GLOBAL_KEYMAP_FILE},
  51. { "menu", &mc_config_str, MC_USERMENU_FILE},
  52. { "cedit" PATH_SEP_STR "Syntax", &mc_config_str, EDIT_SYNTAX_FILE},
  53. { "cedit" PATH_SEP_STR "menu", &mc_config_str, EDIT_HOME_MENU},
  54. { "cedit" PATH_SEP_STR "edit.indent.rc", &mc_config_str, EDIT_DIR PATH_SEP_STR "edit.indent.rc"},
  55. { "cedit" PATH_SEP_STR "edit.spell.rc", &mc_config_str, EDIT_DIR PATH_SEP_STR "edit.spell.rc"},
  56. { "panels.ini", &mc_config_str, MC_PANELS_FILE},
  57. /* User should move this file with applying some changes in file */
  58. { "", &mc_config_str, MC_FILEBIND_FILE},
  59. /* data */
  60. { "skins", &mc_data_str, MC_SKINS_SUBDIR},
  61. { "fish", &mc_data_str, FISH_PREFIX},
  62. { "ashrc", &mc_data_str, "ashrc"},
  63. { "bashrc", &mc_data_str, "bashrc"},
  64. { "inputrc", &mc_data_str, "inputrc"},
  65. { "extfs.d", &mc_data_str, MC_EXTFS_DIR},
  66. { "history", &mc_data_str, MC_HISTORY_FILE},
  67. { "filepos", &mc_data_str, MC_FILEPOS_FILE},
  68. { "cedit" PATH_SEP_STR "cooledit.clip", &mc_data_str, EDIT_CLIP_FILE},
  69. { "", &mc_data_str, MC_MACRO_FILE},
  70. /* cache */
  71. { "log", &mc_cache_str, "mc.log"},
  72. { "Tree", &mc_cache_str, MC_TREESTORE_FILE},
  73. { "cedit" PATH_SEP_STR "cooledit.temp", &mc_cache_str, EDIT_TEMP_FILE},
  74. { "cedit" PATH_SEP_STR "cooledit.block", &mc_cache_str, EDIT_BLOCK_FILE},
  75. {NULL, NULL, NULL}
  76. /* *INDENT-ON* */
  77. };
  78. #if MC_HOMEDIR_XDG
  79. static const struct
  80. {
  81. char **old_basedir;
  82. const char *filename;
  83. char **new_basedir;
  84. } mc_config_migrate_rules_fix[] =
  85. {
  86. /* *INDENT-OFF* */
  87. { &mc_data_str, MC_USERMENU_FILE, &mc_config_str},
  88. { &mc_data_str, EDIT_SYNTAX_FILE, &mc_config_str},
  89. { &mc_data_str, EDIT_HOME_MENU, &mc_config_str},
  90. { &mc_data_str, EDIT_DIR PATH_SEP_STR "edit.indent.rc", &mc_config_str},
  91. { &mc_data_str, EDIT_DIR PATH_SEP_STR "edit.spell.rc", &mc_config_str},
  92. { &mc_data_str, MC_FILEBIND_FILE, &mc_config_str},
  93. { &mc_cache_str, MC_HISTORY_FILE, &mc_data_str},
  94. { &mc_cache_str, MC_FILEPOS_FILE, &mc_data_str},
  95. { &mc_cache_str, EDIT_CLIP_FILE, &mc_data_str},
  96. { &mc_cache_str, MC_PANELS_FILE, &mc_config_str},
  97. {NULL, NULL, NULL}
  98. /* *INDENT-ON* */
  99. };
  100. #endif /* MC_HOMEDIR_XDG */
  101. /* --------------------------------------------------------------------------------------------- */
  102. /*** file scope functions *********************************************************************** */
  103. /* --------------------------------------------------------------------------------------------- */
  104. static void
  105. mc_config_mkdir (const char *directory_name, GError ** mcerror)
  106. {
  107. mc_return_if_error (mcerror);
  108. if ((!g_file_test (directory_name, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) &&
  109. (g_mkdir_with_parents (directory_name, 0700) != 0))
  110. mc_propagate_error (mcerror, 0, _("Cannot create %s directory"), directory_name);
  111. }
  112. /* --------------------------------------------------------------------------------------------- */
  113. static char *
  114. mc_config_init_one_config_path (const char *path_base, const char *subdir, GError ** mcerror)
  115. {
  116. char *full_path;
  117. mc_return_val_if_error (mcerror, FALSE);
  118. full_path = g_build_filename (path_base, subdir, (char *) NULL);
  119. if (g_file_test (full_path, G_FILE_TEST_EXISTS))
  120. {
  121. if (g_file_test (full_path, G_FILE_TEST_IS_DIR))
  122. config_dir_present = TRUE;
  123. else
  124. {
  125. fprintf (stderr, "%s %s\n", _("FATAL: not a directory:"), full_path);
  126. exit (EXIT_FAILURE);
  127. }
  128. }
  129. mc_config_mkdir (full_path, mcerror);
  130. if (mcerror != NULL && *mcerror != NULL)
  131. MC_PTR_FREE (full_path);
  132. return full_path;
  133. }
  134. /* --------------------------------------------------------------------------------------------- */
  135. static char *
  136. mc_config_get_deprecated_path (void)
  137. {
  138. return g_build_filename (mc_config_get_home_dir (), MC_OLD_USERCONF_DIR, (char *) NULL);
  139. }
  140. /* --------------------------------------------------------------------------------------------- */
  141. static void
  142. mc_config_copy (const char *old_name, const char *new_name, GError ** mcerror)
  143. {
  144. mc_return_if_error (mcerror);
  145. if (g_file_test (old_name, G_FILE_TEST_IS_REGULAR))
  146. {
  147. char *contents = NULL;
  148. size_t length;
  149. if (g_file_get_contents (old_name, &contents, &length, mcerror))
  150. g_file_set_contents (new_name, contents, length, mcerror);
  151. g_free (contents);
  152. return;
  153. }
  154. if (g_file_test (old_name, G_FILE_TEST_IS_DIR))
  155. {
  156. GDir *dir;
  157. const char *dir_name;
  158. dir = g_dir_open (old_name, 0, mcerror);
  159. if (dir == NULL)
  160. return;
  161. if (g_mkdir_with_parents (new_name, 0700) == -1)
  162. {
  163. g_dir_close (dir);
  164. mc_propagate_error (mcerror, 0,
  165. _("An error occurred while migrating user settings: %s"),
  166. unix_error_string (errno));
  167. return;
  168. }
  169. while ((dir_name = g_dir_read_name (dir)) != NULL)
  170. {
  171. char *old_name2, *new_name2;
  172. old_name2 = g_build_filename (old_name, dir_name, (char *) NULL);
  173. new_name2 = g_build_filename (new_name, dir_name, (char *) NULL);
  174. mc_config_copy (old_name2, new_name2, mcerror);
  175. g_free (new_name2);
  176. g_free (old_name2);
  177. }
  178. }
  179. }
  180. /* --------------------------------------------------------------------------------------------- */
  181. #if MC_HOMEDIR_XDG
  182. static void
  183. mc_config_fix_migrated_rules (void)
  184. {
  185. size_t rule_index;
  186. for (rule_index = 0; mc_config_migrate_rules_fix[rule_index].old_basedir != NULL; rule_index++)
  187. {
  188. char *old_name;
  189. old_name =
  190. g_build_filename (*mc_config_migrate_rules_fix[rule_index].old_basedir,
  191. mc_config_migrate_rules_fix[rule_index].filename, (char *) NULL);
  192. if (g_file_test (old_name, G_FILE_TEST_EXISTS))
  193. {
  194. char *new_name;
  195. const char *basedir = *mc_config_migrate_rules_fix[rule_index].new_basedir;
  196. const char *filename = mc_config_migrate_rules_fix[rule_index].filename;
  197. new_name = g_build_filename (basedir, filename, (char *) NULL);
  198. rename (old_name, new_name);
  199. g_free (new_name);
  200. }
  201. g_free (old_name);
  202. }
  203. }
  204. #endif /* MC_HOMEDIR_XDG */
  205. /* --------------------------------------------------------------------------------------------- */
  206. static gboolean
  207. mc_config_deprecated_dir_present (void)
  208. {
  209. char *old_dir;
  210. gboolean is_present;
  211. old_dir = mc_config_get_deprecated_path ();
  212. is_present = g_file_test (old_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR);
  213. g_free (old_dir);
  214. return is_present && !config_dir_present;
  215. }
  216. /* --------------------------------------------------------------------------------------------- */
  217. /*** public functions ****************************************************************************/
  218. /* --------------------------------------------------------------------------------------------- */
  219. void
  220. mc_config_init_config_paths (GError ** mcerror)
  221. {
  222. const char *profile_root;
  223. char *dir;
  224. #if MC_HOMEDIR_XDG == 0
  225. char *defined_userconf_dir;
  226. #endif
  227. mc_return_if_error (mcerror);
  228. if (xdg_vars_initialized)
  229. return;
  230. profile_root = mc_get_profile_root ();
  231. #if MC_HOMEDIR_XDG
  232. if (strcmp (profile_root, mc_config_get_home_dir ()) != 0)
  233. {
  234. /*
  235. * The user overrode the default profile root.
  236. *
  237. * In this case we can't use GLib's g_get_user_{config,cache,data}_dir()
  238. * as these functions use the user's home dir as the root.
  239. */
  240. dir = g_build_filename (profile_root, ".config", (char *) NULL);
  241. mc_config_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
  242. g_free (dir);
  243. dir = g_build_filename (profile_root, ".cache", (char *) NULL);
  244. mc_cache_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
  245. g_free (dir);
  246. dir = g_build_filename (profile_root, ".local", "share", (char *) NULL);
  247. mc_data_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
  248. g_free (dir);
  249. }
  250. else
  251. {
  252. mc_config_str =
  253. mc_config_init_one_config_path (g_get_user_config_dir (), MC_USERCONF_DIR, mcerror);
  254. mc_cache_str =
  255. mc_config_init_one_config_path (g_get_user_cache_dir (), MC_USERCONF_DIR, mcerror);
  256. mc_data_str =
  257. mc_config_init_one_config_path (g_get_user_data_dir (), MC_USERCONF_DIR, mcerror);
  258. }
  259. mc_config_fix_migrated_rules ();
  260. #else /* MC_HOMEDIR_XDG */
  261. defined_userconf_dir = tilde_expand (MC_USERCONF_DIR);
  262. if (g_path_is_absolute (defined_userconf_dir))
  263. dir = defined_userconf_dir;
  264. else
  265. {
  266. g_free (defined_userconf_dir);
  267. dir = g_build_filename (profile_root, MC_USERCONF_DIR, (char *) NULL);
  268. }
  269. mc_data_str = mc_cache_str = mc_config_str = mc_config_init_one_config_path (dir, "", mcerror);
  270. g_free (dir);
  271. #endif /* MC_HOMEDIR_XDG */
  272. xdg_vars_initialized = TRUE;
  273. }
  274. /* --------------------------------------------------------------------------------------------- */
  275. void
  276. mc_config_deinit_config_paths (void)
  277. {
  278. if (!xdg_vars_initialized)
  279. return;
  280. g_free (mc_config_str);
  281. #if MC_HOMEDIR_XDG
  282. g_free (mc_cache_str);
  283. g_free (mc_data_str);
  284. #endif /* MC_HOMEDIR_XDG */
  285. g_free (mc_global.share_data_dir);
  286. g_free (mc_global.sysconfig_dir);
  287. xdg_vars_initialized = FALSE;
  288. }
  289. /* --------------------------------------------------------------------------------------------- */
  290. const char *
  291. mc_config_get_data_path (void)
  292. {
  293. if (!xdg_vars_initialized)
  294. mc_config_init_config_paths (NULL);
  295. return (const char *) mc_data_str;
  296. }
  297. /* --------------------------------------------------------------------------------------------- */
  298. const char *
  299. mc_config_get_cache_path (void)
  300. {
  301. if (!xdg_vars_initialized)
  302. mc_config_init_config_paths (NULL);
  303. return (const char *) mc_cache_str;
  304. }
  305. /* --------------------------------------------------------------------------------------------- */
  306. const char *
  307. mc_config_get_home_dir (void)
  308. {
  309. static const char *homedir = NULL;
  310. if (homedir == NULL)
  311. {
  312. /* Prior to GLib 2.36, g_get_home_dir() ignores $HOME, which is why
  313. * we read it ourselves. As that function's documentation explains,
  314. * using $HOME is good for compatibility with other programs and
  315. * for running from test frameworks. */
  316. homedir = g_getenv ("HOME");
  317. if (homedir == NULL || *homedir == '\0')
  318. homedir = g_get_home_dir ();
  319. }
  320. return homedir;
  321. }
  322. /* --------------------------------------------------------------------------------------------- */
  323. const char *
  324. mc_config_get_path (void)
  325. {
  326. if (!xdg_vars_initialized)
  327. mc_config_init_config_paths (NULL);
  328. return (const char *) mc_config_str;
  329. }
  330. /* --------------------------------------------------------------------------------------------- */
  331. gboolean
  332. mc_config_migrate_from_old_place (GError ** mcerror, char **msg)
  333. {
  334. char *old_dir;
  335. size_t rule_index;
  336. mc_return_val_if_error (mcerror, FALSE);
  337. if (!mc_config_deprecated_dir_present ())
  338. return FALSE;
  339. old_dir = mc_config_get_deprecated_path ();
  340. g_free (mc_config_init_one_config_path (mc_config_str, EDIT_DIR, mcerror));
  341. #if MC_HOMEDIR_XDG
  342. g_free (mc_config_init_one_config_path (mc_cache_str, EDIT_DIR, mcerror));
  343. g_free (mc_config_init_one_config_path (mc_data_str, EDIT_DIR, mcerror));
  344. #endif /* MC_HOMEDIR_XDG */
  345. mc_return_val_if_error (mcerror, FALSE);
  346. for (rule_index = 0; mc_config_files_reference[rule_index].old_filename != NULL; rule_index++)
  347. {
  348. char *old_name;
  349. if (*mc_config_files_reference[rule_index].old_filename == '\0')
  350. continue;
  351. old_name =
  352. g_build_filename (old_dir, mc_config_files_reference[rule_index].old_filename,
  353. (char *) NULL);
  354. if (g_file_test (old_name, G_FILE_TEST_EXISTS))
  355. {
  356. char *new_name;
  357. const char *basedir = *mc_config_files_reference[rule_index].new_basedir;
  358. const char *filename = mc_config_files_reference[rule_index].new_filename;
  359. new_name = g_build_filename (basedir, filename, (char *) NULL);
  360. mc_config_copy (old_name, new_name, mcerror);
  361. g_free (new_name);
  362. }
  363. g_free (old_name);
  364. }
  365. #if MC_HOMEDIR_XDG
  366. *msg = g_strdup_printf (_("Your old settings were migrated from %s\n"
  367. "to Freedesktop recommended dirs.\n"
  368. "To get more info, please visit\n"
  369. "http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html"),
  370. old_dir);
  371. #else /* MC_HOMEDIR_XDG */
  372. *msg = g_strdup_printf (_("Your old settings were migrated from %s\n"
  373. "to %s\n"), old_dir, mc_config_str);
  374. #endif /* MC_HOMEDIR_XDG */
  375. g_free (old_dir);
  376. return TRUE;
  377. }
  378. /* --------------------------------------------------------------------------------------------- */
  379. /**
  380. * Get full path to config file by short name.
  381. *
  382. * @param config_name short name
  383. * @return full path to config file
  384. */
  385. char *
  386. mc_config_get_full_path (const char *config_name)
  387. {
  388. size_t rule_index;
  389. if (config_name == NULL)
  390. return NULL;
  391. if (!xdg_vars_initialized)
  392. mc_config_init_config_paths (NULL);
  393. for (rule_index = 0; mc_config_files_reference[rule_index].old_filename != NULL; rule_index++)
  394. if (strcmp (config_name, mc_config_files_reference[rule_index].new_filename) == 0)
  395. return g_build_filename (*mc_config_files_reference[rule_index].new_basedir,
  396. mc_config_files_reference[rule_index].new_filename,
  397. (char *) NULL);
  398. return NULL;
  399. }
  400. /* --------------------------------------------------------------------------------------------- */
  401. /**
  402. * Get full path to config file by short name.
  403. *
  404. * @param config_name short name
  405. * @return object with full path to config file
  406. */
  407. vfs_path_t *
  408. mc_config_get_full_vpath (const char *config_name)
  409. {
  410. vfs_path_t *ret_vpath;
  411. char *str_path;
  412. str_path = mc_config_get_full_path (config_name);
  413. ret_vpath = vfs_path_from_str (str_path);
  414. g_free (str_path);
  415. return ret_vpath;
  416. }
  417. /* --------------------------------------------------------------------------------------------- */