paths.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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 occurred 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. static gboolean
  216. mc_config_deprecated_dir_present (void)
  217. {
  218. char *old_dir;
  219. gboolean is_present;
  220. old_dir = mc_config_get_deprecated_path ();
  221. is_present = g_file_test (old_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR);
  222. g_free (old_dir);
  223. return is_present && !config_dir_present;
  224. }
  225. /* --------------------------------------------------------------------------------------------- */
  226. /*** public functions ****************************************************************************/
  227. /* --------------------------------------------------------------------------------------------- */
  228. void
  229. mc_config_init_config_paths (GError ** error)
  230. {
  231. char *dir;
  232. if (xdg_vars_initialized)
  233. return;
  234. /* init mc_home and homedir if not yet */
  235. (void) mc_config_get_home_dir ();
  236. #ifdef MC_HOMEDIR_XDG
  237. if (mc_home != NULL)
  238. {
  239. dir = g_build_filename (mc_home, ".config", (char *) NULL);
  240. mc_config_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, error);
  241. g_free (dir);
  242. dir = g_build_filename (mc_home, ".cache", (char *) NULL);
  243. mc_cache_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, error);
  244. g_free (dir);
  245. dir = g_build_filename (mc_home, ".local", "share", (char *) NULL);
  246. mc_data_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, error);
  247. g_free (dir);
  248. }
  249. else
  250. {
  251. dir = (char *) g_get_user_config_dir ();
  252. if (dir != NULL && *dir != '\0')
  253. mc_config_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, error);
  254. else
  255. {
  256. dir = g_build_filename (homedir, ".config", (char *) NULL);
  257. mc_config_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, error);
  258. g_free (dir);
  259. }
  260. dir = (char *) g_get_user_cache_dir ();
  261. if (dir != NULL && *dir != '\0')
  262. mc_cache_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, error);
  263. else
  264. {
  265. dir = g_build_filename (homedir, ".cache", (char *) NULL);
  266. mc_cache_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, error);
  267. g_free (dir);
  268. }
  269. dir = (char *) g_get_user_data_dir ();
  270. if (dir != NULL && *dir != '\0')
  271. mc_data_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, error);
  272. else
  273. {
  274. dir = g_build_filename (homedir, ".local", "share", (char *) NULL);
  275. mc_data_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, error);
  276. g_free (dir);
  277. }
  278. }
  279. mc_config_fix_migrated_rules ();
  280. #else /* MC_HOMEDIR_XDG */
  281. char *defined_userconf_dir;
  282. defined_userconf_dir = tilde_expand (MC_USERCONF_DIR);
  283. if (g_path_is_absolute (defined_userconf_dir))
  284. dir = defined_userconf_dir;
  285. else
  286. {
  287. g_free (defined_userconf_dir);
  288. dir = g_build_filename (mc_config_get_home_dir (), MC_USERCONF_DIR, (char *) NULL);
  289. }
  290. mc_data_str = mc_cache_str = mc_config_str = mc_config_init_one_config_path (dir, "", error);
  291. g_free (dir);
  292. #endif /* MC_HOMEDIR_XDG */
  293. xdg_vars_initialized = TRUE;
  294. }
  295. /* --------------------------------------------------------------------------------------------- */
  296. void
  297. mc_config_deinit_config_paths (void)
  298. {
  299. if (!xdg_vars_initialized)
  300. return;
  301. g_free (mc_config_str);
  302. #ifdef MC_HOMEDIR_XDG
  303. g_free (mc_cache_str);
  304. g_free (mc_data_str);
  305. #endif /* MC_HOMEDIR_XDG */
  306. g_free (mc_global.share_data_dir);
  307. g_free (mc_global.sysconfig_dir);
  308. xdg_vars_initialized = FALSE;
  309. }
  310. /* --------------------------------------------------------------------------------------------- */
  311. const char *
  312. mc_config_get_data_path (void)
  313. {
  314. if (!xdg_vars_initialized)
  315. mc_config_init_config_paths (NULL);
  316. return (const char *) mc_data_str;
  317. }
  318. /* --------------------------------------------------------------------------------------------- */
  319. const char *
  320. mc_config_get_cache_path (void)
  321. {
  322. if (!xdg_vars_initialized)
  323. mc_config_init_config_paths (NULL);
  324. return (const char *) mc_cache_str;
  325. }
  326. /* --------------------------------------------------------------------------------------------- */
  327. const char *
  328. mc_config_get_home_dir (void)
  329. {
  330. if (homedir == NULL)
  331. {
  332. homedir = g_getenv ("MC_HOME");
  333. if (homedir == NULL || *homedir == '\0')
  334. homedir = g_getenv ("HOME");
  335. else
  336. mc_home = homedir;
  337. if (homedir == NULL || *homedir == '\0')
  338. homedir = g_get_home_dir ();
  339. }
  340. return homedir;
  341. }
  342. /* --------------------------------------------------------------------------------------------- */
  343. const char *
  344. mc_config_get_path (void)
  345. {
  346. if (!xdg_vars_initialized)
  347. mc_config_init_config_paths (NULL);
  348. return (const char *) mc_config_str;
  349. }
  350. /* --------------------------------------------------------------------------------------------- */
  351. gboolean
  352. mc_config_migrate_from_old_place (GError ** error, char **msg)
  353. {
  354. char *old_dir;
  355. size_t rule_index;
  356. if (!mc_config_deprecated_dir_present ())
  357. return FALSE;
  358. old_dir = mc_config_get_deprecated_path ();
  359. g_free (mc_config_init_one_config_path (mc_config_str, EDIT_DIR, error));
  360. #ifdef MC_HOMEDIR_XDG
  361. g_free (mc_config_init_one_config_path (mc_cache_str, EDIT_DIR, error));
  362. g_free (mc_config_init_one_config_path (mc_data_str, EDIT_DIR, error));
  363. #endif /* MC_HOMEDIR_XDG */
  364. if (*error != NULL)
  365. return FALSE;
  366. for (rule_index = 0; mc_config_files_reference[rule_index].old_filename != NULL; rule_index++)
  367. {
  368. char *old_name;
  369. if (*mc_config_files_reference[rule_index].old_filename == '\0')
  370. continue;
  371. old_name =
  372. g_build_filename (old_dir, mc_config_files_reference[rule_index].old_filename, NULL);
  373. if (g_file_test (old_name, G_FILE_TEST_EXISTS))
  374. {
  375. char *new_name;
  376. const char *basedir = *mc_config_files_reference[rule_index].new_basedir;
  377. const char *filename = mc_config_files_reference[rule_index].new_filename;
  378. new_name = g_build_filename (basedir, filename, NULL);
  379. mc_config_copy (old_name, new_name, error);
  380. g_free (new_name);
  381. }
  382. g_free (old_name);
  383. }
  384. #ifdef MC_HOMEDIR_XDG
  385. *msg = g_strdup_printf (_("Your old settings were migrated from %s\n"
  386. "to Freedesktop recommended dirs.\n"
  387. "To get more info, please visit\n"
  388. "http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html"),
  389. old_dir);
  390. #else /* MC_HOMEDIR_XDG */
  391. *msg = g_strdup_printf (_("Your old settings were migrated from %s\n"
  392. "to %s\n"), old_dir, mc_config_str);
  393. #endif /* MC_HOMEDIR_XDG */
  394. g_free (old_dir);
  395. return TRUE;
  396. }
  397. /* --------------------------------------------------------------------------------------------- */
  398. /**
  399. * Get full path to config file by short name.
  400. *
  401. * @param config_name short name
  402. * @return full path to config file
  403. */
  404. char *
  405. mc_config_get_full_path (const char *config_name)
  406. {
  407. size_t rule_index;
  408. if (config_name == NULL)
  409. return NULL;
  410. if (!xdg_vars_initialized)
  411. mc_config_init_config_paths (NULL);
  412. for (rule_index = 0; mc_config_files_reference[rule_index].old_filename != NULL; rule_index++)
  413. {
  414. if (strcmp (config_name, mc_config_files_reference[rule_index].new_filename) == 0)
  415. {
  416. return g_build_filename (*mc_config_files_reference[rule_index].new_basedir,
  417. mc_config_files_reference[rule_index].new_filename, NULL);
  418. }
  419. }
  420. return NULL;
  421. }
  422. /* --------------------------------------------------------------------------------------------- */
  423. /**
  424. * Get full path to config file by short name.
  425. *
  426. * @param config_name short name
  427. * @return object with full path to config file
  428. */
  429. vfs_path_t *
  430. mc_config_get_full_vpath (const char *config_name)
  431. {
  432. vfs_path_t *ret_vpath;
  433. char *str_path;
  434. str_path = mc_config_get_full_path (config_name);
  435. ret_vpath = vfs_path_from_str (str_path);
  436. g_free (str_path);
  437. return ret_vpath;
  438. }
  439. /* --------------------------------------------------------------------------------------------- */