]>
Commit | Line | Data |
---|---|---|
d04baaba JV |
1 | /* |
2 | * conf.c | |
23229097 | 3 | * |
1dd34058 | 4 | * Copyright (c) 2006-2013 Pacman Development Team <pacman-dev@archlinux.org> |
c72b4543 | 5 | * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org> |
23229097 | 6 | * |
d04baaba JV |
7 | * This program is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (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 | |
9781d0d6 | 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
d04baaba JV |
19 | */ |
20 | ||
bda208f8 DM |
21 | #include <errno.h> |
22 | #include <glob.h> | |
23 | #include <limits.h> | |
95119d46 | 24 | #include <fcntl.h> /* open */ |
d04baaba JV |
25 | #include <stdlib.h> |
26 | #include <stdio.h> | |
c22e381a | 27 | #include <string.h> /* strdup */ |
bda208f8 | 28 | #include <sys/stat.h> |
95119d46 | 29 | #include <sys/types.h> |
bda208f8 DM |
30 | #include <sys/utsname.h> /* uname */ |
31 | #include <unistd.h> | |
d04baaba | 32 | |
d04baaba | 33 | /* pacman */ |
869e81e1 | 34 | #include "conf.h" |
3c3cb001 | 35 | #include "util.h" |
bda208f8 | 36 | #include "pacman.h" |
4fdcf50d | 37 | #include "callback.h" |
d04baaba | 38 | |
86ca39d1 DM |
39 | /* global config variable */ |
40 | config_t *config = NULL; | |
41 | ||
6582f68c SG |
42 | #define NOCOLOR "\033[0m" |
43 | ||
44 | #define BLACK "\033[0;30m" | |
45 | #define RED "\033[0;31m" | |
46 | #define GREEN "\033[0;32m" | |
47 | #define YELLOW "\033[0;33m" | |
48 | #define BLUE "\033[0;34m" | |
49 | #define MAGENTA "\033[0;35m" | |
50 | #define CYAN "\033[0;36m" | |
51 | #define WHITE "\033[0;37m" | |
52 | ||
53 | #define BOLDBLACK "\033[1;30m" | |
54 | #define BOLDRED "\033[1;31m" | |
55 | #define BOLDGREEN "\033[1;32m" | |
56 | #define BOLDYELLOW "\033[1;33m" | |
57 | #define BOLDBLUE "\033[1;34m" | |
58 | #define BOLDMAGENTA "\033[1;35m" | |
59 | #define BOLDCYAN "\033[1;36m" | |
60 | #define BOLDWHITE "\033[1;37m" | |
61 | ||
62 | void enable_colors(int colors) | |
63 | { | |
64 | colstr_t *colstr = &config->colstr; | |
65 | ||
66 | if(colors == PM_COLOR_ON) { | |
67 | colstr->colon = BOLDBLUE "::" BOLDWHITE " "; | |
68 | colstr->title = BOLDWHITE; | |
69 | colstr->repo = BOLDMAGENTA; | |
70 | colstr->version = BOLDGREEN; | |
71 | colstr->groups = BOLDBLUE; | |
72 | colstr->meta = BOLDCYAN; | |
73 | colstr->warn = BOLDYELLOW; | |
74 | colstr->err = BOLDRED; | |
75 | colstr->nocolor = NOCOLOR; | |
76 | } | |
77 | } | |
78 | ||
14606c30 | 79 | config_t *config_new(void) |
98df67cd | 80 | { |
2f0de317 DM |
81 | config_t *newconfig = calloc(1, sizeof(config_t)); |
82 | if(!newconfig) { | |
9e9ecf21 | 83 | pm_printf(ALPM_LOG_ERROR, |
3c3cb001 AM |
84 | _("malloc failure: could not allocate %zd bytes\n"), |
85 | sizeof(config_t)); | |
0303b26b | 86 | return NULL; |
97313ba3 | 87 | } |
c22e381a | 88 | /* defaults which may get overridden later */ |
2f0de317 | 89 | newconfig->op = PM_OP_MAIN; |
ca43fdd9 | 90 | newconfig->logmask = ALPM_LOG_ERROR | ALPM_LOG_WARNING; |
2f0de317 | 91 | newconfig->configfile = strdup(CONFFILE); |
b3612e9c | 92 | newconfig->deltaratio = 0.0; |
69694edd DM |
93 | if(alpm_capabilities() & ALPM_CAPABILITY_SIGNATURES) { |
94 | newconfig->siglevel = ALPM_SIG_PACKAGE | ALPM_SIG_PACKAGE_OPTIONAL | | |
95 | ALPM_SIG_DATABASE | ALPM_SIG_DATABASE_OPTIONAL; | |
33b3b6d9 AM |
96 | newconfig->localfilesiglevel = ALPM_SIG_USE_DEFAULT; |
97 | newconfig->remotefilesiglevel = ALPM_SIG_USE_DEFAULT; | |
69694edd | 98 | } |
98df67cd | 99 | |
6582f68c SG |
100 | newconfig->colstr.colon = ":: "; |
101 | newconfig->colstr.title = ""; | |
102 | newconfig->colstr.repo = ""; | |
103 | newconfig->colstr.version = ""; | |
104 | newconfig->colstr.groups = ""; | |
105 | newconfig->colstr.meta = ""; | |
106 | newconfig->colstr.warn = ""; | |
107 | newconfig->colstr.err = ""; | |
108 | newconfig->colstr.nocolor = ""; | |
109 | ||
0303b26b | 110 | return newconfig; |
98df67cd AF |
111 | } |
112 | ||
2f0de317 | 113 | int config_free(config_t *oldconfig) |
98df67cd | 114 | { |
2f0de317 | 115 | if(oldconfig == NULL) { |
0303b26b | 116 | return -1; |
98df67cd AF |
117 | } |
118 | ||
7edeb276 DM |
119 | alpm_list_free(oldconfig->explicit_adds); |
120 | alpm_list_free(oldconfig->explicit_removes); | |
121 | ||
a888f377 | 122 | FREELIST(oldconfig->holdpkg); |
4fdcf50d DM |
123 | FREELIST(oldconfig->ignorepkg); |
124 | FREELIST(oldconfig->ignoregrp); | |
125 | FREELIST(oldconfig->noupgrade); | |
126 | FREELIST(oldconfig->noextract); | |
2f0de317 | 127 | free(oldconfig->configfile); |
4845207f DM |
128 | free(oldconfig->rootdir); |
129 | free(oldconfig->dbpath); | |
130 | free(oldconfig->logfile); | |
4fdcf50d DM |
131 | free(oldconfig->gpgdir); |
132 | FREELIST(oldconfig->cachedirs); | |
30c4d53c | 133 | free(oldconfig->xfercommand); |
d39b1dbe | 134 | free(oldconfig->print_format); |
4fdcf50d | 135 | free(oldconfig->arch); |
2f0de317 | 136 | free(oldconfig); |
98df67cd | 137 | |
0303b26b | 138 | return 0; |
98df67cd AF |
139 | } |
140 | ||
bda208f8 | 141 | /** Helper function for download_with_xfercommand() */ |
b5709b81 GEP |
142 | static char *get_filename(const char *url) |
143 | { | |
bda208f8 DM |
144 | char *filename = strrchr(url, '/'); |
145 | if(filename != NULL) { | |
146 | filename++; | |
147 | } | |
148 | return filename; | |
149 | } | |
150 | ||
151 | /** Helper function for download_with_xfercommand() */ | |
b5709b81 GEP |
152 | static char *get_destfile(const char *path, const char *filename) |
153 | { | |
bda208f8 DM |
154 | char *destfile; |
155 | /* len = localpath len + filename len + null */ | |
156 | size_t len = strlen(path) + strlen(filename) + 1; | |
157 | destfile = calloc(len, sizeof(char)); | |
158 | snprintf(destfile, len, "%s%s", path, filename); | |
159 | ||
160 | return destfile; | |
161 | } | |
162 | ||
163 | /** Helper function for download_with_xfercommand() */ | |
b5709b81 GEP |
164 | static char *get_tempfile(const char *path, const char *filename) |
165 | { | |
bda208f8 DM |
166 | char *tempfile; |
167 | /* len = localpath len + filename len + '.part' len + null */ | |
168 | size_t len = strlen(path) + strlen(filename) + 6; | |
169 | tempfile = calloc(len, sizeof(char)); | |
170 | snprintf(tempfile, len, "%s%s.part", path, filename); | |
171 | ||
172 | return tempfile; | |
173 | } | |
174 | ||
175 | /** External fetch callback */ | |
176 | static int download_with_xfercommand(const char *url, const char *localpath, | |
b5709b81 GEP |
177 | int force) |
178 | { | |
95119d46 | 179 | int ret = 0, retval; |
bda208f8 | 180 | int usepart = 0; |
95119d46 | 181 | int cwdfd; |
bda208f8 | 182 | struct stat st; |
e13a3bf5 | 183 | char *parsedcmd, *tempcmd; |
bda208f8 DM |
184 | char *destfile, *tempfile, *filename; |
185 | ||
186 | if(!config->xfercommand) { | |
187 | return -1; | |
188 | } | |
189 | ||
190 | filename = get_filename(url); | |
191 | if(!filename) { | |
192 | return -1; | |
193 | } | |
194 | destfile = get_destfile(localpath, filename); | |
195 | tempfile = get_tempfile(localpath, filename); | |
196 | ||
197 | if(force && stat(tempfile, &st) == 0) { | |
198 | unlink(tempfile); | |
199 | } | |
200 | if(force && stat(destfile, &st) == 0) { | |
201 | unlink(destfile); | |
202 | } | |
203 | ||
204 | tempcmd = strdup(config->xfercommand); | |
205 | /* replace all occurrences of %o with fn.part */ | |
206 | if(strstr(tempcmd, "%o")) { | |
207 | usepart = 1; | |
208 | parsedcmd = strreplace(tempcmd, "%o", tempfile); | |
209 | free(tempcmd); | |
210 | tempcmd = parsedcmd; | |
211 | } | |
212 | /* replace all occurrences of %u with the download URL */ | |
213 | parsedcmd = strreplace(tempcmd, "%u", url); | |
214 | free(tempcmd); | |
215 | ||
216 | /* save the cwd so we can restore it later */ | |
95119d46 DM |
217 | do { |
218 | cwdfd = open(".", O_RDONLY); | |
219 | } while(cwdfd == -1 && errno == EINTR); | |
220 | if(cwdfd < 0) { | |
ca43fdd9 | 221 | pm_printf(ALPM_LOG_ERROR, _("could not get current working directory\n")); |
bda208f8 DM |
222 | } |
223 | ||
224 | /* cwd to the download directory */ | |
225 | if(chdir(localpath)) { | |
ca43fdd9 | 226 | pm_printf(ALPM_LOG_WARNING, _("could not chdir to download directory %s\n"), localpath); |
bda208f8 DM |
227 | ret = -1; |
228 | goto cleanup; | |
229 | } | |
230 | /* execute the parsed command via /bin/sh -c */ | |
ca43fdd9 | 231 | pm_printf(ALPM_LOG_DEBUG, "running command: %s\n", parsedcmd); |
bda208f8 DM |
232 | retval = system(parsedcmd); |
233 | ||
234 | if(retval == -1) { | |
ca43fdd9 | 235 | pm_printf(ALPM_LOG_WARNING, _("running XferCommand: fork failed!\n")); |
bda208f8 DM |
236 | ret = -1; |
237 | } else if(retval != 0) { | |
238 | /* download failed */ | |
ca43fdd9 | 239 | pm_printf(ALPM_LOG_DEBUG, "XferCommand command returned non-zero status " |
bda208f8 DM |
240 | "code (%d)\n", retval); |
241 | ret = -1; | |
242 | } else { | |
243 | /* download was successful */ | |
07a12927 | 244 | ret = 0; |
bda208f8 | 245 | if(usepart) { |
07a12927 DM |
246 | if(rename(tempfile, destfile)) { |
247 | pm_printf(ALPM_LOG_ERROR, _("could not rename %s to %s (%s)\n"), | |
248 | tempfile, destfile, strerror(errno)); | |
249 | ret = -1; | |
250 | } | |
bda208f8 | 251 | } |
bda208f8 DM |
252 | } |
253 | ||
254 | cleanup: | |
255 | /* restore the old cwd if we have it */ | |
95119d46 | 256 | if(cwdfd >= 0) { |
a8a1b093 | 257 | int close_ret; |
95119d46 DM |
258 | if(fchdir(cwdfd) != 0) { |
259 | pm_printf(ALPM_LOG_ERROR, _("could not restore working directory (%s)\n"), | |
260 | strerror(errno)); | |
261 | } | |
ba7a056d | 262 | do { |
a8a1b093 DM |
263 | close_ret = close(cwdfd); |
264 | } while(close_ret == -1 && errno == EINTR); | |
bda208f8 DM |
265 | } |
266 | ||
267 | if(ret == -1) { | |
268 | /* hack to let an user the time to cancel a download */ | |
269 | sleep(2); | |
270 | } | |
271 | free(destfile); | |
272 | free(tempfile); | |
273 | free(parsedcmd); | |
274 | ||
275 | return ret; | |
276 | } | |
277 | ||
278 | ||
279 | int config_set_arch(const char *arch) | |
280 | { | |
281 | if(strcmp(arch, "auto") == 0) { | |
282 | struct utsname un; | |
283 | uname(&un); | |
4fdcf50d | 284 | config->arch = strdup(un.machine); |
bda208f8 | 285 | } else { |
4fdcf50d | 286 | config->arch = strdup(arch); |
bda208f8 | 287 | } |
ca43fdd9 | 288 | pm_printf(ALPM_LOG_DEBUG, "config: arch: %s\n", config->arch); |
4fdcf50d | 289 | return 0; |
bda208f8 DM |
290 | } |
291 | ||
b03b06cf DM |
292 | /** |
293 | * Parse a signature verification level line. | |
294 | * @param values the list of parsed option values | |
295 | * @param storage location to store the derived signature level; any existing | |
296 | * value here is used as a starting point | |
e4690232 DM |
297 | * @param file path to the config file |
298 | * @param linenum current line number in file | |
b03b06cf DM |
299 | * @return 0 on success, 1 on any parsing error |
300 | */ | |
e4690232 DM |
301 | static int process_siglevel(alpm_list_t *values, alpm_siglevel_t *storage, |
302 | const char *file, int linenum) | |
0fbdfd02 | 303 | { |
b03b06cf DM |
304 | alpm_siglevel_t level = *storage; |
305 | alpm_list_t *i; | |
306 | int ret = 0; | |
307 | ||
308 | /* Collapse the option names into a single bitmasked value */ | |
309 | for(i = values; i; i = alpm_list_next(i)) { | |
310 | const char *original = i->data, *value; | |
311 | int package = 0, database = 0; | |
312 | ||
3d4656c0 | 313 | if(strncmp(original, "Package", strlen("Package")) == 0) { |
b03b06cf DM |
314 | /* only packages are affected, don't flip flags for databases */ |
315 | value = original + strlen("Package"); | |
316 | package = 1; | |
3d4656c0 | 317 | } else if(strncmp(original, "Database", strlen("Database")) == 0) { |
b03b06cf DM |
318 | /* only databases are affected, don't flip flags for packages */ |
319 | value = original + strlen("Database"); | |
320 | database = 1; | |
321 | } else { | |
322 | /* no prefix, so anything found will affect both packages and dbs */ | |
323 | value = original; | |
324 | package = database = 1; | |
325 | } | |
326 | ||
327 | /* now parse out and store actual flag if it is valid */ | |
328 | if(strcmp(value, "Never") == 0) { | |
329 | if(package) { | |
330 | level &= ~ALPM_SIG_PACKAGE; | |
33b3b6d9 | 331 | level |= ALPM_SIG_PACKAGE_SET; |
b03b06cf DM |
332 | } |
333 | if(database) { | |
334 | level &= ~ALPM_SIG_DATABASE; | |
335 | } | |
336 | } else if(strcmp(value, "Optional") == 0) { | |
337 | if(package) { | |
338 | level |= ALPM_SIG_PACKAGE; | |
339 | level |= ALPM_SIG_PACKAGE_OPTIONAL; | |
33b3b6d9 | 340 | level |= ALPM_SIG_PACKAGE_SET; |
b03b06cf DM |
341 | } |
342 | if(database) { | |
343 | level |= ALPM_SIG_DATABASE; | |
344 | level |= ALPM_SIG_DATABASE_OPTIONAL; | |
345 | } | |
346 | } else if(strcmp(value, "Required") == 0) { | |
347 | if(package) { | |
348 | level |= ALPM_SIG_PACKAGE; | |
349 | level &= ~ALPM_SIG_PACKAGE_OPTIONAL; | |
33b3b6d9 | 350 | level |= ALPM_SIG_PACKAGE_SET; |
b03b06cf DM |
351 | } |
352 | if(database) { | |
353 | level |= ALPM_SIG_DATABASE; | |
354 | level &= ~ALPM_SIG_DATABASE_OPTIONAL; | |
355 | } | |
356 | } else if(strcmp(value, "TrustedOnly") == 0) { | |
357 | if(package) { | |
358 | level &= ~ALPM_SIG_PACKAGE_MARGINAL_OK; | |
359 | level &= ~ALPM_SIG_PACKAGE_UNKNOWN_OK; | |
33b3b6d9 | 360 | level |= ALPM_SIG_PACKAGE_TRUST_SET; |
b03b06cf DM |
361 | } |
362 | if(database) { | |
363 | level &= ~ALPM_SIG_DATABASE_MARGINAL_OK; | |
364 | level &= ~ALPM_SIG_DATABASE_UNKNOWN_OK; | |
365 | } | |
366 | } else if(strcmp(value, "TrustAll") == 0) { | |
367 | if(package) { | |
368 | level |= ALPM_SIG_PACKAGE_MARGINAL_OK; | |
369 | level |= ALPM_SIG_PACKAGE_UNKNOWN_OK; | |
33b3b6d9 | 370 | level |= ALPM_SIG_PACKAGE_TRUST_SET; |
b03b06cf DM |
371 | } |
372 | if(database) { | |
373 | level |= ALPM_SIG_DATABASE_MARGINAL_OK; | |
374 | level |= ALPM_SIG_DATABASE_UNKNOWN_OK; | |
375 | } | |
376 | } else { | |
e4690232 DM |
377 | pm_printf(ALPM_LOG_ERROR, |
378 | _("config file %s, line %d: invalid value for '%s' : '%s'\n"), | |
379 | file, linenum, "SigLevel", original); | |
b03b06cf DM |
380 | ret = 1; |
381 | } | |
382 | level &= ~ALPM_SIG_USE_DEFAULT; | |
0fbdfd02 | 383 | } |
b03b06cf | 384 | |
69694edd DM |
385 | /* ensure we have sig checking ability and are actually turning it on */ |
386 | if(!(alpm_capabilities() & ALPM_CAPABILITY_SIGNATURES) && | |
387 | level & (ALPM_SIG_PACKAGE | ALPM_SIG_DATABASE)) { | |
e4690232 DM |
388 | pm_printf(ALPM_LOG_ERROR, |
389 | _("config file %s, line %d: '%s' option invalid, no signature support\n"), | |
390 | file, linenum, "SigLevel"); | |
69694edd DM |
391 | ret = 1; |
392 | } | |
393 | ||
b03b06cf DM |
394 | if(!ret) { |
395 | *storage = level; | |
396 | } | |
397 | return ret; | |
0fbdfd02 | 398 | } |
bda208f8 | 399 | |
33b3b6d9 AM |
400 | /** |
401 | * Merge the package entires of two signature verification levels. | |
402 | * @param base initial siglevel | |
403 | * @param over overridden siglevel, derived value is stored here | |
404 | */ | |
405 | static void merge_siglevel(alpm_siglevel_t *base, alpm_siglevel_t *over) | |
406 | { | |
407 | alpm_siglevel_t level = *over; | |
408 | if(level & ALPM_SIG_USE_DEFAULT) { | |
409 | level = *base; | |
410 | } else { | |
411 | if(!(level & ALPM_SIG_PACKAGE_SET)) { | |
412 | level |= *base & ALPM_SIG_PACKAGE; | |
413 | level |= *base & ALPM_SIG_PACKAGE_OPTIONAL; | |
414 | } | |
415 | if(!(level & ALPM_SIG_PACKAGE_TRUST_SET)) { | |
416 | level |= *base & ALPM_SIG_PACKAGE_MARGINAL_OK; | |
417 | level |= *base & ALPM_SIG_PACKAGE_UNKNOWN_OK; | |
418 | } | |
419 | } | |
420 | ||
421 | *over = level; | |
422 | } | |
423 | ||
e4690232 DM |
424 | static int process_cleanmethods(alpm_list_t *values, |
425 | const char *file, int linenum) | |
426 | { | |
4fdcf50d DM |
427 | alpm_list_t *i; |
428 | for(i = values; i; i = alpm_list_next(i)) { | |
429 | const char *value = i->data; | |
430 | if(strcmp(value, "KeepInstalled") == 0) { | |
431 | config->cleanmethod |= PM_CLEAN_KEEPINST; | |
432 | } else if(strcmp(value, "KeepCurrent") == 0) { | |
433 | config->cleanmethod |= PM_CLEAN_KEEPCUR; | |
434 | } else { | |
e4690232 DM |
435 | pm_printf(ALPM_LOG_ERROR, |
436 | _("config file %s, line %d: invalid value for '%s' : '%s'\n"), | |
437 | file, linenum, "CleanMethod", value); | |
4fdcf50d DM |
438 | return 1; |
439 | } | |
bda208f8 DM |
440 | } |
441 | return 0; | |
442 | } | |
443 | ||
444 | /** Add repeating options such as NoExtract, NoUpgrade, etc to libalpm | |
445 | * settings. Refactored out of the parseconfig code since all of them did | |
446 | * the exact same thing and duplicated code. | |
447 | * @param ptr a pointer to the start of the multiple options | |
448 | * @param option the string (friendly) name of the option, used for messages | |
4fdcf50d | 449 | * @param list the list to add the option to |
bda208f8 DM |
450 | */ |
451 | static void setrepeatingoption(char *ptr, const char *option, | |
4fdcf50d | 452 | alpm_list_t **list) |
bda208f8 | 453 | { |
16312553 AG |
454 | char *val, *saveptr; |
455 | ||
456 | val = strtok_r(ptr, " ", &saveptr); | |
457 | while(val) { | |
458 | *list = alpm_list_add(*list, strdup(val)); | |
459 | pm_printf(ALPM_LOG_DEBUG, "config: %s: %s\n", option, val); | |
460 | val = strtok_r(NULL, " ", &saveptr); | |
bda208f8 | 461 | } |
bda208f8 DM |
462 | } |
463 | ||
464 | static int _parse_options(const char *key, char *value, | |
465 | const char *file, int linenum) | |
466 | { | |
467 | if(value == NULL) { | |
468 | /* options without settings */ | |
469 | if(strcmp(key, "UseSyslog") == 0) { | |
4fdcf50d | 470 | config->usesyslog = 1; |
ca43fdd9 | 471 | pm_printf(ALPM_LOG_DEBUG, "config: usesyslog\n"); |
bda208f8 DM |
472 | } else if(strcmp(key, "ILoveCandy") == 0) { |
473 | config->chomp = 1; | |
ca43fdd9 | 474 | pm_printf(ALPM_LOG_DEBUG, "config: chomp\n"); |
bda208f8 DM |
475 | } else if(strcmp(key, "VerbosePkgLists") == 0) { |
476 | config->verbosepkglists = 1; | |
ca43fdd9 | 477 | pm_printf(ALPM_LOG_DEBUG, "config: verbosepkglists\n"); |
bda208f8 | 478 | } else if(strcmp(key, "UseDelta") == 0) { |
b3612e9c DM |
479 | config->deltaratio = 0.7; |
480 | pm_printf(ALPM_LOG_DEBUG, "config: usedelta (default 0.7)\n"); | |
bda208f8 DM |
481 | } else if(strcmp(key, "TotalDownload") == 0) { |
482 | config->totaldownload = 1; | |
ca43fdd9 | 483 | pm_printf(ALPM_LOG_DEBUG, "config: totaldownload\n"); |
bda208f8 | 484 | } else if(strcmp(key, "CheckSpace") == 0) { |
4fdcf50d | 485 | config->checkspace = 1; |
c8c7a513 SG |
486 | } else if(strcmp(key, "Color") == 0) { |
487 | if(config->color == PM_COLOR_UNSET) { | |
488 | config->color = isatty(fileno(stdout)) ? PM_COLOR_ON : PM_COLOR_OFF; | |
6582f68c | 489 | enable_colors(config->color); |
c8c7a513 | 490 | } |
bda208f8 | 491 | } else { |
ca43fdd9 | 492 | pm_printf(ALPM_LOG_WARNING, |
bda208f8 DM |
493 | _("config file %s, line %d: directive '%s' in section '%s' not recognized.\n"), |
494 | file, linenum, key, "options"); | |
495 | } | |
496 | } else { | |
497 | /* options with settings */ | |
498 | if(strcmp(key, "NoUpgrade") == 0) { | |
4fdcf50d | 499 | setrepeatingoption(value, "NoUpgrade", &(config->noupgrade)); |
bda208f8 | 500 | } else if(strcmp(key, "NoExtract") == 0) { |
4fdcf50d | 501 | setrepeatingoption(value, "NoExtract", &(config->noextract)); |
bda208f8 | 502 | } else if(strcmp(key, "IgnorePkg") == 0) { |
4fdcf50d | 503 | setrepeatingoption(value, "IgnorePkg", &(config->ignorepkg)); |
bda208f8 | 504 | } else if(strcmp(key, "IgnoreGroup") == 0) { |
4fdcf50d | 505 | setrepeatingoption(value, "IgnoreGroup", &(config->ignoregrp)); |
bda208f8 | 506 | } else if(strcmp(key, "HoldPkg") == 0) { |
4fdcf50d | 507 | setrepeatingoption(value, "HoldPkg", &(config->holdpkg)); |
4fdcf50d DM |
508 | } else if(strcmp(key, "CacheDir") == 0) { |
509 | setrepeatingoption(value, "CacheDir", &(config->cachedirs)); | |
bda208f8 | 510 | } else if(strcmp(key, "Architecture") == 0) { |
4fdcf50d | 511 | if(!config->arch) { |
bda208f8 DM |
512 | config_set_arch(value); |
513 | } | |
b3612e9c DM |
514 | } else if(strcmp(key, "UseDelta") == 0) { |
515 | double ratio; | |
516 | char *endptr; | |
517 | ratio = strtod(value, &endptr); | |
518 | if(*endptr != '\0' || ratio < 0.0 || ratio > 2.0) { | |
519 | pm_printf(ALPM_LOG_ERROR, | |
520 | _("config file %s, line %d: invalid value for '%s' : '%s'\n"), | |
521 | file, linenum, "UseDelta", value); | |
522 | return 1; | |
523 | } | |
524 | config->deltaratio = ratio; | |
525 | pm_printf(ALPM_LOG_DEBUG, "config: usedelta = %f\n", ratio); | |
bda208f8 DM |
526 | } else if(strcmp(key, "DBPath") == 0) { |
527 | /* don't overwrite a path specified on the command line */ | |
528 | if(!config->dbpath) { | |
529 | config->dbpath = strdup(value); | |
ca43fdd9 | 530 | pm_printf(ALPM_LOG_DEBUG, "config: dbpath: %s\n", value); |
bda208f8 | 531 | } |
bda208f8 DM |
532 | } else if(strcmp(key, "RootDir") == 0) { |
533 | /* don't overwrite a path specified on the command line */ | |
534 | if(!config->rootdir) { | |
535 | config->rootdir = strdup(value); | |
ca43fdd9 | 536 | pm_printf(ALPM_LOG_DEBUG, "config: rootdir: %s\n", value); |
bda208f8 DM |
537 | } |
538 | } else if(strcmp(key, "GPGDir") == 0) { | |
539 | if(!config->gpgdir) { | |
540 | config->gpgdir = strdup(value); | |
ca43fdd9 | 541 | pm_printf(ALPM_LOG_DEBUG, "config: gpgdir: %s\n", value); |
bda208f8 DM |
542 | } |
543 | } else if(strcmp(key, "LogFile") == 0) { | |
544 | if(!config->logfile) { | |
545 | config->logfile = strdup(value); | |
ca43fdd9 | 546 | pm_printf(ALPM_LOG_DEBUG, "config: logfile: %s\n", value); |
bda208f8 DM |
547 | } |
548 | } else if(strcmp(key, "XferCommand") == 0) { | |
549 | config->xfercommand = strdup(value); | |
ca43fdd9 | 550 | pm_printf(ALPM_LOG_DEBUG, "config: xfercommand: %s\n", value); |
bda208f8 | 551 | } else if(strcmp(key, "CleanMethod") == 0) { |
4fdcf50d DM |
552 | alpm_list_t *methods = NULL; |
553 | setrepeatingoption(value, "CleanMethod", &methods); | |
e4690232 | 554 | if(process_cleanmethods(methods, file, linenum)) { |
4fdcf50d DM |
555 | FREELIST(methods); |
556 | return 1; | |
557 | } | |
558 | FREELIST(methods); | |
b03b06cf DM |
559 | } else if(strcmp(key, "SigLevel") == 0) { |
560 | alpm_list_t *values = NULL; | |
561 | setrepeatingoption(value, "SigLevel", &values); | |
e4690232 | 562 | if(process_siglevel(values, &config->siglevel, file, linenum)) { |
b03b06cf | 563 | FREELIST(values); |
bda208f8 DM |
564 | return 1; |
565 | } | |
b03b06cf | 566 | FREELIST(values); |
33b3b6d9 AM |
567 | } else if(strcmp(key, "LocalFileSigLevel") == 0) { |
568 | alpm_list_t *values = NULL; | |
569 | setrepeatingoption(value, "LocalFileSigLevel", &values); | |
570 | if(process_siglevel(values, &config->localfilesiglevel, file, linenum)) { | |
571 | FREELIST(values); | |
572 | return 1; | |
573 | } | |
574 | FREELIST(values); | |
575 | } else if(strcmp(key, "RemoteFileSigLevel") == 0) { | |
576 | alpm_list_t *values = NULL; | |
577 | setrepeatingoption(value, "RemoteFileSigLevel", &values); | |
578 | if(process_siglevel(values, &config->remotefilesiglevel, file, linenum)) { | |
579 | FREELIST(values); | |
580 | return 1; | |
581 | } | |
582 | FREELIST(values); | |
bda208f8 | 583 | } else { |
ca43fdd9 | 584 | pm_printf(ALPM_LOG_WARNING, |
bda208f8 DM |
585 | _("config file %s, line %d: directive '%s' in section '%s' not recognized.\n"), |
586 | file, linenum, key, "options"); | |
587 | } | |
588 | ||
589 | } | |
590 | return 0; | |
591 | } | |
592 | ||
939d5a95 | 593 | static int _add_mirror(alpm_db_t *db, char *value) |
bda208f8 DM |
594 | { |
595 | const char *dbname = alpm_db_get_name(db); | |
596 | /* let's attempt a replacement for the current repo */ | |
597 | char *temp = strreplace(value, "$repo", dbname); | |
598 | /* let's attempt a replacement for the arch */ | |
4fdcf50d | 599 | const char *arch = config->arch; |
bda208f8 DM |
600 | char *server; |
601 | if(arch) { | |
602 | server = strreplace(temp, "$arch", arch); | |
603 | free(temp); | |
604 | } else { | |
605 | if(strstr(temp, "$arch")) { | |
606 | free(temp); | |
ca58e326 | 607 | pm_printf(ALPM_LOG_ERROR, |
e4690232 | 608 | _("mirror '%s' contains the '%s' variable, but no '%s' is defined.\n"), |
ca58e326 | 609 | value, "$arch", "Architecture"); |
bda208f8 DM |
610 | return 1; |
611 | } | |
612 | server = temp; | |
613 | } | |
614 | ||
42ab639b | 615 | if(alpm_db_add_server(db, server) != 0) { |
bda208f8 | 616 | /* pm_errno is set by alpm_db_setserver */ |
ca43fdd9 | 617 | pm_printf(ALPM_LOG_ERROR, _("could not add server URL to database '%s': %s (%s)\n"), |
e2aa9526 | 618 | dbname, server, alpm_strerror(alpm_errno(config->handle))); |
bda208f8 DM |
619 | free(server); |
620 | return 1; | |
621 | } | |
622 | ||
623 | free(server); | |
624 | return 0; | |
625 | } | |
626 | ||
4fdcf50d | 627 | /** Sets up libalpm global stuff in one go. Called after the command line |
bda208f8 DM |
628 | * and inital config file parsing. Once this is complete, we can see if any |
629 | * paths were defined. If a rootdir was defined and nothing else, we want all | |
630 | * of our paths to live under the rootdir that was specified. Safe to call | |
631 | * multiple times (will only do anything the first time). | |
632 | */ | |
4fdcf50d | 633 | static int setup_libalpm(void) |
bda208f8 | 634 | { |
73c74355 | 635 | int ret = 0; |
a3f93992 | 636 | alpm_errno_t err; |
64c1cf79 | 637 | alpm_handle_t *handle; |
4fdcf50d | 638 | |
ca43fdd9 | 639 | pm_printf(ALPM_LOG_DEBUG, "setup_libalpm called\n"); |
73c74355 | 640 | |
73c74355 DM |
641 | /* Configure root path first. If it is set and dbpath/logfile were not |
642 | * set, then set those as well to reside under the root. */ | |
643 | if(config->rootdir) { | |
644 | char path[PATH_MAX]; | |
73c74355 | 645 | if(!config->dbpath) { |
4fdcf50d | 646 | snprintf(path, PATH_MAX, "%s/%s", config->rootdir, DBPATH + 1); |
73c74355 | 647 | config->dbpath = strdup(path); |
bda208f8 | 648 | } |
73c74355 | 649 | if(!config->logfile) { |
4fdcf50d | 650 | snprintf(path, PATH_MAX, "%s/%s", config->rootdir, LOGFILE + 1); |
73c74355 | 651 | config->logfile = strdup(path); |
bda208f8 | 652 | } |
4fdcf50d DM |
653 | } else { |
654 | config->rootdir = strdup(ROOTDIR); | |
655 | if(!config->dbpath) { | |
656 | config->dbpath = strdup(DBPATH); | |
bda208f8 | 657 | } |
73c74355 | 658 | } |
4fdcf50d DM |
659 | |
660 | /* initialize library */ | |
17a6ac56 DM |
661 | handle = alpm_initialize(config->rootdir, config->dbpath, &err); |
662 | if(!handle) { | |
ca43fdd9 | 663 | pm_printf(ALPM_LOG_ERROR, _("failed to initialize alpm library (%s)\n"), |
4fdcf50d | 664 | alpm_strerror(err)); |
afc96f2a | 665 | if(err == ALPM_ERR_DB_VERSION) { |
ca43fdd9 | 666 | pm_printf(ALPM_LOG_ERROR, _(" try running pacman-db-upgrade\n")); |
1150d9e1 | 667 | } |
4fdcf50d DM |
668 | return -1; |
669 | } | |
17a6ac56 | 670 | config->handle = handle; |
4fdcf50d | 671 | |
17a6ac56 DM |
672 | alpm_option_set_logcb(handle, cb_log); |
673 | alpm_option_set_dlcb(handle, cb_dl_progress); | |
98fdfa19 DM |
674 | alpm_option_set_eventcb(handle, cb_event); |
675 | alpm_option_set_questioncb(handle, cb_question); | |
676 | alpm_option_set_progresscb(handle, cb_progress); | |
4fdcf50d DM |
677 | |
678 | config->logfile = config->logfile ? config->logfile : strdup(LOGFILE); | |
17a6ac56 | 679 | ret = alpm_option_set_logfile(handle, config->logfile); |
4fdcf50d | 680 | if(ret != 0) { |
ca43fdd9 | 681 | pm_printf(ALPM_LOG_ERROR, _("problem setting logfile '%s' (%s)\n"), |
db3b86e7 | 682 | config->logfile, alpm_strerror(alpm_errno(handle))); |
4fdcf50d | 683 | return ret; |
73c74355 | 684 | } |
bda208f8 | 685 | |
73c74355 DM |
686 | /* Set GnuPG's home directory. This is not relative to rootdir, even if |
687 | * rootdir is defined. Reasoning: gpgdir contains configuration data. */ | |
4fdcf50d | 688 | config->gpgdir = config->gpgdir ? config->gpgdir : strdup(GPGDIR); |
1cd6515a | 689 | ret = alpm_option_set_gpgdir(handle, config->gpgdir); |
4fdcf50d | 690 | if(ret != 0) { |
ca43fdd9 | 691 | pm_printf(ALPM_LOG_ERROR, _("problem setting gpgdir '%s' (%s)\n"), |
db3b86e7 | 692 | config->gpgdir, alpm_strerror(alpm_errno(handle))); |
4fdcf50d | 693 | return ret; |
bda208f8 | 694 | } |
73c74355 DM |
695 | |
696 | /* add a default cachedir if one wasn't specified */ | |
4fdcf50d | 697 | if(config->cachedirs == NULL) { |
17a6ac56 | 698 | alpm_option_add_cachedir(handle, CACHEDIR); |
4fdcf50d | 699 | } else { |
17a6ac56 | 700 | alpm_option_set_cachedirs(handle, config->cachedirs); |
4fdcf50d DM |
701 | } |
702 | ||
33f25185 | 703 | alpm_option_set_default_siglevel(handle, config->siglevel); |
4fdcf50d | 704 | |
33b3b6d9 AM |
705 | merge_siglevel(&config->siglevel, &config->localfilesiglevel); |
706 | merge_siglevel(&config->siglevel, &config->remotefilesiglevel); | |
707 | alpm_option_set_local_file_siglevel(handle, config->localfilesiglevel); | |
708 | alpm_option_set_remote_file_siglevel(handle, config->remotefilesiglevel); | |
709 | ||
4fdcf50d | 710 | if(config->xfercommand) { |
17a6ac56 | 711 | alpm_option_set_fetchcb(handle, download_with_xfercommand); |
361ed6a6 DM |
712 | } else if(!(alpm_capabilities() & ALPM_CAPABILITY_DOWNLOADER)) { |
713 | pm_printf(ALPM_LOG_WARNING, _("no '%s' configured"), "XferCommand"); | |
73c74355 | 714 | } |
4fdcf50d DM |
715 | |
716 | if(config->totaldownload) { | |
17a6ac56 | 717 | alpm_option_set_totaldlcb(handle, cb_dl_total); |
4fdcf50d DM |
718 | } |
719 | ||
17a6ac56 DM |
720 | alpm_option_set_arch(handle, config->arch); |
721 | alpm_option_set_checkspace(handle, config->checkspace); | |
722 | alpm_option_set_usesyslog(handle, config->usesyslog); | |
b3612e9c | 723 | alpm_option_set_deltaratio(handle, config->deltaratio); |
4fdcf50d | 724 | |
17a6ac56 | 725 | alpm_option_set_ignorepkgs(handle, config->ignorepkg); |
f1bb56ce | 726 | alpm_option_set_ignoregroups(handle, config->ignoregrp); |
17a6ac56 DM |
727 | alpm_option_set_noupgrades(handle, config->noupgrade); |
728 | alpm_option_set_noextracts(handle, config->noextract); | |
4fdcf50d | 729 | |
73c74355 | 730 | return 0; |
bda208f8 DM |
731 | } |
732 | ||
a7755309 DM |
733 | /** |
734 | * Allows parsing in advance of an entire config section before we start | |
735 | * calling library methods. | |
736 | */ | |
737 | struct section_t { | |
738 | /* useful for all sections */ | |
739 | char *name; | |
740 | int is_options; | |
741 | /* db section option gathering */ | |
7af0ab1c | 742 | alpm_siglevel_t siglevel; |
a7755309 DM |
743 | alpm_list_t *servers; |
744 | }; | |
745 | ||
746 | /** | |
747 | * Wrap up a section once we have reached the end of it. This should be called | |
748 | * when a subsequent section is encountered, or when we have reached the end of | |
749 | * the root config file. Once called, all existing saved config pieces on the | |
750 | * section struct are freed. | |
751 | * @param section the current parsed and saved section data | |
752 | * @param parse_options whether we are parsing options or repo data | |
753 | * @return 0 on success, 1 on failure | |
754 | */ | |
755 | static int finish_section(struct section_t *section, int parse_options) | |
756 | { | |
757 | int ret = 0; | |
758 | alpm_list_t *i; | |
939d5a95 | 759 | alpm_db_t *db; |
a7755309 | 760 | |
ca43fdd9 | 761 | pm_printf(ALPM_LOG_DEBUG, "config: finish section '%s'\n", section->name); |
a7755309 DM |
762 | |
763 | /* parsing options (or nothing)- nothing to do except free the pieces */ | |
764 | if(!section->name || parse_options || section->is_options) { | |
765 | goto cleanup; | |
766 | } | |
767 | ||
768 | /* if we are not looking at options sections only, register a db */ | |
b488f229 | 769 | db = alpm_register_syncdb(config->handle, section->name, section->siglevel); |
a7755309 | 770 | if(db == NULL) { |
ca43fdd9 | 771 | pm_printf(ALPM_LOG_ERROR, _("could not register '%s' database (%s)\n"), |
a7755309 DM |
772 | section->name, alpm_strerror(alpm_errno(config->handle))); |
773 | ret = 1; | |
774 | goto cleanup; | |
775 | } | |
776 | ||
a7755309 | 777 | for(i = section->servers; i; i = alpm_list_next(i)) { |
6be492d2 | 778 | char *value = i->data; |
a7755309 | 779 | if(_add_mirror(db, value) != 0) { |
ca43fdd9 | 780 | pm_printf(ALPM_LOG_ERROR, |
a7755309 DM |
781 | _("could not add mirror '%s' to database '%s' (%s)\n"), |
782 | value, section->name, alpm_strerror(alpm_errno(config->handle))); | |
783 | ret = 1; | |
784 | goto cleanup; | |
785 | } | |
786 | free(value); | |
787 | } | |
788 | ||
789 | cleanup: | |
790 | alpm_list_free(section->servers); | |
791 | section->servers = NULL; | |
7af0ab1c | 792 | section->siglevel = ALPM_SIG_USE_DEFAULT; |
a7755309 DM |
793 | free(section->name); |
794 | section->name = NULL; | |
795 | return ret; | |
796 | } | |
bda208f8 | 797 | |
c730ca59 DM |
798 | /** The "real" parseconfig. Each "Include" directive will recall this method so |
799 | * recursion and stack depth are limited to 10 levels. The publicly visible | |
800 | * parseconfig calls this with a NULL section argument so we can recall from | |
801 | * within ourself on an include. | |
802 | * @param file path to the config file | |
a7755309 | 803 | * @param section the current active section |
c730ca59 DM |
804 | * @param parse_options whether to parse and call methods for the options |
805 | * section; if 0, parse and call methods for the repos sections | |
806 | * @param depth the current recursion depth | |
a7755309 DM |
807 | * @return 0 on success, 1 on failure |
808 | */ | |
809 | static int _parseconfig(const char *file, struct section_t *section, | |
c730ca59 | 810 | int parse_options, int depth) |
bda208f8 DM |
811 | { |
812 | FILE *fp = NULL; | |
e826c143 | 813 | char line[PATH_MAX]; |
bda208f8 | 814 | int linenum = 0; |
bda208f8 | 815 | int ret = 0; |
c730ca59 DM |
816 | const int max_depth = 10; |
817 | ||
818 | if(depth >= max_depth) { | |
ca43fdd9 | 819 | pm_printf(ALPM_LOG_ERROR, |
c730ca59 DM |
820 | _("config parsing exceeded max recursion depth of %d.\n"), max_depth); |
821 | ret = 1; | |
822 | goto cleanup; | |
823 | } | |
bda208f8 | 824 | |
ca43fdd9 | 825 | pm_printf(ALPM_LOG_DEBUG, "config: attempting to read file %s\n", file); |
bda208f8 DM |
826 | fp = fopen(file, "r"); |
827 | if(fp == NULL) { | |
392fffe2 DR |
828 | pm_printf(ALPM_LOG_ERROR, _("config file %s could not be read: %s\n"), |
829 | file, strerror(errno)); | |
c730ca59 DM |
830 | ret = 1; |
831 | goto cleanup; | |
bda208f8 DM |
832 | } |
833 | ||
bda208f8 | 834 | while(fgets(line, PATH_MAX, fp)) { |
c730ca59 DM |
835 | char *key, *value, *ptr; |
836 | size_t line_len; | |
73c74355 | 837 | |
bda208f8 | 838 | linenum++; |
bda208f8 DM |
839 | |
840 | /* ignore whole line and end of line comments */ | |
bda208f8 DM |
841 | if((ptr = strchr(line, '#'))) { |
842 | *ptr = '\0'; | |
843 | } | |
844 | ||
c34b69b5 | 845 | line_len = strtrim(line); |
d95c0494 AM |
846 | |
847 | if(line_len == 0) { | |
848 | continue; | |
849 | } | |
850 | ||
c730ca59 | 851 | if(line[0] == '[' && line[line_len - 1] == ']') { |
dd95c96b | 852 | char *name; |
c730ca59 DM |
853 | /* only possibility here is a line == '[]' */ |
854 | if(line_len <= 2) { | |
ca43fdd9 | 855 | pm_printf(ALPM_LOG_ERROR, _("config file %s, line %d: bad section name.\n"), |
bda208f8 DM |
856 | file, linenum); |
857 | ret = 1; | |
858 | goto cleanup; | |
859 | } | |
c730ca59 DM |
860 | /* new config section, skip the '[' */ |
861 | name = strdup(line + 1); | |
862 | name[line_len - 2] = '\0'; | |
a7755309 DM |
863 | /* we're at a new section; perform any post-actions for the prior */ |
864 | if(finish_section(section, parse_options)) { | |
865 | ret = 1; | |
866 | goto cleanup; | |
dd95c96b | 867 | } |
ca43fdd9 | 868 | pm_printf(ALPM_LOG_DEBUG, "config: new section '%s'\n", name); |
a7755309 DM |
869 | section->name = name; |
870 | section->is_options = (strcmp(name, "options") == 0); | |
bda208f8 DM |
871 | continue; |
872 | } | |
873 | ||
874 | /* directive */ | |
bda208f8 DM |
875 | /* strsep modifies the 'line' string: 'key \0 value' */ |
876 | key = line; | |
877 | value = line; | |
878 | strsep(&value, "="); | |
879 | strtrim(key); | |
880 | strtrim(value); | |
881 | ||
882 | if(key == NULL) { | |
ca43fdd9 | 883 | pm_printf(ALPM_LOG_ERROR, _("config file %s, line %d: syntax error in config file- missing key.\n"), |
bda208f8 DM |
884 | file, linenum); |
885 | ret = 1; | |
886 | goto cleanup; | |
887 | } | |
888 | /* For each directive, compare to the camelcase string. */ | |
a7755309 | 889 | if(section->name == NULL) { |
ca43fdd9 | 890 | pm_printf(ALPM_LOG_ERROR, _("config file %s, line %d: All directives must belong to a section.\n"), |
bda208f8 DM |
891 | file, linenum); |
892 | ret = 1; | |
893 | goto cleanup; | |
894 | } | |
895 | /* Include is allowed in both options and repo sections */ | |
896 | if(strcmp(key, "Include") == 0) { | |
73c74355 DM |
897 | glob_t globbuf; |
898 | int globret; | |
899 | size_t gindex; | |
900 | ||
bda208f8 | 901 | if(value == NULL) { |
ca43fdd9 | 902 | pm_printf(ALPM_LOG_ERROR, _("config file %s, line %d: directive '%s' needs a value\n"), |
bda208f8 DM |
903 | file, linenum, key); |
904 | ret = 1; | |
905 | goto cleanup; | |
906 | } | |
907 | /* Ignore include failures... assume non-critical */ | |
bda208f8 DM |
908 | globret = glob(value, GLOB_NOCHECK, NULL, &globbuf); |
909 | switch(globret) { | |
910 | case GLOB_NOSPACE: | |
ca43fdd9 | 911 | pm_printf(ALPM_LOG_DEBUG, |
bda208f8 DM |
912 | "config file %s, line %d: include globbing out of space\n", |
913 | file, linenum); | |
914 | break; | |
915 | case GLOB_ABORTED: | |
ca43fdd9 | 916 | pm_printf(ALPM_LOG_DEBUG, |
bda208f8 DM |
917 | "config file %s, line %d: include globbing read error for %s\n", |
918 | file, linenum, value); | |
919 | break; | |
920 | case GLOB_NOMATCH: | |
ca43fdd9 | 921 | pm_printf(ALPM_LOG_DEBUG, |
bda208f8 DM |
922 | "config file %s, line %d: no include found for %s\n", |
923 | file, linenum, value); | |
924 | break; | |
925 | default: | |
73c74355 | 926 | for(gindex = 0; gindex < globbuf.gl_pathc; gindex++) { |
ca43fdd9 | 927 | pm_printf(ALPM_LOG_DEBUG, "config file %s, line %d: including %s\n", |
bda208f8 | 928 | file, linenum, globbuf.gl_pathv[gindex]); |
a7755309 | 929 | _parseconfig(globbuf.gl_pathv[gindex], section, parse_options, depth + 1); |
bda208f8 DM |
930 | } |
931 | break; | |
932 | } | |
933 | globfree(&globbuf); | |
934 | continue; | |
935 | } | |
a7755309 | 936 | if(parse_options && section->is_options) { |
bda208f8 DM |
937 | /* we are either in options ... */ |
938 | if((ret = _parse_options(key, value, file, linenum)) != 0) { | |
939 | goto cleanup; | |
940 | } | |
3d4656c0 | 941 | } else if(!parse_options && !section->is_options) { |
bda208f8 DM |
942 | /* ... or in a repo section */ |
943 | if(strcmp(key, "Server") == 0) { | |
944 | if(value == NULL) { | |
ca43fdd9 | 945 | pm_printf(ALPM_LOG_ERROR, _("config file %s, line %d: directive '%s' needs a value\n"), |
bda208f8 DM |
946 | file, linenum, key); |
947 | ret = 1; | |
948 | goto cleanup; | |
949 | } | |
a7755309 | 950 | section->servers = alpm_list_add(section->servers, strdup(value)); |
b03b06cf DM |
951 | } else if(strcmp(key, "SigLevel") == 0) { |
952 | alpm_list_t *values = NULL; | |
953 | setrepeatingoption(value, "SigLevel", &values); | |
954 | if(values) { | |
955 | if(section->siglevel == ALPM_SIG_USE_DEFAULT) { | |
956 | section->siglevel = config->siglevel; | |
957 | } | |
e4690232 | 958 | if(process_siglevel(values, §ion->siglevel, file, linenum)) { |
b03b06cf DM |
959 | FREELIST(values); |
960 | ret = 1; | |
961 | goto cleanup; | |
962 | } | |
963 | FREELIST(values); | |
bda208f8 | 964 | } |
bda208f8 | 965 | } else { |
ca43fdd9 | 966 | pm_printf(ALPM_LOG_WARNING, |
bda208f8 | 967 | _("config file %s, line %d: directive '%s' in section '%s' not recognized.\n"), |
a7755309 | 968 | file, linenum, key, section->name); |
bda208f8 DM |
969 | } |
970 | } | |
bda208f8 DM |
971 | } |
972 | ||
a7755309 DM |
973 | if(depth == 0) { |
974 | ret = finish_section(section, parse_options); | |
975 | } | |
976 | ||
bda208f8 | 977 | cleanup: |
3d4656c0 | 978 | if(fp) { |
36474af4 FP |
979 | fclose(fp); |
980 | } | |
ca43fdd9 | 981 | pm_printf(ALPM_LOG_DEBUG, "config: finished parsing %s\n", file); |
bda208f8 DM |
982 | return ret; |
983 | } | |
984 | ||
985 | /** Parse a configuration file. | |
c730ca59 | 986 | * @param file path to the config file |
bda208f8 DM |
987 | * @return 0 on success, non-zero on error |
988 | */ | |
989 | int parseconfig(const char *file) | |
990 | { | |
73c74355 | 991 | int ret; |
a7755309 DM |
992 | struct section_t section; |
993 | memset(§ion, 0, sizeof(struct section_t)); | |
7af0ab1c | 994 | section.siglevel = ALPM_SIG_USE_DEFAULT; |
73c74355 DM |
995 | /* the config parse is a two-pass affair. We first parse the entire thing for |
996 | * the [options] section so we can get all default and path options set. | |
997 | * Next, we go back and parse everything but [options]. */ | |
998 | ||
bda208f8 | 999 | /* call the real parseconfig function with a null section & db argument */ |
ca43fdd9 | 1000 | pm_printf(ALPM_LOG_DEBUG, "parseconfig: options pass\n"); |
a7755309 | 1001 | if((ret = _parseconfig(file, §ion, 1, 0))) { |
73c74355 DM |
1002 | return ret; |
1003 | } | |
4fdcf50d | 1004 | if((ret = setup_libalpm())) { |
73c74355 DM |
1005 | return ret; |
1006 | } | |
1007 | /* second pass, repo section parsing */ | |
ca43fdd9 | 1008 | pm_printf(ALPM_LOG_DEBUG, "parseconfig: repo pass\n"); |
a7755309 | 1009 | return _parseconfig(file, §ion, 0, 0); |
bda208f8 DM |
1010 | } |
1011 | ||
d04baaba | 1012 | /* vim: set ts=2 sw=2 noet: */ |