root/lib/mcconfig/common.c

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

DEFINITIONS

This source file includes following definitions.
  1. mc_config_new_or_override_file
  2. mc_config_init
  3. mc_config_deinit
  4. mc_config_has_param
  5. mc_config_has_group
  6. mc_config_del_key
  7. mc_config_del_group
  8. mc_config_read_file
  9. mc_config_save_file
  10. mc_config_save_to_file

   1 /*
   2    Configure module for the Midnight Commander
   3 
   4    Copyright (C) 1994-2022
   5    Free Software Foundation, Inc.
   6 
   7    This file is part of the Midnight Commander.
   8 
   9    The Midnight Commander is free software: you can redistribute it
  10    and/or modify it under the terms of the GNU General Public License as
  11    published by the Free Software Foundation, either version 3 of the License,
  12    or (at your option) any later version.
  13 
  14    The Midnight Commander is distributed in the hope that it will be useful,
  15    but WITHOUT ANY WARRANTY; without even the implied warranty of
  16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17    GNU General Public License for more details.
  18 
  19    You should have received a copy of the GNU General Public License
  20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  21  */
  22 
  23 #include <config.h>
  24 
  25 #include <sys/types.h>
  26 #include <sys/stat.h>
  27 #include <unistd.h>
  28 #include <errno.h>              /* extern int errno */
  29 
  30 #include "lib/global.h"
  31 #include "lib/vfs/vfs.h"        /* mc_stat */
  32 #include "lib/util.h"
  33 
  34 #include "lib/mcconfig.h"
  35 
  36 /*** global variables ****************************************************************************/
  37 
  38 /*** file scope macro definitions ****************************************************************/
  39 
  40 /*** file scope type declarations ****************************************************************/
  41 
  42 /*** file scope variables ************************************************************************/
  43 
  44 /* --------------------------------------------------------------------------------------------- */
  45 /*** file scope functions ************************************************************************/
  46 /* --------------------------------------------------------------------------------------------- */
  47 
  48 static gboolean
  49 mc_config_new_or_override_file (mc_config_t * mc_config, const gchar * ini_path, GError ** mcerror)
     /* [previous][next][first][last][top][bottom][index][help]  */
  50 {
  51     gchar *data, *written_data;
  52     gsize len, total_written;
  53     gboolean ret;
  54     int fd;
  55     ssize_t cur_written;
  56     vfs_path_t *ini_vpath;
  57 
  58     mc_return_val_if_error (mcerror, FALSE);
  59 
  60     data = g_key_file_to_data (mc_config->handle, &len, NULL);
  61     if (!exist_file (ini_path))
  62     {
  63         ret = g_file_set_contents (ini_path, data, len, mcerror);
  64         g_free (data);
  65         return ret;
  66     }
  67 
  68     mc_util_make_backup_if_possible (ini_path, "~");
  69 
  70     ini_vpath = vfs_path_from_str (ini_path);
  71     fd = mc_open (ini_vpath, O_WRONLY | O_TRUNC, 0);
  72     vfs_path_free (ini_vpath, TRUE);
  73 
  74     if (fd == -1)
  75     {
  76         mc_propagate_error (mcerror, 0, "%s", unix_error_string (errno));
  77         g_free (data);
  78         return FALSE;
  79     }
  80 
  81     for (written_data = data, total_written = len;
  82          (cur_written = mc_write (fd, (const void *) written_data, total_written)) > 0;
  83          written_data += cur_written, total_written -= cur_written)
  84         ;
  85 
  86     mc_close (fd);
  87     g_free (data);
  88 
  89     if (cur_written == -1)
  90     {
  91         mc_util_restore_from_backup_if_possible (ini_path, "~");
  92         mc_propagate_error (mcerror, 0, "%s", unix_error_string (errno));
  93         return FALSE;
  94     }
  95 
  96     mc_util_unlink_backup_if_possible (ini_path, "~");
  97     return TRUE;
  98 }
  99 
 100 /* --------------------------------------------------------------------------------------------- */
 101 /*** public functions ****************************************************************************/
 102 /* --------------------------------------------------------------------------------------------- */
 103 
 104 mc_config_t *
 105 mc_config_init (const gchar * ini_path, gboolean read_only)
     /* [previous][next][first][last][top][bottom][index][help]  */
 106 {
 107     mc_config_t *mc_config;
 108     struct stat st;
 109 
 110     mc_config = g_try_malloc0 (sizeof (mc_config_t));
 111     if (mc_config == NULL)
 112         return NULL;
 113 
 114     mc_config->handle = g_key_file_new ();
 115     if (mc_config->handle == NULL)
 116     {
 117         g_free (mc_config);
 118         return NULL;
 119     }
 120 
 121     if (ini_path == NULL)
 122         return mc_config;
 123 
 124     if (exist_file (ini_path))
 125     {
 126         vfs_path_t *vpath;
 127 
 128         vpath = vfs_path_from_str (ini_path);
 129         if (mc_stat (vpath, &st) == 0 && st.st_size != 0)
 130         {
 131             GKeyFileFlags flags = G_KEY_FILE_NONE;
 132 
 133             if (!read_only)
 134                 flags |= G_KEY_FILE_KEEP_COMMENTS;
 135 
 136             /* file exists and not empty */
 137             g_key_file_load_from_file (mc_config->handle, ini_path, flags, NULL);
 138         }
 139         vfs_path_free (vpath, TRUE);
 140     }
 141 
 142     mc_config->ini_path = g_strdup (ini_path);
 143     return mc_config;
 144 }
 145 
 146 /* --------------------------------------------------------------------------------------------- */
 147 
 148 void
 149 mc_config_deinit (mc_config_t * mc_config)
     /* [previous][next][first][last][top][bottom][index][help]  */
 150 {
 151     if (mc_config != NULL)
 152     {
 153         g_free (mc_config->ini_path);
 154         g_key_file_free (mc_config->handle);
 155         g_free (mc_config);
 156     }
 157 }
 158 
 159 /* --------------------------------------------------------------------------------------------- */
 160 
 161 gboolean
 162 mc_config_has_param (const mc_config_t * mc_config, const char *group, const gchar * param)
     /* [previous][next][first][last][top][bottom][index][help]  */
 163 {
 164     char *value;
 165     gboolean ret;
 166 
 167     g_return_val_if_fail (mc_config != NULL, FALSE);
 168 
 169     value = g_key_file_get_value (mc_config->handle, group, param, NULL);
 170     ret = value != NULL;
 171     g_free (value);
 172 
 173     return ret;
 174 }
 175 
 176 /* --------------------------------------------------------------------------------------------- */
 177 
 178 gboolean
 179 mc_config_has_group (mc_config_t * mc_config, const char *group)
     /* [previous][next][first][last][top][bottom][index][help]  */
 180 {
 181     if (mc_config == NULL || group == NULL)
 182         return FALSE;
 183 
 184     return g_key_file_has_group (mc_config->handle, group);
 185 }
 186 
 187 /* --------------------------------------------------------------------------------------------- */
 188 
 189 gboolean
 190 mc_config_del_key (mc_config_t * mc_config, const char *group, const gchar * param)
     /* [previous][next][first][last][top][bottom][index][help]  */
 191 {
 192     if (mc_config == NULL || group == NULL || param == NULL)
 193         return FALSE;
 194 
 195     return g_key_file_remove_key (mc_config->handle, group, param, NULL);
 196 }
 197 
 198 /* --------------------------------------------------------------------------------------------- */
 199 
 200 gboolean
 201 mc_config_del_group (mc_config_t * mc_config, const char *group)
     /* [previous][next][first][last][top][bottom][index][help]  */
 202 {
 203     if (mc_config == NULL || group == NULL)
 204         return FALSE;
 205 
 206     return g_key_file_remove_group (mc_config->handle, group, NULL);
 207 }
 208 
 209 /* --------------------------------------------------------------------------------------------- */
 210 
 211 gboolean
 212 mc_config_read_file (mc_config_t * mc_config, const gchar * ini_path, gboolean read_only,
     /* [previous][next][first][last][top][bottom][index][help]  */
 213                      gboolean remove_empty)
 214 {
 215     mc_config_t *tmp_config;
 216     gchar **groups, **curr_grp;
 217     gchar *value;
 218     gboolean ok;
 219 
 220     if (mc_config == NULL)
 221         return FALSE;
 222 
 223     tmp_config = mc_config_init (ini_path, read_only);
 224     if (tmp_config == NULL)
 225         return FALSE;
 226 
 227     groups = mc_config_get_groups (tmp_config, NULL);
 228     ok = (*groups != NULL);
 229 
 230     for (curr_grp = groups; *curr_grp != NULL; curr_grp++)
 231     {
 232         gchar **keys, **curr_key;
 233 
 234         keys = mc_config_get_keys (tmp_config, *curr_grp, NULL);
 235 
 236         for (curr_key = keys; *curr_key != NULL; curr_key++)
 237         {
 238             value = g_key_file_get_value (tmp_config->handle, *curr_grp, *curr_key, NULL);
 239             if (value != NULL)
 240             {
 241                 if (*value == '\0' && remove_empty)
 242                     g_key_file_remove_key (mc_config->handle, *curr_grp, *curr_key, NULL);
 243                 else
 244                     g_key_file_set_value (mc_config->handle, *curr_grp, *curr_key, value);
 245                 g_free (value);
 246             }
 247             else if (remove_empty)
 248                 g_key_file_remove_key (mc_config->handle, *curr_grp, *curr_key, NULL);
 249         }
 250         g_strfreev (keys);
 251     }
 252 
 253     g_strfreev (groups);
 254     mc_config_deinit (tmp_config);
 255 
 256     return ok;
 257 }
 258 
 259 /* --------------------------------------------------------------------------------------------- */
 260 
 261 gboolean
 262 mc_config_save_file (mc_config_t * mc_config, GError ** mcerror)
     /* [previous][next][first][last][top][bottom][index][help]  */
 263 {
 264     mc_return_val_if_error (mcerror, FALSE);
 265 
 266     if (mc_config == NULL || mc_config->ini_path == NULL)
 267         return FALSE;
 268 
 269     return mc_config_new_or_override_file (mc_config, mc_config->ini_path, mcerror);
 270 }
 271 
 272 /* --------------------------------------------------------------------------------------------- */
 273 
 274 gboolean
 275 mc_config_save_to_file (mc_config_t * mc_config, const gchar * ini_path, GError ** mcerror)
     /* [previous][next][first][last][top][bottom][index][help]  */
 276 {
 277     mc_return_val_if_error (mcerror, FALSE);
 278 
 279     if (mc_config == NULL)
 280         return FALSE;
 281 
 282     return mc_config_new_or_override_file (mc_config, ini_path, mcerror);
 283 }
 284 
 285 /* --------------------------------------------------------------------------------------------- */

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