2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 Hiroyuki Yamamoto
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program 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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 *\brief Open preferences file for reading
34 *\param path Filename with path of preferences file to read
36 *\return PrefFile * preferences file struct
38 PrefFile *prefs_read_open(const gchar *path)
43 g_return_val_if_fail(path != NULL, NULL);
45 if ((fp = g_fopen(path, "rb")) == NULL) {
46 FILE_OP_ERROR(path, "fopen");
50 pfile = g_new(PrefFile, 1);
52 pfile->orig_fp = NULL;
53 pfile->path = g_strdup(path);
54 pfile->writing = FALSE;
60 *\brief Open preferences file for writing
61 * Prefs are written to a temp file: Call prefs_write_close()
62 * to rename this to the final filename
64 *\param path Filename with path of preferences file to write
66 *\return PrefFile * preferences file struct
68 PrefFile *prefs_write_open(const gchar *path)
74 g_return_val_if_fail(path != NULL, NULL);
76 if (prefs_is_readonly(path)) {
77 g_warning("no permission - %s\n", path);
81 tmppath = g_strconcat(path, ".tmp", NULL);
82 if ((fp = g_fopen(tmppath, "wb")) == NULL) {
83 FILE_OP_ERROR(tmppath, "fopen");
88 if (change_file_mode_rw(fp, tmppath) < 0)
89 FILE_OP_ERROR(tmppath, "chmod");
93 pfile = g_new(PrefFile, 1);
95 pfile->orig_fp = NULL;
96 pfile->path = g_strdup(path);
97 pfile->writing = TRUE;
103 *\brief Close and free preferences file
104 * Creates final file from temp, creates backup
106 *\param pfile Preferences file struct
108 *\return 0 on success, -1 on failure
110 #define PREFS_FILE_FREE() \
112 if (path) g_free(path); \
113 if (tmppath) g_free(tmppath); \
114 if (bakpath) g_free(bakpath); \
115 if (pfile) g_free(pfile); \
117 gint prefs_file_close(PrefFile *pfile)
122 gchar *bakpath = NULL;
125 g_return_val_if_fail(pfile != NULL, -1);
128 orig_fp = pfile->orig_fp;
131 if (!pfile->writing) {
139 while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
142 if (fputs(buf, fp) == EOF) {
143 g_warning("failed to write configuration to file\n");
145 prefs_file_close_revert(pfile);
153 while (fgets(buf, sizeof(buf), orig_fp) != NULL)
154 if (fputs(buf, fp) == EOF) {
155 g_warning("failed to write configuration to file\n");
157 prefs_file_close_revert(pfile);
164 tmppath = g_strconcat(path, ".tmp", NULL);
165 if (fclose(fp) == EOF) {
166 FILE_OP_ERROR(tmppath, "fclose");
173 if (is_file_exist(path)) {
174 bakpath = g_strconcat(path, ".bak", NULL);
175 if (rename(path, bakpath) < 0) {
176 FILE_OP_ERROR(path, "rename");
185 if (rename(tmppath, path) < 0) {
186 FILE_OP_ERROR(tmppath, "rename");
200 #undef PREFS_FILE_FREE
203 *\brief Close and free preferences file, delete temp file
205 *\param pfile Preferences file struct
207 gint prefs_file_close_revert(PrefFile *pfile)
209 gchar *tmppath = NULL;
211 g_return_val_if_fail(pfile != NULL, -1);
214 fclose(pfile->orig_fp);
216 tmppath = g_strconcat(pfile->path, ".tmp", NULL);
218 if (pfile->writing) {
219 if (g_unlink(tmppath) < 0) FILE_OP_ERROR(tmppath, "unlink");
229 *\brief Check if "path" is a file and readonly
231 gboolean prefs_is_readonly(const gchar * path)
236 return (access(path, W_OK) != 0 && access(path, F_OK) == 0);
240 *\brief Check if "rcfile" is in rcdir, a file and readonly
242 gboolean prefs_rc_is_readonly(const gchar * rcfile)
250 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, rcfile, NULL);
251 result = prefs_is_readonly(rcpath);
258 *\brief Selects current section in preferences file
259 * Creates section if file is written
261 *\param pfile Preferences file struct
263 *\return 0 on success, -1 on failure
265 gint prefs_set_block_label(PrefFile *pfile, const gchar *label)
270 block_label = g_strdup_printf("[%s]", label);
271 if (!pfile->writing) {
272 while (fgets(buf, sizeof(buf), pfile->fp) != NULL) {
275 val = strncmp(buf, block_label, strlen(block_label));
277 debug_print("Found %s\n", block_label);
282 if ((pfile->orig_fp = g_fopen(pfile->path, "rb")) != NULL) {
283 gboolean block_matched = FALSE;
285 while (fgets(buf, sizeof(buf), pfile->orig_fp) != NULL) {
288 val = strncmp(buf, block_label, strlen(block_label));
290 debug_print("Found %s\n", block_label);
291 block_matched = TRUE;
294 if (fputs(buf, pfile->fp) == EOF) {
295 g_warning("failed to write configuration to file\n");
296 fclose(pfile->orig_fp);
297 prefs_file_close_revert(pfile);
305 if (!block_matched) {
306 fclose(pfile->orig_fp);
307 pfile->orig_fp = NULL;
310 if (fputs(block_label, pfile->fp) == EOF ||
311 fputc('\n', pfile->fp) == EOF) {
312 g_warning("failed to write configuration to file\n");
313 fclose(pfile->orig_fp);
314 prefs_file_close_revert(pfile);