123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498 |
- /*
- paths to configuration files
- Copyright (C) 2010 The Free Software Foundation, Inc.
- Written by:
- Slava Zanko <slavazanko@gmail.com>, 2010.
- This file is part of the Midnight Commander.
- The Midnight Commander is free software; you can redistribute it
- and/or modify it under the terms of the GNU General Public License as
- published by the Free Software Foundation; either version 2 of the
- License, or (at your option) any later version.
- The Midnight Commander is distributed in the hope that it will be
- useful, but WITHOUT ANY WARRANTY; without even the implied warranty
- of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- MA 02110-1301, USA.
- */
- #include <config.h>
- #include <stdio.h>
- #include <errno.h>
- #include "lib/global.h"
- #include "lib/mcconfig.h"
- #include "lib/fileloc.h"
- #include "lib/vfs/mc-vfs/vfs.h"
- /*** global variables ****************************************************************************/
- /* mc_sysconfig_dir: Area for default settings from maintainers of distributuves
- default is /etc/mc or may be defined by MC_DATADIR
- */
- char *mc_sysconfig_dir = NULL;
- /* mc_share_data_dir: Area for default settings from developers */
- char *mc_share_data_dir = NULL;
- /*** file scope macro definitions ****************************************************************/
- /*** file scope type declarations ****************************************************************/
- /*** file scope variables ************************************************************************/
- static gboolean xdg_vars_initialized = FALSE;
- static char *xdg_config = NULL;
- static char *xdg_cache = NULL;
- static char *xdg_data = NULL;
- static const char *homedir = NULL;
- static gboolean config_dir_present = FALSE;
- static const struct
- {
- const char *old_filename;
- char **new_basedir;
- const char *new_filename;
- } mc_config_migrate_rules[] =
- {
- /* *INDENT-OFF* */
- /* config */
- { "ini", &xdg_config, MC_CONFIG_FILE},
- { "filehighlight.ini", &xdg_config, MC_FHL_INI_FILE},
- { "hotlist", &xdg_config, MC_HOTLIST_FILE},
- { "mc.keymap", &xdg_config, GLOBAL_KEYMAP_FILE},
- /* data */
- { "skins", &xdg_data, MC_SKINS_SUBDIR},
- { "fish", &xdg_data, FISH_PREFIX},
- { "bindings", &xdg_data, MC_FILEBIND_FILE},
- { "menu", &xdg_data, MC_USERMENU_FILE},
- { "bashrc", &xdg_data, "bashrc"},
- { "inputrc", &xdg_data, "inputrc"},
- { "extfs.d", &xdg_data, MC_EXTFS_DIR},
- { "cedit" PATH_SEP_STR "cooledit.macros", &xdg_data, EDIT_MACRO_FILE},
- { "cedit" PATH_SEP_STR "Syntax", &xdg_data, EDIT_SYNTAX_FILE},
- { "cedit" PATH_SEP_STR "menu", &xdg_data, EDIT_HOME_MENU},
- { "cedit" PATH_SEP_STR "edit.indent.rc", &xdg_data, EDIT_DIR PATH_SEP_STR "edit.indent.rc"},
- { "cedit" PATH_SEP_STR "edit.spell.rc", &xdg_data, EDIT_DIR PATH_SEP_STR "edit.spell.rc"},
- /* cache */
- { "history", &xdg_cache, MC_HISTORY_FILE},
- { "panels.ini", &xdg_cache, MC_PANELS_FILE},
- { "log", &xdg_cache, "mc.log"},
- { "filepos", &xdg_cache, MC_FILEPOS_FILE},
- { "Tree", &xdg_cache, MC_TREESTORE_FILE},
- { "cedit" PATH_SEP_STR "cooledit.clip", &xdg_cache, EDIT_CLIP_FILE},
- { "cedit" PATH_SEP_STR "cooledit.temp", &xdg_cache, EDIT_TEMP_FILE},
- { "cedit" PATH_SEP_STR "cooledit.block", &xdg_cache, EDIT_BLOCK_FILE},
- {NULL, NULL, NULL}
- /* *INDENT-ON* */
- };
- /*** file scope functions *********************************************************************** */
- /* --------------------------------------------------------------------------------------------- */
- static void
- mc_config_mkdir (const char *directory_name, GError ** error)
- {
- if ((!g_file_test (directory_name, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) &&
- (g_mkdir_with_parents (directory_name, 0700) != 0))
- {
- g_propagate_error (error,
- g_error_new (MC_ERROR, 0, _("Cannot create %s directory"),
- directory_name));
- }
- }
- /* --------------------------------------------------------------------------------------------- */
- static char *
- mc_config_init_one_config_path (const char *path_base, const char *subdir, GError ** error)
- {
- char *full_path;
- full_path = g_build_filename (path_base, subdir, NULL);
- if (g_file_test (full_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
- config_dir_present = TRUE;
- mc_config_mkdir (full_path, error);
- if (error != NULL && *error != NULL)
- {
- g_free (full_path);
- full_path = NULL;
- }
- return full_path;
- }
- /* --------------------------------------------------------------------------------------------- */
- static char *
- mc_config_get_deprecated_path (void)
- {
- return g_build_filename (mc_config_get_home_dir (), "." MC_USERCONF_DIR, NULL);
- }
- /* --------------------------------------------------------------------------------------------- */
- static void
- mc_config_copy (const char *old_name, const char *new_name, GError ** error)
- {
- if (error != NULL && *error != NULL)
- return;
- if (g_file_test (old_name, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))
- {
- char *contents = NULL;
- size_t length;
- if (g_file_get_contents (old_name, &contents, &length, error))
- g_file_set_contents (new_name, contents, length, error);
- g_free (contents);
- return;
- }
- if (g_file_test (old_name, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
- {
- GDir *dir;
- const char *dir_name;
- dir = g_dir_open (old_name, 0, error);
- if (dir == NULL)
- return;
- if (!g_mkdir_with_parents (new_name, 0700))
- {
- g_dir_close (dir);
- g_propagate_error (error,
- g_error_new (MC_ERROR, 0,
- _
- ("An error occured while migrating user settings: %s"),
- g_strerror (errno)));
- return;
- }
- while ((dir_name = g_dir_read_name (dir)) != NULL)
- {
- char *old_name2, *new_name2;
- old_name2 = g_build_filename (old_name, dir_name, NULL);
- new_name2 = g_build_filename (new_name, dir_name, NULL);
- mc_config_copy (old_name2, new_name2, error);
- g_free (new_name2);
- g_free (old_name2);
- }
- }
- if (rename (old_name, new_name) != 0)
- {
- g_propagate_error (error,
- g_error_new (MC_ERROR, 0,
- _
- ("An error occured while migrating user settings: %s"),
- g_strerror (errno)));
- }
- }
- /* --------------------------------------------------------------------------------------------- */
- static char *
- mc_config_get_conffile (const char *base_path, const char *sub_path, const char *conf_name)
- {
- char *filename;
- if (sub_path != NULL)
- filename = g_build_filename (base_path, sub_path, conf_name, NULL);
- else
- filename = g_build_filename (base_path, conf_name, NULL);
- if (g_file_test (filename, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))
- return filename;
- g_free (filename);
- return NULL;
- }
- /* --------------------------------------------------------------------------------------------- */
- /*** public functions ****************************************************************************/
- /* --------------------------------------------------------------------------------------------- */
- void
- mc_config_init_config_paths (GError ** error)
- {
- const char *mc_libdir;
- char *u_config_dir = (char *) g_get_user_config_dir ();
- char *u_data_dir = (char *) g_get_user_data_dir ();
- char *u_cache_dir = (char *) g_get_user_cache_dir ();
- if (xdg_vars_initialized)
- return;
- u_config_dir = (u_config_dir == NULL)
- ? g_build_filename (mc_config_get_home_dir (), ".config", NULL) : g_strdup (u_config_dir);
- u_cache_dir = (u_cache_dir == NULL)
- ? g_build_filename (mc_config_get_home_dir (), ".cache", NULL) : g_strdup (u_cache_dir);
- u_data_dir = (u_data_dir == NULL)
- ? g_build_filename (mc_config_get_home_dir (), ".local", "share", NULL)
- : g_strdup (u_data_dir);
- xdg_config = mc_config_init_one_config_path (u_config_dir, MC_USERCONF_DIR, error);
- xdg_cache = mc_config_init_one_config_path (u_cache_dir, MC_USERCONF_DIR, error);
- xdg_data = mc_config_init_one_config_path (u_data_dir, MC_USERCONF_DIR, error);
- g_free (u_data_dir);
- g_free (u_cache_dir);
- g_free (u_config_dir);
- /* This is the directory, where MC was installed, on Unix this is DATADIR */
- /* and can be overriden by the MC_DATADIR environment variable */
- mc_libdir = g_getenv ("MC_DATADIR");
- if (mc_libdir != NULL)
- mc_sysconfig_dir = g_strdup (mc_libdir);
- else
- mc_sysconfig_dir = g_strdup (SYSCONFDIR);
- mc_share_data_dir = g_strdup (DATADIR);
- xdg_vars_initialized = TRUE;
- }
- /* --------------------------------------------------------------------------------------------- */
- void
- mc_config_deinit_config_paths (void)
- {
- if (!xdg_vars_initialized)
- return;
- g_free (xdg_config);
- g_free (xdg_cache);
- g_free (xdg_data);
- g_free (mc_share_data_dir);
- g_free (mc_sysconfig_dir);
- xdg_vars_initialized = FALSE;
- }
- /* --------------------------------------------------------------------------------------------- */
- const char *
- mc_config_get_data_path (void)
- {
- if (!xdg_vars_initialized)
- mc_config_init_config_paths (NULL);
- return (const char *) xdg_data;
- }
- /* --------------------------------------------------------------------------------------------- */
- const char *
- mc_config_get_cache_path (void)
- {
- if (!xdg_vars_initialized)
- mc_config_init_config_paths (NULL);
- return (const char *) xdg_cache;
- }
- /* --------------------------------------------------------------------------------------------- */
- const char *
- mc_config_get_home_dir (void)
- {
- if (homedir == NULL)
- {
- homedir = g_getenv ("HOME");
- if (homedir == NULL)
- homedir = g_get_home_dir ();
- }
- return homedir;
- }
- /* --------------------------------------------------------------------------------------------- */
- const char *
- mc_config_get_path (void)
- {
- if (!xdg_vars_initialized)
- mc_config_init_config_paths (NULL);
- return (const char *) xdg_config;
- }
- /* --------------------------------------------------------------------------------------------- */
- void
- mc_config_migrate_from_old_place (GError ** error)
- {
- char *old_dir, *tmp_dir_name;
- size_t rule_index;
- old_dir = mc_config_get_deprecated_path ();
- tmp_dir_name = mc_config_init_one_config_path (xdg_config, EDIT_DIR, error);
- g_free (tmp_dir_name);
- tmp_dir_name = mc_config_init_one_config_path (xdg_cache, EDIT_DIR, error);
- g_free (tmp_dir_name);
- tmp_dir_name = mc_config_init_one_config_path (xdg_data, EDIT_DIR, error);
- g_free (tmp_dir_name);
- for (rule_index = 0; mc_config_migrate_rules[rule_index].old_filename != NULL; rule_index++)
- {
- char *old_name, *new_name;
- old_name =
- g_build_filename (old_dir, mc_config_migrate_rules[rule_index].old_filename, NULL);
- if (!g_file_test (old_name, G_FILE_TEST_EXISTS))
- {
- g_free (old_name);
- continue;
- }
- new_name = g_build_filename (*mc_config_migrate_rules[rule_index].new_basedir,
- mc_config_migrate_rules[rule_index].new_filename, NULL);
- mc_config_copy (old_name, new_name, error);
- g_free (new_name);
- g_free (old_name);
- }
- /*
- {
- char *old_dir2;
- old_dir2 = g_strconcat (old_dir, "~", NULL);
- rename (old_dir, old_dir2);
- g_free (old_dir2);
- }
- */
- g_propagate_error (error,
- g_error_new (MC_ERROR, 0,
- _
- ("Your old settings were migrated from %s\n"
- "to Freedesktop recommended dirs.\n"
- "To get more info, please visit\n"
- "http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html"),
- old_dir));
- g_free (old_dir);
- }
- /* --------------------------------------------------------------------------------------------- */
- gboolean
- mc_config_deprecated_dir_present (void)
- {
- char *old_dir = mc_config_get_deprecated_path ();
- gboolean is_present = g_file_test (old_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR);
- g_free (old_dir);
- return is_present && !config_dir_present;
- }
- /* --------------------------------------------------------------------------------------------- */
- char *
- mc_config_search_sysconffile (const char *sub_path, const char *conf_name)
- {
- char *filename;
- filename = mc_config_get_conffile (mc_sysconfig_dir, sub_path, conf_name);
- if (filename != NULL)
- return filename;
- filename = mc_config_get_conffile (mc_share_data_dir, sub_path, conf_name);
- if (filename != NULL)
- return filename;
- return NULL;
- }
- /* --------------------------------------------------------------------------------------------- */
- char *
- mc_config_search_conffile (const char *base_path, const char *sub_path, const char *conf_name)
- {
- char *filename;
- if (conf_name == NULL)
- return NULL;
- if (g_path_is_absolute (conf_name))
- return g_strdup (conf_name);
- filename = mc_config_get_conffile (base_path, sub_path, conf_name);
- if (filename != NULL)
- return filename;
- return mc_config_search_sysconffile (sub_path, conf_name);
- }
- /* --------------------------------------------------------------------------------------------- */
- void
- mc_config_load_all_from_paths (const char *base_path, const char *sub_path, mc_config_t ** config,
- const char *conf_name)
- {
- char *filename;
- /* 1) /usr/share/mc (mc_share_data_dir) */
- filename = mc_config_get_conffile (mc_share_data_dir, sub_path, conf_name);
- mc_config_init_or_append_data (config, filename);
- g_free (filename);
- /* 2) /etc/mc (mc_sysconfig_dir) */
- filename = mc_config_get_conffile (mc_sysconfig_dir, sub_path, conf_name);
- mc_config_init_or_append_data (config, filename);
- g_free (filename);
- /* 3) base_path */
- if (g_path_is_absolute (conf_name))
- filename = g_strdup (conf_name);
- else
- filename = mc_config_get_conffile (base_path, sub_path, conf_name);
- mc_config_init_or_append_data (config, filename);
- g_free (filename);
- }
- /* --------------------------------------------------------------------------------------------- */
- /**
- Create new mc_config object from specified ini-file or
- append data to existing mc_config object from ini-file
- */
- void
- mc_config_init_or_append_data (mc_config_t ** config, const char *fname)
- {
- if (fname != NULL && g_file_test (fname, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))
- {
- if (*config != NULL)
- mc_config_read_file (*config, fname);
- else
- *config = mc_config_init (fname);
- }
- }
- /* --------------------------------------------------------------------------------------------- */
|