common.c 8.4 KB

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