Blame


1 1d126e2d 2019-08-24 stsp /* $OpenBSD: conf.c,v 1.107 2017/10/27 08:29:32 mpi Exp $ */
2 1d126e2d 2019-08-24 stsp /* $EOM: conf.c,v 1.48 2000/12/04 02:04:29 angelos Exp $ */
3 1d126e2d 2019-08-24 stsp
4 1d126e2d 2019-08-24 stsp /*
5 1d126e2d 2019-08-24 stsp * Copyright (c) 1998, 1999, 2000, 2001 Niklas Hallqvist. All rights reserved.
6 1d126e2d 2019-08-24 stsp * Copyright (c) 2000, 2001, 2002 Håkan Olsson. All rights reserved.
7 1d126e2d 2019-08-24 stsp *
8 1d126e2d 2019-08-24 stsp * Redistribution and use in source and binary forms, with or without
9 1d126e2d 2019-08-24 stsp * modification, are permitted provided that the following conditions
10 1d126e2d 2019-08-24 stsp * are met:
11 1d126e2d 2019-08-24 stsp * 1. Redistributions of source code must retain the above copyright
12 1d126e2d 2019-08-24 stsp * notice, this list of conditions and the following disclaimer.
13 1d126e2d 2019-08-24 stsp * 2. Redistributions in binary form must reproduce the above copyright
14 1d126e2d 2019-08-24 stsp * notice, this list of conditions and the following disclaimer in the
15 1d126e2d 2019-08-24 stsp * documentation and/or other materials provided with the distribution.
16 1d126e2d 2019-08-24 stsp *
17 1d126e2d 2019-08-24 stsp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 1d126e2d 2019-08-24 stsp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 1d126e2d 2019-08-24 stsp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 1d126e2d 2019-08-24 stsp * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 1d126e2d 2019-08-24 stsp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 1d126e2d 2019-08-24 stsp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 1d126e2d 2019-08-24 stsp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 1d126e2d 2019-08-24 stsp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 1d126e2d 2019-08-24 stsp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 1d126e2d 2019-08-24 stsp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 1d126e2d 2019-08-24 stsp */
28 1d126e2d 2019-08-24 stsp
29 1d126e2d 2019-08-24 stsp #include <sys/types.h>
30 1d126e2d 2019-08-24 stsp #include <sys/queue.h>
31 1d126e2d 2019-08-24 stsp #include <sys/stat.h>
32 1d126e2d 2019-08-24 stsp
33 1d126e2d 2019-08-24 stsp #include <ctype.h>
34 1d126e2d 2019-08-24 stsp #include <fcntl.h>
35 1d126e2d 2019-08-24 stsp #include <stdarg.h>
36 1d126e2d 2019-08-24 stsp #include <stdio.h>
37 1d126e2d 2019-08-24 stsp #include <stdlib.h>
38 1d126e2d 2019-08-24 stsp #include <string.h>
39 1d126e2d 2019-08-24 stsp #include <unistd.h>
40 1d126e2d 2019-08-24 stsp #include <errno.h>
41 1d126e2d 2019-08-24 stsp
42 1d126e2d 2019-08-24 stsp #include "got_error.h"
43 1d126e2d 2019-08-24 stsp
44 1d126e2d 2019-08-24 stsp #include "got_lib_gitconfig.h"
45 1d126e2d 2019-08-24 stsp
46 1d126e2d 2019-08-24 stsp #ifndef nitems
47 1d126e2d 2019-08-24 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
48 1d126e2d 2019-08-24 stsp #endif
49 1d126e2d 2019-08-24 stsp
50 1d126e2d 2019-08-24 stsp #define LOG_MISC 0
51 1d126e2d 2019-08-24 stsp #define LOG_REPORT 1
52 1d126e2d 2019-08-24 stsp #ifdef GITCONFIG_DEBUG
53 1d126e2d 2019-08-24 stsp #define LOG_DBG(x) log_debug x
54 1d126e2d 2019-08-24 stsp #else
55 1d126e2d 2019-08-24 stsp #define LOG_DBG(x)
56 1d126e2d 2019-08-24 stsp #endif
57 1d126e2d 2019-08-24 stsp
58 1d126e2d 2019-08-24 stsp #define log_print printf
59 1d126e2d 2019-08-24 stsp #define log_error printf
60 1d126e2d 2019-08-24 stsp
61 1d126e2d 2019-08-24 stsp #ifdef GITCONFIG_DEBUG
62 1d126e2d 2019-08-24 stsp static void
63 1d126e2d 2019-08-24 stsp log_debug(int cls, int level, const char *fmt, ...)
64 1d126e2d 2019-08-24 stsp {
65 1d126e2d 2019-08-24 stsp va_list ap;
66 1d126e2d 2019-08-24 stsp
67 1d126e2d 2019-08-24 stsp va_start(ap, fmt);
68 1d126e2d 2019-08-24 stsp vfprintf(stderr, fmt, ap);
69 1d126e2d 2019-08-24 stsp va_end(ap);
70 1d126e2d 2019-08-24 stsp }
71 1d126e2d 2019-08-24 stsp #endif
72 1d126e2d 2019-08-24 stsp
73 1d126e2d 2019-08-24 stsp struct got_gitconfig_trans {
74 1d126e2d 2019-08-24 stsp TAILQ_ENTRY(got_gitconfig_trans) link;
75 1d126e2d 2019-08-24 stsp int trans;
76 1d126e2d 2019-08-24 stsp enum got_gitconfig_op {
77 1d126e2d 2019-08-24 stsp CONF_SET, CONF_REMOVE, CONF_REMOVE_SECTION
78 1d126e2d 2019-08-24 stsp } op;
79 1d126e2d 2019-08-24 stsp char *section;
80 1d126e2d 2019-08-24 stsp char *tag;
81 1d126e2d 2019-08-24 stsp char *value;
82 1d126e2d 2019-08-24 stsp int override;
83 1d126e2d 2019-08-24 stsp int is_default;
84 1d126e2d 2019-08-24 stsp };
85 1d126e2d 2019-08-24 stsp
86 1d126e2d 2019-08-24 stsp TAILQ_HEAD(got_gitconfig_trans_head, got_gitconfig_trans);
87 1d126e2d 2019-08-24 stsp
88 1d126e2d 2019-08-24 stsp struct got_gitconfig_binding {
89 1d126e2d 2019-08-24 stsp LIST_ENTRY(got_gitconfig_binding) link;
90 1d126e2d 2019-08-24 stsp char *section;
91 1d126e2d 2019-08-24 stsp char *tag;
92 1d126e2d 2019-08-24 stsp char *value;
93 1d126e2d 2019-08-24 stsp int is_default;
94 1d126e2d 2019-08-24 stsp };
95 1d126e2d 2019-08-24 stsp
96 1d126e2d 2019-08-24 stsp LIST_HEAD(got_gitconfig_bindings, got_gitconfig_binding);
97 1d126e2d 2019-08-24 stsp
98 1d126e2d 2019-08-24 stsp struct got_gitconfig {
99 1d126e2d 2019-08-24 stsp struct got_gitconfig_bindings bindings[256];
100 1d126e2d 2019-08-24 stsp struct got_gitconfig_trans_head trans_queue;
101 1d126e2d 2019-08-24 stsp char *addr;
102 1d126e2d 2019-08-24 stsp int seq;
103 1d126e2d 2019-08-24 stsp };
104 1d126e2d 2019-08-24 stsp
105 1d126e2d 2019-08-24 stsp static __inline__ u_int8_t
106 fda3525e 2021-09-25 stsp conf_hash(const char *s)
107 1d126e2d 2019-08-24 stsp {
108 1d126e2d 2019-08-24 stsp u_int8_t hash = 0;
109 1d126e2d 2019-08-24 stsp
110 1d126e2d 2019-08-24 stsp while (*s) {
111 1d126e2d 2019-08-24 stsp hash = ((hash << 1) | (hash >> 7)) ^ tolower((unsigned char)*s);
112 1d126e2d 2019-08-24 stsp s++;
113 1d126e2d 2019-08-24 stsp }
114 1d126e2d 2019-08-24 stsp return hash;
115 1d126e2d 2019-08-24 stsp }
116 1d126e2d 2019-08-24 stsp
117 1d126e2d 2019-08-24 stsp /*
118 1d126e2d 2019-08-24 stsp * Insert a tag-value combination from LINE (the equal sign is at POS)
119 1d126e2d 2019-08-24 stsp */
120 1d126e2d 2019-08-24 stsp static int
121 1d126e2d 2019-08-24 stsp conf_remove_now(struct got_gitconfig *conf, char *section, char *tag)
122 1d126e2d 2019-08-24 stsp {
123 1d126e2d 2019-08-24 stsp struct got_gitconfig_binding *cb, *next;
124 1d126e2d 2019-08-24 stsp
125 1d126e2d 2019-08-24 stsp for (cb = LIST_FIRST(&conf->bindings[conf_hash(section)]); cb;
126 1d126e2d 2019-08-24 stsp cb = next) {
127 1d126e2d 2019-08-24 stsp next = LIST_NEXT(cb, link);
128 1d126e2d 2019-08-24 stsp if (strcasecmp(cb->section, section) == 0 &&
129 1d126e2d 2019-08-24 stsp strcasecmp(cb->tag, tag) == 0) {
130 1d126e2d 2019-08-24 stsp LIST_REMOVE(cb, link);
131 1d126e2d 2019-08-24 stsp LOG_DBG((LOG_MISC, 95, "[%s]:%s->%s removed", section,
132 1d126e2d 2019-08-24 stsp tag, cb->value));
133 1d126e2d 2019-08-24 stsp free(cb->section);
134 1d126e2d 2019-08-24 stsp free(cb->tag);
135 1d126e2d 2019-08-24 stsp free(cb->value);
136 1d126e2d 2019-08-24 stsp free(cb);
137 1d126e2d 2019-08-24 stsp return 0;
138 1d126e2d 2019-08-24 stsp }
139 1d126e2d 2019-08-24 stsp }
140 1d126e2d 2019-08-24 stsp return 1;
141 1d126e2d 2019-08-24 stsp }
142 1d126e2d 2019-08-24 stsp
143 1d126e2d 2019-08-24 stsp static int
144 1d126e2d 2019-08-24 stsp conf_remove_section_now(struct got_gitconfig *conf, char *section)
145 1d126e2d 2019-08-24 stsp {
146 1d126e2d 2019-08-24 stsp struct got_gitconfig_binding *cb, *next;
147 1d126e2d 2019-08-24 stsp int unseen = 1;
148 1d126e2d 2019-08-24 stsp
149 1d126e2d 2019-08-24 stsp for (cb = LIST_FIRST(&conf->bindings[conf_hash(section)]); cb;
150 1d126e2d 2019-08-24 stsp cb = next) {
151 1d126e2d 2019-08-24 stsp next = LIST_NEXT(cb, link);
152 1d126e2d 2019-08-24 stsp if (strcasecmp(cb->section, section) == 0) {
153 1d126e2d 2019-08-24 stsp unseen = 0;
154 1d126e2d 2019-08-24 stsp LIST_REMOVE(cb, link);
155 1d126e2d 2019-08-24 stsp LOG_DBG((LOG_MISC, 95, "[%s]:%s->%s removed", section,
156 1d126e2d 2019-08-24 stsp cb->tag, cb->value));
157 1d126e2d 2019-08-24 stsp free(cb->section);
158 1d126e2d 2019-08-24 stsp free(cb->tag);
159 1d126e2d 2019-08-24 stsp free(cb->value);
160 1d126e2d 2019-08-24 stsp free(cb);
161 1d126e2d 2019-08-24 stsp }
162 1d126e2d 2019-08-24 stsp }
163 1d126e2d 2019-08-24 stsp return unseen;
164 1d126e2d 2019-08-24 stsp }
165 1d126e2d 2019-08-24 stsp
166 1d126e2d 2019-08-24 stsp /*
167 1d126e2d 2019-08-24 stsp * Insert a tag-value combination from LINE (the equal sign is at POS)
168 1d126e2d 2019-08-24 stsp * into SECTION of our configuration database.
169 1d126e2d 2019-08-24 stsp */
170 1d126e2d 2019-08-24 stsp static int
171 1d126e2d 2019-08-24 stsp conf_set_now(struct got_gitconfig *conf, char *section, char *tag,
172 1d126e2d 2019-08-24 stsp char *value, int override, int is_default)
173 1d126e2d 2019-08-24 stsp {
174 1d126e2d 2019-08-24 stsp struct got_gitconfig_binding *node = 0;
175 1d126e2d 2019-08-24 stsp
176 1d126e2d 2019-08-24 stsp if (override)
177 1d126e2d 2019-08-24 stsp conf_remove_now(conf, section, tag);
178 1d126e2d 2019-08-24 stsp else if (got_gitconfig_get_str(conf, section, tag)) {
179 1d126e2d 2019-08-24 stsp if (!is_default)
180 300ea754 2021-01-05 stsp LOG_DBG((LOG_MISC,
181 300ea754 2021-01-05 stsp "conf_set_now: duplicate tag [%s]:%s, "
182 300ea754 2021-01-05 stsp "ignoring...\n", section, tag));
183 1d126e2d 2019-08-24 stsp return 1;
184 1d126e2d 2019-08-24 stsp }
185 1d126e2d 2019-08-24 stsp node = calloc(1, sizeof *node);
186 1d126e2d 2019-08-24 stsp if (!node) {
187 1d126e2d 2019-08-24 stsp log_error("conf_set_now: calloc (1, %lu) failed",
188 1d126e2d 2019-08-24 stsp (unsigned long)sizeof *node);
189 1d126e2d 2019-08-24 stsp return 1;
190 1d126e2d 2019-08-24 stsp }
191 1d126e2d 2019-08-24 stsp node->section = node->tag = node->value = NULL;
192 1d126e2d 2019-08-24 stsp if ((node->section = strdup(section)) == NULL)
193 1d126e2d 2019-08-24 stsp goto fail;
194 1d126e2d 2019-08-24 stsp if ((node->tag = strdup(tag)) == NULL)
195 1d126e2d 2019-08-24 stsp goto fail;
196 1d126e2d 2019-08-24 stsp if ((node->value = strdup(value)) == NULL)
197 1d126e2d 2019-08-24 stsp goto fail;
198 1d126e2d 2019-08-24 stsp node->is_default = is_default;
199 1d126e2d 2019-08-24 stsp
200 1d126e2d 2019-08-24 stsp LIST_INSERT_HEAD(&conf->bindings[conf_hash(section)], node, link);
201 1d126e2d 2019-08-24 stsp LOG_DBG((LOG_MISC, 95, "conf_set_now: [%s]:%s->%s", node->section,
202 1d126e2d 2019-08-24 stsp node->tag, node->value));
203 1d126e2d 2019-08-24 stsp return 0;
204 1d126e2d 2019-08-24 stsp fail:
205 1d126e2d 2019-08-24 stsp free(node->value);
206 1d126e2d 2019-08-24 stsp free(node->tag);
207 1d126e2d 2019-08-24 stsp free(node->section);
208 1d126e2d 2019-08-24 stsp free(node);
209 1d126e2d 2019-08-24 stsp return 1;
210 1d126e2d 2019-08-24 stsp }
211 1d126e2d 2019-08-24 stsp
212 1d126e2d 2019-08-24 stsp /*
213 1d126e2d 2019-08-24 stsp * Parse the line LINE of SZ bytes. Skip Comments, recognize section
214 1d126e2d 2019-08-24 stsp * headers and feed tag-value pairs into our configuration database.
215 1d126e2d 2019-08-24 stsp */
216 1d126e2d 2019-08-24 stsp static const struct got_error *
217 1d126e2d 2019-08-24 stsp conf_parse_line(char **section, struct got_gitconfig *conf, int trans,
218 1d126e2d 2019-08-24 stsp char *line, int ln, size_t sz)
219 1d126e2d 2019-08-24 stsp {
220 1d126e2d 2019-08-24 stsp char *val;
221 1d126e2d 2019-08-24 stsp size_t i;
222 1d126e2d 2019-08-24 stsp int j;
223 1d126e2d 2019-08-24 stsp
224 1d126e2d 2019-08-24 stsp /* Lines starting with '#' or ';' are comments. */
225 1d126e2d 2019-08-24 stsp if (*line == '#' || *line == ';')
226 1d126e2d 2019-08-24 stsp return NULL;
227 1d126e2d 2019-08-24 stsp
228 1d126e2d 2019-08-24 stsp /* '[section]' parsing... */
229 1d126e2d 2019-08-24 stsp if (*line == '[') {
230 1d126e2d 2019-08-24 stsp for (i = 1; i < sz; i++)
231 1d126e2d 2019-08-24 stsp if (line[i] == ']')
232 1d126e2d 2019-08-24 stsp break;
233 1d126e2d 2019-08-24 stsp free(*section);
234 1d126e2d 2019-08-24 stsp if (i == sz) {
235 1d126e2d 2019-08-24 stsp log_print("conf_parse_line: %d:"
236 1d126e2d 2019-08-24 stsp "unmatched ']', ignoring until next section", ln);
237 1d126e2d 2019-08-24 stsp *section = NULL;
238 1d126e2d 2019-08-24 stsp return NULL;
239 1d126e2d 2019-08-24 stsp }
240 00b3e9ae 2023-01-11 op *section = strndup(line + 1, i - 1);
241 1d126e2d 2019-08-24 stsp if (*section == NULL)
242 00b3e9ae 2023-01-11 op return got_error_from_errno("strndup");
243 1d126e2d 2019-08-24 stsp return NULL;
244 1d126e2d 2019-08-24 stsp }
245 1d126e2d 2019-08-24 stsp while (isspace((unsigned char)*line))
246 1d126e2d 2019-08-24 stsp line++;
247 1d126e2d 2019-08-24 stsp
248 1d126e2d 2019-08-24 stsp /* Deal with assignments. */
249 1d126e2d 2019-08-24 stsp for (i = 0; i < sz; i++)
250 1d126e2d 2019-08-24 stsp if (line[i] == '=') {
251 1d126e2d 2019-08-24 stsp /* If no section, we are ignoring the lines. */
252 1d126e2d 2019-08-24 stsp if (!*section) {
253 1d126e2d 2019-08-24 stsp log_print("conf_parse_line: %d: ignoring line "
254 1d126e2d 2019-08-24 stsp "due to no section", ln);
255 1d126e2d 2019-08-24 stsp return NULL;
256 1d126e2d 2019-08-24 stsp }
257 1d126e2d 2019-08-24 stsp line[strcspn(line, " \t=")] = '\0';
258 1d126e2d 2019-08-24 stsp val = line + i + 1 + strspn(line + i + 1, " \t");
259 1d126e2d 2019-08-24 stsp /* Skip trailing whitespace, if any */
260 1d126e2d 2019-08-24 stsp for (j = sz - (val - line) - 1; j > 0 &&
261 1d126e2d 2019-08-24 stsp isspace((unsigned char)val[j]); j--)
262 1d126e2d 2019-08-24 stsp val[j] = '\0';
263 1d126e2d 2019-08-24 stsp /* XXX Perhaps should we not ignore errors? */
264 1d126e2d 2019-08-24 stsp got_gitconfig_set(conf, trans, *section, line, val,
265 1d126e2d 2019-08-24 stsp 0, 0);
266 1d126e2d 2019-08-24 stsp return NULL;
267 1d126e2d 2019-08-24 stsp }
268 1d126e2d 2019-08-24 stsp /* Other non-empty lines are weird. */
269 1d126e2d 2019-08-24 stsp i = strspn(line, " \t");
270 1d126e2d 2019-08-24 stsp if (line[i])
271 1d126e2d 2019-08-24 stsp log_print("conf_parse_line: %d: syntax error", ln);
272 1d126e2d 2019-08-24 stsp
273 1d126e2d 2019-08-24 stsp return NULL;
274 1d126e2d 2019-08-24 stsp }
275 1d126e2d 2019-08-24 stsp
276 1d126e2d 2019-08-24 stsp /* Parse the mapped configuration file. */
277 1d126e2d 2019-08-24 stsp static const struct got_error *
278 1d126e2d 2019-08-24 stsp conf_parse(struct got_gitconfig *conf, int trans, char *buf, size_t sz)
279 1d126e2d 2019-08-24 stsp {
280 1d126e2d 2019-08-24 stsp const struct got_error *err = NULL;
281 1d126e2d 2019-08-24 stsp char *cp = buf;
282 1d126e2d 2019-08-24 stsp char *bufend = buf + sz;
283 1d126e2d 2019-08-24 stsp char *line, *section = NULL;
284 1d126e2d 2019-08-24 stsp int ln = 1;
285 1d126e2d 2019-08-24 stsp
286 1d126e2d 2019-08-24 stsp line = cp;
287 1d126e2d 2019-08-24 stsp while (cp < bufend) {
288 1d126e2d 2019-08-24 stsp if (*cp == '\n') {
289 1d126e2d 2019-08-24 stsp /* Check for escaped newlines. */
290 1d126e2d 2019-08-24 stsp if (cp > buf && *(cp - 1) == '\\')
291 1d126e2d 2019-08-24 stsp *(cp - 1) = *cp = ' ';
292 1d126e2d 2019-08-24 stsp else {
293 1d126e2d 2019-08-24 stsp *cp = '\0';
294 1d126e2d 2019-08-24 stsp err = conf_parse_line(&section, conf, trans,
295 1d126e2d 2019-08-24 stsp line, ln, cp - line);
296 1d126e2d 2019-08-24 stsp if (err)
297 1d126e2d 2019-08-24 stsp return err;
298 1d126e2d 2019-08-24 stsp line = cp + 1;
299 1d126e2d 2019-08-24 stsp }
300 1d126e2d 2019-08-24 stsp ln++;
301 1d126e2d 2019-08-24 stsp }
302 1d126e2d 2019-08-24 stsp cp++;
303 1d126e2d 2019-08-24 stsp }
304 1d126e2d 2019-08-24 stsp if (cp != line)
305 1d126e2d 2019-08-24 stsp log_print("conf_parse: last line unterminated, ignored.");
306 1d126e2d 2019-08-24 stsp return NULL;
307 1d126e2d 2019-08-24 stsp }
308 1d126e2d 2019-08-24 stsp
309 1d126e2d 2019-08-24 stsp const struct got_error *
310 aba9c984 2019-09-08 stsp got_gitconfig_open(struct got_gitconfig **conf, int fd)
311 1d126e2d 2019-08-24 stsp {
312 16aeacf7 2020-11-26 stsp size_t i;
313 1d126e2d 2019-08-24 stsp
314 1d126e2d 2019-08-24 stsp *conf = calloc(1, sizeof(**conf));
315 1d126e2d 2019-08-24 stsp if (*conf == NULL)
316 1d126e2d 2019-08-24 stsp return got_error_from_errno("malloc");
317 1d126e2d 2019-08-24 stsp
318 1d126e2d 2019-08-24 stsp for (i = 0; i < nitems((*conf)->bindings); i++)
319 1d126e2d 2019-08-24 stsp LIST_INIT(&(*conf)->bindings[i]);
320 1d126e2d 2019-08-24 stsp TAILQ_INIT(&(*conf)->trans_queue);
321 aba9c984 2019-09-08 stsp return got_gitconfig_reinit(*conf, fd);
322 1d126e2d 2019-08-24 stsp }
323 1d126e2d 2019-08-24 stsp
324 1d126e2d 2019-08-24 stsp static void
325 1d126e2d 2019-08-24 stsp conf_clear(struct got_gitconfig *conf)
326 1d126e2d 2019-08-24 stsp {
327 1d126e2d 2019-08-24 stsp struct got_gitconfig_binding *cb;
328 16aeacf7 2020-11-26 stsp size_t i;
329 1d126e2d 2019-08-24 stsp
330 1d126e2d 2019-08-24 stsp if (conf->addr) {
331 1d126e2d 2019-08-24 stsp for (i = 0; i < nitems(conf->bindings); i++)
332 1d126e2d 2019-08-24 stsp for (cb = LIST_FIRST(&conf->bindings[i]); cb;
333 1d126e2d 2019-08-24 stsp cb = LIST_FIRST(&conf->bindings[i]))
334 1d126e2d 2019-08-24 stsp conf_remove_now(conf, cb->section, cb->tag);
335 1d126e2d 2019-08-24 stsp free(conf->addr);
336 1d126e2d 2019-08-24 stsp conf->addr = NULL;
337 1d126e2d 2019-08-24 stsp }
338 1d126e2d 2019-08-24 stsp }
339 1d126e2d 2019-08-24 stsp
340 1d126e2d 2019-08-24 stsp /* Execute all queued operations for this transaction. Cleanup. */
341 1d126e2d 2019-08-24 stsp static int
342 1d126e2d 2019-08-24 stsp conf_end(struct got_gitconfig *conf, int transaction, int commit)
343 1d126e2d 2019-08-24 stsp {
344 1d126e2d 2019-08-24 stsp struct got_gitconfig_trans *node, *next;
345 1d126e2d 2019-08-24 stsp
346 1d126e2d 2019-08-24 stsp for (node = TAILQ_FIRST(&conf->trans_queue); node; node = next) {
347 1d126e2d 2019-08-24 stsp next = TAILQ_NEXT(node, link);
348 1d126e2d 2019-08-24 stsp if (node->trans == transaction) {
349 1d126e2d 2019-08-24 stsp if (commit)
350 1d126e2d 2019-08-24 stsp switch (node->op) {
351 1d126e2d 2019-08-24 stsp case CONF_SET:
352 1d126e2d 2019-08-24 stsp conf_set_now(conf, node->section,
353 1d126e2d 2019-08-24 stsp node->tag, node->value,
354 1d126e2d 2019-08-24 stsp node->override, node->is_default);
355 1d126e2d 2019-08-24 stsp break;
356 1d126e2d 2019-08-24 stsp case CONF_REMOVE:
357 1d126e2d 2019-08-24 stsp conf_remove_now(conf, node->section,
358 1d126e2d 2019-08-24 stsp node->tag);
359 1d126e2d 2019-08-24 stsp break;
360 1d126e2d 2019-08-24 stsp case CONF_REMOVE_SECTION:
361 1d126e2d 2019-08-24 stsp conf_remove_section_now(conf, node->section);
362 1d126e2d 2019-08-24 stsp break;
363 1d126e2d 2019-08-24 stsp default:
364 1d126e2d 2019-08-24 stsp log_print("got_gitconfig_end: unknown "
365 1d126e2d 2019-08-24 stsp "operation: %d", node->op);
366 1d126e2d 2019-08-24 stsp }
367 1d126e2d 2019-08-24 stsp TAILQ_REMOVE(&conf->trans_queue, node, link);
368 1d126e2d 2019-08-24 stsp free(node->section);
369 1d126e2d 2019-08-24 stsp free(node->tag);
370 1d126e2d 2019-08-24 stsp free(node->value);
371 1d126e2d 2019-08-24 stsp free(node);
372 1d126e2d 2019-08-24 stsp }
373 1d126e2d 2019-08-24 stsp }
374 1d126e2d 2019-08-24 stsp return 0;
375 1d126e2d 2019-08-24 stsp }
376 1d126e2d 2019-08-24 stsp
377 1d126e2d 2019-08-24 stsp
378 1d126e2d 2019-08-24 stsp void
379 1d126e2d 2019-08-24 stsp got_gitconfig_close(struct got_gitconfig *conf)
380 1d126e2d 2019-08-24 stsp {
381 1d126e2d 2019-08-24 stsp conf_clear(conf);
382 1d126e2d 2019-08-24 stsp free(conf);
383 1d126e2d 2019-08-24 stsp }
384 1d126e2d 2019-08-24 stsp
385 1d126e2d 2019-08-24 stsp static int
386 1d126e2d 2019-08-24 stsp conf_begin(struct got_gitconfig *conf)
387 1d126e2d 2019-08-24 stsp {
388 1d126e2d 2019-08-24 stsp return ++conf->seq;
389 1d126e2d 2019-08-24 stsp }
390 1d126e2d 2019-08-24 stsp
391 1d126e2d 2019-08-24 stsp /* Open the config file and map it into our address space, then parse it. */
392 1d126e2d 2019-08-24 stsp const struct got_error *
393 aba9c984 2019-09-08 stsp got_gitconfig_reinit(struct got_gitconfig *conf, int fd)
394 1d126e2d 2019-08-24 stsp {
395 1d126e2d 2019-08-24 stsp const struct got_error *err = NULL;
396 aba9c984 2019-09-08 stsp int trans;
397 1d126e2d 2019-08-24 stsp size_t sz;
398 1d126e2d 2019-08-24 stsp char *new_conf_addr = 0;
399 1d126e2d 2019-08-24 stsp struct stat st;
400 1d126e2d 2019-08-24 stsp
401 1d126e2d 2019-08-24 stsp if (fstat(fd, &st)) {
402 aba9c984 2019-09-08 stsp err = got_error_from_errno("fstat");
403 1d126e2d 2019-08-24 stsp goto fail;
404 1d126e2d 2019-08-24 stsp }
405 1d126e2d 2019-08-24 stsp
406 1d126e2d 2019-08-24 stsp sz = st.st_size;
407 1d126e2d 2019-08-24 stsp new_conf_addr = malloc(sz);
408 1d126e2d 2019-08-24 stsp if (new_conf_addr == NULL) {
409 1d126e2d 2019-08-24 stsp err = got_error_from_errno("malloc");
410 1d126e2d 2019-08-24 stsp goto fail;
411 1d126e2d 2019-08-24 stsp }
412 1d126e2d 2019-08-24 stsp /* XXX I assume short reads won't happen here. */
413 1d126e2d 2019-08-24 stsp if (read(fd, new_conf_addr, sz) != (int)sz) {
414 1d126e2d 2019-08-24 stsp err = got_error_from_errno("read");
415 1d126e2d 2019-08-24 stsp goto fail;
416 1d126e2d 2019-08-24 stsp }
417 1d126e2d 2019-08-24 stsp
418 1d126e2d 2019-08-24 stsp trans = conf_begin(conf);
419 1d126e2d 2019-08-24 stsp
420 1d126e2d 2019-08-24 stsp err = conf_parse(conf, trans, new_conf_addr, sz);
421 1d126e2d 2019-08-24 stsp if (err)
422 1d126e2d 2019-08-24 stsp goto fail;
423 1d126e2d 2019-08-24 stsp
424 1d126e2d 2019-08-24 stsp /* Free potential existing configuration. */
425 1d126e2d 2019-08-24 stsp conf_clear(conf);
426 1d126e2d 2019-08-24 stsp conf_end(conf, trans, 1);
427 1d126e2d 2019-08-24 stsp conf->addr = new_conf_addr;
428 1d126e2d 2019-08-24 stsp return NULL;
429 1d126e2d 2019-08-24 stsp
430 1d126e2d 2019-08-24 stsp fail:
431 1d126e2d 2019-08-24 stsp free(new_conf_addr);
432 1d126e2d 2019-08-24 stsp return err;
433 1d126e2d 2019-08-24 stsp }
434 1d126e2d 2019-08-24 stsp
435 1d126e2d 2019-08-24 stsp /*
436 1d126e2d 2019-08-24 stsp * Return the numeric value denoted by TAG in section SECTION or DEF
437 1d126e2d 2019-08-24 stsp * if that tag does not exist.
438 1d126e2d 2019-08-24 stsp */
439 1d126e2d 2019-08-24 stsp int
440 fda3525e 2021-09-25 stsp got_gitconfig_get_num(struct got_gitconfig *conf, const char *section,
441 fda3525e 2021-09-25 stsp const char *tag, int def)
442 1d126e2d 2019-08-24 stsp {
443 1d126e2d 2019-08-24 stsp char *value = got_gitconfig_get_str(conf, section, tag);
444 1d126e2d 2019-08-24 stsp
445 1d126e2d 2019-08-24 stsp if (value)
446 1d126e2d 2019-08-24 stsp return atoi(value);
447 1d126e2d 2019-08-24 stsp return def;
448 1d126e2d 2019-08-24 stsp }
449 1d126e2d 2019-08-24 stsp
450 1d126e2d 2019-08-24 stsp /* Validate X according to the range denoted by TAG in section SECTION. */
451 1d126e2d 2019-08-24 stsp int
452 1d126e2d 2019-08-24 stsp got_gitconfig_match_num(struct got_gitconfig *conf, char *section, char *tag,
453 1d126e2d 2019-08-24 stsp int x)
454 1d126e2d 2019-08-24 stsp {
455 1d126e2d 2019-08-24 stsp char *value = got_gitconfig_get_str(conf, section, tag);
456 1d126e2d 2019-08-24 stsp int val, min, max, n;
457 1d126e2d 2019-08-24 stsp
458 1d126e2d 2019-08-24 stsp if (!value)
459 1d126e2d 2019-08-24 stsp return 0;
460 1d126e2d 2019-08-24 stsp n = sscanf(value, "%d,%d:%d", &val, &min, &max);
461 1d126e2d 2019-08-24 stsp switch (n) {
462 1d126e2d 2019-08-24 stsp case 1:
463 1d126e2d 2019-08-24 stsp LOG_DBG((LOG_MISC, 95, "got_gitconfig_match_num: %s:%s %d==%d?",
464 1d126e2d 2019-08-24 stsp section, tag, val, x));
465 1d126e2d 2019-08-24 stsp return x == val;
466 1d126e2d 2019-08-24 stsp case 3:
467 1d126e2d 2019-08-24 stsp LOG_DBG((LOG_MISC, 95, "got_gitconfig_match_num: %s:%s %d<=%d<=%d?",
468 1d126e2d 2019-08-24 stsp section, tag, min, x, max));
469 1d126e2d 2019-08-24 stsp return min <= x && max >= x;
470 1d126e2d 2019-08-24 stsp default:
471 1d126e2d 2019-08-24 stsp log_error("got_gitconfig_match_num: section %s tag %s: invalid number "
472 1d126e2d 2019-08-24 stsp "spec %s", section, tag, value);
473 1d126e2d 2019-08-24 stsp }
474 1d126e2d 2019-08-24 stsp return 0;
475 1d126e2d 2019-08-24 stsp }
476 1d126e2d 2019-08-24 stsp
477 1d126e2d 2019-08-24 stsp /* Return the string value denoted by TAG in section SECTION. */
478 1d126e2d 2019-08-24 stsp char *
479 fda3525e 2021-09-25 stsp got_gitconfig_get_str(struct got_gitconfig *conf, const char *section,
480 fda3525e 2021-09-25 stsp const char *tag)
481 1d126e2d 2019-08-24 stsp {
482 1d126e2d 2019-08-24 stsp struct got_gitconfig_binding *cb;
483 1d126e2d 2019-08-24 stsp
484 1d126e2d 2019-08-24 stsp for (cb = LIST_FIRST(&conf->bindings[conf_hash(section)]); cb;
485 1d126e2d 2019-08-24 stsp cb = LIST_NEXT(cb, link))
486 1d126e2d 2019-08-24 stsp if (strcasecmp(section, cb->section) == 0 &&
487 1d126e2d 2019-08-24 stsp strcasecmp(tag, cb->tag) == 0) {
488 1d126e2d 2019-08-24 stsp LOG_DBG((LOG_MISC, 95, "got_gitconfig_get_str: [%s]:%s->%s",
489 1d126e2d 2019-08-24 stsp section, tag, cb->value));
490 1d126e2d 2019-08-24 stsp return cb->value;
491 1d126e2d 2019-08-24 stsp }
492 1d126e2d 2019-08-24 stsp LOG_DBG((LOG_MISC, 95,
493 1d126e2d 2019-08-24 stsp "got_gitconfig_get_str: configuration value not found [%s]:%s", section,
494 1d126e2d 2019-08-24 stsp tag));
495 1d126e2d 2019-08-24 stsp return 0;
496 1d126e2d 2019-08-24 stsp }
497 cd95becd 2019-11-29 stsp
498 cd95becd 2019-11-29 stsp const struct got_error *
499 cd95becd 2019-11-29 stsp got_gitconfig_get_section_list(struct got_gitconfig_list **sections,
500 cd95becd 2019-11-29 stsp struct got_gitconfig *conf)
501 cd95becd 2019-11-29 stsp {
502 cd95becd 2019-11-29 stsp const struct got_error *err = NULL;
503 cd95becd 2019-11-29 stsp struct got_gitconfig_list *list = NULL;
504 cd95becd 2019-11-29 stsp struct got_gitconfig_list_node *node = 0;
505 cd95becd 2019-11-29 stsp struct got_gitconfig_binding *cb;
506 16aeacf7 2020-11-26 stsp size_t i;
507 cd95becd 2019-11-29 stsp
508 cd95becd 2019-11-29 stsp *sections = NULL;
509 1d126e2d 2019-08-24 stsp
510 cd95becd 2019-11-29 stsp list = malloc(sizeof *list);
511 cd95becd 2019-11-29 stsp if (!list)
512 cd95becd 2019-11-29 stsp return got_error_from_errno("malloc");
513 cd95becd 2019-11-29 stsp TAILQ_INIT(&list->fields);
514 cd95becd 2019-11-29 stsp list->cnt = 0;
515 cd95becd 2019-11-29 stsp for (i = 0; i < nitems(conf->bindings); i++) {
516 cd95becd 2019-11-29 stsp for (cb = LIST_FIRST(&conf->bindings[i]); cb;
517 cd95becd 2019-11-29 stsp cb = LIST_NEXT(cb, link)) {
518 83310ac9 2020-03-20 stsp int section_present = 0;
519 83310ac9 2020-03-20 stsp TAILQ_FOREACH(node, &list->fields, link) {
520 83310ac9 2020-03-20 stsp if (strcmp(node->field, cb->section) == 0) {
521 83310ac9 2020-03-20 stsp section_present = 1;
522 83310ac9 2020-03-20 stsp break;
523 83310ac9 2020-03-20 stsp }
524 83310ac9 2020-03-20 stsp }
525 83310ac9 2020-03-20 stsp if (section_present)
526 83310ac9 2020-03-20 stsp continue;
527 cd95becd 2019-11-29 stsp list->cnt++;
528 cd95becd 2019-11-29 stsp node = calloc(1, sizeof *node);
529 cd95becd 2019-11-29 stsp if (!node) {
530 cd95becd 2019-11-29 stsp err = got_error_from_errno("calloc");
531 cd95becd 2019-11-29 stsp goto cleanup;
532 cd95becd 2019-11-29 stsp }
533 cd95becd 2019-11-29 stsp node->field = strdup(cb->section);
534 cd95becd 2019-11-29 stsp if (!node->field) {
535 cd95becd 2019-11-29 stsp err = got_error_from_errno("strdup");
536 cd95becd 2019-11-29 stsp goto cleanup;
537 cd95becd 2019-11-29 stsp }
538 cd95becd 2019-11-29 stsp TAILQ_INSERT_TAIL(&list->fields, node, link);
539 cd95becd 2019-11-29 stsp }
540 cd95becd 2019-11-29 stsp }
541 cd95becd 2019-11-29 stsp
542 cd95becd 2019-11-29 stsp *sections = list;
543 cd95becd 2019-11-29 stsp return NULL;
544 cd95becd 2019-11-29 stsp
545 cd95becd 2019-11-29 stsp cleanup:
546 cd95becd 2019-11-29 stsp free(node);
547 cd95becd 2019-11-29 stsp if (list)
548 cd95becd 2019-11-29 stsp got_gitconfig_free_list(list);
549 cd95becd 2019-11-29 stsp return err;
550 cd95becd 2019-11-29 stsp }
551 cd95becd 2019-11-29 stsp
552 1d126e2d 2019-08-24 stsp /*
553 1d126e2d 2019-08-24 stsp * Build a list of string values out of the comma separated value denoted by
554 1d126e2d 2019-08-24 stsp * TAG in SECTION.
555 1d126e2d 2019-08-24 stsp */
556 1d126e2d 2019-08-24 stsp struct got_gitconfig_list *
557 1d126e2d 2019-08-24 stsp got_gitconfig_get_list(struct got_gitconfig *conf, char *section, char *tag)
558 1d126e2d 2019-08-24 stsp {
559 1d126e2d 2019-08-24 stsp char *liststr = 0, *p, *field, *t;
560 1d126e2d 2019-08-24 stsp struct got_gitconfig_list *list = 0;
561 1d126e2d 2019-08-24 stsp struct got_gitconfig_list_node *node = 0;
562 1d126e2d 2019-08-24 stsp
563 1d126e2d 2019-08-24 stsp list = malloc(sizeof *list);
564 1d126e2d 2019-08-24 stsp if (!list)
565 1d126e2d 2019-08-24 stsp goto cleanup;
566 1d126e2d 2019-08-24 stsp TAILQ_INIT(&list->fields);
567 1d126e2d 2019-08-24 stsp list->cnt = 0;
568 1d126e2d 2019-08-24 stsp liststr = got_gitconfig_get_str(conf, section, tag);
569 1d126e2d 2019-08-24 stsp if (!liststr)
570 1d126e2d 2019-08-24 stsp goto cleanup;
571 1d126e2d 2019-08-24 stsp liststr = strdup(liststr);
572 1d126e2d 2019-08-24 stsp if (!liststr)
573 1d126e2d 2019-08-24 stsp goto cleanup;
574 1d126e2d 2019-08-24 stsp p = liststr;
575 1d126e2d 2019-08-24 stsp while ((field = strsep(&p, ",")) != NULL) {
576 1d126e2d 2019-08-24 stsp /* Skip leading whitespace */
577 1d126e2d 2019-08-24 stsp while (isspace((unsigned char)*field))
578 1d126e2d 2019-08-24 stsp field++;
579 1d126e2d 2019-08-24 stsp /* Skip trailing whitespace */
580 1d126e2d 2019-08-24 stsp if (p)
581 1d126e2d 2019-08-24 stsp for (t = p - 1; t > field && isspace((unsigned char)*t); t--)
582 1d126e2d 2019-08-24 stsp *t = '\0';
583 1d126e2d 2019-08-24 stsp if (*field == '\0') {
584 1d126e2d 2019-08-24 stsp log_print("got_gitconfig_get_list: empty field, ignoring...");
585 1d126e2d 2019-08-24 stsp continue;
586 1d126e2d 2019-08-24 stsp }
587 1d126e2d 2019-08-24 stsp list->cnt++;
588 1d126e2d 2019-08-24 stsp node = calloc(1, sizeof *node);
589 1d126e2d 2019-08-24 stsp if (!node)
590 1d126e2d 2019-08-24 stsp goto cleanup;
591 1d126e2d 2019-08-24 stsp node->field = strdup(field);
592 1d126e2d 2019-08-24 stsp if (!node->field)
593 1d126e2d 2019-08-24 stsp goto cleanup;
594 1d126e2d 2019-08-24 stsp TAILQ_INSERT_TAIL(&list->fields, node, link);
595 1d126e2d 2019-08-24 stsp }
596 1d126e2d 2019-08-24 stsp free(liststr);
597 1d126e2d 2019-08-24 stsp return list;
598 1d126e2d 2019-08-24 stsp
599 1d126e2d 2019-08-24 stsp cleanup:
600 1d126e2d 2019-08-24 stsp free(node);
601 1d126e2d 2019-08-24 stsp if (list)
602 1d126e2d 2019-08-24 stsp got_gitconfig_free_list(list);
603 1d126e2d 2019-08-24 stsp free(liststr);
604 1d126e2d 2019-08-24 stsp return 0;
605 1d126e2d 2019-08-24 stsp }
606 1d126e2d 2019-08-24 stsp
607 1d126e2d 2019-08-24 stsp struct got_gitconfig_list *
608 fda3525e 2021-09-25 stsp got_gitconfig_get_tag_list(struct got_gitconfig *conf, const char *section)
609 1d126e2d 2019-08-24 stsp {
610 1d126e2d 2019-08-24 stsp struct got_gitconfig_list *list = 0;
611 1d126e2d 2019-08-24 stsp struct got_gitconfig_list_node *node = 0;
612 1d126e2d 2019-08-24 stsp struct got_gitconfig_binding *cb;
613 1d126e2d 2019-08-24 stsp
614 1d126e2d 2019-08-24 stsp list = malloc(sizeof *list);
615 1d126e2d 2019-08-24 stsp if (!list)
616 1d126e2d 2019-08-24 stsp goto cleanup;
617 1d126e2d 2019-08-24 stsp TAILQ_INIT(&list->fields);
618 1d126e2d 2019-08-24 stsp list->cnt = 0;
619 1d126e2d 2019-08-24 stsp for (cb = LIST_FIRST(&conf->bindings[conf_hash(section)]); cb;
620 1d126e2d 2019-08-24 stsp cb = LIST_NEXT(cb, link))
621 1d126e2d 2019-08-24 stsp if (strcasecmp(section, cb->section) == 0) {
622 1d126e2d 2019-08-24 stsp list->cnt++;
623 1d126e2d 2019-08-24 stsp node = calloc(1, sizeof *node);
624 1d126e2d 2019-08-24 stsp if (!node)
625 1d126e2d 2019-08-24 stsp goto cleanup;
626 1d126e2d 2019-08-24 stsp node->field = strdup(cb->tag);
627 1d126e2d 2019-08-24 stsp if (!node->field)
628 1d126e2d 2019-08-24 stsp goto cleanup;
629 1d126e2d 2019-08-24 stsp TAILQ_INSERT_TAIL(&list->fields, node, link);
630 1d126e2d 2019-08-24 stsp }
631 1d126e2d 2019-08-24 stsp return list;
632 1d126e2d 2019-08-24 stsp
633 1d126e2d 2019-08-24 stsp cleanup:
634 1d126e2d 2019-08-24 stsp free(node);
635 1d126e2d 2019-08-24 stsp if (list)
636 1d126e2d 2019-08-24 stsp got_gitconfig_free_list(list);
637 1d126e2d 2019-08-24 stsp return 0;
638 1d126e2d 2019-08-24 stsp }
639 1d126e2d 2019-08-24 stsp
640 1d126e2d 2019-08-24 stsp void
641 1d126e2d 2019-08-24 stsp got_gitconfig_free_list(struct got_gitconfig_list *list)
642 1d126e2d 2019-08-24 stsp {
643 1d126e2d 2019-08-24 stsp struct got_gitconfig_list_node *node = TAILQ_FIRST(&list->fields);
644 1d126e2d 2019-08-24 stsp
645 1d126e2d 2019-08-24 stsp while (node) {
646 1d126e2d 2019-08-24 stsp TAILQ_REMOVE(&list->fields, node, link);
647 1d126e2d 2019-08-24 stsp free(node->field);
648 1d126e2d 2019-08-24 stsp free(node);
649 1d126e2d 2019-08-24 stsp node = TAILQ_FIRST(&list->fields);
650 1d126e2d 2019-08-24 stsp }
651 1d126e2d 2019-08-24 stsp free(list);
652 1d126e2d 2019-08-24 stsp }
653 1d126e2d 2019-08-24 stsp
654 1d126e2d 2019-08-24 stsp static int
655 1d126e2d 2019-08-24 stsp got_gitconfig_trans_node(struct got_gitconfig *conf, int transaction,
656 1d126e2d 2019-08-24 stsp enum got_gitconfig_op op, char *section, char *tag, char *value,
657 1d126e2d 2019-08-24 stsp int override, int is_default)
658 1d126e2d 2019-08-24 stsp {
659 1d126e2d 2019-08-24 stsp struct got_gitconfig_trans *node;
660 1d126e2d 2019-08-24 stsp
661 1d126e2d 2019-08-24 stsp node = calloc(1, sizeof *node);
662 1d126e2d 2019-08-24 stsp if (!node) {
663 1d126e2d 2019-08-24 stsp log_error("got_gitconfig_trans_node: calloc (1, %lu) failed",
664 1d126e2d 2019-08-24 stsp (unsigned long)sizeof *node);
665 1d126e2d 2019-08-24 stsp return 1;
666 1d126e2d 2019-08-24 stsp }
667 1d126e2d 2019-08-24 stsp node->trans = transaction;
668 1d126e2d 2019-08-24 stsp node->op = op;
669 1d126e2d 2019-08-24 stsp node->override = override;
670 1d126e2d 2019-08-24 stsp node->is_default = is_default;
671 1d126e2d 2019-08-24 stsp if (section && (node->section = strdup(section)) == NULL)
672 1d126e2d 2019-08-24 stsp goto fail;
673 1d126e2d 2019-08-24 stsp if (tag && (node->tag = strdup(tag)) == NULL)
674 1d126e2d 2019-08-24 stsp goto fail;
675 1d126e2d 2019-08-24 stsp if (value && (node->value = strdup(value)) == NULL)
676 1d126e2d 2019-08-24 stsp goto fail;
677 1d126e2d 2019-08-24 stsp TAILQ_INSERT_TAIL(&conf->trans_queue, node, link);
678 1d126e2d 2019-08-24 stsp return 0;
679 1d126e2d 2019-08-24 stsp
680 1d126e2d 2019-08-24 stsp fail:
681 1d126e2d 2019-08-24 stsp free(node->section);
682 1d126e2d 2019-08-24 stsp free(node->tag);
683 1d126e2d 2019-08-24 stsp free(node->value);
684 1d126e2d 2019-08-24 stsp free(node);
685 1d126e2d 2019-08-24 stsp return 1;
686 1d126e2d 2019-08-24 stsp }
687 1d126e2d 2019-08-24 stsp
688 1d126e2d 2019-08-24 stsp /* Queue a set operation. */
689 1d126e2d 2019-08-24 stsp int
690 1d126e2d 2019-08-24 stsp got_gitconfig_set(struct got_gitconfig *conf, int transaction, char *section,
691 1d126e2d 2019-08-24 stsp char *tag, char *value, int override, int is_default)
692 1d126e2d 2019-08-24 stsp {
693 1d126e2d 2019-08-24 stsp return got_gitconfig_trans_node(conf, transaction, CONF_SET, section,
694 1d126e2d 2019-08-24 stsp tag, value, override, is_default);
695 1d126e2d 2019-08-24 stsp }
696 1d126e2d 2019-08-24 stsp
697 1d126e2d 2019-08-24 stsp /* Queue a remove operation. */
698 1d126e2d 2019-08-24 stsp int
699 1d126e2d 2019-08-24 stsp got_gitconfig_remove(struct got_gitconfig *conf, int transaction,
700 1d126e2d 2019-08-24 stsp char *section, char *tag)
701 1d126e2d 2019-08-24 stsp {
702 1d126e2d 2019-08-24 stsp return got_gitconfig_trans_node(conf, transaction, CONF_REMOVE,
703 1d126e2d 2019-08-24 stsp section, tag, NULL, 0, 0);
704 1d126e2d 2019-08-24 stsp }
705 1d126e2d 2019-08-24 stsp
706 1d126e2d 2019-08-24 stsp /* Queue a remove section operation. */
707 1d126e2d 2019-08-24 stsp int
708 1d126e2d 2019-08-24 stsp got_gitconfig_remove_section(struct got_gitconfig *conf, int transaction,
709 1d126e2d 2019-08-24 stsp char *section)
710 1d126e2d 2019-08-24 stsp {
711 1d126e2d 2019-08-24 stsp return got_gitconfig_trans_node(conf, transaction, CONF_REMOVE_SECTION,
712 1d126e2d 2019-08-24 stsp section, NULL, NULL, 0, 0);
713 1d126e2d 2019-08-24 stsp }