paths.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*
  2. paths to configuration files
  3. Copyright (C) 2010 The Free Software Foundation, Inc.
  4. Written by:
  5. Slava Zanko <slavazanko@gmail.com>, 2010.
  6. This file is part of the Midnight Commander.
  7. The Midnight Commander is free software; you can redistribute it
  8. and/or modify it under the terms of the GNU General Public License as
  9. published by the Free Software Foundation; either version 2 of the
  10. License, or (at your option) any later version.
  11. The Midnight Commander is distributed in the hope that it will be
  12. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  13. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. MA 02110-1301, USA.
  19. */
  20. #include <config.h>
  21. #include <stdio.h>
  22. #include <errno.h>
  23. #include "lib/global.h"
  24. #include "lib/mcconfig.h"
  25. #include "lib/fileloc.h"
  26. #include "lib/vfs/mc-vfs/vfs.h"
  27. /*** global variables ****************************************************************************/
  28. /* mc_sysconfig_dir: Area for default settings from maintainers of distributuves
  29. default is /etc/mc or may be defined by MC_DATADIR
  30. */
  31. char *mc_sysconfig_dir = NULL;
  32. /* mc_share_data_dir: Area for default settings from developers */
  33. char *mc_share_data_dir = NULL;
  34. /*** file scope macro definitions ****************************************************************/
  35. /*** file scope type declarations ****************************************************************/
  36. /*** file scope variables ************************************************************************/
  37. static gboolean xdg_vars_initialized = FALSE;
  38. static char *xdg_config = NULL;
  39. static char *xdg_cache = NULL;
  40. static char *xdg_data = NULL;
  41. static const char *homedir = NULL;
  42. static gboolean config_dir_present = FALSE;
  43. static const struct
  44. {
  45. const char *old_filename;
  46. char **new_basedir;
  47. const char *new_filename;
  48. } mc_config_migrate_rules[] =
  49. {
  50. /* *INDENT-OFF* */
  51. /* config */
  52. { "ini", &xdg_config, MC_CONFIG_FILE},
  53. { "filehighlight.ini", &xdg_config, MC_FHL_INI_FILE},
  54. { "hotlist", &xdg_config, MC_HOTLIST_FILE},
  55. { "mc.keymap", &xdg_config, GLOBAL_KEYMAP_FILE},
  56. /* data */
  57. { "skins", &xdg_data, MC_SKINS_SUBDIR},
  58. { "fish", &xdg_data, FISH_PREFIX},
  59. { "bindings", &xdg_data, MC_FILEBIND_FILE},
  60. { "menu", &xdg_data, MC_USERMENU_FILE},
  61. { "bashrc", &xdg_data, "bashrc"},
  62. { "inputrc", &xdg_data, "inputrc"},
  63. { "extfs.d", &xdg_data, MC_EXTFS_DIR},
  64. { "cedit" PATH_SEP_STR "cooledit.macros", &xdg_data, EDIT_MACRO_FILE},
  65. { "cedit" PATH_SEP_STR "Syntax", &xdg_data, EDIT_SYNTAX_FILE},
  66. { "cedit" PATH_SEP_STR "menu", &xdg_data, EDIT_HOME_MENU},
  67. { "cedit" PATH_SEP_STR "edit.indent.rc", &xdg_data, EDIT_DIR PATH_SEP_STR "edit.indent.rc"},
  68. { "cedit" PATH_SEP_STR "edit.spell.rc", &xdg_data, EDIT_DIR PATH_SEP_STR "edit.spell.rc"},
  69. /* cache */
  70. { "history", &xdg_cache, MC_HISTORY_FILE},
  71. { "panels.ini", &xdg_cache, MC_PANELS_FILE},
  72. { "log", &xdg_cache, "mc.log"},
  73. { "filepos", &xdg_cache, MC_FILEPOS_FILE},
  74. { "Tree", &xdg_cache, MC_TREESTORE_FILE},
  75. { "cedit" PATH_SEP_STR "cooledit.clip", &xdg_cache, EDIT_CLIP_FILE},
  76. { "cedit" PATH_SEP_STR "cooledit.temp", &xdg_cache, EDIT_TEMP_FILE},
  77. { "cedit" PATH_SEP_STR "cooledit.block", &xdg_cache, EDIT_BLOCK_FILE},
  78. {NULL, NULL, NULL}
  79. /* *INDENT-ON* */
  80. };
  81. /*** file scope functions *********************************************************************** */
  82. /* --------------------------------------------------------------------------------------------- */
  83. static void
  84. mc_config_mkdir (const char *directory_name, GError ** error)
  85. {
  86. if ((!g_file_test (directory_name, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) &&
  87. (g_mkdir_with_parents (directory_name, 0700) != 0))
  88. {
  89. g_propagate_error (error,
  90. g_error_new (MC_ERROR, 0, _("Cannot create %s directory"),
  91. directory_name));
  92. }
  93. }
  94. /* --------------------------------------------------------------------------------------------- */
  95. static char *
  96. mc_config_init_one_config_path (const char *path_base, const char *subdir, GError ** error)
  97. {
  98. char *full_path;
  99. full_path = g_build_filename (path_base, subdir, NULL);
  100. if (g_file_test (full_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
  101. config_dir_present = TRUE;
  102. mc_config_mkdir (full_path, error);
  103. if (error != NULL && *error != NULL)
  104. {
  105. g_free (full_path);
  106. full_path = NULL;
  107. }
  108. return full_path;
  109. }
  110. /* --------------------------------------------------------------------------------------------- */
  111. static char *
  112. mc_config_get_deprecated_path (void)
  113. {
  114. return g_build_filename (mc_config_get_home_dir (), "." MC_USERCONF_DIR, NULL);
  115. }
  116. /* --------------------------------------------------------------------------------------------- */
  117. static void
  118. mc_config_copy (const char *old_name, const char *new_name, GError ** error)
  119. {
  120. if (error != NULL && *error != NULL)
  121. return;
  122. if (g_file_test (old_name, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))
  123. {
  124. char *contents = NULL;
  125. size_t length;
  126. if (g_file_get_contents (old_name, &contents, &length, error))
  127. g_file_set_contents (new_name, contents, length, error);
  128. g_free (contents);
  129. return;
  130. }
  131. if (g_file_test (old_name, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
  132. {
  133. GDir *dir;
  134. const char *dir_name;
  135. dir = g_dir_open (old_name, 0, error);
  136. if (dir == NULL)
  137. return;
  138. if (!g_mkdir_with_parents (new_name, 0700))
  139. {
  140. g_dir_close (dir);
  141. g_propagate_error (error,
  142. g_error_new (MC_ERROR, 0,
  143. _
  144. ("An error occured while migrating user settings: %s"),
  145. g_strerror (errno)));
  146. return;
  147. }
  148. while ((dir_name = g_dir_read_name (dir)) != NULL)
  149. {
  150. char *old_name2, *new_name2;
  151. old_name2 = g_build_filename (old_name, dir_name, NULL);
  152. new_name2 = g_build_filename (new_name, dir_name, NULL);
  153. mc_config_copy (old_name2, new_name2, error);
  154. g_free (new_name2);
  155. g_free (old_name2);
  156. }
  157. }
  158. if (rename (old_name, new_name) != 0)
  159. {
  160. g_propagate_error (error,
  161. g_error_new (MC_ERROR, 0,
  162. _
  163. ("An error occured while migrating user settings: %s"),
  164. g_strerror (errno)));
  165. }
  166. }
  167. /* --------------------------------------------------------------------------------------------- */
  168. static char *
  169. mc_config_get_conffile (const char *base_path, const char *sub_path, const char *conf_name)
  170. {
  171. char *filename;
  172. if (sub_path != NULL)
  173. filename = g_build_filename (base_path, sub_path, conf_name, NULL);
  174. else
  175. filename = g_build_filename (base_path, conf_name, NULL);
  176. if (g_file_test (filename, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))
  177. return filename;
  178. g_free (filename);
  179. return NULL;
  180. }
  181. /* --------------------------------------------------------------------------------------------- */
  182. /*** public functions ****************************************************************************/
  183. /* --------------------------------------------------------------------------------------------- */
  184. void
  185. mc_config_init_config_paths (GError ** error)
  186. {
  187. const char *mc_libdir;
  188. char *u_config_dir = (char *) g_get_user_config_dir ();
  189. char *u_data_dir = (char *) g_get_user_data_dir ();
  190. char *u_cache_dir = (char *) g_get_user_cache_dir ();
  191. if (xdg_vars_initialized)
  192. return;
  193. u_config_dir = (u_config_dir == NULL)
  194. ? g_build_filename (mc_config_get_home_dir (), ".config", NULL) : g_strdup (u_config_dir);
  195. u_cache_dir = (u_cache_dir == NULL)
  196. ? g_build_filename (mc_config_get_home_dir (), ".cache", NULL) : g_strdup (u_cache_dir);
  197. u_data_dir = (u_data_dir == NULL)
  198. ? g_build_filename (mc_config_get_home_dir (), ".local", "share", NULL)
  199. : g_strdup (u_data_dir);
  200. xdg_config = mc_config_init_one_config_path (u_config_dir, MC_USERCONF_DIR, error);
  201. xdg_cache = mc_config_init_one_config_path (u_cache_dir, MC_USERCONF_DIR, error);
  202. xdg_data = mc_config_init_one_config_path (u_data_dir, MC_USERCONF_DIR, error);
  203. g_free (u_data_dir);
  204. g_free (u_cache_dir);
  205. g_free (u_config_dir);
  206. /* This is the directory, where MC was installed, on Unix this is DATADIR */
  207. /* and can be overriden by the MC_DATADIR environment variable */
  208. mc_libdir = g_getenv ("MC_DATADIR");
  209. if (mc_libdir != NULL)
  210. mc_sysconfig_dir = g_strdup (mc_libdir);
  211. else
  212. mc_sysconfig_dir = g_strdup (SYSCONFDIR);
  213. mc_share_data_dir = g_strdup (DATADIR);
  214. xdg_vars_initialized = TRUE;
  215. }
  216. /* --------------------------------------------------------------------------------------------- */
  217. void
  218. mc_config_deinit_config_paths (void)
  219. {
  220. if (!xdg_vars_initialized)
  221. return;
  222. g_free (xdg_config);
  223. g_free (xdg_cache);
  224. g_free (xdg_data);
  225. g_free (mc_share_data_dir);
  226. g_free (mc_sysconfig_dir);
  227. xdg_vars_initialized = FALSE;
  228. }
  229. /* --------------------------------------------------------------------------------------------- */
  230. const char *
  231. mc_config_get_data_path (void)
  232. {
  233. if (!xdg_vars_initialized)
  234. mc_config_init_config_paths (NULL);
  235. return (const char *) xdg_data;
  236. }
  237. /* --------------------------------------------------------------------------------------------- */
  238. const char *
  239. mc_config_get_cache_path (void)
  240. {
  241. if (!xdg_vars_initialized)
  242. mc_config_init_config_paths (NULL);
  243. return (const char *) xdg_cache;
  244. }
  245. /* --------------------------------------------------------------------------------------------- */
  246. const char *
  247. mc_config_get_home_dir (void)
  248. {
  249. if (homedir == NULL)
  250. {
  251. homedir = g_getenv ("HOME");
  252. if (homedir == NULL)
  253. homedir = g_get_home_dir ();
  254. }
  255. return homedir;
  256. }
  257. /* --------------------------------------------------------------------------------------------- */
  258. const char *
  259. mc_config_get_path (void)
  260. {
  261. if (!xdg_vars_initialized)
  262. mc_config_init_config_paths (NULL);
  263. return (const char *) xdg_config;
  264. }
  265. /* --------------------------------------------------------------------------------------------- */
  266. void
  267. mc_config_migrate_from_old_place (GError ** error)
  268. {
  269. char *old_dir, *tmp_dir_name;
  270. size_t rule_index;
  271. old_dir = mc_config_get_deprecated_path ();
  272. tmp_dir_name = mc_config_init_one_config_path (xdg_config, EDIT_DIR, error);
  273. g_free (tmp_dir_name);
  274. tmp_dir_name = mc_config_init_one_config_path (xdg_cache, EDIT_DIR, error);
  275. g_free (tmp_dir_name);
  276. tmp_dir_name = mc_config_init_one_config_path (xdg_data, EDIT_DIR, error);
  277. g_free (tmp_dir_name);
  278. for (rule_index = 0; mc_config_migrate_rules[rule_index].old_filename != NULL; rule_index++)
  279. {
  280. char *old_name, *new_name;
  281. old_name =
  282. g_build_filename (old_dir, mc_config_migrate_rules[rule_index].old_filename, NULL);
  283. if (!g_file_test (old_name, G_FILE_TEST_EXISTS))
  284. {
  285. g_free (old_name);
  286. continue;
  287. }
  288. new_name = g_build_filename (*mc_config_migrate_rules[rule_index].new_basedir,
  289. mc_config_migrate_rules[rule_index].new_filename, NULL);
  290. mc_config_copy (old_name, new_name, error);
  291. g_free (new_name);
  292. g_free (old_name);
  293. }
  294. /*
  295. {
  296. char *old_dir2;
  297. old_dir2 = g_strconcat (old_dir, "~", NULL);
  298. rename (old_dir, old_dir2);
  299. g_free (old_dir2);
  300. }
  301. */
  302. g_propagate_error (error,
  303. g_error_new (MC_ERROR, 0,
  304. _
  305. ("Your old settings were migrated from %s\n"
  306. "to Freedesktop recommended dirs.\n"
  307. "To get more info, please visit\n"
  308. "http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html"),
  309. old_dir));
  310. g_free (old_dir);
  311. }
  312. /* --------------------------------------------------------------------------------------------- */
  313. gboolean
  314. mc_config_deprecated_dir_present (void)
  315. {
  316. char *old_dir = mc_config_get_deprecated_path ();
  317. gboolean is_present = g_file_test (old_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR);
  318. g_free (old_dir);
  319. return is_present && !config_dir_present;
  320. }
  321. /* --------------------------------------------------------------------------------------------- */
  322. char *
  323. mc_config_search_sysconffile (const char *sub_path, const char *conf_name)
  324. {
  325. char *filename;
  326. filename = mc_config_get_conffile (mc_sysconfig_dir, sub_path, conf_name);
  327. if (filename != NULL)
  328. return filename;
  329. filename = mc_config_get_conffile (mc_share_data_dir, sub_path, conf_name);
  330. if (filename != NULL)
  331. return filename;
  332. return NULL;
  333. }
  334. /* --------------------------------------------------------------------------------------------- */
  335. char *
  336. mc_config_search_conffile (const char *base_path, const char *sub_path, const char *conf_name)
  337. {
  338. char *filename;
  339. if (conf_name == NULL)
  340. return NULL;
  341. if (g_path_is_absolute (conf_name))
  342. return g_strdup (conf_name);
  343. filename = mc_config_get_conffile (base_path, sub_path, conf_name);
  344. if (filename != NULL)
  345. return filename;
  346. return mc_config_search_sysconffile (sub_path, conf_name);
  347. }
  348. /* --------------------------------------------------------------------------------------------- */
  349. void
  350. mc_config_load_all_from_paths (const char *base_path, const char *sub_path, mc_config_t ** config,
  351. const char *conf_name)
  352. {
  353. char *filename;
  354. /* 1) /usr/share/mc (mc_share_data_dir) */
  355. filename = mc_config_get_conffile (mc_share_data_dir, sub_path, conf_name);
  356. mc_config_init_or_append_data (config, filename);
  357. g_free (filename);
  358. /* 2) /etc/mc (mc_sysconfig_dir) */
  359. filename = mc_config_get_conffile (mc_sysconfig_dir, sub_path, conf_name);
  360. mc_config_init_or_append_data (config, filename);
  361. g_free (filename);
  362. /* 3) base_path */
  363. if (g_path_is_absolute (conf_name))
  364. filename = g_strdup (conf_name);
  365. else
  366. filename = mc_config_get_conffile (base_path, sub_path, conf_name);
  367. mc_config_init_or_append_data (config, filename);
  368. g_free (filename);
  369. }
  370. /* --------------------------------------------------------------------------------------------- */
  371. /**
  372. Create new mc_config object from specified ini-file or
  373. append data to existing mc_config object from ini-file
  374. */
  375. void
  376. mc_config_init_or_append_data (mc_config_t ** config, const char *fname)
  377. {
  378. if (fname != NULL && g_file_test (fname, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))
  379. {
  380. if (*config != NULL)
  381. mc_config_read_file (*config, fname);
  382. else
  383. *config = mc_config_init (fname);
  384. }
  385. }
  386. /* --------------------------------------------------------------------------------------------- */