Merge branch '3684_mc_profile_root'
[midnight-commander.git] / lib / mcconfig / common.c
blob37ee70bbd996a455a2228be1b68099ef78b7941d
1 /*
2 Configure module for the Midnight Commander
4 Copyright (C) 1994-2016
5 Free Software Foundation, Inc.
7 This file is part of the Midnight Commander.
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.
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.
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/>.
23 #include <config.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <errno.h> /* extern int errno */
30 #include "lib/global.h"
31 #include "lib/vfs/vfs.h" /* mc_stat */
32 #include "lib/util.h"
33 #include "lib/mcconfig.h"
35 /*** global variables **************************************************/
37 /*** file scope macro definitions **************************************/
39 /*** file scope type declarations **************************************/
41 /*** file scope variables **********************************************/
43 /*** file scope functions **********************************************/
44 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
46 static gboolean
47 mc_config_new_or_override_file (mc_config_t * mc_config, const gchar * ini_path, GError ** mcerror)
49 gchar *data, *written_data;
50 gsize len, total_written;
51 gboolean ret;
52 int fd;
53 ssize_t cur_written;
54 vfs_path_t *ini_vpath;
56 mc_return_val_if_error (mcerror, FALSE);
58 data = g_key_file_to_data (mc_config->handle, &len, NULL);
59 if (!exist_file (ini_path))
61 ret = g_file_set_contents (ini_path, data, len, mcerror);
62 g_free (data);
63 return ret;
66 mc_util_make_backup_if_possible (ini_path, "~");
68 ini_vpath = vfs_path_from_str (ini_path);
69 fd = mc_open (ini_vpath, O_WRONLY | O_TRUNC, 0);
70 vfs_path_free (ini_vpath);
72 if (fd == -1)
74 mc_propagate_error (mcerror, 0, "%s", unix_error_string (errno));
75 g_free (data);
76 return FALSE;
79 for (written_data = data, total_written = len;
80 (cur_written = mc_write (fd, (const void *) written_data, total_written)) > 0;
81 written_data += cur_written, total_written -= cur_written)
83 mc_close (fd);
84 g_free (data);
86 if (cur_written == -1)
88 mc_util_restore_from_backup_if_possible (ini_path, "~");
89 mc_propagate_error (mcerror, 0, "%s", unix_error_string (errno));
90 return FALSE;
93 mc_util_unlink_backup_if_possible (ini_path, "~");
94 return TRUE;
97 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
98 /*** public functions **************************************************/
99 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
101 mc_config_t *
102 mc_config_init (const gchar * ini_path, gboolean read_only)
104 mc_config_t *mc_config;
105 struct stat st;
107 mc_config = g_try_malloc0 (sizeof (mc_config_t));
109 if (mc_config == NULL)
110 return NULL;
112 mc_config->handle = g_key_file_new ();
113 if (mc_config->handle == NULL)
115 g_free (mc_config);
116 return NULL;
118 if (ini_path == NULL)
119 return mc_config;
121 if (exist_file (ini_path))
123 vfs_path_t *vpath;
125 vpath = vfs_path_from_str (ini_path);
126 if (mc_stat (vpath, &st) == 0 && st.st_size != 0)
128 GKeyFileFlags flags = G_KEY_FILE_NONE;
130 if (!read_only)
131 flags |= G_KEY_FILE_KEEP_COMMENTS;
133 /* file exists and not empty */
134 g_key_file_load_from_file (mc_config->handle, ini_path, flags, NULL);
136 vfs_path_free (vpath);
139 mc_config->ini_path = g_strdup (ini_path);
140 return mc_config;
143 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
145 void
146 mc_config_deinit (mc_config_t * mc_config)
148 if (mc_config != NULL)
150 g_free (mc_config->ini_path);
151 g_key_file_free (mc_config->handle);
152 g_free (mc_config);
156 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
158 gboolean
159 mc_config_has_param (const mc_config_t * mc_config, const char *group, const gchar * param)
161 if (!mc_config || !group || !param)
162 return FALSE;
164 return g_key_file_has_key (mc_config->handle, group, param, NULL);
167 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
169 gboolean
170 mc_config_has_group (mc_config_t * mc_config, const char *group)
172 if (!mc_config || !group)
173 return FALSE;
175 return g_key_file_has_group (mc_config->handle, group);
178 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
180 gboolean
181 mc_config_del_key (mc_config_t * mc_config, const char *group, const gchar * param)
183 if (!mc_config || !group || !param)
184 return FALSE;
186 return g_key_file_remove_key (mc_config->handle, group, param, NULL);
189 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
191 gboolean
192 mc_config_del_group (mc_config_t * mc_config, const char *group)
194 if (!mc_config || !group)
195 return FALSE;
197 return g_key_file_remove_group (mc_config->handle, group, NULL);
200 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
202 gboolean
203 mc_config_read_file (mc_config_t * mc_config, const gchar * ini_path, gboolean read_only,
204 gboolean remove_empty)
206 mc_config_t *tmp_config;
207 gchar **groups, **curr_grp;
208 gchar *value;
209 gboolean ok;
211 if (mc_config == NULL)
212 return FALSE;
214 tmp_config = mc_config_init (ini_path, read_only);
215 if (tmp_config == NULL)
216 return FALSE;
218 groups = mc_config_get_groups (tmp_config, NULL);
219 ok = (*groups != NULL);
221 for (curr_grp = groups; *curr_grp != NULL; curr_grp++)
223 gchar **keys, **curr_key;
225 keys = mc_config_get_keys (tmp_config, *curr_grp, NULL);
227 for (curr_key = keys; *curr_key != NULL; curr_key++)
229 value = g_key_file_get_value (tmp_config->handle, *curr_grp, *curr_key, NULL);
230 if (value != NULL)
232 if (*value == '\0' && remove_empty)
233 g_key_file_remove_key (mc_config->handle, *curr_grp, *curr_key, NULL);
234 else
235 g_key_file_set_value (mc_config->handle, *curr_grp, *curr_key, value);
236 g_free (value);
238 else if (remove_empty)
239 g_key_file_remove_key (mc_config->handle, *curr_grp, *curr_key, NULL);
241 g_strfreev (keys);
244 g_strfreev (groups);
245 mc_config_deinit (tmp_config);
247 return ok;
250 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
252 gboolean
253 mc_config_save_file (mc_config_t * mc_config, GError ** mcerror)
255 mc_return_val_if_error (mcerror, FALSE);
257 if (mc_config == NULL || mc_config->ini_path == NULL)
258 return FALSE;
260 return mc_config_new_or_override_file (mc_config, mc_config->ini_path, mcerror);
263 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
265 gboolean
266 mc_config_save_to_file (mc_config_t * mc_config, const gchar * ini_path, GError ** mcerror)
268 mc_return_val_if_error (mcerror, FALSE);
270 if (mc_config == NULL)
271 return FALSE;
273 return mc_config_new_or_override_file (mc_config, ini_path, mcerror);
277 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */