paths.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 <errno.h>
  22. #include "lib/global.h"
  23. #include "lib/fileloc.h"
  24. #include "lib/vfs/vfs.h"
  25. #include "lib/util.h" /* unix_error_string() */
  26. #include "lib/mcconfig.h"
  27. /*** global variables ****************************************************************************/
  28. /*** file scope macro definitions ****************************************************************/
  29. /*** file scope type declarations ****************************************************************/
  30. /*** file scope variables ************************************************************************/
  31. static gboolean xdg_vars_initialized = FALSE;
  32. static char *xdg_config = NULL;
  33. static char *xdg_cache = NULL;
  34. static char *xdg_data = NULL;
  35. static const char *homedir = NULL;
  36. static gboolean config_dir_present = FALSE;
  37. static const struct
  38. {
  39. const char *old_filename;
  40. char **new_basedir;
  41. const char *new_filename;
  42. } mc_config_migrate_rules[] =
  43. {
  44. /* *INDENT-OFF* */
  45. /* config */
  46. { "ini", &xdg_config, MC_CONFIG_FILE},
  47. { "filehighlight.ini", &xdg_config, MC_FHL_INI_FILE},
  48. { "hotlist", &xdg_config, MC_HOTLIST_FILE},
  49. { "mc.keymap", &xdg_config, GLOBAL_KEYMAP_FILE},
  50. /* data */
  51. { "skins", &xdg_data, MC_SKINS_SUBDIR},
  52. { "fish", &xdg_data, FISH_PREFIX},
  53. { "bindings", &xdg_data, MC_FILEBIND_FILE},
  54. { "menu", &xdg_data, MC_USERMENU_FILE},
  55. { "bashrc", &xdg_data, "bashrc"},
  56. { "inputrc", &xdg_data, "inputrc"},
  57. { "extfs.d", &xdg_data, MC_EXTFS_DIR},
  58. { "cedit" PATH_SEP_STR "Syntax", &xdg_data, EDIT_SYNTAX_FILE},
  59. { "cedit" PATH_SEP_STR "menu", &xdg_data, EDIT_HOME_MENU},
  60. { "cedit" PATH_SEP_STR "edit.indent.rc", &xdg_data, EDIT_DIR PATH_SEP_STR "edit.indent.rc"},
  61. { "cedit" PATH_SEP_STR "edit.spell.rc", &xdg_data, EDIT_DIR PATH_SEP_STR "edit.spell.rc"},
  62. /* cache */
  63. { "history", &xdg_cache, MC_HISTORY_FILE},
  64. { "panels.ini", &xdg_cache, MC_PANELS_FILE},
  65. { "log", &xdg_cache, "mc.log"},
  66. { "filepos", &xdg_cache, MC_FILEPOS_FILE},
  67. { "Tree", &xdg_cache, MC_TREESTORE_FILE},
  68. { "cedit" PATH_SEP_STR "cooledit.clip", &xdg_cache, EDIT_CLIP_FILE},
  69. { "cedit" PATH_SEP_STR "cooledit.temp", &xdg_cache, EDIT_TEMP_FILE},
  70. { "cedit" PATH_SEP_STR "cooledit.block", &xdg_cache, EDIT_BLOCK_FILE},
  71. {NULL, NULL, NULL}
  72. /* *INDENT-ON* */
  73. };
  74. /*** file scope functions *********************************************************************** */
  75. /* --------------------------------------------------------------------------------------------- */
  76. static void
  77. mc_config_mkdir (const char *directory_name, GError ** error)
  78. {
  79. if ((!g_file_test (directory_name, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) &&
  80. (g_mkdir_with_parents (directory_name, 0700) != 0))
  81. {
  82. g_propagate_error (error,
  83. g_error_new (MC_ERROR, 0, _("Cannot create %s directory"),
  84. directory_name));
  85. }
  86. }
  87. /* --------------------------------------------------------------------------------------------- */
  88. static char *
  89. mc_config_init_one_config_path (const char *path_base, const char *subdir, GError ** error)
  90. {
  91. char *full_path;
  92. full_path = g_build_filename (path_base, subdir, NULL);
  93. if (g_file_test (full_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
  94. config_dir_present = TRUE;
  95. mc_config_mkdir (full_path, error);
  96. if (error != NULL && *error != NULL)
  97. {
  98. g_free (full_path);
  99. full_path = NULL;
  100. }
  101. return full_path;
  102. }
  103. /* --------------------------------------------------------------------------------------------- */
  104. static char *
  105. mc_config_get_deprecated_path (void)
  106. {
  107. return g_build_filename (mc_config_get_home_dir (), "." MC_USERCONF_DIR, NULL);
  108. }
  109. /* --------------------------------------------------------------------------------------------- */
  110. static void
  111. mc_config_copy (const char *old_name, const char *new_name, GError ** error)
  112. {
  113. if (g_file_test (old_name, G_FILE_TEST_IS_REGULAR))
  114. {
  115. char *contents = NULL;
  116. size_t length;
  117. if (g_file_get_contents (old_name, &contents, &length, error))
  118. g_file_set_contents (new_name, contents, length, error);
  119. g_free (contents);
  120. return;
  121. }
  122. if (g_file_test (old_name, G_FILE_TEST_IS_DIR))
  123. {
  124. GDir *dir;
  125. const char *dir_name;
  126. dir = g_dir_open (old_name, 0, error);
  127. if (dir == NULL)
  128. return;
  129. if (g_mkdir_with_parents (new_name, 0700) == -1)
  130. {
  131. g_dir_close (dir);
  132. g_propagate_error (error,
  133. g_error_new (MC_ERROR, 0,
  134. _
  135. ("An error occured while migrating user settings: %s"),
  136. unix_error_string (errno)));
  137. return;
  138. }
  139. while ((dir_name = g_dir_read_name (dir)) != NULL)
  140. {
  141. char *old_name2, *new_name2;
  142. old_name2 = g_build_filename (old_name, dir_name, NULL);
  143. new_name2 = g_build_filename (new_name, dir_name, NULL);
  144. mc_config_copy (old_name2, new_name2, error);
  145. g_free (new_name2);
  146. g_free (old_name2);
  147. }
  148. }
  149. }
  150. /* --------------------------------------------------------------------------------------------- */
  151. /*** public functions ****************************************************************************/
  152. /* --------------------------------------------------------------------------------------------- */
  153. void
  154. mc_config_init_config_paths (GError ** error)
  155. {
  156. const char *mc_datadir;
  157. char *u_config_dir = (char *) g_get_user_config_dir ();
  158. char *u_data_dir = (char *) g_get_user_data_dir ();
  159. char *u_cache_dir = (char *) g_get_user_cache_dir ();
  160. if (xdg_vars_initialized)
  161. return;
  162. u_config_dir = (u_config_dir == NULL)
  163. ? g_build_filename (mc_config_get_home_dir (), ".config", NULL) : g_strdup (u_config_dir);
  164. u_cache_dir = (u_cache_dir == NULL)
  165. ? g_build_filename (mc_config_get_home_dir (), ".cache", NULL) : g_strdup (u_cache_dir);
  166. u_data_dir = (u_data_dir == NULL)
  167. ? g_build_filename (mc_config_get_home_dir (), ".local", "share", NULL)
  168. : g_strdup (u_data_dir);
  169. xdg_config = mc_config_init_one_config_path (u_config_dir, MC_USERCONF_DIR, error);
  170. xdg_cache = mc_config_init_one_config_path (u_cache_dir, MC_USERCONF_DIR, error);
  171. xdg_data = mc_config_init_one_config_path (u_data_dir, MC_USERCONF_DIR, error);
  172. g_free (u_data_dir);
  173. g_free (u_cache_dir);
  174. g_free (u_config_dir);
  175. /* This is the directory, where MC was installed, on Unix this is DATADIR */
  176. /* and can be overriden by the MC_DATADIR environment variable */
  177. mc_datadir = g_getenv ("MC_DATADIR");
  178. if (mc_datadir != NULL)
  179. mc_global.sysconfig_dir = g_strdup (mc_datadir);
  180. else
  181. mc_global.sysconfig_dir = g_strdup (SYSCONFDIR);
  182. mc_global.share_data_dir = g_strdup (DATADIR);
  183. xdg_vars_initialized = TRUE;
  184. }
  185. /* --------------------------------------------------------------------------------------------- */
  186. void
  187. mc_config_deinit_config_paths (void)
  188. {
  189. if (!xdg_vars_initialized)
  190. return;
  191. g_free (xdg_config);
  192. g_free (xdg_cache);
  193. g_free (xdg_data);
  194. g_free (mc_global.share_data_dir);
  195. g_free (mc_global.sysconfig_dir);
  196. xdg_vars_initialized = FALSE;
  197. }
  198. /* --------------------------------------------------------------------------------------------- */
  199. const char *
  200. mc_config_get_data_path (void)
  201. {
  202. if (!xdg_vars_initialized)
  203. mc_config_init_config_paths (NULL);
  204. return (const char *) xdg_data;
  205. }
  206. /* --------------------------------------------------------------------------------------------- */
  207. const char *
  208. mc_config_get_cache_path (void)
  209. {
  210. if (!xdg_vars_initialized)
  211. mc_config_init_config_paths (NULL);
  212. return (const char *) xdg_cache;
  213. }
  214. /* --------------------------------------------------------------------------------------------- */
  215. const char *
  216. mc_config_get_home_dir (void)
  217. {
  218. if (homedir == NULL)
  219. {
  220. homedir = g_getenv ("HOME");
  221. if (homedir == NULL)
  222. homedir = g_get_home_dir ();
  223. }
  224. return homedir;
  225. }
  226. /* --------------------------------------------------------------------------------------------- */
  227. const char *
  228. mc_config_get_path (void)
  229. {
  230. if (!xdg_vars_initialized)
  231. mc_config_init_config_paths (NULL);
  232. return (const char *) xdg_config;
  233. }
  234. /* --------------------------------------------------------------------------------------------- */
  235. void
  236. mc_config_migrate_from_old_place (GError ** error)
  237. {
  238. char *old_dir;
  239. size_t rule_index;
  240. old_dir = mc_config_get_deprecated_path ();
  241. g_free (mc_config_init_one_config_path (xdg_config, EDIT_DIR, error));
  242. g_free (mc_config_init_one_config_path (xdg_cache, EDIT_DIR, error));
  243. g_free (mc_config_init_one_config_path (xdg_data, EDIT_DIR, error));
  244. for (rule_index = 0; mc_config_migrate_rules[rule_index].old_filename != NULL; rule_index++)
  245. {
  246. char *old_name;
  247. old_name =
  248. g_build_filename (old_dir, mc_config_migrate_rules[rule_index].old_filename, NULL);
  249. if (g_file_test (old_name, G_FILE_TEST_EXISTS))
  250. {
  251. char *new_name;
  252. new_name = g_build_filename (*mc_config_migrate_rules[rule_index].new_basedir,
  253. mc_config_migrate_rules[rule_index].new_filename, NULL);
  254. mc_config_copy (old_name, new_name, error);
  255. g_free (new_name);
  256. }
  257. g_free (old_name);
  258. }
  259. g_propagate_error (error,
  260. g_error_new (MC_ERROR, 0,
  261. _
  262. ("Your old settings were migrated from %s\n"
  263. "to Freedesktop recommended dirs.\n"
  264. "To get more info, please visit\n"
  265. "http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html"),
  266. old_dir));
  267. g_free (old_dir);
  268. }
  269. /* --------------------------------------------------------------------------------------------- */
  270. gboolean
  271. mc_config_deprecated_dir_present (void)
  272. {
  273. char *old_dir;
  274. gboolean is_present;
  275. old_dir = mc_config_get_deprecated_path ();
  276. is_present = g_file_test (old_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR);
  277. g_free (old_dir);
  278. return is_present && !config_dir_present;
  279. }
  280. /* --------------------------------------------------------------------------------------------- */