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