Update copyright years.
[midnight-commander.git] / lib / mcconfig / paths.c
blob00dedfa5531754360e1625e3c910fc9a1563ec82
1 /*
2 paths to configuration files
4 Copyright (C) 2010-2017
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 static gboolean config_dir_present = FALSE;
56 static const struct
58 const char *old_filename;
60 char **new_basedir;
61 const char *new_filename;
62 } mc_config_files_reference[] =
64 /* *INDENT-OFF* */
65 /* config */
66 { "ini", &mc_config_str, MC_CONFIG_FILE},
67 { "filehighlight.ini", &mc_config_str, MC_FHL_INI_FILE},
68 { "mc.keymap", &mc_config_str, GLOBAL_KEYMAP_FILE},
69 { "menu", &mc_config_str, MC_USERMENU_FILE},
70 { "cedit" PATH_SEP_STR "Syntax", &mc_config_str, EDIT_SYNTAX_FILE},
71 { "cedit" PATH_SEP_STR "menu", &mc_config_str, EDIT_HOME_MENU},
72 { "cedit" PATH_SEP_STR "edit.indent.rc", &mc_config_str, EDIT_DIR PATH_SEP_STR "edit.indent.rc"},
73 { "cedit" PATH_SEP_STR "edit.spell.rc", &mc_config_str, EDIT_DIR PATH_SEP_STR "edit.spell.rc"},
74 { "panels.ini", &mc_config_str, MC_PANELS_FILE},
76 /* User should move this file with applying some changes in file */
77 { "", &mc_config_str, MC_FILEBIND_FILE},
79 /* data */
80 { "skins", &mc_data_str, MC_SKINS_SUBDIR},
81 { "fish", &mc_data_str, FISH_PREFIX},
82 { "ashrc", &mc_data_str, "ashrc"},
83 { "bashrc", &mc_data_str, "bashrc"},
84 { "inputrc", &mc_data_str, "inputrc"},
85 { "extfs.d", &mc_data_str, MC_EXTFS_DIR},
86 { "history", &mc_data_str, MC_HISTORY_FILE},
87 { "filepos", &mc_data_str, MC_FILEPOS_FILE},
88 { "hotlist", &mc_data_str, MC_HOTLIST_FILE},
89 { "cedit" PATH_SEP_STR "cooledit.clip", &mc_data_str, EDIT_CLIP_FILE},
90 { "", &mc_data_str, MC_MACRO_FILE},
92 /* cache */
93 { "log", &mc_cache_str, "mc.log"},
94 { "Tree", &mc_cache_str, MC_TREESTORE_FILE},
95 { "cedit" PATH_SEP_STR "cooledit.temp", &mc_cache_str, EDIT_TEMP_FILE},
96 { "cedit" PATH_SEP_STR "cooledit.block", &mc_cache_str, EDIT_BLOCK_FILE},
98 {NULL, NULL, NULL}
99 /* *INDENT-ON* */
102 #if MC_HOMEDIR_XDG
103 static const struct
105 char **old_basedir;
106 const char *filename;
108 char **new_basedir;
109 } mc_config_migrate_rules_fix[] =
111 /* *INDENT-OFF* */
112 { &mc_config_str, MC_HOTLIST_FILE, &mc_data_str},
114 { &mc_data_str, MC_USERMENU_FILE, &mc_config_str},
115 { &mc_data_str, EDIT_SYNTAX_FILE, &mc_config_str},
116 { &mc_data_str, EDIT_HOME_MENU, &mc_config_str},
117 { &mc_data_str, EDIT_DIR PATH_SEP_STR "edit.indent.rc", &mc_config_str},
118 { &mc_data_str, EDIT_DIR PATH_SEP_STR "edit.spell.rc", &mc_config_str},
119 { &mc_data_str, MC_FILEBIND_FILE, &mc_config_str},
121 { &mc_cache_str, MC_HISTORY_FILE, &mc_data_str},
122 { &mc_cache_str, MC_FILEPOS_FILE, &mc_data_str},
123 { &mc_cache_str, EDIT_CLIP_FILE, &mc_data_str},
125 { &mc_cache_str, MC_PANELS_FILE, &mc_config_str},
127 {NULL, NULL, NULL}
128 /* *INDENT-ON* */
130 #endif /* MC_HOMEDIR_XDG */
132 /*** file scope functions *********************************************************************** */
133 /* --------------------------------------------------------------------------------------------- */
135 static void
136 mc_config_mkdir (const char *directory_name, GError ** mcerror)
138 mc_return_if_error (mcerror);
140 if ((!g_file_test (directory_name, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) &&
141 (g_mkdir_with_parents (directory_name, 0700) != 0))
142 mc_propagate_error (mcerror, 0, _("Cannot create %s directory"), directory_name);
145 /* --------------------------------------------------------------------------------------------- */
147 static char *
148 mc_config_init_one_config_path (const char *path_base, const char *subdir, GError ** mcerror)
150 char *full_path;
152 mc_return_val_if_error (mcerror, FALSE);
154 full_path = g_build_filename (path_base, subdir, (char *) NULL);
155 if (g_file_test (full_path, G_FILE_TEST_EXISTS))
157 if (g_file_test (full_path, G_FILE_TEST_IS_DIR))
159 config_dir_present = TRUE;
161 else
163 fprintf (stderr, "%s %s\n", _("FATAL: not a directory:"), full_path);
164 exit (EXIT_FAILURE);
168 mc_config_mkdir (full_path, mcerror);
169 if (mcerror != NULL && *mcerror != NULL)
170 MC_PTR_FREE (full_path);
172 return full_path;
175 /* --------------------------------------------------------------------------------------------- */
177 static char *
178 mc_config_get_deprecated_path (void)
180 return g_build_filename (mc_config_get_home_dir (), MC_OLD_USERCONF_DIR, (char *) NULL);
183 /* --------------------------------------------------------------------------------------------- */
185 static void
186 mc_config_copy (const char *old_name, const char *new_name, GError ** mcerror)
188 mc_return_if_error (mcerror);
190 if (g_file_test (old_name, G_FILE_TEST_IS_REGULAR))
192 char *contents = NULL;
193 size_t length;
195 if (g_file_get_contents (old_name, &contents, &length, mcerror))
196 g_file_set_contents (new_name, contents, length, mcerror);
198 g_free (contents);
199 return;
202 if (g_file_test (old_name, G_FILE_TEST_IS_DIR))
205 GDir *dir;
206 const char *dir_name;
208 dir = g_dir_open (old_name, 0, mcerror);
209 if (dir == NULL)
210 return;
212 if (g_mkdir_with_parents (new_name, 0700) == -1)
214 g_dir_close (dir);
215 mc_propagate_error (mcerror, 0,
216 _("An error occurred while migrating user settings: %s"),
217 unix_error_string (errno));
218 return;
221 while ((dir_name = g_dir_read_name (dir)) != NULL)
223 char *old_name2, *new_name2;
225 old_name2 = g_build_filename (old_name, dir_name, (char *) NULL);
226 new_name2 = g_build_filename (new_name, dir_name, (char *) NULL);
227 mc_config_copy (old_name2, new_name2, mcerror);
228 g_free (new_name2);
229 g_free (old_name2);
234 /* --------------------------------------------------------------------------------------------- */
236 #if MC_HOMEDIR_XDG
237 static void
238 mc_config_fix_migrated_rules (void)
240 size_t rule_index;
242 for (rule_index = 0; mc_config_migrate_rules_fix[rule_index].old_basedir != NULL; rule_index++)
244 char *old_name;
246 old_name =
247 g_build_filename (*mc_config_migrate_rules_fix[rule_index].old_basedir,
248 mc_config_migrate_rules_fix[rule_index].filename, (char *) NULL);
250 if (g_file_test (old_name, G_FILE_TEST_EXISTS))
252 char *new_name;
253 const char *basedir = *mc_config_migrate_rules_fix[rule_index].new_basedir;
254 const char *filename = mc_config_migrate_rules_fix[rule_index].filename;
256 new_name = g_build_filename (basedir, filename, (char *) NULL);
257 rename (old_name, new_name);
258 g_free (new_name);
260 g_free (old_name);
263 #endif /* MC_HOMEDIR_XDG */
265 /* --------------------------------------------------------------------------------------------- */
267 static gboolean
268 mc_config_deprecated_dir_present (void)
270 char *old_dir;
271 gboolean is_present;
273 old_dir = mc_config_get_deprecated_path ();
274 is_present = g_file_test (old_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR);
275 g_free (old_dir);
277 return is_present && !config_dir_present;
280 /* --------------------------------------------------------------------------------------------- */
281 /*** public functions ****************************************************************************/
282 /* --------------------------------------------------------------------------------------------- */
284 void
285 mc_config_init_config_paths (GError ** mcerror)
287 const char *profile_root;
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 profile_root = mc_get_profile_root ();
300 #if MC_HOMEDIR_XDG
301 if (strcmp (profile_root, mc_config_get_home_dir ()) != 0)
304 * The user overrode the default profile root.
306 * In this case we can't use GLib's g_get_user_{config,cache,data}_dir()
307 * as these functions use the user's home dir as the root.
310 dir = g_build_filename (profile_root, ".config", (char *) NULL);
311 mc_config_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
312 g_free (dir);
314 dir = g_build_filename (profile_root, ".cache", (char *) NULL);
315 mc_cache_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
316 g_free (dir);
318 dir = g_build_filename (profile_root, ".local", "share", (char *) NULL);
319 mc_data_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
320 g_free (dir);
322 else
324 mc_config_str =
325 mc_config_init_one_config_path (g_get_user_config_dir (), MC_USERCONF_DIR, mcerror);
326 mc_cache_str =
327 mc_config_init_one_config_path (g_get_user_cache_dir (), MC_USERCONF_DIR, mcerror);
328 mc_data_str =
329 mc_config_init_one_config_path (g_get_user_data_dir (), MC_USERCONF_DIR, mcerror);
332 mc_config_fix_migrated_rules ();
333 #else /* MC_HOMEDIR_XDG */
334 defined_userconf_dir = tilde_expand (MC_USERCONF_DIR);
335 if (g_path_is_absolute (defined_userconf_dir))
336 dir = defined_userconf_dir;
337 else
339 g_free (defined_userconf_dir);
340 dir = g_build_filename (profile_root, MC_USERCONF_DIR, (char *) NULL);
343 mc_data_str = mc_cache_str = mc_config_str = mc_config_init_one_config_path (dir, "", mcerror);
345 g_free (dir);
346 #endif /* MC_HOMEDIR_XDG */
348 xdg_vars_initialized = TRUE;
351 /* --------------------------------------------------------------------------------------------- */
353 void
354 mc_config_deinit_config_paths (void)
356 if (!xdg_vars_initialized)
357 return;
359 g_free (mc_config_str);
360 #if MC_HOMEDIR_XDG
361 g_free (mc_cache_str);
362 g_free (mc_data_str);
363 #endif /* MC_HOMEDIR_XDG */
365 g_free (mc_global.share_data_dir);
366 g_free (mc_global.sysconfig_dir);
368 xdg_vars_initialized = FALSE;
371 /* --------------------------------------------------------------------------------------------- */
373 const char *
374 mc_config_get_data_path (void)
376 if (!xdg_vars_initialized)
377 mc_config_init_config_paths (NULL);
379 return (const char *) mc_data_str;
382 /* --------------------------------------------------------------------------------------------- */
384 const char *
385 mc_config_get_cache_path (void)
387 if (!xdg_vars_initialized)
388 mc_config_init_config_paths (NULL);
390 return (const char *) mc_cache_str;
393 /* --------------------------------------------------------------------------------------------- */
395 const char *
396 mc_config_get_home_dir (void)
398 static const char *homedir = NULL;
400 if (homedir == NULL)
402 /* Prior to GLib 2.36, g_get_home_dir() ignores $HOME, which is why
403 * we read it ourselves. As that function's documentation explains,
404 * using $HOME is good for compatibility with other programs and
405 * for running from test frameworks. */
406 homedir = g_getenv ("HOME");
407 if (homedir == NULL || *homedir == '\0')
408 homedir = g_get_home_dir ();
410 return homedir;
413 /* --------------------------------------------------------------------------------------------- */
415 const char *
416 mc_config_get_path (void)
418 if (!xdg_vars_initialized)
419 mc_config_init_config_paths (NULL);
421 return (const char *) mc_config_str;
424 /* --------------------------------------------------------------------------------------------- */
426 gboolean
427 mc_config_migrate_from_old_place (GError ** mcerror, char **msg)
429 char *old_dir;
430 size_t rule_index;
432 mc_return_val_if_error (mcerror, FALSE);
434 if (!mc_config_deprecated_dir_present ())
435 return FALSE;
437 old_dir = mc_config_get_deprecated_path ();
439 g_free (mc_config_init_one_config_path (mc_config_str, EDIT_DIR, mcerror));
440 #if MC_HOMEDIR_XDG
441 g_free (mc_config_init_one_config_path (mc_cache_str, EDIT_DIR, mcerror));
442 g_free (mc_config_init_one_config_path (mc_data_str, EDIT_DIR, mcerror));
443 #endif /* MC_HOMEDIR_XDG */
445 mc_return_val_if_error (mcerror, FALSE);
447 for (rule_index = 0; mc_config_files_reference[rule_index].old_filename != NULL; rule_index++)
449 char *old_name;
450 if (*mc_config_files_reference[rule_index].old_filename == '\0')
451 continue;
453 old_name =
454 g_build_filename (old_dir, mc_config_files_reference[rule_index].old_filename,
455 (char *) NULL);
457 if (g_file_test (old_name, G_FILE_TEST_EXISTS))
459 char *new_name;
460 const char *basedir = *mc_config_files_reference[rule_index].new_basedir;
461 const char *filename = mc_config_files_reference[rule_index].new_filename;
463 new_name = g_build_filename (basedir, filename, (char *) NULL);
464 mc_config_copy (old_name, new_name, mcerror);
465 g_free (new_name);
467 g_free (old_name);
470 #if MC_HOMEDIR_XDG
471 *msg = g_strdup_printf (_("Your old settings were migrated from %s\n"
472 "to Freedesktop recommended dirs.\n"
473 "To get more info, please visit\n"
474 "http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html"),
475 old_dir);
476 #else /* MC_HOMEDIR_XDG */
477 *msg = g_strdup_printf (_("Your old settings were migrated from %s\n"
478 "to %s\n"), old_dir, mc_config_str);
479 #endif /* MC_HOMEDIR_XDG */
481 g_free (old_dir);
483 return TRUE;
486 /* --------------------------------------------------------------------------------------------- */
488 * Get full path to config file by short name.
490 * @param config_name short name
491 * @return full path to config file
494 char *
495 mc_config_get_full_path (const char *config_name)
497 size_t rule_index;
499 if (config_name == NULL)
500 return NULL;
502 if (!xdg_vars_initialized)
503 mc_config_init_config_paths (NULL);
505 for (rule_index = 0; mc_config_files_reference[rule_index].old_filename != NULL; rule_index++)
507 if (strcmp (config_name, mc_config_files_reference[rule_index].new_filename) == 0)
509 return g_build_filename (*mc_config_files_reference[rule_index].new_basedir,
510 mc_config_files_reference[rule_index].new_filename,
511 (char *) NULL);
514 return NULL;
517 /* --------------------------------------------------------------------------------------------- */
519 * Get full path to config file by short name.
521 * @param config_name short name
522 * @return object with full path to config file
525 vfs_path_t *
526 mc_config_get_full_vpath (const char *config_name)
528 vfs_path_t *ret_vpath;
529 char *str_path;
531 str_path = mc_config_get_full_path (config_name);
533 ret_vpath = vfs_path_from_str (str_path);
534 g_free (str_path);
535 return ret_vpath;
538 /* --------------------------------------------------------------------------------------------- */