]> wolfpit.net Git - tool/Arch-pacman/.git/blame - src/pacman/conf.c
conf.c: move section handling out of _parseconfig
[tool/Arch-pacman/.git] / src / pacman / conf.c
CommitLineData
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 */
41config_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
65void 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 82config_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 116int 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
145static 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
155static 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
167static 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 */
179static 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
257cleanup:
258 /* restore the old cwd if we have it */
95119d46
DM
259 if(cwdfd >= 0) {
260 if(fchdir(cwdfd) != 0) {
261 pm_printf(ALPM_LOG_ERROR, _("could not restore working directory (%s)\n"),
262 strerror(errno));
263 }
eb19d41d 264 close(cwdfd);
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
279int 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
301static 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 */
405static 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
424static 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 */
451static 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
464static 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;
62f82801
DM
517 const char *oldlocale;
518
769facca 519 /* set the locale to 'C' for consistent decimal parsing (0.7 and never
62f82801
DM
520 * 0,7) from config files, then restore old setting when we are done */
521 oldlocale = setlocale(LC_NUMERIC, NULL);
522 setlocale(LC_NUMERIC, "C");
b3612e9c 523 ratio = strtod(value, &endptr);
62f82801
DM
524 setlocale(LC_NUMERIC, oldlocale);
525
b3612e9c
DM
526 if(*endptr != '\0' || ratio < 0.0 || ratio > 2.0) {
527 pm_printf(ALPM_LOG_ERROR,
528 _("config file %s, line %d: invalid value for '%s' : '%s'\n"),
529 file, linenum, "UseDelta", value);
530 return 1;
531 }
532 config->deltaratio = ratio;
533 pm_printf(ALPM_LOG_DEBUG, "config: usedelta = %f\n", ratio);
bda208f8
DM
534 } else if(strcmp(key, "DBPath") == 0) {
535 /* don't overwrite a path specified on the command line */
536 if(!config->dbpath) {
537 config->dbpath = strdup(value);
ca43fdd9 538 pm_printf(ALPM_LOG_DEBUG, "config: dbpath: %s\n", value);
bda208f8 539 }
bda208f8
DM
540 } else if(strcmp(key, "RootDir") == 0) {
541 /* don't overwrite a path specified on the command line */
542 if(!config->rootdir) {
543 config->rootdir = strdup(value);
ca43fdd9 544 pm_printf(ALPM_LOG_DEBUG, "config: rootdir: %s\n", value);
bda208f8
DM
545 }
546 } else if(strcmp(key, "GPGDir") == 0) {
547 if(!config->gpgdir) {
548 config->gpgdir = strdup(value);
ca43fdd9 549 pm_printf(ALPM_LOG_DEBUG, "config: gpgdir: %s\n", value);
bda208f8
DM
550 }
551 } else if(strcmp(key, "LogFile") == 0) {
552 if(!config->logfile) {
553 config->logfile = strdup(value);
ca43fdd9 554 pm_printf(ALPM_LOG_DEBUG, "config: logfile: %s\n", value);
bda208f8
DM
555 }
556 } else if(strcmp(key, "XferCommand") == 0) {
557 config->xfercommand = strdup(value);
ca43fdd9 558 pm_printf(ALPM_LOG_DEBUG, "config: xfercommand: %s\n", value);
bda208f8 559 } else if(strcmp(key, "CleanMethod") == 0) {
4fdcf50d
DM
560 alpm_list_t *methods = NULL;
561 setrepeatingoption(value, "CleanMethod", &methods);
e4690232 562 if(process_cleanmethods(methods, file, linenum)) {
4fdcf50d
DM
563 FREELIST(methods);
564 return 1;
565 }
566 FREELIST(methods);
b03b06cf
DM
567 } else if(strcmp(key, "SigLevel") == 0) {
568 alpm_list_t *values = NULL;
569 setrepeatingoption(value, "SigLevel", &values);
e4690232 570 if(process_siglevel(values, &config->siglevel, file, linenum)) {
b03b06cf 571 FREELIST(values);
bda208f8
DM
572 return 1;
573 }
b03b06cf 574 FREELIST(values);
33b3b6d9
AM
575 } else if(strcmp(key, "LocalFileSigLevel") == 0) {
576 alpm_list_t *values = NULL;
577 setrepeatingoption(value, "LocalFileSigLevel", &values);
578 if(process_siglevel(values, &config->localfilesiglevel, file, linenum)) {
579 FREELIST(values);
580 return 1;
581 }
582 FREELIST(values);
583 } else if(strcmp(key, "RemoteFileSigLevel") == 0) {
584 alpm_list_t *values = NULL;
585 setrepeatingoption(value, "RemoteFileSigLevel", &values);
586 if(process_siglevel(values, &config->remotefilesiglevel, file, linenum)) {
587 FREELIST(values);
588 return 1;
589 }
590 FREELIST(values);
bda208f8 591 } else {
ca43fdd9 592 pm_printf(ALPM_LOG_WARNING,
bda208f8
DM
593 _("config file %s, line %d: directive '%s' in section '%s' not recognized.\n"),
594 file, linenum, key, "options");
595 }
596
597 }
598 return 0;
599}
600
939d5a95 601static int _add_mirror(alpm_db_t *db, char *value)
bda208f8
DM
602{
603 const char *dbname = alpm_db_get_name(db);
604 /* let's attempt a replacement for the current repo */
605 char *temp = strreplace(value, "$repo", dbname);
606 /* let's attempt a replacement for the arch */
4fdcf50d 607 const char *arch = config->arch;
bda208f8
DM
608 char *server;
609 if(arch) {
610 server = strreplace(temp, "$arch", arch);
611 free(temp);
612 } else {
613 if(strstr(temp, "$arch")) {
614 free(temp);
ca58e326 615 pm_printf(ALPM_LOG_ERROR,
e4690232 616 _("mirror '%s' contains the '%s' variable, but no '%s' is defined.\n"),
ca58e326 617 value, "$arch", "Architecture");
bda208f8
DM
618 return 1;
619 }
620 server = temp;
621 }
622
42ab639b 623 if(alpm_db_add_server(db, server) != 0) {
bda208f8 624 /* pm_errno is set by alpm_db_setserver */
ca43fdd9 625 pm_printf(ALPM_LOG_ERROR, _("could not add server URL to database '%s': %s (%s)\n"),
e2aa9526 626 dbname, server, alpm_strerror(alpm_errno(config->handle)));
bda208f8
DM
627 free(server);
628 return 1;
629 }
630
631 free(server);
632 return 0;
633}
634
4fdcf50d 635/** Sets up libalpm global stuff in one go. Called after the command line
769facca 636 * and initial config file parsing. Once this is complete, we can see if any
bda208f8
DM
637 * paths were defined. If a rootdir was defined and nothing else, we want all
638 * of our paths to live under the rootdir that was specified. Safe to call
639 * multiple times (will only do anything the first time).
640 */
4fdcf50d 641static int setup_libalpm(void)
bda208f8 642{
73c74355 643 int ret = 0;
a3f93992 644 alpm_errno_t err;
64c1cf79 645 alpm_handle_t *handle;
4fdcf50d 646
ca43fdd9 647 pm_printf(ALPM_LOG_DEBUG, "setup_libalpm called\n");
73c74355 648
73c74355
DM
649 /* Configure root path first. If it is set and dbpath/logfile were not
650 * set, then set those as well to reside under the root. */
651 if(config->rootdir) {
652 char path[PATH_MAX];
73c74355 653 if(!config->dbpath) {
4fdcf50d 654 snprintf(path, PATH_MAX, "%s/%s", config->rootdir, DBPATH + 1);
73c74355 655 config->dbpath = strdup(path);
bda208f8 656 }
73c74355 657 if(!config->logfile) {
4fdcf50d 658 snprintf(path, PATH_MAX, "%s/%s", config->rootdir, LOGFILE + 1);
73c74355 659 config->logfile = strdup(path);
bda208f8 660 }
4fdcf50d
DM
661 } else {
662 config->rootdir = strdup(ROOTDIR);
663 if(!config->dbpath) {
664 config->dbpath = strdup(DBPATH);
bda208f8 665 }
73c74355 666 }
4fdcf50d
DM
667
668 /* initialize library */
17a6ac56
DM
669 handle = alpm_initialize(config->rootdir, config->dbpath, &err);
670 if(!handle) {
48861f50
AM
671 pm_printf(ALPM_LOG_ERROR, _("failed to initialize alpm library\n(%s: %s)\n"),
672 alpm_strerror(err), config->dbpath);
afc96f2a 673 if(err == ALPM_ERR_DB_VERSION) {
ca43fdd9 674 pm_printf(ALPM_LOG_ERROR, _(" try running pacman-db-upgrade\n"));
1150d9e1 675 }
4fdcf50d
DM
676 return -1;
677 }
17a6ac56 678 config->handle = handle;
4fdcf50d 679
17a6ac56
DM
680 alpm_option_set_logcb(handle, cb_log);
681 alpm_option_set_dlcb(handle, cb_dl_progress);
98fdfa19
DM
682 alpm_option_set_eventcb(handle, cb_event);
683 alpm_option_set_questioncb(handle, cb_question);
684 alpm_option_set_progresscb(handle, cb_progress);
4fdcf50d
DM
685
686 config->logfile = config->logfile ? config->logfile : strdup(LOGFILE);
17a6ac56 687 ret = alpm_option_set_logfile(handle, config->logfile);
4fdcf50d 688 if(ret != 0) {
ca43fdd9 689 pm_printf(ALPM_LOG_ERROR, _("problem setting logfile '%s' (%s)\n"),
db3b86e7 690 config->logfile, alpm_strerror(alpm_errno(handle)));
4fdcf50d 691 return ret;
73c74355 692 }
bda208f8 693
73c74355
DM
694 /* Set GnuPG's home directory. This is not relative to rootdir, even if
695 * rootdir is defined. Reasoning: gpgdir contains configuration data. */
4fdcf50d 696 config->gpgdir = config->gpgdir ? config->gpgdir : strdup(GPGDIR);
1cd6515a 697 ret = alpm_option_set_gpgdir(handle, config->gpgdir);
4fdcf50d 698 if(ret != 0) {
ca43fdd9 699 pm_printf(ALPM_LOG_ERROR, _("problem setting gpgdir '%s' (%s)\n"),
db3b86e7 700 config->gpgdir, alpm_strerror(alpm_errno(handle)));
4fdcf50d 701 return ret;
bda208f8 702 }
73c74355
DM
703
704 /* add a default cachedir if one wasn't specified */
4fdcf50d 705 if(config->cachedirs == NULL) {
17a6ac56 706 alpm_option_add_cachedir(handle, CACHEDIR);
4fdcf50d 707 } else {
17a6ac56 708 alpm_option_set_cachedirs(handle, config->cachedirs);
4fdcf50d
DM
709 }
710
33f25185 711 alpm_option_set_default_siglevel(handle, config->siglevel);
4fdcf50d 712
33b3b6d9
AM
713 merge_siglevel(&config->siglevel, &config->localfilesiglevel);
714 merge_siglevel(&config->siglevel, &config->remotefilesiglevel);
715 alpm_option_set_local_file_siglevel(handle, config->localfilesiglevel);
716 alpm_option_set_remote_file_siglevel(handle, config->remotefilesiglevel);
717
4fdcf50d 718 if(config->xfercommand) {
17a6ac56 719 alpm_option_set_fetchcb(handle, download_with_xfercommand);
361ed6a6
DM
720 } else if(!(alpm_capabilities() & ALPM_CAPABILITY_DOWNLOADER)) {
721 pm_printf(ALPM_LOG_WARNING, _("no '%s' configured"), "XferCommand");
73c74355 722 }
4fdcf50d
DM
723
724 if(config->totaldownload) {
17a6ac56 725 alpm_option_set_totaldlcb(handle, cb_dl_total);
4fdcf50d
DM
726 }
727
17a6ac56
DM
728 alpm_option_set_arch(handle, config->arch);
729 alpm_option_set_checkspace(handle, config->checkspace);
730 alpm_option_set_usesyslog(handle, config->usesyslog);
b3612e9c 731 alpm_option_set_deltaratio(handle, config->deltaratio);
4fdcf50d 732
17a6ac56 733 alpm_option_set_ignorepkgs(handle, config->ignorepkg);
f1bb56ce 734 alpm_option_set_ignoregroups(handle, config->ignoregrp);
17a6ac56
DM
735 alpm_option_set_noupgrades(handle, config->noupgrade);
736 alpm_option_set_noextracts(handle, config->noextract);
4fdcf50d 737
73c74355 738 return 0;
bda208f8
DM
739}
740
a7755309
DM
741/**
742 * Allows parsing in advance of an entire config section before we start
743 * calling library methods.
744 */
745struct section_t {
746 /* useful for all sections */
26da037f 747 const char *name;
a7755309 748 int is_options;
74c89436 749 int parse_options;
a7755309 750 /* db section option gathering */
7af0ab1c 751 alpm_siglevel_t siglevel;
a7755309
DM
752 alpm_list_t *servers;
753};
754
36f702ba
AG
755static int _parse_repo(const char *key, char *value, const char *file,
756 int line, struct section_t *section)
757{
758 int ret = 0;
759
760 if(strcmp(key, "Server") == 0) {
761 if(!value) {
762 pm_printf(ALPM_LOG_ERROR, _("config file %s, line %d: directive '%s' needs a value\n"),
763 file, line, key);
764 ret = 1;
765 } else {
766 section->servers = alpm_list_add(section->servers, strdup(value));
767 }
768 } else if(strcmp(key, "SigLevel") == 0) {
769 if(!value) {
770 pm_printf(ALPM_LOG_ERROR, _("config file %s, line %d: directive '%s' needs a value\n"),
771 file, line, key);
772 } else {
773 alpm_list_t *values = NULL;
774 setrepeatingoption(value, "SigLevel", &values);
775 if(values) {
776 if(section->siglevel == ALPM_SIG_USE_DEFAULT) {
777 section->siglevel = config->siglevel;
778 }
779 ret = process_siglevel(values, &section->siglevel, file, line);
780 FREELIST(values);
781 }
782 }
783 } else {
784 pm_printf(ALPM_LOG_WARNING,
785 _("config file %s, line %d: directive '%s' in section '%s' not recognized.\n"),
786 file, line, key, section->name);
787 }
788
789 return ret;
790}
791
a7755309
DM
792/**
793 * Wrap up a section once we have reached the end of it. This should be called
794 * when a subsequent section is encountered, or when we have reached the end of
795 * the root config file. Once called, all existing saved config pieces on the
796 * section struct are freed.
797 * @param section the current parsed and saved section data
798 * @param parse_options whether we are parsing options or repo data
799 * @return 0 on success, 1 on failure
800 */
74c89436 801static int finish_section(struct section_t *section)
a7755309
DM
802{
803 int ret = 0;
804 alpm_list_t *i;
939d5a95 805 alpm_db_t *db;
a7755309 806
ca43fdd9 807 pm_printf(ALPM_LOG_DEBUG, "config: finish section '%s'\n", section->name);
a7755309
DM
808
809 /* parsing options (or nothing)- nothing to do except free the pieces */
74c89436 810 if(!section->name || section->parse_options || section->is_options) {
a7755309
DM
811 goto cleanup;
812 }
813
814 /* if we are not looking at options sections only, register a db */
b488f229 815 db = alpm_register_syncdb(config->handle, section->name, section->siglevel);
a7755309 816 if(db == NULL) {
ca43fdd9 817 pm_printf(ALPM_LOG_ERROR, _("could not register '%s' database (%s)\n"),
a7755309
DM
818 section->name, alpm_strerror(alpm_errno(config->handle)));
819 ret = 1;
820 goto cleanup;
821 }
822
a7755309 823 for(i = section->servers; i; i = alpm_list_next(i)) {
6be492d2 824 char *value = i->data;
a7755309 825 if(_add_mirror(db, value) != 0) {
ca43fdd9 826 pm_printf(ALPM_LOG_ERROR,
a7755309
DM
827 _("could not add mirror '%s' to database '%s' (%s)\n"),
828 value, section->name, alpm_strerror(alpm_errno(config->handle)));
829 ret = 1;
830 goto cleanup;
831 }
832 free(value);
833 }
834
835cleanup:
836 alpm_list_free(section->servers);
837 section->servers = NULL;
7af0ab1c 838 section->siglevel = ALPM_SIG_USE_DEFAULT;
a7755309
DM
839 section->name = NULL;
840 return ret;
841}
bda208f8 842
26da037f 843static int _parse_directive(const char *file, int linenum, const char *name,
8287312c
AG
844 char *key, char *value, struct section_t *section)
845{
26da037f
AG
846 if(!key && !value) {
847 int ret = finish_section(section);
848 pm_printf(ALPM_LOG_DEBUG, "config: new section '%s'\n", name);
849 section->name = name;
850 if(name && strcmp(name, "options") == 0) {
851 section->is_options = 1;
852 } else {
853 section->is_options = 0;
854 }
855 return ret;
856 }
857
8287312c
AG
858 if(section->name == NULL) {
859 pm_printf(ALPM_LOG_ERROR, _("config file %s, line %d: All directives must belong to a section.\n"),
860 file, linenum);
861 return 1;
862 }
863
864 if(section->parse_options && section->is_options) {
865 /* we are either in options ... */
866 return _parse_options(key, value, file, linenum);
867 } else if(!section->parse_options && !section->is_options) {
868 /* ... or in a repo section */
869 return _parse_repo(key, value, file, linenum, section);
870 }
26da037f 871
8287312c
AG
872 return 0;
873}
874
c730ca59
DM
875/** The "real" parseconfig. Each "Include" directive will recall this method so
876 * recursion and stack depth are limited to 10 levels. The publicly visible
877 * parseconfig calls this with a NULL section argument so we can recall from
878 * within ourself on an include.
879 * @param file path to the config file
a7755309 880 * @param section the current active section
c730ca59 881 * @param depth the current recursion depth
a7755309
DM
882 * @return 0 on success, 1 on failure
883 */
26da037f
AG
884static int _parseconfig(const char *file, struct section_t *section,
885 char **section_name, int depth)
bda208f8
DM
886{
887 FILE *fp = NULL;
e826c143 888 char line[PATH_MAX];
bda208f8 889 int linenum = 0;
bda208f8 890 int ret = 0;
c730ca59
DM
891 const int max_depth = 10;
892
893 if(depth >= max_depth) {
ca43fdd9 894 pm_printf(ALPM_LOG_ERROR,
c730ca59
DM
895 _("config parsing exceeded max recursion depth of %d.\n"), max_depth);
896 ret = 1;
897 goto cleanup;
898 }
bda208f8 899
ca43fdd9 900 pm_printf(ALPM_LOG_DEBUG, "config: attempting to read file %s\n", file);
bda208f8
DM
901 fp = fopen(file, "r");
902 if(fp == NULL) {
392fffe2
DR
903 pm_printf(ALPM_LOG_ERROR, _("config file %s could not be read: %s\n"),
904 file, strerror(errno));
c730ca59
DM
905 ret = 1;
906 goto cleanup;
bda208f8
DM
907 }
908
bda208f8 909 while(fgets(line, PATH_MAX, fp)) {
c730ca59
DM
910 char *key, *value, *ptr;
911 size_t line_len;
73c74355 912
bda208f8 913 linenum++;
bda208f8
DM
914
915 /* ignore whole line and end of line comments */
bda208f8
DM
916 if((ptr = strchr(line, '#'))) {
917 *ptr = '\0';
918 }
919
c34b69b5 920 line_len = strtrim(line);
d95c0494
AM
921
922 if(line_len == 0) {
923 continue;
924 }
925
c730ca59 926 if(line[0] == '[' && line[line_len - 1] == ']') {
dd95c96b 927 char *name;
c730ca59
DM
928 /* only possibility here is a line == '[]' */
929 if(line_len <= 2) {
ca43fdd9 930 pm_printf(ALPM_LOG_ERROR, _("config file %s, line %d: bad section name.\n"),
bda208f8
DM
931 file, linenum);
932 ret = 1;
933 goto cleanup;
934 }
c730ca59
DM
935 /* new config section, skip the '[' */
936 name = strdup(line + 1);
937 name[line_len - 2] = '\0';
26da037f
AG
938
939 ret = _parse_directive(file, linenum, name, NULL, NULL, section);
940 free(*section_name);
941 *section_name = name;
942
943 if(ret) {
a7755309 944 goto cleanup;
dd95c96b 945 }
bda208f8
DM
946 continue;
947 }
948
949 /* directive */
bda208f8
DM
950 /* strsep modifies the 'line' string: 'key \0 value' */
951 key = line;
952 value = line;
953 strsep(&value, "=");
954 strtrim(key);
955 strtrim(value);
956
957 if(key == NULL) {
ca43fdd9 958 pm_printf(ALPM_LOG_ERROR, _("config file %s, line %d: syntax error in config file- missing key.\n"),
bda208f8
DM
959 file, linenum);
960 ret = 1;
961 goto cleanup;
962 }
bda208f8
DM
963 /* Include is allowed in both options and repo sections */
964 if(strcmp(key, "Include") == 0) {
73c74355
DM
965 glob_t globbuf;
966 int globret;
967 size_t gindex;
968
bda208f8 969 if(value == NULL) {
ca43fdd9 970 pm_printf(ALPM_LOG_ERROR, _("config file %s, line %d: directive '%s' needs a value\n"),
bda208f8
DM
971 file, linenum, key);
972 ret = 1;
973 goto cleanup;
974 }
975 /* Ignore include failures... assume non-critical */
bda208f8
DM
976 globret = glob(value, GLOB_NOCHECK, NULL, &globbuf);
977 switch(globret) {
978 case GLOB_NOSPACE:
ca43fdd9 979 pm_printf(ALPM_LOG_DEBUG,
bda208f8
DM
980 "config file %s, line %d: include globbing out of space\n",
981 file, linenum);
982 break;
983 case GLOB_ABORTED:
ca43fdd9 984 pm_printf(ALPM_LOG_DEBUG,
bda208f8
DM
985 "config file %s, line %d: include globbing read error for %s\n",
986 file, linenum, value);
987 break;
988 case GLOB_NOMATCH:
ca43fdd9 989 pm_printf(ALPM_LOG_DEBUG,
bda208f8
DM
990 "config file %s, line %d: no include found for %s\n",
991 file, linenum, value);
992 break;
993 default:
73c74355 994 for(gindex = 0; gindex < globbuf.gl_pathc; gindex++) {
ca43fdd9 995 pm_printf(ALPM_LOG_DEBUG, "config file %s, line %d: including %s\n",
bda208f8 996 file, linenum, globbuf.gl_pathv[gindex]);
26da037f
AG
997 _parseconfig(globbuf.gl_pathv[gindex], section,
998 section_name, depth + 1);
bda208f8
DM
999 }
1000 break;
1001 }
1002 globfree(&globbuf);
1003 continue;
1004 }
26da037f 1005 if((ret = _parse_directive(file, linenum, *section_name, key, value, section)) != 0) {
8287312c 1006 goto cleanup;
bda208f8 1007 }
bda208f8
DM
1008 }
1009
a7755309 1010 if(depth == 0) {
26da037f 1011 ret = _parse_directive(NULL, 0, NULL, NULL, NULL, section);
a7755309
DM
1012 }
1013
bda208f8 1014cleanup:
3d4656c0 1015 if(fp) {
36474af4
FP
1016 fclose(fp);
1017 }
26da037f
AG
1018 if(depth == 0) {
1019 free(*section_name);
1020 *section_name = NULL;
1021 }
ca43fdd9 1022 pm_printf(ALPM_LOG_DEBUG, "config: finished parsing %s\n", file);
bda208f8
DM
1023 return ret;
1024}
1025
1026/** Parse a configuration file.
c730ca59 1027 * @param file path to the config file
bda208f8
DM
1028 * @return 0 on success, non-zero on error
1029 */
1030int parseconfig(const char *file)
1031{
73c74355 1032 int ret;
a7755309 1033 struct section_t section;
26da037f 1034 char *section_name = NULL;
a7755309 1035 memset(&section, 0, sizeof(struct section_t));
7af0ab1c 1036 section.siglevel = ALPM_SIG_USE_DEFAULT;
73c74355
DM
1037 /* the config parse is a two-pass affair. We first parse the entire thing for
1038 * the [options] section so we can get all default and path options set.
1039 * Next, we go back and parse everything but [options]. */
1040
bda208f8 1041 /* call the real parseconfig function with a null section & db argument */
ca43fdd9 1042 pm_printf(ALPM_LOG_DEBUG, "parseconfig: options pass\n");
74c89436 1043 section.parse_options = 1;
26da037f 1044 if((ret = _parseconfig(file, &section, &section_name, 0))) {
73c74355
DM
1045 return ret;
1046 }
4fdcf50d 1047 if((ret = setup_libalpm())) {
73c74355
DM
1048 return ret;
1049 }
1050 /* second pass, repo section parsing */
ca43fdd9 1051 pm_printf(ALPM_LOG_DEBUG, "parseconfig: repo pass\n");
74c89436 1052 section.parse_options = 0;
26da037f 1053 return _parseconfig(file, &section, &section_name, 0);
bda208f8
DM
1054}
1055
d04baaba 1056/* vim: set ts=2 sw=2 noet: */