common.c 7.8 KB

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