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

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