root/lib/mcconfig/paths.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. mc_config_mkdir
  2. mc_config_init_one_config_path
  3. mc_config_init_config_paths
  4. mc_config_deinit_config_paths
  5. mc_config_get_data_path
  6. mc_config_get_cache_path
  7. mc_config_get_home_dir
  8. mc_config_get_path
  9. mc_config_get_full_path
  10. mc_config_get_full_vpath

   1 /*
   2    paths to configuration files
   3 
   4    Copyright (C) 2010-2020
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Slava Zanko <slavazanko@gmail.com>, 2010.
   9 
  10    This file is part of the Midnight Commander.
  11 
  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.
  16 
  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.
  21 
  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/>.
  24  */
  25 
  26 #include <config.h>
  27 
  28 #include <stdio.h>
  29 #include <stdlib.h>
  30 #include <errno.h>
  31 
  32 #include "lib/global.h"
  33 #include "lib/fileloc.h"
  34 #include "lib/vfs/vfs.h"
  35 #include "lib/util.h"           /* unix_error_string() */
  36 
  37 #include "lib/mcconfig.h"
  38 
  39 /*** global variables ****************************************************************************/
  40 
  41 /*** file scope macro definitions ****************************************************************/
  42 
  43 /*** file scope type declarations ****************************************************************/
  44 
  45 /*** file scope variables ************************************************************************/
  46 
  47 static gboolean xdg_vars_initialized = FALSE;
  48 static char *mc_config_str = NULL;
  49 static char *mc_cache_str = NULL;
  50 static char *mc_data_str = NULL;
  51 
  52 static gboolean config_dir_present = FALSE;
  53 
  54 static const struct
  55 {
  56     char **basedir;
  57     const char *filename;
  58 } mc_config_files_reference[] =
  59 {
  60     /* *INDENT-OFF* */
  61     /* config */
  62     { &mc_config_str, MC_CONFIG_FILE },
  63     { &mc_config_str, MC_FHL_INI_FILE },
  64     { &mc_config_str, MC_HOTLIST_FILE },
  65     { &mc_config_str, GLOBAL_KEYMAP_FILE },
  66     { &mc_config_str, MC_USERMENU_FILE },
  67     { &mc_config_str, EDIT_HOME_SYNTAX_FILE },
  68     { &mc_config_str, EDIT_HOME_MENU },
  69     { &mc_config_str, MC_PANELS_FILE },
  70 
  71     /* User should move this file with applying some changes in file */
  72     { &mc_config_str, MC_FILEBIND_FILE },
  73 
  74     /* data */
  75     { &mc_data_str, MC_SKINS_DIR },
  76     { &mc_data_str, FISH_PREFIX },
  77     { &mc_data_str, "ashrc" },
  78     { &mc_data_str, "bashrc" },
  79     { &mc_data_str, "inputrc" },
  80     { &mc_data_str, MC_EXTFS_DIR },
  81     { &mc_data_str, MC_HISTORY_FILE },
  82     { &mc_data_str, MC_FILEPOS_FILE },
  83     { &mc_data_str, EDIT_HOME_CLIP_FILE },
  84     { &mc_data_str, MC_MACRO_FILE },
  85 
  86     /* cache */
  87     { &mc_cache_str, "mc.log" },
  88     { &mc_cache_str, MC_TREESTORE_FILE },
  89     { &mc_cache_str, EDIT_HOME_TEMP_FILE },
  90     { &mc_cache_str, EDIT_HOME_BLOCK_FILE },
  91 
  92     { NULL, NULL }
  93     /* *INDENT-ON* */
  94 };
  95 
  96 /* --------------------------------------------------------------------------------------------- */
  97 /*** file scope functions *********************************************************************** */
  98 /* --------------------------------------------------------------------------------------------- */
  99 
 100 static void
 101 mc_config_mkdir (const char *directory_name, GError ** mcerror)
     /* [previous][next][first][last][top][bottom][index][help]  */
 102 {
 103     mc_return_if_error (mcerror);
 104 
 105     if ((!g_file_test (directory_name, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) &&
 106         (g_mkdir_with_parents (directory_name, 0700) != 0))
 107         mc_propagate_error (mcerror, 0, _("Cannot create %s directory"), directory_name);
 108 }
 109 
 110 /* --------------------------------------------------------------------------------------------- */
 111 
 112 static char *
 113 mc_config_init_one_config_path (const char *path_base, const char *subdir, GError ** mcerror)
     /* [previous][next][first][last][top][bottom][index][help]  */
 114 {
 115     char *full_path;
 116 
 117     mc_return_val_if_error (mcerror, FALSE);
 118 
 119     full_path = g_build_filename (path_base, subdir, (char *) NULL);
 120 
 121     if (g_file_test (full_path, G_FILE_TEST_EXISTS))
 122     {
 123         if (g_file_test (full_path, G_FILE_TEST_IS_DIR))
 124             config_dir_present = TRUE;
 125         else
 126         {
 127             fprintf (stderr, "%s %s\n", _("FATAL: not a directory:"), full_path);
 128             exit (EXIT_FAILURE);
 129         }
 130     }
 131 
 132     mc_config_mkdir (full_path, mcerror);
 133     if (mcerror != NULL && *mcerror != NULL)
 134         MC_PTR_FREE (full_path);
 135 
 136     return full_path;
 137 }
 138 
 139 /* --------------------------------------------------------------------------------------------- */
 140 /*** public functions ****************************************************************************/
 141 /* --------------------------------------------------------------------------------------------- */
 142 
 143 void
 144 mc_config_init_config_paths (GError ** mcerror)
     /* [previous][next][first][last][top][bottom][index][help]  */
 145 {
 146     const char *profile_root;
 147     char *dir;
 148 
 149     mc_return_if_error (mcerror);
 150 
 151     if (xdg_vars_initialized)
 152         return;
 153 
 154     profile_root = mc_get_profile_root ();
 155 
 156     if (strcmp (profile_root, mc_config_get_home_dir ()) != 0)
 157     {
 158         /*
 159          * The user overrode the default profile root.
 160          *
 161          * In this case we can't use GLib's g_get_user_{config,cache,data}_dir()
 162          * as these functions use the user's home dir as the root.
 163          */
 164 
 165         dir = g_build_filename (profile_root, ".config", (char *) NULL);
 166         mc_config_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
 167         g_free (dir);
 168 
 169         dir = g_build_filename (profile_root, ".cache", (char *) NULL);
 170         mc_cache_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
 171         g_free (dir);
 172 
 173         dir = g_build_filename (profile_root, ".local", "share", (char *) NULL);
 174         mc_data_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
 175         g_free (dir);
 176     }
 177     else
 178     {
 179         mc_config_str =
 180             mc_config_init_one_config_path (g_get_user_config_dir (), MC_USERCONF_DIR, mcerror);
 181         mc_cache_str =
 182             mc_config_init_one_config_path (g_get_user_cache_dir (), MC_USERCONF_DIR, mcerror);
 183         mc_data_str =
 184             mc_config_init_one_config_path (g_get_user_data_dir (), MC_USERCONF_DIR, mcerror);
 185     }
 186 
 187     xdg_vars_initialized = TRUE;
 188 }
 189 
 190 /* --------------------------------------------------------------------------------------------- */
 191 
 192 void
 193 mc_config_deinit_config_paths (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 194 {
 195     if (!xdg_vars_initialized)
 196         return;
 197 
 198     g_free (mc_config_str);
 199     g_free (mc_cache_str);
 200     g_free (mc_data_str);
 201 
 202     g_free (mc_global.share_data_dir);
 203     g_free (mc_global.sysconfig_dir);
 204 
 205     xdg_vars_initialized = FALSE;
 206 }
 207 
 208 /* --------------------------------------------------------------------------------------------- */
 209 
 210 const char *
 211 mc_config_get_data_path (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 212 {
 213     if (!xdg_vars_initialized)
 214         mc_config_init_config_paths (NULL);
 215 
 216     return (const char *) mc_data_str;
 217 }
 218 
 219 /* --------------------------------------------------------------------------------------------- */
 220 
 221 const char *
 222 mc_config_get_cache_path (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 223 {
 224     if (!xdg_vars_initialized)
 225         mc_config_init_config_paths (NULL);
 226 
 227     return (const char *) mc_cache_str;
 228 }
 229 
 230 /* --------------------------------------------------------------------------------------------- */
 231 
 232 const char *
 233 mc_config_get_home_dir (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 234 {
 235     static const char *homedir = NULL;
 236 
 237     if (homedir == NULL)
 238     {
 239         /* Prior to GLib 2.36, g_get_home_dir() ignores $HOME, which is why
 240          * we read it ourselves. As that function's documentation explains,
 241          * using $HOME is good for compatibility with other programs and
 242          * for running from test frameworks. */
 243         homedir = g_getenv ("HOME");
 244         if (homedir == NULL || *homedir == '\0')
 245             homedir = g_get_home_dir ();
 246     }
 247 
 248     return homedir;
 249 }
 250 
 251 /* --------------------------------------------------------------------------------------------- */
 252 
 253 const char *
 254 mc_config_get_path (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 255 {
 256     if (!xdg_vars_initialized)
 257         mc_config_init_config_paths (NULL);
 258 
 259     return (const char *) mc_config_str;
 260 }
 261 
 262 /* --------------------------------------------------------------------------------------------- */
 263 /**
 264  * Get full path to config file by short name.
 265  *
 266  * @param config_name short name
 267  * @return full path to config file
 268  */
 269 
 270 char *
 271 mc_config_get_full_path (const char *config_name)
     /* [previous][next][first][last][top][bottom][index][help]  */
 272 {
 273     size_t rule_index;
 274 
 275     if (config_name == NULL)
 276         return NULL;
 277 
 278     if (!xdg_vars_initialized)
 279         mc_config_init_config_paths (NULL);
 280 
 281     for (rule_index = 0; mc_config_files_reference[rule_index].filename != NULL; rule_index++)
 282         if (strcmp (config_name, mc_config_files_reference[rule_index].filename) == 0)
 283             return g_build_filename (*mc_config_files_reference[rule_index].basedir,
 284                                      mc_config_files_reference[rule_index].filename, (char *) NULL);
 285 
 286     return NULL;
 287 }
 288 
 289 /* --------------------------------------------------------------------------------------------- */
 290 /**
 291  * Get full path to config file by short name.
 292  *
 293  * @param config_name short name
 294  * @return object with full path to config file
 295  */
 296 
 297 vfs_path_t *
 298 mc_config_get_full_vpath (const char *config_name)
     /* [previous][next][first][last][top][bottom][index][help]  */
 299 {
 300     vfs_path_t *ret_vpath;
 301     char *str_path;
 302 
 303     str_path = mc_config_get_full_path (config_name);
 304 
 305     ret_vpath = vfs_path_from_str (str_path);
 306     g_free (str_path);
 307 
 308     return ret_vpath;
 309 }
 310 
 311 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */