6d6097d6a58e044cb8cee4af9d8fa84dd0508c6d
[midnight-commander.git] / lib / mcconfig / paths.c
blob6d6097d6a58e044cb8cee4af9d8fa84dd0508c6d
1 /*
2 paths to configuration files
4 Copyright (C) 2010, 2011
5 The 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 const char *homedir = NULL;
55 /* value of $MC_HOME */
56 static const char *mc_home = NULL;
58 static gboolean config_dir_present = FALSE;
60 static const struct
62 const char *old_filename;
64 char **new_basedir;
65 const char *new_filename;
66 } mc_config_files_reference[] =
68 /* *INDENT-OFF* */
69 /* config */
70 { "ini", &mc_config_str, MC_CONFIG_FILE},
71 { "filehighlight.ini", &mc_config_str, MC_FHL_INI_FILE},
72 { "hotlist", &mc_config_str, MC_HOTLIST_FILE},
73 { "mc.keymap", &mc_config_str, GLOBAL_KEYMAP_FILE},
74 { "menu", &mc_config_str, MC_USERMENU_FILE},
75 { "cedit" PATH_SEP_STR "Syntax", &mc_config_str, EDIT_SYNTAX_FILE},
76 { "cedit" PATH_SEP_STR "menu", &mc_config_str, EDIT_HOME_MENU},
77 { "cedit" PATH_SEP_STR "edit.indent.rc", &mc_config_str, EDIT_DIR PATH_SEP_STR "edit.indent.rc"},
78 { "cedit" PATH_SEP_STR "edit.spell.rc", &mc_config_str, EDIT_DIR PATH_SEP_STR "edit.spell.rc"},
79 { "panels.ini", &mc_config_str, MC_PANELS_FILE},
81 /* User should move this file with applying some changes in file */
82 { "", &mc_config_str, MC_FILEBIND_FILE},
84 /* data */
85 { "skins", &mc_data_str, MC_SKINS_SUBDIR},
86 { "fish", &mc_data_str, FISH_PREFIX},
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 #ifdef 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 ** error)
139 if ((!g_file_test (directory_name, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) &&
140 (g_mkdir_with_parents (directory_name, 0700) != 0))
142 g_propagate_error (error,
143 g_error_new (MC_ERROR, 0, _("Cannot create %s directory"),
144 directory_name));
148 /* --------------------------------------------------------------------------------------------- */
150 static char *
151 mc_config_init_one_config_path (const char *path_base, const char *subdir, GError ** error)
153 char *full_path;
155 full_path = g_build_filename (path_base, subdir, NULL);
157 if (g_file_test (full_path, G_FILE_TEST_EXISTS))
159 if (g_file_test (full_path, G_FILE_TEST_IS_DIR))
161 config_dir_present = TRUE;
163 else
165 fprintf (stderr, "%s %s\n", _("FATAL: not a directory:"), full_path);
166 exit (EXIT_FAILURE);
170 mc_config_mkdir (full_path, error);
171 if (error != NULL && *error != NULL)
173 g_free (full_path);
174 full_path = NULL;
176 return full_path;
179 /* --------------------------------------------------------------------------------------------- */
181 static char *
182 mc_config_get_deprecated_path (void)
184 return g_build_filename (mc_config_get_home_dir (), MC_OLD_USERCONF_DIR, NULL);
187 /* --------------------------------------------------------------------------------------------- */
189 static void
190 mc_config_copy (const char *old_name, const char *new_name, GError ** error)
192 if (g_file_test (old_name, G_FILE_TEST_IS_REGULAR))
194 char *contents = NULL;
195 size_t length;
197 if (g_file_get_contents (old_name, &contents, &length, error))
198 g_file_set_contents (new_name, contents, length, error);
200 g_free (contents);
201 return;
204 if (g_file_test (old_name, G_FILE_TEST_IS_DIR))
207 GDir *dir;
208 const char *dir_name;
210 dir = g_dir_open (old_name, 0, error);
211 if (dir == NULL)
212 return;
214 if (g_mkdir_with_parents (new_name, 0700) == -1)
216 g_dir_close (dir);
217 g_propagate_error (error,
218 g_error_new (MC_ERROR, 0,
220 ("An error occured while migrating user settings: %s"),
221 unix_error_string (errno)));
222 return;
225 while ((dir_name = g_dir_read_name (dir)) != NULL)
227 char *old_name2, *new_name2;
229 old_name2 = g_build_filename (old_name, dir_name, NULL);
230 new_name2 = g_build_filename (new_name, dir_name, NULL);
231 mc_config_copy (old_name2, new_name2, error);
232 g_free (new_name2);
233 g_free (old_name2);
238 /* --------------------------------------------------------------------------------------------- */
240 #if MC_HOMEDIR_XDG
241 static void
242 mc_config_fix_migrated_rules (void)
244 size_t rule_index;
246 for (rule_index = 0; mc_config_migrate_rules_fix[rule_index].old_basedir != NULL; rule_index++)
248 char *old_name;
250 old_name =
251 g_build_filename (*mc_config_migrate_rules_fix[rule_index].old_basedir,
252 mc_config_migrate_rules_fix[rule_index].filename, NULL);
254 if (g_file_test (old_name, G_FILE_TEST_EXISTS))
256 char *new_name;
257 const char *basedir = *mc_config_migrate_rules_fix[rule_index].new_basedir;
258 const char *filename = mc_config_migrate_rules_fix[rule_index].filename;
260 new_name = g_build_filename (basedir, filename, NULL);
261 rename (old_name, new_name);
262 g_free (new_name);
264 g_free (old_name);
267 #endif /* MC_HOMEDIR_XDG */
269 /* --------------------------------------------------------------------------------------------- */
271 static gboolean
272 mc_config_deprecated_dir_present (void)
274 char *old_dir;
275 gboolean is_present;
277 old_dir = mc_config_get_deprecated_path ();
278 is_present = g_file_test (old_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR);
279 g_free (old_dir);
281 return is_present && !config_dir_present;
284 /* --------------------------------------------------------------------------------------------- */
285 /*** public functions ****************************************************************************/
286 /* --------------------------------------------------------------------------------------------- */
288 void
289 mc_config_init_config_paths (GError ** error)
291 char *dir;
293 if (xdg_vars_initialized)
294 return;
296 /* init mc_home and homedir if not yet */
297 (void) mc_config_get_home_dir ();
299 #ifdef MC_HOMEDIR_XDG
300 if (mc_home != NULL)
302 dir = g_build_filename (mc_home, ".config", (char *) NULL);
303 mc_config_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, error);
304 g_free (dir);
306 dir = g_build_filename (mc_home, ".cache", (char *) NULL);
307 mc_cache_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, error);
308 g_free (dir);
310 dir = g_build_filename (mc_home, ".local", "share", (char *) NULL);
311 mc_data_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, error);
312 g_free (dir);
314 else
316 dir = (char *) g_get_user_config_dir ();
317 if (dir != NULL && *dir != '\0')
318 mc_config_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, error);
319 else
321 dir = g_build_filename (homedir, ".config", (char *) NULL);
322 mc_config_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, error);
323 g_free (dir);
326 dir = (char *) g_get_user_cache_dir ();
327 if (dir != NULL && *dir != '\0')
328 mc_cache_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, error);
329 else
331 dir = g_build_filename (homedir, ".cache", (char *) NULL);
332 mc_cache_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, error);
333 g_free (dir);
336 dir = (char *) g_get_user_data_dir ();
337 if (dir != NULL && *dir != '\0')
338 mc_data_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, error);
339 else
341 dir = g_build_filename (homedir, ".local", "share", (char *) NULL);
342 mc_data_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, error);
343 g_free (dir);
347 mc_config_fix_migrated_rules ();
348 #else /* MC_HOMEDIR_XDG */
349 char *defined_userconf_dir;
351 defined_userconf_dir = tilde_expand (MC_USERCONF_DIR);
352 if (g_path_is_absolute (defined_userconf_dir))
353 dir = defined_userconf_dir;
354 else
356 g_free (defined_userconf_dir);
357 dir = g_build_filename (mc_config_get_home_dir (), MC_USERCONF_DIR, (char *) NULL);
360 mc_data_str = mc_cache_str = mc_config_str = mc_config_init_one_config_path (dir, "", error);
362 g_free (dir);
363 #endif /* MC_HOMEDIR_XDG */
365 xdg_vars_initialized = TRUE;
368 /* --------------------------------------------------------------------------------------------- */
370 void
371 mc_config_deinit_config_paths (void)
373 if (!xdg_vars_initialized)
374 return;
376 g_free (mc_config_str);
377 #ifdef MC_HOMEDIR_XDG
378 g_free (mc_cache_str);
379 g_free (mc_data_str);
380 #endif /* MC_HOMEDIR_XDG */
382 g_free (mc_global.share_data_dir);
383 g_free (mc_global.sysconfig_dir);
385 xdg_vars_initialized = FALSE;
388 /* --------------------------------------------------------------------------------------------- */
390 const char *
391 mc_config_get_data_path (void)
393 if (!xdg_vars_initialized)
394 mc_config_init_config_paths (NULL);
396 return (const char *) mc_data_str;
399 /* --------------------------------------------------------------------------------------------- */
401 const char *
402 mc_config_get_cache_path (void)
404 if (!xdg_vars_initialized)
405 mc_config_init_config_paths (NULL);
407 return (const char *) mc_cache_str;
410 /* --------------------------------------------------------------------------------------------- */
412 const char *
413 mc_config_get_home_dir (void)
415 if (homedir == NULL)
417 homedir = g_getenv ("MC_HOME");
418 if (homedir == NULL || *homedir == '\0')
419 homedir = g_getenv ("HOME");
420 else
421 mc_home = homedir;
422 if (homedir == NULL || *homedir == '\0')
423 homedir = g_get_home_dir ();
425 return homedir;
428 /* --------------------------------------------------------------------------------------------- */
430 const char *
431 mc_config_get_path (void)
433 if (!xdg_vars_initialized)
434 mc_config_init_config_paths (NULL);
436 return (const char *) mc_config_str;
439 /* --------------------------------------------------------------------------------------------- */
441 gboolean
442 mc_config_migrate_from_old_place (GError ** error, char **msg)
444 char *old_dir;
445 size_t rule_index;
447 if (!mc_config_deprecated_dir_present ())
448 return FALSE;
450 old_dir = mc_config_get_deprecated_path ();
452 g_free (mc_config_init_one_config_path (mc_config_str, EDIT_DIR, error));
453 #ifdef MC_HOMEDIR_XDG
454 g_free (mc_config_init_one_config_path (mc_cache_str, EDIT_DIR, error));
455 g_free (mc_config_init_one_config_path (mc_data_str, EDIT_DIR, error));
456 #endif /* MC_HOMEDIR_XDG */
458 if (*error != NULL)
459 return FALSE;
461 for (rule_index = 0; mc_config_files_reference[rule_index].old_filename != NULL; rule_index++)
463 char *old_name;
464 if (*mc_config_files_reference[rule_index].old_filename == '\0')
465 continue;
467 old_name =
468 g_build_filename (old_dir, mc_config_files_reference[rule_index].old_filename, NULL);
470 if (g_file_test (old_name, G_FILE_TEST_EXISTS))
472 char *new_name;
473 const char *basedir = *mc_config_files_reference[rule_index].new_basedir;
474 const char *filename = mc_config_files_reference[rule_index].new_filename;
476 new_name = g_build_filename (basedir, filename, NULL);
477 mc_config_copy (old_name, new_name, error);
478 g_free (new_name);
480 g_free (old_name);
483 #ifdef MC_HOMEDIR_XDG
484 *msg = g_strdup_printf (_("Your old settings were migrated from %s\n"
485 "to Freedesktop recommended dirs.\n"
486 "To get more info, please visit\n"
487 "http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html"),
488 old_dir);
489 #else /* MC_HOMEDIR_XDG */
490 *msg = g_strdup_printf (_("Your old settings were migrated from %s\n"
491 "to %s\n"), old_dir, mc_config_str);
492 #endif /* MC_HOMEDIR_XDG */
494 g_free (old_dir);
496 return TRUE;
499 /* --------------------------------------------------------------------------------------------- */
501 * Get full path to config file by short name.
503 * @param config_name short name
504 * @return full path to config file
507 char *
508 mc_config_get_full_path (const char *config_name)
510 size_t rule_index;
512 if (config_name == NULL)
513 return NULL;
515 if (!xdg_vars_initialized)
516 mc_config_init_config_paths (NULL);
518 for (rule_index = 0; mc_config_files_reference[rule_index].old_filename != NULL; rule_index++)
520 if (strcmp (config_name, mc_config_files_reference[rule_index].new_filename) == 0)
522 return g_build_filename (*mc_config_files_reference[rule_index].new_basedir,
523 mc_config_files_reference[rule_index].new_filename, NULL);
526 return NULL;
529 /* --------------------------------------------------------------------------------------------- */
531 * Get full path to config file by short name.
533 * @param config_name short name
534 * @return object with full path to config file
537 vfs_path_t *
538 mc_config_get_full_vpath (const char *config_name)
540 vfs_path_t *ret_vpath;
541 char *str_path;
543 str_path = mc_config_get_full_path (config_name);
545 ret_vpath = vfs_path_from_str (str_path);
546 g_free (str_path);
547 return ret_vpath;
550 /* --------------------------------------------------------------------------------------------- */