paths.c 18 KB

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