common.c 8.1 KB

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