(edit_move_block_to_left): reduce variable scope.
[midnight-commander.git] / lib / mcconfig / paths.c
blob7c346223488b74c5c3c97527f88e166ca9d825f3
1 /*
2 paths to configuration files
4 Copyright (C) 2010-2016
5 Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2010.
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include <config.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <errno.h>
32 #include "lib/global.h"
33 #include "lib/fileloc.h"
34 #include "lib/vfs/vfs.h"
35 #include "lib/util.h" /* unix_error_string() */
37 #include "lib/mcconfig.h"
39 /*** global variables ****************************************************************************/
41 /*** file scope macro definitions ****************************************************************/
43 #define MC_OLD_USERCONF_DIR ".mc"
45 /*** file scope type declarations ****************************************************************/
47 /*** file scope variables ************************************************************************/
49 static gboolean xdg_vars_initialized = FALSE;
50 static char *mc_config_str = NULL;
51 static char *mc_cache_str = NULL;
52 static char *mc_data_str = NULL;
54 /* value of $MC_HOME */
55 static const char *mc_home = NULL;
57 static gboolean config_dir_present = FALSE;
59 static const struct
61 const char *old_filename;
63 char **new_basedir;
64 const char *new_filename;
65 } mc_config_files_reference[] =
67 /* *INDENT-OFF* */
68 /* config */
69 { "ini", &mc_config_str, MC_CONFIG_FILE},
70 { "filehighlight.ini", &mc_config_str, MC_FHL_INI_FILE},
71 { "hotlist", &mc_config_str, MC_HOTLIST_FILE},
72 { "mc.keymap", &mc_config_str, GLOBAL_KEYMAP_FILE},
73 { "menu", &mc_config_str, MC_USERMENU_FILE},
74 { "cedit" PATH_SEP_STR "Syntax", &mc_config_str, EDIT_SYNTAX_FILE},
75 { "cedit" PATH_SEP_STR "menu", &mc_config_str, EDIT_HOME_MENU},
76 { "cedit" PATH_SEP_STR "edit.indent.rc", &mc_config_str, EDIT_DIR PATH_SEP_STR "edit.indent.rc"},
77 { "cedit" PATH_SEP_STR "edit.spell.rc", &mc_config_str, EDIT_DIR PATH_SEP_STR "edit.spell.rc"},
78 { "panels.ini", &mc_config_str, MC_PANELS_FILE},
80 /* User should move this file with applying some changes in file */
81 { "", &mc_config_str, MC_FILEBIND_FILE},
83 /* data */
84 { "skins", &mc_data_str, MC_SKINS_SUBDIR},
85 { "fish", &mc_data_str, FISH_PREFIX},
86 { "ashrc", &mc_data_str, "ashrc"},
87 { "bashrc", &mc_data_str, "bashrc"},
88 { "inputrc", &mc_data_str, "inputrc"},
89 { "extfs.d", &mc_data_str, MC_EXTFS_DIR},
90 { "history", &mc_data_str, MC_HISTORY_FILE},
91 { "filepos", &mc_data_str, MC_FILEPOS_FILE},
92 { "cedit" PATH_SEP_STR "cooledit.clip", &mc_data_str, EDIT_CLIP_FILE},
93 { "", &mc_data_str, MC_MACRO_FILE},
95 /* cache */
96 { "log", &mc_cache_str, "mc.log"},
97 { "Tree", &mc_cache_str, MC_TREESTORE_FILE},
98 { "cedit" PATH_SEP_STR "cooledit.temp", &mc_cache_str, EDIT_TEMP_FILE},
99 { "cedit" PATH_SEP_STR "cooledit.block", &mc_cache_str, EDIT_BLOCK_FILE},
101 {NULL, NULL, NULL}
102 /* *INDENT-ON* */
105 #if MC_HOMEDIR_XDG
106 static const struct
108 char **old_basedir;
109 const char *filename;
111 char **new_basedir;
112 } mc_config_migrate_rules_fix[] =
114 /* *INDENT-OFF* */
115 { &mc_data_str, MC_USERMENU_FILE, &mc_config_str},
116 { &mc_data_str, EDIT_SYNTAX_FILE, &mc_config_str},
117 { &mc_data_str, EDIT_HOME_MENU, &mc_config_str},
118 { &mc_data_str, EDIT_DIR PATH_SEP_STR "edit.indent.rc", &mc_config_str},
119 { &mc_data_str, EDIT_DIR PATH_SEP_STR "edit.spell.rc", &mc_config_str},
120 { &mc_data_str, MC_FILEBIND_FILE, &mc_config_str},
122 { &mc_cache_str, MC_HISTORY_FILE, &mc_data_str},
123 { &mc_cache_str, MC_FILEPOS_FILE, &mc_data_str},
124 { &mc_cache_str, EDIT_CLIP_FILE, &mc_data_str},
126 { &mc_cache_str, MC_PANELS_FILE, &mc_config_str},
128 {NULL, NULL, NULL}
129 /* *INDENT-ON* */
131 #endif /* MC_HOMEDIR_XDG */
133 /*** file scope functions *********************************************************************** */
134 /* --------------------------------------------------------------------------------------------- */
136 static void
137 mc_config_mkdir (const char *directory_name, GError ** mcerror)
139 mc_return_if_error (mcerror);
141 if ((!g_file_test (directory_name, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) &&
142 (g_mkdir_with_parents (directory_name, 0700) != 0))
143 mc_propagate_error (mcerror, 0, _("Cannot create %s directory"), directory_name);
146 /* --------------------------------------------------------------------------------------------- */
148 static char *
149 mc_config_init_one_config_path (const char *path_base, const char *subdir, GError ** mcerror)
151 char *full_path;
153 mc_return_val_if_error (mcerror, FALSE);
155 full_path = g_build_filename (path_base, subdir, (char *) NULL);
156 if (g_file_test (full_path, G_FILE_TEST_EXISTS))
158 if (g_file_test (full_path, G_FILE_TEST_IS_DIR))
160 config_dir_present = TRUE;
162 else
164 fprintf (stderr, "%s %s\n", _("FATAL: not a directory:"), full_path);
165 exit (EXIT_FAILURE);
169 mc_config_mkdir (full_path, mcerror);
170 if (mcerror != NULL && *mcerror != NULL)
171 MC_PTR_FREE (full_path);
173 return full_path;
176 /* --------------------------------------------------------------------------------------------- */
178 static char *
179 mc_config_get_deprecated_path (void)
181 return g_build_filename (mc_config_get_home_dir (), MC_OLD_USERCONF_DIR, (char *) NULL);
184 /* --------------------------------------------------------------------------------------------- */
186 static void
187 mc_config_copy (const char *old_name, const char *new_name, GError ** mcerror)
189 mc_return_if_error (mcerror);
191 if (g_file_test (old_name, G_FILE_TEST_IS_REGULAR))
193 char *contents = NULL;
194 size_t length;
196 if (g_file_get_contents (old_name, &contents, &length, mcerror))
197 g_file_set_contents (new_name, contents, length, mcerror);
199 g_free (contents);
200 return;
203 if (g_file_test (old_name, G_FILE_TEST_IS_DIR))
206 GDir *dir;
207 const char *dir_name;
209 dir = g_dir_open (old_name, 0, mcerror);
210 if (dir == NULL)
211 return;
213 if (g_mkdir_with_parents (new_name, 0700) == -1)
215 g_dir_close (dir);
216 mc_propagate_error (mcerror, 0,
217 _("An error occurred while migrating user settings: %s"),
218 unix_error_string (errno));
219 return;
222 while ((dir_name = g_dir_read_name (dir)) != NULL)
224 char *old_name2, *new_name2;
226 old_name2 = g_build_filename (old_name, dir_name, (char *) NULL);
227 new_name2 = g_build_filename (new_name, dir_name, (char *) NULL);
228 mc_config_copy (old_name2, new_name2, mcerror);
229 g_free (new_name2);
230 g_free (old_name2);
235 /* --------------------------------------------------------------------------------------------- */
237 #if MC_HOMEDIR_XDG
238 static void
239 mc_config_fix_migrated_rules (void)
241 size_t rule_index;
243 for (rule_index = 0; mc_config_migrate_rules_fix[rule_index].old_basedir != NULL; rule_index++)
245 char *old_name;
247 old_name =
248 g_build_filename (*mc_config_migrate_rules_fix[rule_index].old_basedir,
249 mc_config_migrate_rules_fix[rule_index].filename, (char *) NULL);
251 if (g_file_test (old_name, G_FILE_TEST_EXISTS))
253 char *new_name;
254 const char *basedir = *mc_config_migrate_rules_fix[rule_index].new_basedir;
255 const char *filename = mc_config_migrate_rules_fix[rule_index].filename;
257 new_name = g_build_filename (basedir, filename, (char *) NULL);
258 rename (old_name, new_name);
259 g_free (new_name);
261 g_free (old_name);
264 #endif /* MC_HOMEDIR_XDG */
266 /* --------------------------------------------------------------------------------------------- */
268 static gboolean
269 mc_config_deprecated_dir_present (void)
271 char *old_dir;
272 gboolean is_present;
274 old_dir = mc_config_get_deprecated_path ();
275 is_present = g_file_test (old_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR);
276 g_free (old_dir);
278 return is_present && !config_dir_present;
281 /* --------------------------------------------------------------------------------------------- */
282 /*** public functions ****************************************************************************/
283 /* --------------------------------------------------------------------------------------------- */
285 void
286 mc_config_init_config_paths (GError ** mcerror)
288 char *dir;
289 #if MC_HOMEDIR_XDG == 0
290 char *defined_userconf_dir;
291 #endif
293 mc_return_if_error (mcerror);
295 if (xdg_vars_initialized)
296 return;
298 /* init mc_home if not yet */
299 (void) mc_config_get_home_dir ();
301 #if MC_HOMEDIR_XDG
302 if (mc_home != NULL)
304 dir = g_build_filename (mc_home, ".config", (char *) NULL);
305 mc_config_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
306 g_free (dir);
308 dir = g_build_filename (mc_home, ".cache", (char *) NULL);
309 mc_cache_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
310 g_free (dir);
312 dir = g_build_filename (mc_home, ".local", "share", (char *) NULL);
313 mc_data_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
314 g_free (dir);
316 else
318 mc_config_str =
319 mc_config_init_one_config_path (g_get_user_config_dir (), MC_USERCONF_DIR, mcerror);
320 mc_cache_str =
321 mc_config_init_one_config_path (g_get_user_cache_dir (), MC_USERCONF_DIR, mcerror);
322 mc_data_str =
323 mc_config_init_one_config_path (g_get_user_data_dir (), MC_USERCONF_DIR, mcerror);
326 mc_config_fix_migrated_rules ();
327 #else /* MC_HOMEDIR_XDG */
328 defined_userconf_dir = tilde_expand (MC_USERCONF_DIR);
329 if (g_path_is_absolute (defined_userconf_dir))
330 dir = defined_userconf_dir;
331 else
333 g_free (defined_userconf_dir);
334 dir = g_build_filename (mc_config_get_home_dir (), MC_USERCONF_DIR, (char *) NULL);
337 mc_data_str = mc_cache_str = mc_config_str = mc_config_init_one_config_path (dir, "", mcerror);
339 g_free (dir);
340 #endif /* MC_HOMEDIR_XDG */
342 xdg_vars_initialized = TRUE;
345 /* --------------------------------------------------------------------------------------------- */
347 void
348 mc_config_deinit_config_paths (void)
350 if (!xdg_vars_initialized)
351 return;
353 g_free (mc_config_str);
354 #if MC_HOMEDIR_XDG
355 g_free (mc_cache_str);
356 g_free (mc_data_str);
357 #endif /* MC_HOMEDIR_XDG */
359 g_free (mc_global.share_data_dir);
360 g_free (mc_global.sysconfig_dir);
362 xdg_vars_initialized = FALSE;
365 /* --------------------------------------------------------------------------------------------- */
367 const char *
368 mc_config_get_data_path (void)
370 if (!xdg_vars_initialized)
371 mc_config_init_config_paths (NULL);
373 return (const char *) mc_data_str;
376 /* --------------------------------------------------------------------------------------------- */
378 const char *
379 mc_config_get_cache_path (void)
381 if (!xdg_vars_initialized)
382 mc_config_init_config_paths (NULL);
384 return (const char *) mc_cache_str;
387 /* --------------------------------------------------------------------------------------------- */
389 const char *
390 mc_config_get_home_dir (void)
392 static const char *homedir = NULL;
394 if (homedir == NULL)
396 homedir = g_getenv ("MC_HOME");
397 /* Prior to GLib 2.36, g_get_home_dir() ignores $HOME, which is why
398 * we read it ourselves. As that function's documentation explains,
399 * using $HOME is good for compatibility with other programs and
400 * for running from test frameworks. */
401 if (homedir == NULL || *homedir == '\0')
402 homedir = g_getenv ("HOME");
403 else
404 mc_home = homedir;
405 if (homedir == NULL || *homedir == '\0')
406 homedir = g_get_home_dir ();
408 return homedir;
411 /* --------------------------------------------------------------------------------------------- */
413 const char *
414 mc_config_get_path (void)
416 if (!xdg_vars_initialized)
417 mc_config_init_config_paths (NULL);
419 return (const char *) mc_config_str;
422 /* --------------------------------------------------------------------------------------------- */
424 gboolean
425 mc_config_migrate_from_old_place (GError ** mcerror, char **msg)
427 char *old_dir;
428 size_t rule_index;
430 mc_return_val_if_error (mcerror, FALSE);
432 if (!mc_config_deprecated_dir_present ())
433 return FALSE;
435 old_dir = mc_config_get_deprecated_path ();
437 g_free (mc_config_init_one_config_path (mc_config_str, EDIT_DIR, mcerror));
438 #if MC_HOMEDIR_XDG
439 g_free (mc_config_init_one_config_path (mc_cache_str, EDIT_DIR, mcerror));
440 g_free (mc_config_init_one_config_path (mc_data_str, EDIT_DIR, mcerror));
441 #endif /* MC_HOMEDIR_XDG */
443 mc_return_val_if_error (mcerror, FALSE);
445 for (rule_index = 0; mc_config_files_reference[rule_index].old_filename != NULL; rule_index++)
447 char *old_name;
448 if (*mc_config_files_reference[rule_index].old_filename == '\0')
449 continue;
451 old_name =
452 g_build_filename (old_dir, mc_config_files_reference[rule_index].old_filename,
453 (char *) NULL);
455 if (g_file_test (old_name, G_FILE_TEST_EXISTS))
457 char *new_name;
458 const char *basedir = *mc_config_files_reference[rule_index].new_basedir;
459 const char *filename = mc_config_files_reference[rule_index].new_filename;
461 new_name = g_build_filename (basedir, filename, (char *) NULL);
462 mc_config_copy (old_name, new_name, mcerror);
463 g_free (new_name);
465 g_free (old_name);
468 #if MC_HOMEDIR_XDG
469 *msg = g_strdup_printf (_("Your old settings were migrated from %s\n"
470 "to Freedesktop recommended dirs.\n"
471 "To get more info, please visit\n"
472 "http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html"),
473 old_dir);
474 #else /* MC_HOMEDIR_XDG */
475 *msg = g_strdup_printf (_("Your old settings were migrated from %s\n"
476 "to %s\n"), old_dir, mc_config_str);
477 #endif /* MC_HOMEDIR_XDG */
479 g_free (old_dir);
481 return TRUE;
484 /* --------------------------------------------------------------------------------------------- */
486 * Get full path to config file by short name.
488 * @param config_name short name
489 * @return full path to config file
492 char *
493 mc_config_get_full_path (const char *config_name)
495 size_t rule_index;
497 if (config_name == NULL)
498 return NULL;
500 if (!xdg_vars_initialized)
501 mc_config_init_config_paths (NULL);
503 for (rule_index = 0; mc_config_files_reference[rule_index].old_filename != NULL; rule_index++)
505 if (strcmp (config_name, mc_config_files_reference[rule_index].new_filename) == 0)
507 return g_build_filename (*mc_config_files_reference[rule_index].new_basedir,
508 mc_config_files_reference[rule_index].new_filename,
509 (char *) NULL);
512 return NULL;
515 /* --------------------------------------------------------------------------------------------- */
517 * Get full path to config file by short name.
519 * @param config_name short name
520 * @return object with full path to config file
523 vfs_path_t *
524 mc_config_get_full_vpath (const char *config_name)
526 vfs_path_t *ret_vpath;
527 char *str_path;
529 str_path = mc_config_get_full_path (config_name);
531 ret_vpath = vfs_path_from_str (str_path);
532 g_free (str_path);
533 return ret_vpath;
536 /* --------------------------------------------------------------------------------------------- */