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