From 239a8d01179cd3c05f14f88a0444b5c5757a39e0 Mon Sep 17 00:00:00 2001 From: Mooffie Date: Thu, 8 Sep 2016 17:02:43 +0300 Subject: [PATCH 1/4] Ticket #3684: replace $MC_HOME with $MC_PROFILE_ROOT, a better "profile" mechanism. Signed-off-by: Andrew Borodin --- lib/mcconfig.h | 2 ++ lib/mcconfig/paths.c | 57 +++++++++++++++++++++++++++++++------------- src/textconf.c | 3 ++- 3 files changed, 45 insertions(+), 17 deletions(-) diff --git a/lib/mcconfig.h b/lib/mcconfig.h index 931342e64..426822b35 100644 --- a/lib/mcconfig.h +++ b/lib/mcconfig.h @@ -107,6 +107,8 @@ const char *mc_config_get_cache_path (void); const char *mc_config_get_path (void); +const char *mc_config_get_profile_root (void); + const char *mc_config_get_home_dir (void); char *mc_config_get_full_path (const char *config_name); diff --git a/lib/mcconfig/paths.c b/lib/mcconfig/paths.c index 7c3462234..d2f478a91 100644 --- a/lib/mcconfig/paths.c +++ b/lib/mcconfig/paths.c @@ -51,9 +51,6 @@ static char *mc_config_str = NULL; static char *mc_cache_str = NULL; static char *mc_data_str = NULL; -/* value of $MC_HOME */ -static const char *mc_home = NULL; - static gboolean config_dir_present = FALSE; static const struct @@ -282,6 +279,30 @@ mc_config_deprecated_dir_present (void) /*** public functions ****************************************************************************/ /* --------------------------------------------------------------------------------------------- */ +/** + * The "profile root" is the tree under which all of MC's user data & + * settings are stored. + * + * It defaults to the user's home dir. The user may override this default + * with the environment variable $MC_PROFILE_ROOT. + */ +const char * +mc_config_get_profile_root (void) +{ + static const char *profile_root = NULL; + + if (profile_root == NULL) + { + profile_root = g_getenv ("MC_PROFILE_ROOT"); + if (profile_root == NULL || *profile_root == '\0') + profile_root = mc_config_get_home_dir (); + } + + return profile_root; +} + +/* --------------------------------------------------------------------------------------------- */ + void mc_config_init_config_paths (GError ** mcerror) { @@ -295,21 +316,29 @@ mc_config_init_config_paths (GError ** mcerror) if (xdg_vars_initialized) return; - /* init mc_home if not yet */ - (void) mc_config_get_home_dir (); - #if MC_HOMEDIR_XDG - if (mc_home != NULL) + if (strcmp (mc_config_get_profile_root (), mc_config_get_home_dir ()) != 0) { - dir = g_build_filename (mc_home, ".config", (char *) NULL); + /* + * The user overrode the default profile root. + * + * In this case we can't use GLib's g_get_user_{config,cache,data}_dir() + * as these functions use the user's home dir as the root. + */ + + const char *profile_root; + + profile_root = mc_config_get_profile_root (); + + dir = g_build_filename (profile_root, ".config", (char *) NULL); mc_config_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror); g_free (dir); - dir = g_build_filename (mc_home, ".cache", (char *) NULL); + dir = g_build_filename (profile_root, ".cache", (char *) NULL); mc_cache_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror); g_free (dir); - dir = g_build_filename (mc_home, ".local", "share", (char *) NULL); + dir = g_build_filename (profile_root, ".local", "share", (char *) NULL); mc_data_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror); g_free (dir); } @@ -331,7 +360,7 @@ mc_config_init_config_paths (GError ** mcerror) else { g_free (defined_userconf_dir); - dir = g_build_filename (mc_config_get_home_dir (), MC_USERCONF_DIR, (char *) NULL); + dir = g_build_filename (mc_config_get_profile_root (), MC_USERCONF_DIR, (char *) NULL); } mc_data_str = mc_cache_str = mc_config_str = mc_config_init_one_config_path (dir, "", mcerror); @@ -393,15 +422,11 @@ mc_config_get_home_dir (void) if (homedir == NULL) { - homedir = g_getenv ("MC_HOME"); /* Prior to GLib 2.36, g_get_home_dir() ignores $HOME, which is why * we read it ourselves. As that function's documentation explains, * using $HOME is good for compatibility with other programs and * for running from test frameworks. */ - if (homedir == NULL || *homedir == '\0') - homedir = g_getenv ("HOME"); - else - mc_home = homedir; + homedir = g_getenv ("HOME"); if (homedir == NULL || *homedir == '\0') homedir = g_get_home_dir (); } diff --git a/src/textconf.c b/src/textconf.c index d6ee6717e..ca648f20f 100644 --- a/src/textconf.c +++ b/src/textconf.c @@ -180,7 +180,8 @@ show_version (void) void show_datadirs_extended (void) { - (void) printf ("%s %s\n", _("Root directory:"), mc_config_get_home_dir ()); + (void) printf ("%s %s\n", _("Home directory:"), mc_config_get_home_dir ()); + (void) printf ("%s %s\n", _("Profile root directory:"), mc_config_get_profile_root ()); (void) puts (""); PRINTF_GROUP (_("System data")); From d1ad225177a937843a31908bf118ba4f74619abe Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Tue, 29 Nov 2016 11:17:50 +0300 Subject: [PATCH 2/4] Fix manual pages. Signed-off-by: Andrew Borodin --- doc/man/es/mc.1.in | 8 ++++---- doc/man/hu/mc.1.in | 8 ++++---- doc/man/it/mc.1.in | 8 ++++---- doc/man/mc.1.in | 8 ++++---- doc/man/pl/mc.1.in | 8 ++++---- doc/man/ru/mc.1.in | 4 ++-- 6 files changed, 22 insertions(+), 22 deletions(-) diff --git a/doc/man/es/mc.1.in b/doc/man/es/mc.1.in index 30165fdf0..2f07a0be8 100644 --- a/doc/man/es/mc.1.in +++ b/doc/man/es/mc.1.in @@ -4258,10 +4258,10 @@ personal o de sistema. .PP Para cambiar el directorio de incio de MC se puede utilizar la variable de entorno -.BR MC_HOME . -El valor de MC_HOME tiene que ser una ruta absoluta. Si MC_HOME no existe o -está vacía se usa la variable HOME. Si HOME no existe o está vacía se recurre -a la biblioteca GLib para obtener los directorios de MC. +.BR MC_PROFILE_ROOT . +El valor de MC_PROFILE_ROOT tiene que ser una ruta absoluta. Si MC_PROFILE_ROOT +no existe o está vacía se usa la variable HOME. Si HOME no existe o está vacía +se recurre a la biblioteca GLib para obtener los directorios de MC. .\"SKIP_SECTION" .SH "LICENCIA" Este programa se distribuye en los términos que recoge la Licencia Pública diff --git a/doc/man/hu/mc.1.in b/doc/man/hu/mc.1.in index 2a67463c0..229928250 100644 --- a/doc/man/hu/mc.1.in +++ b/doc/man/hu/mc.1.in @@ -3242,10 +3242,10 @@ Helyi felhasználó által definiált menü. Ha ez a fájl létezik, ezt használja a home, vagy rendszerszintű alkalmazás menü helyett. .PP To change default home directory of MC, you can use -.BR MC_HOME -environment variable. The value of MC_HOME must be an absolute path. If MC_HOME -is unset or empty, HOME variable is used. If HOME is unset or empty, MC -directories are get from GLib library. +.BR MC_PROFILE_ROOT +environment variable. The value of MC_PROFILE_ROOT must be an absolute path. +If MC_PROFILE_ROOT is unset or empty, HOME variable is used. If HOME is unset +or empty, MC directories are get from GLib library. .\"NODE "AVAILABILITY" .SH "A Midnight Commander frissítése" A program legutolsó verzióját az ftp.nuclecu.unam.mx címen a diff --git a/doc/man/it/mc.1.in b/doc/man/it/mc.1.in index e549f19ea..cbe9a2e7e 100644 --- a/doc/man/it/mc.1.in +++ b/doc/man/it/mc.1.in @@ -3231,10 +3231,10 @@ Menu locale definito dall'utente. Se questo file è presente viene usato al posto del menu delle applicazioni utente o di sistema. .PP To change default home directory of MC, you can use -.BR MC_HOME -environment variable. The value of MC_HOME must be an absolute path. If MC_HOME -is unset or empty, HOME variable is used. If HOME is unset or empty, MC -directories are get from GLib library. +.BR MC_PROFILE_ROOT +environment variable. The value of MC_PROFILE_ROOT must be an absolute path. +If MC_PROFILE_ROOT is unset or empty, HOME variable is used. If HOME is unset +or empty, MC directories are get from GLib library. .\"SKIP_SECTION" .SH "LICENZA" Questo programma è distribuito sotto i termini della Licenza Generale diff --git a/doc/man/mc.1.in b/doc/man/mc.1.in index 1d6cb16de..b63223e86 100644 --- a/doc/man/mc.1.in +++ b/doc/man/mc.1.in @@ -4341,10 +4341,10 @@ Local user\-defined menu. If this file is present, it is used instead of the home or system\-wide applications menu. .PP To change default root directory of MC, you can use -.BR MC_HOME -environment variable. The value of MC_HOME must be an absolute path. If MC_HOME -is unset or empty, HOME variable is used. If HOME is unset or empty, MC -directories are get from GLib library. +.BR MC_PROFILE_ROOT +environment variable. The value of MC_PROFILE_ROOT must be an absolute path. +If MC_PROFILE_ROOT is unset or empty, HOME variable is used. If HOME is unset +or empty, MC directories are get from GLib library. .\"SKIP_SECTION" .SH "LICENSE" This program is distributed under the terms of the GNU General Public diff --git a/doc/man/pl/mc.1.in b/doc/man/pl/mc.1.in index 4d7018d54..89085fbf4 100644 --- a/doc/man/pl/mc.1.in +++ b/doc/man/pl/mc.1.in @@ -2939,10 +2939,10 @@ Lokalny plik zdefiniowany przez użytkownika. Jeśli ten plik jest dostępny, jest używany zamiast pliku w katalogu domowym i ogólnosystemowego. .PP To change default home directory of MC, you can use -.BR MC_HOME -environment variable. The value of MC_HOME must be an absolute path. If MC_HOME -is unset or empty, HOME variable is used. If HOME is unset or empty, MC -directories are get from GLib library. +.BR MC_PROFILE_ROOT +environment variable. The value of MC_PROFILE_ROOT must be an absolute path. +If MC_PROFILE_ROOT is unset or empty, HOME variable is used. If HOME is unset +or empty, MC directories are get from GLib library. .\"SKIP_SECTION" .SH LICENCJA Program jest dystrybuowany na zasadach licencji GNU General Public License diff --git a/doc/man/ru/mc.1.in b/doc/man/ru/mc.1.in index f50aec653..ebe017fd0 100644 --- a/doc/man/ru/mc.1.in +++ b/doc/man/ru/mc.1.in @@ -4727,8 +4727,8 @@ Commander; используется только в тех случаях, ко .PP Для того, чтобы изменить корневой каталог MC, установленный по умолчанию, можно использовать переменную окружения -.BR MC_HOME . -Значением этой переменный должен быть абсолютный путь. Если переменная MC_HOME +.BR MC_PROFILE_ROOT . +Значением этой переменный должен быть абсолютный путь. Если переменная MC_PROFILE_ROOT не определена или пуста, используется переменная окружения HOME. Если и HOME не определена или пуста, каталоги MC определяются средствами библиотеки GLib. .\"SKIP_SECTION" From 496abc0e1ffa6c5d447e0d4b8195c8568a00d3d3 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Tue, 29 Nov 2016 11:33:10 +0300 Subject: [PATCH 3/4] Rename mc_config_get_profile_root() to mc_get_profile_root() ...and move it out from lib/mcconfig. Signed-off-by: Andrew Borodin --- lib/mcconfig.h | 2 -- lib/mcconfig/paths.c | 30 +++--------------------------- lib/util.c | 24 ++++++++++++++++++++++++ lib/util.h | 2 ++ src/textconf.c | 3 ++- 5 files changed, 31 insertions(+), 30 deletions(-) diff --git a/lib/mcconfig.h b/lib/mcconfig.h index 426822b35..931342e64 100644 --- a/lib/mcconfig.h +++ b/lib/mcconfig.h @@ -107,8 +107,6 @@ const char *mc_config_get_cache_path (void); const char *mc_config_get_path (void); -const char *mc_config_get_profile_root (void); - const char *mc_config_get_home_dir (void); char *mc_config_get_full_path (const char *config_name); diff --git a/lib/mcconfig/paths.c b/lib/mcconfig/paths.c index d2f478a91..149d412ca 100644 --- a/lib/mcconfig/paths.c +++ b/lib/mcconfig/paths.c @@ -279,30 +279,6 @@ mc_config_deprecated_dir_present (void) /*** public functions ****************************************************************************/ /* --------------------------------------------------------------------------------------------- */ -/** - * The "profile root" is the tree under which all of MC's user data & - * settings are stored. - * - * It defaults to the user's home dir. The user may override this default - * with the environment variable $MC_PROFILE_ROOT. - */ -const char * -mc_config_get_profile_root (void) -{ - static const char *profile_root = NULL; - - if (profile_root == NULL) - { - profile_root = g_getenv ("MC_PROFILE_ROOT"); - if (profile_root == NULL || *profile_root == '\0') - profile_root = mc_config_get_home_dir (); - } - - return profile_root; -} - -/* --------------------------------------------------------------------------------------------- */ - void mc_config_init_config_paths (GError ** mcerror) { @@ -317,7 +293,7 @@ mc_config_init_config_paths (GError ** mcerror) return; #if MC_HOMEDIR_XDG - if (strcmp (mc_config_get_profile_root (), mc_config_get_home_dir ()) != 0) + if (strcmp (mc_get_profile_root (), mc_config_get_home_dir ()) != 0) { /* * The user overrode the default profile root. @@ -328,7 +304,7 @@ mc_config_init_config_paths (GError ** mcerror) const char *profile_root; - profile_root = mc_config_get_profile_root (); + profile_root = mc_get_profile_root (); dir = g_build_filename (profile_root, ".config", (char *) NULL); mc_config_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror); @@ -360,7 +336,7 @@ mc_config_init_config_paths (GError ** mcerror) else { g_free (defined_userconf_dir); - dir = g_build_filename (mc_config_get_profile_root (), MC_USERCONF_DIR, (char *) NULL); + dir = g_build_filename (mc_get_profile_root (), MC_USERCONF_DIR, (char *) NULL); } mc_data_str = mc_cache_str = mc_config_str = mc_config_init_one_config_path (dir, "", mcerror); diff --git a/lib/util.c b/lib/util.c index 304351ddd..2112e2dfe 100644 --- a/lib/util.c +++ b/lib/util.c @@ -1418,6 +1418,30 @@ guess_message_value (void) return g_strdup (locale); } +/* --------------------------------------------------------------------------------------------- */ + +/** + * The "profile root" is the tree under which all of MC's user data & + * settings are stored. + * + * It defaults to the user's home dir. The user may override this default + * with the environment variable $MC_PROFILE_ROOT. + */ +const char * +mc_get_profile_root (void) +{ + static const char *profile_root = NULL; + + if (profile_root == NULL) + { + profile_root = g_getenv ("MC_PROFILE_ROOT"); + if (profile_root == NULL || *profile_root == '\0') + profile_root = mc_config_get_home_dir (); + } + + return profile_root; +} + /* --------------------------------------------------------------------------------------------- */ /** * Propagate error in simple way. diff --git a/lib/util.h b/lib/util.h index af4022ee7..6b986a3fd 100644 --- a/lib/util.h +++ b/lib/util.h @@ -249,6 +249,8 @@ char *guess_message_value (void); char *mc_build_filename (const char *first_element, ...); char *mc_build_filenamev (const char *first_element, va_list args); +const char *mc_get_profile_root (void); + /* *INDENT-OFF* */ void mc_propagate_error (GError ** dest, int code, const char *format, ...) G_GNUC_PRINTF (3, 4); void mc_replace_error (GError ** dest, int code, const char *format, ...) G_GNUC_PRINTF (3, 4); diff --git a/src/textconf.c b/src/textconf.c index ca648f20f..d48f17e44 100644 --- a/src/textconf.c +++ b/src/textconf.c @@ -33,6 +33,7 @@ #include "lib/global.h" #include "lib/fileloc.h" #include "lib/mcconfig.h" +#include "lib/util.h" /* mc_get_profile_root() */ #include "src/textconf.h" @@ -181,7 +182,7 @@ void show_datadirs_extended (void) { (void) printf ("%s %s\n", _("Home directory:"), mc_config_get_home_dir ()); - (void) printf ("%s %s\n", _("Profile root directory:"), mc_config_get_profile_root ()); + (void) printf ("%s %s\n", _("Profile root directory:"), mc_get_profile_root ()); (void) puts (""); PRINTF_GROUP (_("System data")); From 8002e522937c2e920d474476afc43ac9cac64f6d Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Thu, 1 Dec 2016 12:00:33 +0300 Subject: [PATCH 4/4] (mc_config_init_config_paths): minor optimization. Signed-off-by: Andrew Borodin --- lib/mcconfig/paths.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/mcconfig/paths.c b/lib/mcconfig/paths.c index 149d412ca..cccdf8b39 100644 --- a/lib/mcconfig/paths.c +++ b/lib/mcconfig/paths.c @@ -282,6 +282,7 @@ mc_config_deprecated_dir_present (void) void mc_config_init_config_paths (GError ** mcerror) { + const char *profile_root; char *dir; #if MC_HOMEDIR_XDG == 0 char *defined_userconf_dir; @@ -292,8 +293,10 @@ mc_config_init_config_paths (GError ** mcerror) if (xdg_vars_initialized) return; + profile_root = mc_get_profile_root (); + #if MC_HOMEDIR_XDG - if (strcmp (mc_get_profile_root (), mc_config_get_home_dir ()) != 0) + if (strcmp (profile_root, mc_config_get_home_dir ()) != 0) { /* * The user overrode the default profile root. @@ -302,10 +305,6 @@ mc_config_init_config_paths (GError ** mcerror) * as these functions use the user's home dir as the root. */ - const char *profile_root; - - profile_root = mc_get_profile_root (); - dir = g_build_filename (profile_root, ".config", (char *) NULL); mc_config_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror); g_free (dir); @@ -336,7 +335,7 @@ mc_config_init_config_paths (GError ** mcerror) else { g_free (defined_userconf_dir); - dir = g_build_filename (mc_get_profile_root (), MC_USERCONF_DIR, (char *) NULL); + dir = g_build_filename (profile_root, MC_USERCONF_DIR, (char *) NULL); } mc_data_str = mc_cache_str = mc_config_str = mc_config_init_one_config_path (dir, "", mcerror);