paths.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*
  2. paths to configuration files
  3. Copyright (C) 2010-2017
  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. /*** file scope functions *********************************************************************** */
  102. /* --------------------------------------------------------------------------------------------- */
  103. static void
  104. mc_config_mkdir (const char *directory_name, GError ** mcerror)
  105. {
  106. mc_return_if_error (mcerror);
  107. if ((!g_file_test (directory_name, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) &&
  108. (g_mkdir_with_parents (directory_name, 0700) != 0))
  109. mc_propagate_error (mcerror, 0, _("Cannot create %s directory"), directory_name);
  110. }
  111. /* --------------------------------------------------------------------------------------------- */
  112. static char *
  113. mc_config_init_one_config_path (const char *path_base, const char *subdir, GError ** mcerror)
  114. {
  115. char *full_path;
  116. mc_return_val_if_error (mcerror, FALSE);
  117. full_path = g_build_filename (path_base, subdir, (char *) NULL);
  118. if (g_file_test (full_path, G_FILE_TEST_EXISTS))
  119. {
  120. if (g_file_test (full_path, G_FILE_TEST_IS_DIR))
  121. {
  122. config_dir_present = TRUE;
  123. }
  124. else
  125. {
  126. fprintf (stderr, "%s %s\n", _("FATAL: not a directory:"), full_path);
  127. exit (EXIT_FAILURE);
  128. }
  129. }
  130. mc_config_mkdir (full_path, mcerror);
  131. if (mcerror != NULL && *mcerror != NULL)
  132. MC_PTR_FREE (full_path);
  133. return full_path;
  134. }
  135. /* --------------------------------------------------------------------------------------------- */
  136. static char *
  137. mc_config_get_deprecated_path (void)
  138. {
  139. return g_build_filename (mc_config_get_home_dir (), MC_OLD_USERCONF_DIR, (char *) NULL);
  140. }
  141. /* --------------------------------------------------------------------------------------------- */
  142. static void
  143. mc_config_copy (const char *old_name, const char *new_name, GError ** mcerror)
  144. {
  145. mc_return_if_error (mcerror);
  146. if (g_file_test (old_name, G_FILE_TEST_IS_REGULAR))
  147. {
  148. char *contents = NULL;
  149. size_t length;
  150. if (g_file_get_contents (old_name, &contents, &length, mcerror))
  151. g_file_set_contents (new_name, contents, length, mcerror);
  152. g_free (contents);
  153. return;
  154. }
  155. if (g_file_test (old_name, G_FILE_TEST_IS_DIR))
  156. {
  157. GDir *dir;
  158. const char *dir_name;
  159. dir = g_dir_open (old_name, 0, mcerror);
  160. if (dir == NULL)
  161. return;
  162. if (g_mkdir_with_parents (new_name, 0700) == -1)
  163. {
  164. g_dir_close (dir);
  165. mc_propagate_error (mcerror, 0,
  166. _("An error occurred while migrating user settings: %s"),
  167. unix_error_string (errno));
  168. return;
  169. }
  170. while ((dir_name = g_dir_read_name (dir)) != NULL)
  171. {
  172. char *old_name2, *new_name2;
  173. old_name2 = g_build_filename (old_name, dir_name, (char *) NULL);
  174. new_name2 = g_build_filename (new_name, dir_name, (char *) NULL);
  175. mc_config_copy (old_name2, new_name2, mcerror);
  176. g_free (new_name2);
  177. g_free (old_name2);
  178. }
  179. }
  180. }
  181. /* --------------------------------------------------------------------------------------------- */
  182. #if MC_HOMEDIR_XDG
  183. static void
  184. mc_config_fix_migrated_rules (void)
  185. {
  186. size_t rule_index;
  187. for (rule_index = 0; mc_config_migrate_rules_fix[rule_index].old_basedir != NULL; rule_index++)
  188. {
  189. char *old_name;
  190. old_name =
  191. g_build_filename (*mc_config_migrate_rules_fix[rule_index].old_basedir,
  192. mc_config_migrate_rules_fix[rule_index].filename, (char *) NULL);
  193. if (g_file_test (old_name, G_FILE_TEST_EXISTS))
  194. {
  195. char *new_name;
  196. const char *basedir = *mc_config_migrate_rules_fix[rule_index].new_basedir;
  197. const char *filename = mc_config_migrate_rules_fix[rule_index].filename;
  198. new_name = g_build_filename (basedir, filename, (char *) NULL);
  199. rename (old_name, new_name);
  200. g_free (new_name);
  201. }
  202. g_free (old_name);
  203. }
  204. }
  205. #endif /* MC_HOMEDIR_XDG */
  206. /* --------------------------------------------------------------------------------------------- */
  207. static gboolean
  208. mc_config_deprecated_dir_present (void)
  209. {
  210. char *old_dir;
  211. gboolean is_present;
  212. old_dir = mc_config_get_deprecated_path ();
  213. is_present = g_file_test (old_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR);
  214. g_free (old_dir);
  215. return is_present && !config_dir_present;
  216. }
  217. /* --------------------------------------------------------------------------------------------- */
  218. /*** public functions ****************************************************************************/
  219. /* --------------------------------------------------------------------------------------------- */
  220. void
  221. mc_config_init_config_paths (GError ** mcerror)
  222. {
  223. const char *profile_root;
  224. char *dir;
  225. #if MC_HOMEDIR_XDG == 0
  226. char *defined_userconf_dir;
  227. #endif
  228. mc_return_if_error (mcerror);
  229. if (xdg_vars_initialized)
  230. return;
  231. profile_root = mc_get_profile_root ();
  232. #if MC_HOMEDIR_XDG
  233. if (strcmp (profile_root, mc_config_get_home_dir ()) != 0)
  234. {
  235. /*
  236. * The user overrode the default profile root.
  237. *
  238. * In this case we can't use GLib's g_get_user_{config,cache,data}_dir()
  239. * as these functions use the user's home dir as the root.
  240. */
  241. dir = g_build_filename (profile_root, ".config", (char *) NULL);
  242. mc_config_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
  243. g_free (dir);
  244. dir = g_build_filename (profile_root, ".cache", (char *) NULL);
  245. mc_cache_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
  246. g_free (dir);
  247. dir = g_build_filename (profile_root, ".local", "share", (char *) NULL);
  248. mc_data_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
  249. g_free (dir);
  250. }
  251. else
  252. {
  253. mc_config_str =
  254. mc_config_init_one_config_path (g_get_user_config_dir (), MC_USERCONF_DIR, mcerror);
  255. mc_cache_str =
  256. mc_config_init_one_config_path (g_get_user_cache_dir (), MC_USERCONF_DIR, mcerror);
  257. mc_data_str =
  258. mc_config_init_one_config_path (g_get_user_data_dir (), MC_USERCONF_DIR, mcerror);
  259. }
  260. mc_config_fix_migrated_rules ();
  261. #else /* MC_HOMEDIR_XDG */
  262. defined_userconf_dir = tilde_expand (MC_USERCONF_DIR);
  263. if (g_path_is_absolute (defined_userconf_dir))
  264. dir = defined_userconf_dir;
  265. else
  266. {
  267. g_free (defined_userconf_dir);
  268. dir = g_build_filename (profile_root, MC_USERCONF_DIR, (char *) NULL);
  269. }
  270. mc_data_str = mc_cache_str = mc_config_str = mc_config_init_one_config_path (dir, "", mcerror);
  271. g_free (dir);
  272. #endif /* MC_HOMEDIR_XDG */
  273. xdg_vars_initialized = TRUE;
  274. }
  275. /* --------------------------------------------------------------------------------------------- */
  276. void
  277. mc_config_deinit_config_paths (void)
  278. {
  279. if (!xdg_vars_initialized)
  280. return;
  281. g_free (mc_config_str);
  282. #if MC_HOMEDIR_XDG
  283. g_free (mc_cache_str);
  284. g_free (mc_data_str);
  285. #endif /* MC_HOMEDIR_XDG */
  286. g_free (mc_global.share_data_dir);
  287. g_free (mc_global.sysconfig_dir);
  288. xdg_vars_initialized = FALSE;
  289. }
  290. /* --------------------------------------------------------------------------------------------- */
  291. const char *
  292. mc_config_get_data_path (void)
  293. {
  294. if (!xdg_vars_initialized)
  295. mc_config_init_config_paths (NULL);
  296. return (const char *) mc_data_str;
  297. }
  298. /* --------------------------------------------------------------------------------------------- */
  299. const char *
  300. mc_config_get_cache_path (void)
  301. {
  302. if (!xdg_vars_initialized)
  303. mc_config_init_config_paths (NULL);
  304. return (const char *) mc_cache_str;
  305. }
  306. /* --------------------------------------------------------------------------------------------- */
  307. const char *
  308. mc_config_get_home_dir (void)
  309. {
  310. static const char *homedir = NULL;
  311. if (homedir == NULL)
  312. {
  313. /* Prior to GLib 2.36, g_get_home_dir() ignores $HOME, which is why
  314. * we read it ourselves. As that function's documentation explains,
  315. * using $HOME is good for compatibility with other programs and
  316. * for running from test frameworks. */
  317. homedir = g_getenv ("HOME");
  318. if (homedir == NULL || *homedir == '\0')
  319. homedir = g_get_home_dir ();
  320. }
  321. return homedir;
  322. }
  323. /* --------------------------------------------------------------------------------------------- */
  324. const char *
  325. mc_config_get_path (void)
  326. {
  327. if (!xdg_vars_initialized)
  328. mc_config_init_config_paths (NULL);
  329. return (const char *) mc_config_str;
  330. }
  331. /* --------------------------------------------------------------------------------------------- */
  332. gboolean
  333. mc_config_migrate_from_old_place (GError ** mcerror, char **msg)
  334. {
  335. char *old_dir;
  336. size_t rule_index;
  337. mc_return_val_if_error (mcerror, FALSE);
  338. if (!mc_config_deprecated_dir_present ())
  339. return FALSE;
  340. old_dir = mc_config_get_deprecated_path ();
  341. g_free (mc_config_init_one_config_path (mc_config_str, EDIT_DIR, mcerror));
  342. #if MC_HOMEDIR_XDG
  343. g_free (mc_config_init_one_config_path (mc_cache_str, EDIT_DIR, mcerror));
  344. g_free (mc_config_init_one_config_path (mc_data_str, EDIT_DIR, mcerror));
  345. #endif /* MC_HOMEDIR_XDG */
  346. mc_return_val_if_error (mcerror, FALSE);
  347. for (rule_index = 0; mc_config_files_reference[rule_index].old_filename != NULL; rule_index++)
  348. {
  349. char *old_name;
  350. if (*mc_config_files_reference[rule_index].old_filename == '\0')
  351. continue;
  352. old_name =
  353. g_build_filename (old_dir, mc_config_files_reference[rule_index].old_filename,
  354. (char *) NULL);
  355. if (g_file_test (old_name, G_FILE_TEST_EXISTS))
  356. {
  357. char *new_name;
  358. const char *basedir = *mc_config_files_reference[rule_index].new_basedir;
  359. const char *filename = mc_config_files_reference[rule_index].new_filename;
  360. new_name = g_build_filename (basedir, filename, (char *) NULL);
  361. mc_config_copy (old_name, new_name, mcerror);
  362. g_free (new_name);
  363. }
  364. g_free (old_name);
  365. }
  366. #if MC_HOMEDIR_XDG
  367. *msg = g_strdup_printf (_("Your old settings were migrated from %s\n"
  368. "to Freedesktop recommended dirs.\n"
  369. "To get more info, please visit\n"
  370. "http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html"),
  371. old_dir);
  372. #else /* MC_HOMEDIR_XDG */
  373. *msg = g_strdup_printf (_("Your old settings were migrated from %s\n"
  374. "to %s\n"), old_dir, mc_config_str);
  375. #endif /* MC_HOMEDIR_XDG */
  376. g_free (old_dir);
  377. return TRUE;
  378. }
  379. /* --------------------------------------------------------------------------------------------- */
  380. /**
  381. * Get full path to config file by short name.
  382. *
  383. * @param config_name short name
  384. * @return full path to config file
  385. */
  386. char *
  387. mc_config_get_full_path (const char *config_name)
  388. {
  389. size_t rule_index;
  390. if (config_name == NULL)
  391. return NULL;
  392. if (!xdg_vars_initialized)
  393. mc_config_init_config_paths (NULL);
  394. for (rule_index = 0; mc_config_files_reference[rule_index].old_filename != NULL; rule_index++)
  395. {
  396. if (strcmp (config_name, mc_config_files_reference[rule_index].new_filename) == 0)
  397. {
  398. return g_build_filename (*mc_config_files_reference[rule_index].new_basedir,
  399. mc_config_files_reference[rule_index].new_filename,
  400. (char *) NULL);
  401. }
  402. }
  403. return NULL;
  404. }
  405. /* --------------------------------------------------------------------------------------------- */
  406. /**
  407. * Get full path to config file by short name.
  408. *
  409. * @param config_name short name
  410. * @return object with full path to config file
  411. */
  412. vfs_path_t *
  413. mc_config_get_full_vpath (const char *config_name)
  414. {
  415. vfs_path_t *ret_vpath;
  416. char *str_path;
  417. str_path = mc_config_get_full_path (config_name);
  418. ret_vpath = vfs_path_from_str (str_path);
  419. g_free (str_path);
  420. return ret_vpath;
  421. }
  422. /* --------------------------------------------------------------------------------------------- */