0238fb01b60afc19d62d64de21200210d1619b39
[claws.git] / src / common / prefs.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 Hiroyuki Yamamoto
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25
26 #include "prefs.h"
27 #include "utils.h"
28
29 PrefFile *prefs_write_open(const gchar *path)
30 {
31         PrefFile *pfile;
32         gchar *tmppath;
33         FILE *fp;
34
35         g_return_val_if_fail(path != NULL, NULL);
36
37         if (prefs_is_readonly(path)) {
38                 g_warning("no permission - %s\n", path);
39                 return NULL;
40         }
41
42         tmppath = g_strconcat(path, ".tmp", NULL);
43         if ((fp = fopen(tmppath, "wb")) == NULL) {
44                 FILE_OP_ERROR(tmppath, "fopen");
45                 g_free(tmppath);
46                 return NULL;
47         }
48
49         if (change_file_mode_rw(fp, tmppath) < 0)
50                 FILE_OP_ERROR(tmppath, "chmod");
51
52         g_free(tmppath);
53
54         pfile = g_new(PrefFile, 1);
55         pfile->fp = fp;
56         pfile->path = g_strdup(path);
57
58         return pfile;
59 }
60
61 gint prefs_write_close(PrefFile *pfile)
62 {
63         FILE *fp;
64         gchar *path;
65         gchar *tmppath;
66         gchar *bakpath = NULL;
67
68         g_return_val_if_fail(pfile != NULL, -1);
69
70         fp = pfile->fp;
71         path = pfile->path;
72         g_free(pfile);
73
74         tmppath = g_strconcat(path, ".tmp", NULL);
75         if (fclose(fp) == EOF) {
76                 FILE_OP_ERROR(tmppath, "fclose");
77                 unlink(tmppath);
78                 g_free(path);
79                 g_free(tmppath);
80                 return -1;
81         }
82
83         if (is_file_exist(path)) {
84                 bakpath = g_strconcat(path, ".bak", NULL);
85                 if (rename(path, bakpath) < 0) {
86                         FILE_OP_ERROR(path, "rename");
87                         unlink(tmppath);
88                         g_free(path);
89                         g_free(tmppath);
90                         g_free(bakpath);
91                         return -1;
92                 }
93         }
94
95         if (rename(tmppath, path) < 0) {
96                 FILE_OP_ERROR(tmppath, "rename");
97                 unlink(tmppath);
98                 g_free(path);
99                 g_free(tmppath);
100                 g_free(bakpath);
101                 return -1;
102         }
103
104         g_free(path);
105         g_free(tmppath);
106         g_free(bakpath);
107         return 0;
108 }
109
110 gint prefs_write_close_revert(PrefFile *pfile)
111 {
112         gchar *tmppath;
113
114         g_return_val_if_fail(pfile != NULL, -1);
115
116         tmppath = g_strconcat(pfile->path, ".tmp", NULL);
117         fclose(pfile->fp);
118         if (unlink(tmppath) < 0) FILE_OP_ERROR(tmppath, "unlink");
119         g_free(tmppath);
120         g_free(pfile->path);
121         g_free(pfile);
122
123         return 0;
124 }
125
126 gboolean prefs_is_readonly(const gchar * path)
127 {
128         if (path == NULL)
129                 return TRUE;
130
131         return (access(path, W_OK) != 0 && access(path, F_OK) == 0);
132 }
133
134 gboolean prefs_rc_is_readonly(const gchar * rcfile)
135 {
136         gboolean result;
137         gchar * rcpath;
138
139         if (rcfile == NULL)
140                 return TRUE;
141
142         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, rcfile, NULL);
143         result = prefs_is_readonly(rcpath);
144         g_free(rcpath);
145
146         return result;
147 }