Clean up configuration again
[browser-switch] / config-ui / save-config.c
1 /*
2  * save-config.c -- functions to save a Browser Switchboard configuration
3  *
4  * Copyright (C) 2009-2010 Steven Luo
5  * Derived from a Python implementation by Jason Simpson and Steven Luo
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20  * USA.
21  */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29
30 #include "configfile.h"
31 #include "config.h"
32
33 extern struct swb_config_option swb_config_options[];
34
35 /* Outputs a config file line for the named option to a file descriptor */
36 static void swb_config_output_option(FILE *fp, unsigned int *oldcfg_seen,
37                               struct swb_config *cfg, char *name) {
38         int i;
39         struct swb_config_option opt;
40         
41         for (i = 0; swb_config_options[i].name; ++i) {
42                 opt = swb_config_options[i];
43                 if (strcmp(opt.name, name))
44                         continue;
45
46                 if (!(*oldcfg_seen & opt.set_mask) &&
47                     (cfg->flags & opt.set_mask)) {
48                         switch (opt.type) {
49                           case SWB_CONFIG_OPT_STRING:
50                                 fprintf(fp, "%s = \"%s\"\n",
51                                         opt.name,
52                                         *(char **)cfg->entries[i]);
53                                 *oldcfg_seen |= opt.set_mask;
54                                 break;
55                           case SWB_CONFIG_OPT_INT:
56                                 fprintf(fp, "%s = %d\n",
57                                         opt.name,
58                                         *(int *)cfg->entries[i]);
59                                 *oldcfg_seen |= opt.set_mask;
60                                 break;
61                         }
62                 }
63                 break;
64         }
65 }
66
67 /* Save the settings in the provided swb_config struct to the config file
68    Returns true on success, false otherwise */
69 int swb_config_save(struct swb_config *cfg) {
70         FILE *fp = NULL, *tmpfp = NULL;
71         char *homedir, *tempfile, *newfile;
72         size_t len;
73         int retval = 1;
74         struct swb_config_line line;
75         unsigned int oldcfg_seen = 0;
76         int i;
77
78         /* If CONFIGFILE_DIR doesn't exist already, try to create it */
79         if (!(homedir = getenv("HOME")))
80                 homedir = DEFAULT_HOMEDIR;
81         len = strlen(homedir) + strlen(CONFIGFILE_DIR) + 1;
82         if (!(newfile = calloc(len, sizeof(char))))
83                 return 0;
84         snprintf(newfile, len, "%s%s", homedir, CONFIGFILE_DIR);
85         if (access(newfile, F_OK) == -1 && errno == ENOENT) {
86                 mkdir(newfile, 0750);
87         }
88         free(newfile);
89
90         /* Put together the path to the new config file and the tempfile */
91         len = strlen(homedir) + strlen(CONFIGFILE_LOC) + 1;
92         if (!(newfile = calloc(len, sizeof(char))))
93                 return 0;
94         /* 4 = strlen(".tmp") */
95         if (!(tempfile = calloc(len+4, sizeof(char)))) {
96                 free(newfile);
97                 return 0;
98         }
99         snprintf(newfile, len, "%s%s", homedir, CONFIGFILE_LOC);
100         snprintf(tempfile, len+4, "%s%s", newfile, ".tmp");
101
102         /* Open the temporary file for writing */
103         if (!(tmpfp = fopen(tempfile, "w"))) {
104                 retval = 0;
105                 goto out;
106         }
107
108         /* Open the old config file, if it exists */
109         if ((fp = open_config_file()) && parse_config_file_begin()) {
110                 /* Copy the old config file over to the new one line by line,
111                    replacing old config values with new ones
112                    TODO: should we handle errors differently than EOF? */
113                 while (!parse_config_file_line(fp, &line)) {
114                         if (line.parsed) {
115                                 /* Is a config line, print the new value here */
116                                 swb_config_output_option(tmpfp, &oldcfg_seen,
117                                                          cfg, line.key);
118                         } else {
119                                 /* Just copy the old line over */
120                                 fprintf(tmpfp, "%s\n", line.key);
121                         }
122                         free(line.key);
123                         free(line.value);
124                 }
125                 parse_config_file_end();
126         }
127
128         /* If we haven't written them yet, write out any new config values */
129         for (i = 0; swb_config_options[i].name; ++i)
130                 swb_config_output_option(tmpfp, &oldcfg_seen, cfg,
131                                          swb_config_options[i].name);
132
133         /* Replace the old config file with the new one */
134         fclose(tmpfp);
135         tmpfp = NULL;
136         rename(tempfile, newfile);
137
138 out:
139         free(newfile);
140         free(tempfile);
141         if (tmpfp)
142                 fclose(tmpfp);
143         if (fp)
144                 fclose(fp);
145         return retval;
146 }