common.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. Configure module for the Midnight Commander
  3. Copyright (C) 1994-2019
  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. /*** file scope variables ************************************************************************/
  30. /* --------------------------------------------------------------------------------------------- */
  31. /*** file scope functions ************************************************************************/
  32. /* --------------------------------------------------------------------------------------------- */
  33. static gboolean
  34. mc_config_new_or_override_file (mc_config_t * mc_config, const gchar * ini_path, GError ** mcerror)
  35. {
  36. gchar *data, *written_data;
  37. gsize len, total_written;
  38. gboolean ret;
  39. int fd;
  40. ssize_t cur_written;
  41. vfs_path_t *ini_vpath;
  42. mc_return_val_if_error (mcerror, FALSE);
  43. data = g_key_file_to_data (mc_config->handle, &len, NULL);
  44. if (!exist_file (ini_path))
  45. {
  46. ret = g_file_set_contents (ini_path, data, len, mcerror);
  47. g_free (data);
  48. return ret;
  49. }
  50. mc_util_make_backup_if_possible (ini_path, "~");
  51. ini_vpath = vfs_path_from_str (ini_path);
  52. fd = mc_open (ini_vpath, O_WRONLY | O_TRUNC, 0);
  53. vfs_path_free (ini_vpath);
  54. if (fd == -1)
  55. {
  56. mc_propagate_error (mcerror, 0, "%s", unix_error_string (errno));
  57. g_free (data);
  58. return FALSE;
  59. }
  60. for (written_data = data, total_written = len;
  61. (cur_written = mc_write (fd, (const void *) written_data, total_written)) > 0;
  62. written_data += cur_written, total_written -= cur_written)
  63. ;
  64. mc_close (fd);
  65. g_free (data);
  66. if (cur_written == -1)
  67. {
  68. mc_util_restore_from_backup_if_possible (ini_path, "~");
  69. mc_propagate_error (mcerror, 0, "%s", unix_error_string (errno));
  70. return FALSE;
  71. }
  72. mc_util_unlink_backup_if_possible (ini_path, "~");
  73. return TRUE;
  74. }
  75. /* --------------------------------------------------------------------------------------------- */
  76. /*** public functions ****************************************************************************/
  77. /* --------------------------------------------------------------------------------------------- */
  78. mc_config_t *
  79. mc_config_init (const gchar * ini_path, gboolean read_only)
  80. {
  81. mc_config_t *mc_config;
  82. struct stat st;
  83. mc_config = g_try_malloc0 (sizeof (mc_config_t));
  84. if (mc_config == NULL)
  85. return NULL;
  86. mc_config->handle = g_key_file_new ();
  87. if (mc_config->handle == NULL)
  88. {
  89. g_free (mc_config);
  90. return NULL;
  91. }
  92. if (ini_path == NULL)
  93. return mc_config;
  94. if (exist_file (ini_path))
  95. {
  96. vfs_path_t *vpath;
  97. vpath = vfs_path_from_str (ini_path);
  98. if (mc_stat (vpath, &st) == 0 && st.st_size != 0)
  99. {
  100. GKeyFileFlags flags = G_KEY_FILE_NONE;
  101. if (!read_only)
  102. flags |= G_KEY_FILE_KEEP_COMMENTS;
  103. /* file exists and not empty */
  104. g_key_file_load_from_file (mc_config->handle, ini_path, flags, NULL);
  105. }
  106. vfs_path_free (vpath);
  107. }
  108. mc_config->ini_path = g_strdup (ini_path);
  109. return mc_config;
  110. }
  111. /* --------------------------------------------------------------------------------------------- */
  112. void
  113. mc_config_deinit (mc_config_t * mc_config)
  114. {
  115. if (mc_config != NULL)
  116. {
  117. g_free (mc_config->ini_path);
  118. g_key_file_free (mc_config->handle);
  119. g_free (mc_config);
  120. }
  121. }
  122. /* --------------------------------------------------------------------------------------------- */
  123. gboolean
  124. mc_config_has_param (const mc_config_t * mc_config, const char *group, const gchar * param)
  125. {
  126. if (mc_config == NULL || group == NULL || param == NULL)
  127. return FALSE;
  128. return g_key_file_has_key (mc_config->handle, group, param, NULL);
  129. }
  130. /* --------------------------------------------------------------------------------------------- */
  131. gboolean
  132. mc_config_has_group (mc_config_t * mc_config, const char *group)
  133. {
  134. if (mc_config == NULL || group == NULL)
  135. return FALSE;
  136. return g_key_file_has_group (mc_config->handle, group);
  137. }
  138. /* --------------------------------------------------------------------------------------------- */
  139. gboolean
  140. mc_config_del_key (mc_config_t * mc_config, const char *group, const gchar * param)
  141. {
  142. if (mc_config == NULL || group == NULL || param == NULL)
  143. return FALSE;
  144. return g_key_file_remove_key (mc_config->handle, group, param, NULL);
  145. }
  146. /* --------------------------------------------------------------------------------------------- */
  147. gboolean
  148. mc_config_del_group (mc_config_t * mc_config, const char *group)
  149. {
  150. if (mc_config == NULL || group == NULL)
  151. return FALSE;
  152. return g_key_file_remove_group (mc_config->handle, group, NULL);
  153. }
  154. /* --------------------------------------------------------------------------------------------- */
  155. gboolean
  156. mc_config_read_file (mc_config_t * mc_config, const gchar * ini_path, gboolean read_only,
  157. gboolean remove_empty)
  158. {
  159. mc_config_t *tmp_config;
  160. gchar **groups, **curr_grp;
  161. gchar *value;
  162. gboolean ok;
  163. if (mc_config == NULL)
  164. return FALSE;
  165. tmp_config = mc_config_init (ini_path, read_only);
  166. if (tmp_config == NULL)
  167. return FALSE;
  168. groups = mc_config_get_groups (tmp_config, NULL);
  169. ok = (*groups != NULL);
  170. for (curr_grp = groups; *curr_grp != NULL; curr_grp++)
  171. {
  172. gchar **keys, **curr_key;
  173. keys = mc_config_get_keys (tmp_config, *curr_grp, NULL);
  174. for (curr_key = keys; *curr_key != NULL; curr_key++)
  175. {
  176. value = g_key_file_get_value (tmp_config->handle, *curr_grp, *curr_key, NULL);
  177. if (value != NULL)
  178. {
  179. if (*value == '\0' && remove_empty)
  180. g_key_file_remove_key (mc_config->handle, *curr_grp, *curr_key, NULL);
  181. else
  182. g_key_file_set_value (mc_config->handle, *curr_grp, *curr_key, value);
  183. g_free (value);
  184. }
  185. else if (remove_empty)
  186. g_key_file_remove_key (mc_config->handle, *curr_grp, *curr_key, NULL);
  187. }
  188. g_strfreev (keys);
  189. }
  190. g_strfreev (groups);
  191. mc_config_deinit (tmp_config);
  192. return ok;
  193. }
  194. /* --------------------------------------------------------------------------------------------- */
  195. gboolean
  196. mc_config_save_file (mc_config_t * mc_config, GError ** mcerror)
  197. {
  198. mc_return_val_if_error (mcerror, FALSE);
  199. if (mc_config == NULL || mc_config->ini_path == NULL)
  200. return FALSE;
  201. return mc_config_new_or_override_file (mc_config, mc_config->ini_path, mcerror);
  202. }
  203. /* --------------------------------------------------------------------------------------------- */
  204. gboolean
  205. mc_config_save_to_file (mc_config_t * mc_config, const gchar * ini_path, GError ** mcerror)
  206. {
  207. mc_return_val_if_error (mcerror, FALSE);
  208. if (mc_config == NULL)
  209. return FALSE;
  210. return mc_config_new_or_override_file (mc_config, ini_path, mcerror);
  211. }
  212. /* --------------------------------------------------------------------------------------------- */