paths.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. static gboolean config_dir_present = FALSE;
  39. static const struct
  40. {
  41. const char *old_filename;
  42. char **new_basedir;
  43. const char *new_filename;
  44. } mc_config_files_reference[] =
  45. {
  46. /* *INDENT-OFF* */
  47. /* config */
  48. { "ini", &mc_config_str, MC_CONFIG_FILE},
  49. { "filehighlight.ini", &mc_config_str, MC_FHL_INI_FILE},
  50. { "hotlist", &mc_config_str, MC_HOTLIST_FILE},
  51. { "mc.keymap", &mc_config_str, GLOBAL_KEYMAP_FILE},
  52. { "menu", &mc_config_str, MC_USERMENU_FILE},
  53. { "cedit" PATH_SEP_STR "Syntax", &mc_config_str, EDIT_SYNTAX_FILE},
  54. { "cedit" PATH_SEP_STR "menu", &mc_config_str, EDIT_HOME_MENU},
  55. { "cedit" PATH_SEP_STR "edit.indent.rc", &mc_config_str, EDIT_DIR PATH_SEP_STR "edit.indent.rc"},
  56. { "cedit" PATH_SEP_STR "edit.spell.rc", &mc_config_str, EDIT_DIR PATH_SEP_STR "edit.spell.rc"},
  57. { "panels.ini", &mc_config_str, MC_PANELS_FILE},
  58. /* User should move this file with applying some changes in file */
  59. { "", &mc_config_str, MC_FILEBIND_FILE},
  60. /* data */
  61. { "skins", &mc_data_str, MC_SKINS_SUBDIR},
  62. { "fish", &mc_data_str, FISH_PREFIX},
  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. #ifdef 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 ** error)
  105. {
  106. if ((!g_file_test (directory_name, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) &&
  107. (g_mkdir_with_parents (directory_name, 0700) != 0))
  108. {
  109. g_propagate_error (error,
  110. g_error_new (MC_ERROR, 0, _("Cannot create %s directory"),
  111. directory_name));
  112. }
  113. }
  114. /* --------------------------------------------------------------------------------------------- */
  115. static char *
  116. mc_config_init_one_config_path (const char *path_base, const char *subdir, GError ** error)
  117. {
  118. char *full_path;
  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, error);
  133. if (error != NULL && *error != NULL)
  134. {
  135. g_free (full_path);
  136. full_path = NULL;
  137. }
  138. return full_path;
  139. }
  140. /* --------------------------------------------------------------------------------------------- */
  141. static char *
  142. mc_config_get_deprecated_path (void)
  143. {
  144. return g_build_filename (mc_config_get_home_dir (), MC_OLD_USERCONF_DIR, NULL);
  145. }
  146. /* --------------------------------------------------------------------------------------------- */
  147. static void
  148. mc_config_copy (const char *old_name, const char *new_name, GError ** error)
  149. {
  150. if (g_file_test (old_name, G_FILE_TEST_IS_REGULAR))
  151. {
  152. char *contents = NULL;
  153. size_t length;
  154. if (g_file_get_contents (old_name, &contents, &length, error))
  155. g_file_set_contents (new_name, contents, length, error);
  156. g_free (contents);
  157. return;
  158. }
  159. if (g_file_test (old_name, G_FILE_TEST_IS_DIR))
  160. {
  161. GDir *dir;
  162. const char *dir_name;
  163. dir = g_dir_open (old_name, 0, error);
  164. if (dir == NULL)
  165. return;
  166. if (g_mkdir_with_parents (new_name, 0700) == -1)
  167. {
  168. g_dir_close (dir);
  169. g_propagate_error (error,
  170. g_error_new (MC_ERROR, 0,
  171. _
  172. ("An error occured while migrating user settings: %s"),
  173. unix_error_string (errno)));
  174. return;
  175. }
  176. while ((dir_name = g_dir_read_name (dir)) != NULL)
  177. {
  178. char *old_name2, *new_name2;
  179. old_name2 = g_build_filename (old_name, dir_name, NULL);
  180. new_name2 = g_build_filename (new_name, dir_name, NULL);
  181. mc_config_copy (old_name2, new_name2, error);
  182. g_free (new_name2);
  183. g_free (old_name2);
  184. }
  185. }
  186. }
  187. /* --------------------------------------------------------------------------------------------- */
  188. #if MC_HOMEDIR_XDG
  189. static void
  190. mc_config_fix_migrated_rules (void)
  191. {
  192. size_t rule_index;
  193. for (rule_index = 0; mc_config_migrate_rules_fix[rule_index].old_basedir != NULL; rule_index++)
  194. {
  195. char *old_name;
  196. old_name =
  197. g_build_filename (*mc_config_migrate_rules_fix[rule_index].old_basedir,
  198. mc_config_migrate_rules_fix[rule_index].filename, NULL);
  199. if (g_file_test (old_name, G_FILE_TEST_EXISTS))
  200. {
  201. char *new_name;
  202. const char *basedir = *mc_config_migrate_rules_fix[rule_index].new_basedir;
  203. const char *filename = mc_config_migrate_rules_fix[rule_index].filename;
  204. new_name = g_build_filename (basedir, filename, NULL);
  205. rename (old_name, new_name);
  206. g_free (new_name);
  207. }
  208. g_free (old_name);
  209. }
  210. }
  211. #endif /* MC_HOMEDIR_XDG */
  212. /* --------------------------------------------------------------------------------------------- */
  213. /*** public functions ****************************************************************************/
  214. /* --------------------------------------------------------------------------------------------- */
  215. void
  216. mc_config_init_config_paths (GError ** error)
  217. {
  218. const char *mc_datadir;
  219. #ifdef MC_HOMEDIR_XDG
  220. char *u_config_dir = (char *) g_get_user_config_dir ();
  221. char *u_data_dir = (char *) g_get_user_data_dir ();
  222. char *u_cache_dir = (char *) g_get_user_cache_dir ();
  223. if (xdg_vars_initialized)
  224. return;
  225. u_config_dir = (u_config_dir == NULL)
  226. ? g_build_filename (mc_config_get_home_dir (), ".config", NULL) : g_strdup (u_config_dir);
  227. u_cache_dir = (u_cache_dir == NULL)
  228. ? g_build_filename (mc_config_get_home_dir (), ".cache", NULL) : g_strdup (u_cache_dir);
  229. u_data_dir = (u_data_dir == NULL)
  230. ? g_build_filename (mc_config_get_home_dir (), ".local", "share", NULL)
  231. : g_strdup (u_data_dir);
  232. mc_config_str = mc_config_init_one_config_path (u_config_dir, MC_USERCONF_DIR, error);
  233. mc_cache_str = mc_config_init_one_config_path (u_cache_dir, MC_USERCONF_DIR, error);
  234. mc_data_str = mc_config_init_one_config_path (u_data_dir, MC_USERCONF_DIR, error);
  235. g_free (u_data_dir);
  236. g_free (u_cache_dir);
  237. g_free (u_config_dir);
  238. mc_config_fix_migrated_rules ();
  239. #else /* MC_HOMEDIR_XDG */
  240. char *defined_userconf_dir;
  241. char *u_config_dir;
  242. defined_userconf_dir = tilde_expand (MC_USERCONF_DIR);
  243. if (!g_path_is_absolute (defined_userconf_dir))
  244. {
  245. u_config_dir = g_build_filename (mc_config_get_home_dir (), MC_USERCONF_DIR, NULL);
  246. g_free (defined_userconf_dir);
  247. }
  248. else
  249. u_config_dir = defined_userconf_dir;
  250. mc_data_str = mc_cache_str = mc_config_str =
  251. mc_config_init_one_config_path (u_config_dir, "", error);
  252. g_free (u_config_dir);
  253. #endif /* MC_HOMEDIR_XDG */
  254. /* This is the directory, where MC was installed, on Unix this is DATADIR */
  255. /* and can be overriden by the MC_DATADIR environment variable */
  256. mc_datadir = g_getenv ("MC_DATADIR");
  257. if (mc_datadir != NULL)
  258. mc_global.sysconfig_dir = g_strdup (mc_datadir);
  259. else
  260. mc_global.sysconfig_dir = g_strdup (SYSCONFDIR);
  261. mc_global.share_data_dir = g_strdup (DATADIR);
  262. xdg_vars_initialized = TRUE;
  263. }
  264. /* --------------------------------------------------------------------------------------------- */
  265. void
  266. mc_config_deinit_config_paths (void)
  267. {
  268. if (!xdg_vars_initialized)
  269. return;
  270. g_free (mc_config_str);
  271. #ifdef MC_HOMEDIR_XDG
  272. g_free (mc_cache_str);
  273. g_free (mc_data_str);
  274. #endif /* MC_HOMEDIR_XDG */
  275. g_free (mc_global.share_data_dir);
  276. g_free (mc_global.sysconfig_dir);
  277. xdg_vars_initialized = FALSE;
  278. }
  279. /* --------------------------------------------------------------------------------------------- */
  280. const char *
  281. mc_config_get_data_path (void)
  282. {
  283. if (!xdg_vars_initialized)
  284. mc_config_init_config_paths (NULL);
  285. return (const char *) mc_data_str;
  286. }
  287. /* --------------------------------------------------------------------------------------------- */
  288. const char *
  289. mc_config_get_cache_path (void)
  290. {
  291. if (!xdg_vars_initialized)
  292. mc_config_init_config_paths (NULL);
  293. return (const char *) mc_cache_str;
  294. }
  295. /* --------------------------------------------------------------------------------------------- */
  296. const char *
  297. mc_config_get_home_dir (void)
  298. {
  299. if (homedir == NULL)
  300. {
  301. homedir = g_getenv ("HOME");
  302. if (homedir == NULL)
  303. homedir = g_get_home_dir ();
  304. }
  305. return homedir;
  306. }
  307. /* --------------------------------------------------------------------------------------------- */
  308. const char *
  309. mc_config_get_path (void)
  310. {
  311. if (!xdg_vars_initialized)
  312. mc_config_init_config_paths (NULL);
  313. return (const char *) mc_config_str;
  314. }
  315. /* --------------------------------------------------------------------------------------------- */
  316. void
  317. mc_config_migrate_from_old_place (GError ** error)
  318. {
  319. char *old_dir;
  320. size_t rule_index;
  321. old_dir = mc_config_get_deprecated_path ();
  322. g_free (mc_config_init_one_config_path (mc_config_str, EDIT_DIR, error));
  323. #ifdef MC_HOMEDIR_XDG
  324. g_free (mc_config_init_one_config_path (mc_cache_str, EDIT_DIR, error));
  325. g_free (mc_config_init_one_config_path (mc_data_str, EDIT_DIR, error));
  326. #endif /* MC_HOMEDIR_XDG */
  327. for (rule_index = 0; mc_config_files_reference[rule_index].old_filename != NULL; rule_index++)
  328. {
  329. char *old_name;
  330. if (*mc_config_files_reference[rule_index].old_filename == '\0')
  331. continue;
  332. old_name =
  333. g_build_filename (old_dir, mc_config_files_reference[rule_index].old_filename, NULL);
  334. if (g_file_test (old_name, G_FILE_TEST_EXISTS))
  335. {
  336. char *new_name;
  337. const char *basedir = *mc_config_files_reference[rule_index].new_basedir;
  338. const char *filename = mc_config_files_reference[rule_index].new_filename;
  339. new_name = g_build_filename (basedir, filename, NULL);
  340. mc_config_copy (old_name, new_name, error);
  341. g_free (new_name);
  342. }
  343. g_free (old_name);
  344. }
  345. #ifdef MC_HOMEDIR_XDG
  346. g_propagate_error (error,
  347. g_error_new (MC_ERROR, 0,
  348. _
  349. ("Your old settings were migrated from %s\n"
  350. "to Freedesktop recommended dirs.\n"
  351. "To get more info, please visit\n"
  352. "http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html"),
  353. old_dir));
  354. #else /* MC_HOMEDIR_XDG */
  355. g_propagate_error (error,
  356. g_error_new (MC_ERROR, 0,
  357. _
  358. ("Your old settings were migrated from %s\n"
  359. "to %s\n"), old_dir, mc_config_str));
  360. #endif /* MC_HOMEDIR_XDG */
  361. g_free (old_dir);
  362. }
  363. /* --------------------------------------------------------------------------------------------- */
  364. gboolean
  365. mc_config_deprecated_dir_present (void)
  366. {
  367. char *old_dir;
  368. gboolean is_present;
  369. old_dir = mc_config_get_deprecated_path ();
  370. is_present = g_file_test (old_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR);
  371. g_free (old_dir);
  372. return is_present && !config_dir_present;
  373. }
  374. /* --------------------------------------------------------------------------------------------- */
  375. /**
  376. * Get full path to config file by short name.
  377. *
  378. * @param config_name short name
  379. * @return full path to config file
  380. */
  381. char *
  382. mc_config_get_full_path (const char *config_name)
  383. {
  384. size_t rule_index;
  385. if (config_name == NULL)
  386. return NULL;
  387. if (!xdg_vars_initialized)
  388. mc_config_init_config_paths (NULL);
  389. for (rule_index = 0; mc_config_files_reference[rule_index].old_filename != NULL; rule_index++)
  390. {
  391. if (strcmp (config_name, mc_config_files_reference[rule_index].new_filename) == 0)
  392. {
  393. return g_build_filename (*mc_config_files_reference[rule_index].new_basedir,
  394. mc_config_files_reference[rule_index].new_filename, NULL);
  395. }
  396. }
  397. return NULL;
  398. }
  399. /* --------------------------------------------------------------------------------------------- */