]> wolfpit.net Git - tool/Arch-pacman/.git/blob - src/pacman/conf.c
Report missing directory if we can not initialise alpm library
[tool/Arch-pacman/.git] / src / pacman / conf.c
1 /*
2 * conf.c
3 *
4 * Copyright (c) 2006-2013 Pacman Development Team <pacman-dev@archlinux.org>
5 * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
6 *
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
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <errno.h>
22 #include <glob.h>
23 #include <limits.h>
24 #include <locale.h> /* setlocale */
25 #include <fcntl.h> /* open */
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h> /* strdup */
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <sys/utsname.h> /* uname */
32 #include <unistd.h>
33
34 /* pacman */
35 #include "conf.h"
36 #include "util.h"
37 #include "pacman.h"
38 #include "callback.h"
39
40 /* global config variable */
41 config_t *config = NULL;
42
43 #define NOCOLOR "\033[0m"
44
45 #define BOLD "\033[0;1m"
46
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) {
70 colstr->colon = BOLDBLUE "::" BOLD " ";
71 colstr->title = BOLD;
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
82 config_t *config_new(void)
83 {
84 config_t *newconfig = calloc(1, sizeof(config_t));
85 if(!newconfig) {
86 pm_printf(ALPM_LOG_ERROR,
87 _("malloc failure: could not allocate %zd bytes\n"),
88 sizeof(config_t));
89 return NULL;
90 }
91 /* defaults which may get overridden later */
92 newconfig->op = PM_OP_MAIN;
93 newconfig->logmask = ALPM_LOG_ERROR | ALPM_LOG_WARNING;
94 newconfig->configfile = strdup(CONFFILE);
95 newconfig->deltaratio = 0.0;
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;
99 newconfig->localfilesiglevel = ALPM_SIG_USE_DEFAULT;
100 newconfig->remotefilesiglevel = ALPM_SIG_USE_DEFAULT;
101 }
102
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
113 return newconfig;
114 }
115
116 int config_free(config_t *oldconfig)
117 {
118 if(oldconfig == NULL) {
119 return -1;
120 }
121
122 alpm_list_free(oldconfig->explicit_adds);
123 alpm_list_free(oldconfig->explicit_removes);
124
125 FREELIST(oldconfig->holdpkg);
126 FREELIST(oldconfig->ignorepkg);
127 FREELIST(oldconfig->ignoregrp);
128 FREELIST(oldconfig->noupgrade);
129 FREELIST(oldconfig->noextract);
130 free(oldconfig->configfile);
131 free(oldconfig->rootdir);
132 free(oldconfig->dbpath);
133 free(oldconfig->logfile);
134 free(oldconfig->gpgdir);
135 FREELIST(oldconfig->cachedirs);
136 free(oldconfig->xfercommand);
137 free(oldconfig->print_format);
138 free(oldconfig->arch);
139 free(oldconfig);
140
141 return 0;
142 }
143
144 /** Helper function for download_with_xfercommand() */
145 static char *get_filename(const char *url)
146 {
147 char *filename = strrchr(url, '/');
148 if(filename != NULL) {
149 filename++;
150 }
151 return filename;
152 }
153
154 /** Helper function for download_with_xfercommand() */
155 static char *get_destfile(const char *path, const char *filename)
156 {
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() */
167 static char *get_tempfile(const char *path, const char *filename)
168 {
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,
180 int force)
181 {
182 int ret = 0, retval;
183 int usepart = 0;
184 int cwdfd;
185 struct stat st;
186 char *parsedcmd, *tempcmd;
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 */
220 do {
221 cwdfd = open(".", O_RDONLY);
222 } while(cwdfd == -1 && errno == EINTR);
223 if(cwdfd < 0) {
224 pm_printf(ALPM_LOG_ERROR, _("could not get current working directory\n"));
225 }
226
227 /* cwd to the download directory */
228 if(chdir(localpath)) {
229 pm_printf(ALPM_LOG_WARNING, _("could not chdir to download directory %s\n"), localpath);
230 ret = -1;
231 goto cleanup;
232 }
233 /* execute the parsed command via /bin/sh -c */
234 pm_printf(ALPM_LOG_DEBUG, "running command: %s\n", parsedcmd);
235 retval = system(parsedcmd);
236
237 if(retval == -1) {
238 pm_printf(ALPM_LOG_WARNING, _("running XferCommand: fork failed!\n"));
239 ret = -1;
240 } else if(retval != 0) {
241 /* download failed */
242 pm_printf(ALPM_LOG_DEBUG, "XferCommand command returned non-zero status "
243 "code (%d)\n", retval);
244 ret = -1;
245 } else {
246 /* download was successful */
247 ret = 0;
248 if(usepart) {
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 }
254 }
255 }
256
257 cleanup:
258 /* restore the old cwd if we have it */
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 }
264 close(cwdfd);
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);
284 config->arch = strdup(un.machine);
285 } else {
286 config->arch = strdup(arch);
287 }
288 pm_printf(ALPM_LOG_DEBUG, "config: arch: %s\n", config->arch);
289 return 0;
290 }
291
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
297 * @param file path to the config file
298 * @param linenum current line number in file
299 * @return 0 on success, 1 on any parsing error
300 */
301 static int process_siglevel(alpm_list_t *values, alpm_siglevel_t *storage,
302 const char *file, int linenum)
303 {
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
313 if(strncmp(original, "Package", strlen("Package")) == 0) {
314 /* only packages are affected, don't flip flags for databases */
315 value = original + strlen("Package");
316 package = 1;
317 } else if(strncmp(original, "Database", strlen("Database")) == 0) {
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;
331 level |= ALPM_SIG_PACKAGE_SET;
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;
340 level |= ALPM_SIG_PACKAGE_SET;
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;
350 level |= ALPM_SIG_PACKAGE_SET;
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;
360 level |= ALPM_SIG_PACKAGE_TRUST_SET;
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;
370 level |= ALPM_SIG_PACKAGE_TRUST_SET;
371 }
372 if(database) {
373 level |= ALPM_SIG_DATABASE_MARGINAL_OK;
374 level |= ALPM_SIG_DATABASE_UNKNOWN_OK;
375 }
376 } else {
377 pm_printf(ALPM_LOG_ERROR,
378 _("config file %s, line %d: invalid value for '%s' : '%s'\n"),
379 file, linenum, "SigLevel", original);
380 ret = 1;
381 }
382 level &= ~ALPM_SIG_USE_DEFAULT;
383 }
384
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)) {
388 pm_printf(ALPM_LOG_ERROR,
389 _("config file %s, line %d: '%s' option invalid, no signature support\n"),
390 file, linenum, "SigLevel");
391 ret = 1;
392 }
393
394 if(!ret) {
395 *storage = level;
396 }
397 return ret;
398 }
399
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
424 static int process_cleanmethods(alpm_list_t *values,
425 const char *file, int linenum)
426 {
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 {
435 pm_printf(ALPM_LOG_ERROR,
436 _("config file %s, line %d: invalid value for '%s' : '%s'\n"),
437 file, linenum, "CleanMethod", value);
438 return 1;
439 }
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
449 * @param list the list to add the option to
450 */
451 static void setrepeatingoption(char *ptr, const char *option,
452 alpm_list_t **list)
453 {
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);
461 }
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) {
470 config->usesyslog = 1;
471 pm_printf(ALPM_LOG_DEBUG, "config: usesyslog\n");
472 } else if(strcmp(key, "ILoveCandy") == 0) {
473 config->chomp = 1;
474 pm_printf(ALPM_LOG_DEBUG, "config: chomp\n");
475 } else if(strcmp(key, "VerbosePkgLists") == 0) {
476 config->verbosepkglists = 1;
477 pm_printf(ALPM_LOG_DEBUG, "config: verbosepkglists\n");
478 } else if(strcmp(key, "UseDelta") == 0) {
479 config->deltaratio = 0.7;
480 pm_printf(ALPM_LOG_DEBUG, "config: usedelta (default 0.7)\n");
481 } else if(strcmp(key, "TotalDownload") == 0) {
482 config->totaldownload = 1;
483 pm_printf(ALPM_LOG_DEBUG, "config: totaldownload\n");
484 } else if(strcmp(key, "CheckSpace") == 0) {
485 config->checkspace = 1;
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;
489 enable_colors(config->color);
490 }
491 } else {
492 pm_printf(ALPM_LOG_WARNING,
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) {
499 setrepeatingoption(value, "NoUpgrade", &(config->noupgrade));
500 } else if(strcmp(key, "NoExtract") == 0) {
501 setrepeatingoption(value, "NoExtract", &(config->noextract));
502 } else if(strcmp(key, "IgnorePkg") == 0) {
503 setrepeatingoption(value, "IgnorePkg", &(config->ignorepkg));
504 } else if(strcmp(key, "IgnoreGroup") == 0) {
505 setrepeatingoption(value, "IgnoreGroup", &(config->ignoregrp));
506 } else if(strcmp(key, "HoldPkg") == 0) {
507 setrepeatingoption(value, "HoldPkg", &(config->holdpkg));
508 } else if(strcmp(key, "CacheDir") == 0) {
509 setrepeatingoption(value, "CacheDir", &(config->cachedirs));
510 } else if(strcmp(key, "Architecture") == 0) {
511 if(!config->arch) {
512 config_set_arch(value);
513 }
514 } else if(strcmp(key, "UseDelta") == 0) {
515 double ratio;
516 char *endptr;
517 const char *oldlocale;
518
519 /* set the locale to 'C' for consistent decimal parsing (0.7 and never
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");
523 ratio = strtod(value, &endptr);
524 setlocale(LC_NUMERIC, oldlocale);
525
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);
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);
538 pm_printf(ALPM_LOG_DEBUG, "config: dbpath: %s\n", value);
539 }
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);
544 pm_printf(ALPM_LOG_DEBUG, "config: rootdir: %s\n", value);
545 }
546 } else if(strcmp(key, "GPGDir") == 0) {
547 if(!config->gpgdir) {
548 config->gpgdir = strdup(value);
549 pm_printf(ALPM_LOG_DEBUG, "config: gpgdir: %s\n", value);
550 }
551 } else if(strcmp(key, "LogFile") == 0) {
552 if(!config->logfile) {
553 config->logfile = strdup(value);
554 pm_printf(ALPM_LOG_DEBUG, "config: logfile: %s\n", value);
555 }
556 } else if(strcmp(key, "XferCommand") == 0) {
557 config->xfercommand = strdup(value);
558 pm_printf(ALPM_LOG_DEBUG, "config: xfercommand: %s\n", value);
559 } else if(strcmp(key, "CleanMethod") == 0) {
560 alpm_list_t *methods = NULL;
561 setrepeatingoption(value, "CleanMethod", &methods);
562 if(process_cleanmethods(methods, file, linenum)) {
563 FREELIST(methods);
564 return 1;
565 }
566 FREELIST(methods);
567 } else if(strcmp(key, "SigLevel") == 0) {
568 alpm_list_t *values = NULL;
569 setrepeatingoption(value, "SigLevel", &values);
570 if(process_siglevel(values, &config->siglevel, file, linenum)) {
571 FREELIST(values);
572 return 1;
573 }
574 FREELIST(values);
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);
591 } else {
592 pm_printf(ALPM_LOG_WARNING,
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
601 static int _add_mirror(alpm_db_t *db, char *value)
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 */
607 const char *arch = config->arch;
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);
615 pm_printf(ALPM_LOG_ERROR,
616 _("mirror '%s' contains the '%s' variable, but no '%s' is defined.\n"),
617 value, "$arch", "Architecture");
618 return 1;
619 }
620 server = temp;
621 }
622
623 if(alpm_db_add_server(db, server) != 0) {
624 /* pm_errno is set by alpm_db_setserver */
625 pm_printf(ALPM_LOG_ERROR, _("could not add server URL to database '%s': %s (%s)\n"),
626 dbname, server, alpm_strerror(alpm_errno(config->handle)));
627 free(server);
628 return 1;
629 }
630
631 free(server);
632 return 0;
633 }
634
635 /** Sets up libalpm global stuff in one go. Called after the command line
636 * and initial config file parsing. Once this is complete, we can see if any
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 */
641 static int setup_libalpm(void)
642 {
643 int ret = 0;
644 alpm_errno_t err;
645 alpm_handle_t *handle;
646
647 pm_printf(ALPM_LOG_DEBUG, "setup_libalpm called\n");
648
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];
653 if(!config->dbpath) {
654 snprintf(path, PATH_MAX, "%s/%s", config->rootdir, DBPATH + 1);
655 config->dbpath = strdup(path);
656 }
657 if(!config->logfile) {
658 snprintf(path, PATH_MAX, "%s/%s", config->rootdir, LOGFILE + 1);
659 config->logfile = strdup(path);
660 }
661 } else {
662 config->rootdir = strdup(ROOTDIR);
663 if(!config->dbpath) {
664 config->dbpath = strdup(DBPATH);
665 }
666 }
667
668 /* initialize library */
669 handle = alpm_initialize(config->rootdir, config->dbpath, &err);
670 if(!handle) {
671 pm_printf(ALPM_LOG_ERROR, _("failed to initialize alpm library\n(%s: %s)\n"),
672 alpm_strerror(err), config->dbpath);
673 if(err == ALPM_ERR_DB_VERSION) {
674 pm_printf(ALPM_LOG_ERROR, _(" try running pacman-db-upgrade\n"));
675 }
676 return -1;
677 }
678 config->handle = handle;
679
680 alpm_option_set_logcb(handle, cb_log);
681 alpm_option_set_dlcb(handle, cb_dl_progress);
682 alpm_option_set_eventcb(handle, cb_event);
683 alpm_option_set_questioncb(handle, cb_question);
684 alpm_option_set_progresscb(handle, cb_progress);
685
686 config->logfile = config->logfile ? config->logfile : strdup(LOGFILE);
687 ret = alpm_option_set_logfile(handle, config->logfile);
688 if(ret != 0) {
689 pm_printf(ALPM_LOG_ERROR, _("problem setting logfile '%s' (%s)\n"),
690 config->logfile, alpm_strerror(alpm_errno(handle)));
691 return ret;
692 }
693
694 /* Set GnuPG's home directory. This is not relative to rootdir, even if
695 * rootdir is defined. Reasoning: gpgdir contains configuration data. */
696 config->gpgdir = config->gpgdir ? config->gpgdir : strdup(GPGDIR);
697 ret = alpm_option_set_gpgdir(handle, config->gpgdir);
698 if(ret != 0) {
699 pm_printf(ALPM_LOG_ERROR, _("problem setting gpgdir '%s' (%s)\n"),
700 config->gpgdir, alpm_strerror(alpm_errno(handle)));
701 return ret;
702 }
703
704 /* add a default cachedir if one wasn't specified */
705 if(config->cachedirs == NULL) {
706 alpm_option_add_cachedir(handle, CACHEDIR);
707 } else {
708 alpm_option_set_cachedirs(handle, config->cachedirs);
709 }
710
711 alpm_option_set_default_siglevel(handle, config->siglevel);
712
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
718 if(config->xfercommand) {
719 alpm_option_set_fetchcb(handle, download_with_xfercommand);
720 } else if(!(alpm_capabilities() & ALPM_CAPABILITY_DOWNLOADER)) {
721 pm_printf(ALPM_LOG_WARNING, _("no '%s' configured"), "XferCommand");
722 }
723
724 if(config->totaldownload) {
725 alpm_option_set_totaldlcb(handle, cb_dl_total);
726 }
727
728 alpm_option_set_arch(handle, config->arch);
729 alpm_option_set_checkspace(handle, config->checkspace);
730 alpm_option_set_usesyslog(handle, config->usesyslog);
731 alpm_option_set_deltaratio(handle, config->deltaratio);
732
733 alpm_option_set_ignorepkgs(handle, config->ignorepkg);
734 alpm_option_set_ignoregroups(handle, config->ignoregrp);
735 alpm_option_set_noupgrades(handle, config->noupgrade);
736 alpm_option_set_noextracts(handle, config->noextract);
737
738 return 0;
739 }
740
741 /**
742 * Allows parsing in advance of an entire config section before we start
743 * calling library methods.
744 */
745 struct section_t {
746 /* useful for all sections */
747 char *name;
748 int is_options;
749 /* db section option gathering */
750 alpm_siglevel_t siglevel;
751 alpm_list_t *servers;
752 };
753
754 /**
755 * Wrap up a section once we have reached the end of it. This should be called
756 * when a subsequent section is encountered, or when we have reached the end of
757 * the root config file. Once called, all existing saved config pieces on the
758 * section struct are freed.
759 * @param section the current parsed and saved section data
760 * @param parse_options whether we are parsing options or repo data
761 * @return 0 on success, 1 on failure
762 */
763 static int finish_section(struct section_t *section, int parse_options)
764 {
765 int ret = 0;
766 alpm_list_t *i;
767 alpm_db_t *db;
768
769 pm_printf(ALPM_LOG_DEBUG, "config: finish section '%s'\n", section->name);
770
771 /* parsing options (or nothing)- nothing to do except free the pieces */
772 if(!section->name || parse_options || section->is_options) {
773 goto cleanup;
774 }
775
776 /* if we are not looking at options sections only, register a db */
777 db = alpm_register_syncdb(config->handle, section->name, section->siglevel);
778 if(db == NULL) {
779 pm_printf(ALPM_LOG_ERROR, _("could not register '%s' database (%s)\n"),
780 section->name, alpm_strerror(alpm_errno(config->handle)));
781 ret = 1;
782 goto cleanup;
783 }
784
785 for(i = section->servers; i; i = alpm_list_next(i)) {
786 char *value = i->data;
787 if(_add_mirror(db, value) != 0) {
788 pm_printf(ALPM_LOG_ERROR,
789 _("could not add mirror '%s' to database '%s' (%s)\n"),
790 value, section->name, alpm_strerror(alpm_errno(config->handle)));
791 ret = 1;
792 goto cleanup;
793 }
794 free(value);
795 }
796
797 cleanup:
798 alpm_list_free(section->servers);
799 section->servers = NULL;
800 section->siglevel = ALPM_SIG_USE_DEFAULT;
801 free(section->name);
802 section->name = NULL;
803 return ret;
804 }
805
806 /** The "real" parseconfig. Each "Include" directive will recall this method so
807 * recursion and stack depth are limited to 10 levels. The publicly visible
808 * parseconfig calls this with a NULL section argument so we can recall from
809 * within ourself on an include.
810 * @param file path to the config file
811 * @param section the current active section
812 * @param parse_options whether to parse and call methods for the options
813 * section; if 0, parse and call methods for the repos sections
814 * @param depth the current recursion depth
815 * @return 0 on success, 1 on failure
816 */
817 static int _parseconfig(const char *file, struct section_t *section,
818 int parse_options, int depth)
819 {
820 FILE *fp = NULL;
821 char line[PATH_MAX];
822 int linenum = 0;
823 int ret = 0;
824 const int max_depth = 10;
825
826 if(depth >= max_depth) {
827 pm_printf(ALPM_LOG_ERROR,
828 _("config parsing exceeded max recursion depth of %d.\n"), max_depth);
829 ret = 1;
830 goto cleanup;
831 }
832
833 pm_printf(ALPM_LOG_DEBUG, "config: attempting to read file %s\n", file);
834 fp = fopen(file, "r");
835 if(fp == NULL) {
836 pm_printf(ALPM_LOG_ERROR, _("config file %s could not be read: %s\n"),
837 file, strerror(errno));
838 ret = 1;
839 goto cleanup;
840 }
841
842 while(fgets(line, PATH_MAX, fp)) {
843 char *key, *value, *ptr;
844 size_t line_len;
845
846 linenum++;
847
848 /* ignore whole line and end of line comments */
849 if((ptr = strchr(line, '#'))) {
850 *ptr = '\0';
851 }
852
853 line_len = strtrim(line);
854
855 if(line_len == 0) {
856 continue;
857 }
858
859 if(line[0] == '[' && line[line_len - 1] == ']') {
860 char *name;
861 /* only possibility here is a line == '[]' */
862 if(line_len <= 2) {
863 pm_printf(ALPM_LOG_ERROR, _("config file %s, line %d: bad section name.\n"),
864 file, linenum);
865 ret = 1;
866 goto cleanup;
867 }
868 /* new config section, skip the '[' */
869 name = strdup(line + 1);
870 name[line_len - 2] = '\0';
871 /* we're at a new section; perform any post-actions for the prior */
872 if(finish_section(section, parse_options)) {
873 ret = 1;
874 goto cleanup;
875 }
876 pm_printf(ALPM_LOG_DEBUG, "config: new section '%s'\n", name);
877 section->name = name;
878 section->is_options = (strcmp(name, "options") == 0);
879 continue;
880 }
881
882 /* directive */
883 /* strsep modifies the 'line' string: 'key \0 value' */
884 key = line;
885 value = line;
886 strsep(&value, "=");
887 strtrim(key);
888 strtrim(value);
889
890 if(key == NULL) {
891 pm_printf(ALPM_LOG_ERROR, _("config file %s, line %d: syntax error in config file- missing key.\n"),
892 file, linenum);
893 ret = 1;
894 goto cleanup;
895 }
896 /* For each directive, compare to the camelcase string. */
897 if(section->name == NULL) {
898 pm_printf(ALPM_LOG_ERROR, _("config file %s, line %d: All directives must belong to a section.\n"),
899 file, linenum);
900 ret = 1;
901 goto cleanup;
902 }
903 /* Include is allowed in both options and repo sections */
904 if(strcmp(key, "Include") == 0) {
905 glob_t globbuf;
906 int globret;
907 size_t gindex;
908
909 if(value == NULL) {
910 pm_printf(ALPM_LOG_ERROR, _("config file %s, line %d: directive '%s' needs a value\n"),
911 file, linenum, key);
912 ret = 1;
913 goto cleanup;
914 }
915 /* Ignore include failures... assume non-critical */
916 globret = glob(value, GLOB_NOCHECK, NULL, &globbuf);
917 switch(globret) {
918 case GLOB_NOSPACE:
919 pm_printf(ALPM_LOG_DEBUG,
920 "config file %s, line %d: include globbing out of space\n",
921 file, linenum);
922 break;
923 case GLOB_ABORTED:
924 pm_printf(ALPM_LOG_DEBUG,
925 "config file %s, line %d: include globbing read error for %s\n",
926 file, linenum, value);
927 break;
928 case GLOB_NOMATCH:
929 pm_printf(ALPM_LOG_DEBUG,
930 "config file %s, line %d: no include found for %s\n",
931 file, linenum, value);
932 break;
933 default:
934 for(gindex = 0; gindex < globbuf.gl_pathc; gindex++) {
935 pm_printf(ALPM_LOG_DEBUG, "config file %s, line %d: including %s\n",
936 file, linenum, globbuf.gl_pathv[gindex]);
937 _parseconfig(globbuf.gl_pathv[gindex], section, parse_options, depth + 1);
938 }
939 break;
940 }
941 globfree(&globbuf);
942 continue;
943 }
944 if(parse_options && section->is_options) {
945 /* we are either in options ... */
946 if((ret = _parse_options(key, value, file, linenum)) != 0) {
947 goto cleanup;
948 }
949 } else if(!parse_options && !section->is_options) {
950 /* ... or in a repo section */
951 if(strcmp(key, "Server") == 0) {
952 if(value == NULL) {
953 pm_printf(ALPM_LOG_ERROR, _("config file %s, line %d: directive '%s' needs a value\n"),
954 file, linenum, key);
955 ret = 1;
956 goto cleanup;
957 }
958 section->servers = alpm_list_add(section->servers, strdup(value));
959 } else if(strcmp(key, "SigLevel") == 0) {
960 alpm_list_t *values = NULL;
961 setrepeatingoption(value, "SigLevel", &values);
962 if(values) {
963 if(section->siglevel == ALPM_SIG_USE_DEFAULT) {
964 section->siglevel = config->siglevel;
965 }
966 if(process_siglevel(values, &section->siglevel, file, linenum)) {
967 FREELIST(values);
968 ret = 1;
969 goto cleanup;
970 }
971 FREELIST(values);
972 }
973 } else {
974 pm_printf(ALPM_LOG_WARNING,
975 _("config file %s, line %d: directive '%s' in section '%s' not recognized.\n"),
976 file, linenum, key, section->name);
977 }
978 }
979 }
980
981 if(depth == 0) {
982 ret = finish_section(section, parse_options);
983 }
984
985 cleanup:
986 if(fp) {
987 fclose(fp);
988 }
989 pm_printf(ALPM_LOG_DEBUG, "config: finished parsing %s\n", file);
990 return ret;
991 }
992
993 /** Parse a configuration file.
994 * @param file path to the config file
995 * @return 0 on success, non-zero on error
996 */
997 int parseconfig(const char *file)
998 {
999 int ret;
1000 struct section_t section;
1001 memset(&section, 0, sizeof(struct section_t));
1002 section.siglevel = ALPM_SIG_USE_DEFAULT;
1003 /* the config parse is a two-pass affair. We first parse the entire thing for
1004 * the [options] section so we can get all default and path options set.
1005 * Next, we go back and parse everything but [options]. */
1006
1007 /* call the real parseconfig function with a null section & db argument */
1008 pm_printf(ALPM_LOG_DEBUG, "parseconfig: options pass\n");
1009 if((ret = _parseconfig(file, &section, 1, 0))) {
1010 return ret;
1011 }
1012 if((ret = setup_libalpm())) {
1013 return ret;
1014 }
1015 /* second pass, repo section parsing */
1016 pm_printf(ALPM_LOG_DEBUG, "parseconfig: repo pass\n");
1017 return _parseconfig(file, &section, 0, 0);
1018 }
1019
1020 /* vim: set ts=2 sw=2 noet: */