common.c 7.7 KB

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