paths.c 17 KB

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