]>
Commit | Line | Data |
---|---|---|
d04baaba JV |
1 | /* |
2 | * conf.c | |
23229097 | 3 | * |
d2882404 | 4 | * Copyright (c) 2006-2011 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 | ||
869e81e1 DM |
21 | #include "config.h" |
22 | ||
bda208f8 DM |
23 | #include <errno.h> |
24 | #include <glob.h> | |
25 | #include <limits.h> | |
d04baaba JV |
26 | #include <stdlib.h> |
27 | #include <stdio.h> | |
c22e381a | 28 | #include <string.h> /* strdup */ |
bda208f8 DM |
29 | #include <sys/stat.h> |
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) { | |
3c3cb001 AM |
46 | pm_fprintf(stderr, PM_LOG_ERROR, |
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 DM |
52 | newconfig->op = PM_OP_MAIN; |
53 | newconfig->logmask = PM_LOG_ERROR | PM_LOG_WARNING; | |
2f0de317 | 54 | newconfig->configfile = strdup(CONFFILE); |
4fdcf50d | 55 | newconfig->sigverify = PM_PGP_VERIFY_UNKNOWN; |
98df67cd | 56 | |
0303b26b | 57 | return newconfig; |
98df67cd AF |
58 | } |
59 | ||
2f0de317 | 60 | int config_free(config_t *oldconfig) |
98df67cd | 61 | { |
2f0de317 | 62 | if(oldconfig == NULL) { |
0303b26b | 63 | return -1; |
98df67cd AF |
64 | } |
65 | ||
a888f377 | 66 | FREELIST(oldconfig->holdpkg); |
d5278ebb | 67 | FREELIST(oldconfig->syncfirst); |
4fdcf50d DM |
68 | FREELIST(oldconfig->ignorepkg); |
69 | FREELIST(oldconfig->ignoregrp); | |
70 | FREELIST(oldconfig->noupgrade); | |
71 | FREELIST(oldconfig->noextract); | |
2f0de317 | 72 | free(oldconfig->configfile); |
4845207f DM |
73 | free(oldconfig->rootdir); |
74 | free(oldconfig->dbpath); | |
75 | free(oldconfig->logfile); | |
4fdcf50d DM |
76 | free(oldconfig->gpgdir); |
77 | FREELIST(oldconfig->cachedirs); | |
30c4d53c | 78 | free(oldconfig->xfercommand); |
d39b1dbe | 79 | free(oldconfig->print_format); |
4fdcf50d | 80 | free(oldconfig->arch); |
2f0de317 DM |
81 | free(oldconfig); |
82 | oldconfig = NULL; | |
98df67cd | 83 | |
0303b26b | 84 | return 0; |
98df67cd AF |
85 | } |
86 | ||
bda208f8 DM |
87 | /** Helper function for download_with_xfercommand() */ |
88 | static char *get_filename(const char *url) { | |
89 | char *filename = strrchr(url, '/'); | |
90 | if(filename != NULL) { | |
91 | filename++; | |
92 | } | |
93 | return filename; | |
94 | } | |
95 | ||
96 | /** Helper function for download_with_xfercommand() */ | |
97 | static char *get_destfile(const char *path, const char *filename) { | |
98 | char *destfile; | |
99 | /* len = localpath len + filename len + null */ | |
100 | size_t len = strlen(path) + strlen(filename) + 1; | |
101 | destfile = calloc(len, sizeof(char)); | |
102 | snprintf(destfile, len, "%s%s", path, filename); | |
103 | ||
104 | return destfile; | |
105 | } | |
106 | ||
107 | /** Helper function for download_with_xfercommand() */ | |
108 | static char *get_tempfile(const char *path, const char *filename) { | |
109 | char *tempfile; | |
110 | /* len = localpath len + filename len + '.part' len + null */ | |
111 | size_t len = strlen(path) + strlen(filename) + 6; | |
112 | tempfile = calloc(len, sizeof(char)); | |
113 | snprintf(tempfile, len, "%s%s.part", path, filename); | |
114 | ||
115 | return tempfile; | |
116 | } | |
117 | ||
118 | /** External fetch callback */ | |
119 | static int download_with_xfercommand(const char *url, const char *localpath, | |
120 | int force) { | |
121 | int ret = 0; | |
122 | int retval; | |
123 | int usepart = 0; | |
124 | struct stat st; | |
125 | char *parsedcmd,*tempcmd; | |
126 | char cwd[PATH_MAX]; | |
127 | int restore_cwd = 0; | |
128 | char *destfile, *tempfile, *filename; | |
129 | ||
130 | if(!config->xfercommand) { | |
131 | return -1; | |
132 | } | |
133 | ||
134 | filename = get_filename(url); | |
135 | if(!filename) { | |
136 | return -1; | |
137 | } | |
138 | destfile = get_destfile(localpath, filename); | |
139 | tempfile = get_tempfile(localpath, filename); | |
140 | ||
141 | if(force && stat(tempfile, &st) == 0) { | |
142 | unlink(tempfile); | |
143 | } | |
144 | if(force && stat(destfile, &st) == 0) { | |
145 | unlink(destfile); | |
146 | } | |
147 | ||
148 | tempcmd = strdup(config->xfercommand); | |
149 | /* replace all occurrences of %o with fn.part */ | |
150 | if(strstr(tempcmd, "%o")) { | |
151 | usepart = 1; | |
152 | parsedcmd = strreplace(tempcmd, "%o", tempfile); | |
153 | free(tempcmd); | |
154 | tempcmd = parsedcmd; | |
155 | } | |
156 | /* replace all occurrences of %u with the download URL */ | |
157 | parsedcmd = strreplace(tempcmd, "%u", url); | |
158 | free(tempcmd); | |
159 | ||
160 | /* save the cwd so we can restore it later */ | |
161 | if(getcwd(cwd, PATH_MAX) == NULL) { | |
162 | pm_printf(PM_LOG_ERROR, _("could not get current working directory\n")); | |
163 | } else { | |
164 | restore_cwd = 1; | |
165 | } | |
166 | ||
167 | /* cwd to the download directory */ | |
168 | if(chdir(localpath)) { | |
169 | pm_printf(PM_LOG_WARNING, _("could not chdir to download directory %s\n"), localpath); | |
170 | ret = -1; | |
171 | goto cleanup; | |
172 | } | |
173 | /* execute the parsed command via /bin/sh -c */ | |
174 | pm_printf(PM_LOG_DEBUG, "running command: %s\n", parsedcmd); | |
175 | retval = system(parsedcmd); | |
176 | ||
177 | if(retval == -1) { | |
178 | pm_printf(PM_LOG_WARNING, _("running XferCommand: fork failed!\n")); | |
179 | ret = -1; | |
180 | } else if(retval != 0) { | |
181 | /* download failed */ | |
182 | pm_printf(PM_LOG_DEBUG, "XferCommand command returned non-zero status " | |
183 | "code (%d)\n", retval); | |
184 | ret = -1; | |
185 | } else { | |
186 | /* download was successful */ | |
187 | if(usepart) { | |
188 | rename(tempfile, destfile); | |
189 | } | |
190 | ret = 0; | |
191 | } | |
192 | ||
193 | cleanup: | |
194 | /* restore the old cwd if we have it */ | |
195 | if(restore_cwd && chdir(cwd) != 0) { | |
196 | pm_printf(PM_LOG_ERROR, _("could not change directory to %s (%s)\n"), | |
197 | cwd, strerror(errno)); | |
198 | } | |
199 | ||
200 | if(ret == -1) { | |
201 | /* hack to let an user the time to cancel a download */ | |
202 | sleep(2); | |
203 | } | |
204 | free(destfile); | |
205 | free(tempfile); | |
206 | free(parsedcmd); | |
207 | ||
208 | return ret; | |
209 | } | |
210 | ||
211 | ||
212 | int config_set_arch(const char *arch) | |
213 | { | |
214 | if(strcmp(arch, "auto") == 0) { | |
215 | struct utsname un; | |
216 | uname(&un); | |
4fdcf50d | 217 | config->arch = strdup(un.machine); |
bda208f8 | 218 | } else { |
4fdcf50d | 219 | config->arch = strdup(arch); |
bda208f8 | 220 | } |
4fdcf50d DM |
221 | pm_printf(PM_LOG_DEBUG, "config: arch: %s\n", config->arch); |
222 | return 0; | |
bda208f8 DM |
223 | } |
224 | ||
0fbdfd02 DM |
225 | static pgp_verify_t option_verifysig(const char *value) |
226 | { | |
227 | pgp_verify_t level; | |
228 | if(strcmp(value, "Always") == 0) { | |
229 | level = PM_PGP_VERIFY_ALWAYS; | |
230 | } else if(strcmp(value, "Optional") == 0) { | |
231 | level = PM_PGP_VERIFY_OPTIONAL; | |
232 | } else if(strcmp(value, "Never") == 0) { | |
233 | level = PM_PGP_VERIFY_NEVER; | |
234 | } else { | |
235 | level = PM_PGP_VERIFY_UNKNOWN; | |
236 | } | |
237 | pm_printf(PM_LOG_DEBUG, "config: VerifySig = %s (%d)\n", value, level); | |
238 | return level; | |
239 | } | |
bda208f8 | 240 | |
4fdcf50d DM |
241 | static int process_cleanmethods(alpm_list_t *values) { |
242 | alpm_list_t *i; | |
243 | for(i = values; i; i = alpm_list_next(i)) { | |
244 | const char *value = i->data; | |
245 | if(strcmp(value, "KeepInstalled") == 0) { | |
246 | config->cleanmethod |= PM_CLEAN_KEEPINST; | |
247 | } else if(strcmp(value, "KeepCurrent") == 0) { | |
248 | config->cleanmethod |= PM_CLEAN_KEEPCUR; | |
249 | } else { | |
250 | pm_printf(PM_LOG_ERROR, _("invalid value for 'CleanMethod' : '%s'\n"), | |
251 | value); | |
252 | return 1; | |
253 | } | |
bda208f8 DM |
254 | } |
255 | return 0; | |
256 | } | |
257 | ||
258 | /** Add repeating options such as NoExtract, NoUpgrade, etc to libalpm | |
259 | * settings. Refactored out of the parseconfig code since all of them did | |
260 | * the exact same thing and duplicated code. | |
261 | * @param ptr a pointer to the start of the multiple options | |
262 | * @param option the string (friendly) name of the option, used for messages | |
4fdcf50d | 263 | * @param list the list to add the option to |
bda208f8 DM |
264 | */ |
265 | static void setrepeatingoption(char *ptr, const char *option, | |
4fdcf50d | 266 | alpm_list_t **list) |
bda208f8 DM |
267 | { |
268 | char *q; | |
269 | ||
270 | while((q = strchr(ptr, ' '))) { | |
271 | *q = '\0'; | |
4fdcf50d | 272 | *list = alpm_list_add(*list, strdup(ptr)); |
bda208f8 DM |
273 | pm_printf(PM_LOG_DEBUG, "config: %s: %s\n", option, ptr); |
274 | ptr = q; | |
275 | ptr++; | |
276 | } | |
4fdcf50d | 277 | *list = alpm_list_add(*list, strdup(ptr)); |
bda208f8 DM |
278 | pm_printf(PM_LOG_DEBUG, "config: %s: %s\n", option, ptr); |
279 | } | |
280 | ||
281 | static int _parse_options(const char *key, char *value, | |
282 | const char *file, int linenum) | |
283 | { | |
284 | if(value == NULL) { | |
285 | /* options without settings */ | |
286 | if(strcmp(key, "UseSyslog") == 0) { | |
4fdcf50d | 287 | config->usesyslog = 1; |
bda208f8 DM |
288 | pm_printf(PM_LOG_DEBUG, "config: usesyslog\n"); |
289 | } else if(strcmp(key, "ILoveCandy") == 0) { | |
290 | config->chomp = 1; | |
291 | pm_printf(PM_LOG_DEBUG, "config: chomp\n"); | |
292 | } else if(strcmp(key, "VerbosePkgLists") == 0) { | |
293 | config->verbosepkglists = 1; | |
294 | pm_printf(PM_LOG_DEBUG, "config: verbosepkglists\n"); | |
295 | } else if(strcmp(key, "UseDelta") == 0) { | |
4fdcf50d | 296 | config->usedelta = 1; |
bda208f8 DM |
297 | pm_printf(PM_LOG_DEBUG, "config: usedelta\n"); |
298 | } else if(strcmp(key, "TotalDownload") == 0) { | |
299 | config->totaldownload = 1; | |
300 | pm_printf(PM_LOG_DEBUG, "config: totaldownload\n"); | |
301 | } else if(strcmp(key, "CheckSpace") == 0) { | |
4fdcf50d | 302 | config->checkspace = 1; |
bda208f8 DM |
303 | } else { |
304 | pm_printf(PM_LOG_WARNING, | |
305 | _("config file %s, line %d: directive '%s' in section '%s' not recognized.\n"), | |
306 | file, linenum, key, "options"); | |
307 | } | |
308 | } else { | |
309 | /* options with settings */ | |
310 | if(strcmp(key, "NoUpgrade") == 0) { | |
4fdcf50d | 311 | setrepeatingoption(value, "NoUpgrade", &(config->noupgrade)); |
bda208f8 | 312 | } else if(strcmp(key, "NoExtract") == 0) { |
4fdcf50d | 313 | setrepeatingoption(value, "NoExtract", &(config->noextract)); |
bda208f8 | 314 | } else if(strcmp(key, "IgnorePkg") == 0) { |
4fdcf50d | 315 | setrepeatingoption(value, "IgnorePkg", &(config->ignorepkg)); |
bda208f8 | 316 | } else if(strcmp(key, "IgnoreGroup") == 0) { |
4fdcf50d | 317 | setrepeatingoption(value, "IgnoreGroup", &(config->ignoregrp)); |
bda208f8 | 318 | } else if(strcmp(key, "HoldPkg") == 0) { |
4fdcf50d | 319 | setrepeatingoption(value, "HoldPkg", &(config->holdpkg)); |
bda208f8 | 320 | } else if(strcmp(key, "SyncFirst") == 0) { |
4fdcf50d DM |
321 | setrepeatingoption(value, "SyncFirst", &(config->syncfirst)); |
322 | } else if(strcmp(key, "CacheDir") == 0) { | |
323 | setrepeatingoption(value, "CacheDir", &(config->cachedirs)); | |
bda208f8 | 324 | } else if(strcmp(key, "Architecture") == 0) { |
4fdcf50d | 325 | if(!config->arch) { |
bda208f8 DM |
326 | config_set_arch(value); |
327 | } | |
328 | } else if(strcmp(key, "DBPath") == 0) { | |
329 | /* don't overwrite a path specified on the command line */ | |
330 | if(!config->dbpath) { | |
331 | config->dbpath = strdup(value); | |
332 | pm_printf(PM_LOG_DEBUG, "config: dbpath: %s\n", value); | |
333 | } | |
bda208f8 DM |
334 | } else if(strcmp(key, "RootDir") == 0) { |
335 | /* don't overwrite a path specified on the command line */ | |
336 | if(!config->rootdir) { | |
337 | config->rootdir = strdup(value); | |
338 | pm_printf(PM_LOG_DEBUG, "config: rootdir: %s\n", value); | |
339 | } | |
340 | } else if(strcmp(key, "GPGDir") == 0) { | |
341 | if(!config->gpgdir) { | |
342 | config->gpgdir = strdup(value); | |
343 | pm_printf(PM_LOG_DEBUG, "config: gpgdir: %s\n", value); | |
344 | } | |
345 | } else if(strcmp(key, "LogFile") == 0) { | |
346 | if(!config->logfile) { | |
347 | config->logfile = strdup(value); | |
348 | pm_printf(PM_LOG_DEBUG, "config: logfile: %s\n", value); | |
349 | } | |
350 | } else if(strcmp(key, "XferCommand") == 0) { | |
351 | config->xfercommand = strdup(value); | |
bda208f8 DM |
352 | pm_printf(PM_LOG_DEBUG, "config: xfercommand: %s\n", value); |
353 | } else if(strcmp(key, "CleanMethod") == 0) { | |
4fdcf50d DM |
354 | alpm_list_t *methods = NULL; |
355 | setrepeatingoption(value, "CleanMethod", &methods); | |
356 | if(process_cleanmethods(methods)) { | |
357 | FREELIST(methods); | |
358 | return 1; | |
359 | } | |
360 | FREELIST(methods); | |
bda208f8 | 361 | } else if(strcmp(key, "VerifySig") == 0) { |
0fbdfd02 DM |
362 | pgp_verify_t level = option_verifysig(value); |
363 | if(level != PM_PGP_VERIFY_UNKNOWN) { | |
4fdcf50d | 364 | config->sigverify = level; |
bda208f8 | 365 | } else { |
0fbdfd02 DM |
366 | pm_printf(PM_LOG_ERROR, |
367 | _("config file %s, line %d: directive '%s' has invalid value '%s'\n"), | |
368 | file, linenum, key, value); | |
bda208f8 DM |
369 | return 1; |
370 | } | |
bda208f8 | 371 | } else { |
bda208f8 DM |
372 | pm_printf(PM_LOG_WARNING, |
373 | _("config file %s, line %d: directive '%s' in section '%s' not recognized.\n"), | |
374 | file, linenum, key, "options"); | |
375 | } | |
376 | ||
377 | } | |
378 | return 0; | |
379 | } | |
380 | ||
381 | static int _add_mirror(pmdb_t *db, char *value) | |
382 | { | |
383 | const char *dbname = alpm_db_get_name(db); | |
384 | /* let's attempt a replacement for the current repo */ | |
385 | char *temp = strreplace(value, "$repo", dbname); | |
386 | /* let's attempt a replacement for the arch */ | |
4fdcf50d | 387 | const char *arch = config->arch; |
bda208f8 DM |
388 | char *server; |
389 | if(arch) { | |
390 | server = strreplace(temp, "$arch", arch); | |
391 | free(temp); | |
392 | } else { | |
393 | if(strstr(temp, "$arch")) { | |
394 | free(temp); | |
395 | pm_printf(PM_LOG_ERROR, _("The mirror '%s' contains the $arch" | |
396 | " variable, but no Architecture is defined.\n"), value); | |
397 | return 1; | |
398 | } | |
399 | server = temp; | |
400 | } | |
401 | ||
42ab639b | 402 | if(alpm_db_add_server(db, server) != 0) { |
bda208f8 DM |
403 | /* pm_errno is set by alpm_db_setserver */ |
404 | pm_printf(PM_LOG_ERROR, _("could not add server URL to database '%s': %s (%s)\n"), | |
405 | dbname, server, alpm_strerrorlast()); | |
406 | free(server); | |
407 | return 1; | |
408 | } | |
409 | ||
410 | free(server); | |
411 | return 0; | |
412 | } | |
413 | ||
4fdcf50d | 414 | /** Sets up libalpm global stuff in one go. Called after the command line |
bda208f8 DM |
415 | * and inital config file parsing. Once this is complete, we can see if any |
416 | * paths were defined. If a rootdir was defined and nothing else, we want all | |
417 | * of our paths to live under the rootdir that was specified. Safe to call | |
418 | * multiple times (will only do anything the first time). | |
419 | */ | |
4fdcf50d | 420 | static int setup_libalpm(void) |
bda208f8 | 421 | { |
73c74355 | 422 | int ret = 0; |
4fdcf50d DM |
423 | enum _pmerrno_t err; |
424 | ||
425 | pm_printf(PM_LOG_DEBUG, "setup_libalpm called\n"); | |
73c74355 | 426 | |
73c74355 DM |
427 | /* Configure root path first. If it is set and dbpath/logfile were not |
428 | * set, then set those as well to reside under the root. */ | |
429 | if(config->rootdir) { | |
430 | char path[PATH_MAX]; | |
73c74355 | 431 | if(!config->dbpath) { |
4fdcf50d | 432 | snprintf(path, PATH_MAX, "%s/%s", config->rootdir, DBPATH + 1); |
73c74355 | 433 | config->dbpath = strdup(path); |
bda208f8 | 434 | } |
73c74355 | 435 | if(!config->logfile) { |
4fdcf50d | 436 | snprintf(path, PATH_MAX, "%s/%s", config->rootdir, LOGFILE + 1); |
73c74355 | 437 | config->logfile = strdup(path); |
bda208f8 | 438 | } |
4fdcf50d DM |
439 | } else { |
440 | config->rootdir = strdup(ROOTDIR); | |
441 | if(!config->dbpath) { | |
442 | config->dbpath = strdup(DBPATH); | |
bda208f8 | 443 | } |
73c74355 | 444 | } |
4fdcf50d DM |
445 | |
446 | /* initialize library */ | |
447 | config->handle = alpm_initialize(config->rootdir, config->dbpath, &err); | |
448 | if(!config->handle) { | |
449 | pm_printf(PM_LOG_ERROR, _("failed to initialize alpm library (%s)\n"), | |
450 | alpm_strerror(err)); | |
451 | return -1; | |
452 | } | |
453 | ||
454 | alpm_option_set_logcb(cb_log); | |
455 | alpm_option_set_dlcb(cb_dl_progress); | |
456 | ||
457 | config->logfile = config->logfile ? config->logfile : strdup(LOGFILE); | |
458 | ret = alpm_option_set_logfile(config->logfile); | |
459 | if(ret != 0) { | |
460 | pm_printf(PM_LOG_ERROR, _("problem setting logfile '%s' (%s)\n"), | |
461 | config->logfile, alpm_strerrorlast()); | |
462 | return ret; | |
73c74355 | 463 | } |
bda208f8 | 464 | |
73c74355 DM |
465 | /* Set GnuPG's home directory. This is not relative to rootdir, even if |
466 | * rootdir is defined. Reasoning: gpgdir contains configuration data. */ | |
4fdcf50d DM |
467 | config->gpgdir = config->gpgdir ? config->gpgdir : strdup(GPGDIR); |
468 | ret = alpm_option_set_signaturedir(config->gpgdir); | |
469 | if(ret != 0) { | |
470 | pm_printf(PM_LOG_ERROR, _("problem setting gpgdir '%s' (%s)\n"), | |
471 | config->gpgdir, alpm_strerrorlast()); | |
472 | return ret; | |
bda208f8 | 473 | } |
73c74355 DM |
474 | |
475 | /* add a default cachedir if one wasn't specified */ | |
4fdcf50d | 476 | if(config->cachedirs == NULL) { |
73c74355 | 477 | alpm_option_add_cachedir(CACHEDIR); |
4fdcf50d DM |
478 | } else { |
479 | alpm_option_set_cachedirs(config->cachedirs); | |
480 | } | |
481 | ||
482 | if(config->sigverify != PM_PGP_VERIFY_UNKNOWN) { | |
483 | alpm_option_set_default_sigverify(config->sigverify); | |
484 | } | |
485 | ||
486 | if(config->xfercommand) { | |
487 | alpm_option_set_fetchcb(download_with_xfercommand); | |
73c74355 | 488 | } |
4fdcf50d DM |
489 | |
490 | if(config->totaldownload) { | |
491 | alpm_option_set_totaldlcb(cb_dl_total); | |
492 | } | |
493 | ||
494 | alpm_option_set_arch(config->arch); | |
495 | alpm_option_set_checkspace(config->checkspace); | |
496 | alpm_option_set_usesyslog(config->usesyslog); | |
497 | alpm_option_set_usedelta(config->usedelta); | |
498 | alpm_option_set_default_sigverify(config->sigverify); | |
499 | ||
500 | alpm_option_set_ignorepkgs(config->ignorepkg); | |
501 | alpm_option_set_ignoregrps(config->ignoregrp); | |
502 | alpm_option_set_noupgrades(config->noupgrade); | |
503 | alpm_option_set_noextracts(config->noextract); | |
504 | ||
73c74355 | 505 | return 0; |
bda208f8 DM |
506 | } |
507 | ||
508 | ||
509 | /* The real parseconfig. Called with a null section argument by the publicly | |
510 | * visible parseconfig so we can recall from within ourself on an include */ | |
73c74355 | 511 | static int _parseconfig(const char *file, int parse_options, |
dd95c96b | 512 | char **section, pmdb_t *db) |
bda208f8 DM |
513 | { |
514 | FILE *fp = NULL; | |
515 | char line[PATH_MAX+1]; | |
516 | int linenum = 0; | |
dd95c96b | 517 | char *ptr; |
bda208f8 DM |
518 | int ret = 0; |
519 | ||
520 | pm_printf(PM_LOG_DEBUG, "config: attempting to read file %s\n", file); | |
521 | fp = fopen(file, "r"); | |
522 | if(fp == NULL) { | |
523 | pm_printf(PM_LOG_ERROR, _("config file %s could not be read.\n"), file); | |
524 | return 1; | |
525 | } | |
526 | ||
bda208f8 | 527 | while(fgets(line, PATH_MAX, fp)) { |
73c74355 DM |
528 | char *key, *value; |
529 | ||
bda208f8 DM |
530 | linenum++; |
531 | strtrim(line); | |
532 | ||
533 | /* ignore whole line and end of line comments */ | |
534 | if(strlen(line) == 0 || line[0] == '#') { | |
535 | continue; | |
536 | } | |
537 | if((ptr = strchr(line, '#'))) { | |
538 | *ptr = '\0'; | |
539 | } | |
540 | ||
73c74355 DM |
541 | /* sanity check */ |
542 | if(parse_options && db) { | |
543 | pm_printf(PM_LOG_ERROR, _("config file %s, line %d: parsing options but have a database.\n"), | |
544 | file, linenum); | |
545 | ret = 1; | |
546 | goto cleanup; | |
547 | } | |
548 | ||
bda208f8 | 549 | if(line[0] == '[' && line[strlen(line)-1] == ']') { |
dd95c96b | 550 | char *name; |
bda208f8 DM |
551 | /* new config section, skip the '[' */ |
552 | ptr = line; | |
553 | ptr++; | |
dd95c96b DM |
554 | name = strdup(ptr); |
555 | name[strlen(name)-1] = '\0'; | |
556 | if(!strlen(name)) { | |
bda208f8 DM |
557 | pm_printf(PM_LOG_ERROR, _("config file %s, line %d: bad section name.\n"), |
558 | file, linenum); | |
559 | ret = 1; | |
560 | goto cleanup; | |
561 | } | |
dd95c96b | 562 | pm_printf(PM_LOG_DEBUG, "config: new section '%s'\n", name); |
bda208f8 | 563 | /* if we are not looking at the options section, register a db */ |
dd95c96b DM |
564 | if(!parse_options && strcmp(name, "options") != 0) { |
565 | db = alpm_db_register_sync(name); | |
bda208f8 DM |
566 | if(db == NULL) { |
567 | pm_printf(PM_LOG_ERROR, _("could not register '%s' database (%s)\n"), | |
dd95c96b | 568 | name, alpm_strerrorlast()); |
bda208f8 DM |
569 | ret = 1; |
570 | goto cleanup; | |
571 | } | |
572 | } | |
dd95c96b DM |
573 | if(*section) { |
574 | free(*section); | |
575 | } | |
576 | *section = name; | |
bda208f8 DM |
577 | continue; |
578 | } | |
579 | ||
580 | /* directive */ | |
bda208f8 DM |
581 | /* strsep modifies the 'line' string: 'key \0 value' */ |
582 | key = line; | |
583 | value = line; | |
584 | strsep(&value, "="); | |
585 | strtrim(key); | |
586 | strtrim(value); | |
587 | ||
588 | if(key == NULL) { | |
589 | pm_printf(PM_LOG_ERROR, _("config file %s, line %d: syntax error in config file- missing key.\n"), | |
590 | file, linenum); | |
591 | ret = 1; | |
592 | goto cleanup; | |
593 | } | |
594 | /* For each directive, compare to the camelcase string. */ | |
dd95c96b | 595 | if(*section == NULL) { |
bda208f8 DM |
596 | pm_printf(PM_LOG_ERROR, _("config file %s, line %d: All directives must belong to a section.\n"), |
597 | file, linenum); | |
598 | ret = 1; | |
599 | goto cleanup; | |
600 | } | |
601 | /* Include is allowed in both options and repo sections */ | |
602 | if(strcmp(key, "Include") == 0) { | |
73c74355 DM |
603 | glob_t globbuf; |
604 | int globret; | |
605 | size_t gindex; | |
606 | ||
bda208f8 DM |
607 | if(value == NULL) { |
608 | pm_printf(PM_LOG_ERROR, _("config file %s, line %d: directive '%s' needs a value\n"), | |
609 | file, linenum, key); | |
610 | ret = 1; | |
611 | goto cleanup; | |
612 | } | |
613 | /* Ignore include failures... assume non-critical */ | |
bda208f8 DM |
614 | globret = glob(value, GLOB_NOCHECK, NULL, &globbuf); |
615 | switch(globret) { | |
616 | case GLOB_NOSPACE: | |
617 | pm_printf(PM_LOG_DEBUG, | |
618 | "config file %s, line %d: include globbing out of space\n", | |
619 | file, linenum); | |
620 | break; | |
621 | case GLOB_ABORTED: | |
622 | pm_printf(PM_LOG_DEBUG, | |
623 | "config file %s, line %d: include globbing read error for %s\n", | |
624 | file, linenum, value); | |
625 | break; | |
626 | case GLOB_NOMATCH: | |
627 | pm_printf(PM_LOG_DEBUG, | |
628 | "config file %s, line %d: no include found for %s\n", | |
629 | file, linenum, value); | |
630 | break; | |
631 | default: | |
73c74355 | 632 | for(gindex = 0; gindex < globbuf.gl_pathc; gindex++) { |
bda208f8 DM |
633 | pm_printf(PM_LOG_DEBUG, "config file %s, line %d: including %s\n", |
634 | file, linenum, globbuf.gl_pathv[gindex]); | |
73c74355 | 635 | _parseconfig(globbuf.gl_pathv[gindex], parse_options, section, db); |
bda208f8 DM |
636 | } |
637 | break; | |
638 | } | |
639 | globfree(&globbuf); | |
640 | continue; | |
641 | } | |
dd95c96b | 642 | if(parse_options && strcmp(*section, "options") == 0) { |
bda208f8 DM |
643 | /* we are either in options ... */ |
644 | if((ret = _parse_options(key, value, file, linenum)) != 0) { | |
645 | goto cleanup; | |
646 | } | |
dd95c96b | 647 | } else if (!parse_options && strcmp(*section, "options") != 0) { |
bda208f8 DM |
648 | /* ... or in a repo section */ |
649 | if(strcmp(key, "Server") == 0) { | |
650 | if(value == NULL) { | |
651 | pm_printf(PM_LOG_ERROR, _("config file %s, line %d: directive '%s' needs a value\n"), | |
652 | file, linenum, key); | |
653 | ret = 1; | |
654 | goto cleanup; | |
655 | } | |
656 | if(_add_mirror(db, value) != 0) { | |
657 | ret = 1; | |
658 | goto cleanup; | |
659 | } | |
660 | } else if(strcmp(key, "VerifySig") == 0) { | |
0fbdfd02 DM |
661 | pgp_verify_t level = option_verifysig(value); |
662 | if(level != PM_PGP_VERIFY_UNKNOWN) { | |
663 | ret = alpm_db_set_pgp_verify(db, level); | |
664 | if(ret != 0) { | |
665 | pm_printf(PM_LOG_ERROR, _("could not add set verify option for database '%s': %s (%s)\n"), | |
666 | alpm_db_get_name(db), value, alpm_strerrorlast()); | |
667 | goto cleanup; | |
668 | } | |
bda208f8 | 669 | } else { |
0fbdfd02 DM |
670 | pm_printf(PM_LOG_ERROR, |
671 | _("config file %s, line %d: directive '%s' has invalid value '%s'\n"), | |
672 | file, linenum, key, value); | |
bda208f8 DM |
673 | ret = 1; |
674 | goto cleanup; | |
675 | } | |
bda208f8 DM |
676 | } else { |
677 | pm_printf(PM_LOG_WARNING, | |
678 | _("config file %s, line %d: directive '%s' in section '%s' not recognized.\n"), | |
dd95c96b | 679 | file, linenum, key, *section); |
bda208f8 DM |
680 | } |
681 | } | |
bda208f8 DM |
682 | } |
683 | ||
684 | cleanup: | |
685 | fclose(fp); | |
bda208f8 DM |
686 | pm_printf(PM_LOG_DEBUG, "config: finished parsing %s\n", file); |
687 | return ret; | |
688 | } | |
689 | ||
690 | /** Parse a configuration file. | |
691 | * @param file path to the config file. | |
692 | * @return 0 on success, non-zero on error | |
693 | */ | |
694 | int parseconfig(const char *file) | |
695 | { | |
73c74355 | 696 | int ret; |
dd95c96b | 697 | char *section = NULL; |
73c74355 DM |
698 | /* the config parse is a two-pass affair. We first parse the entire thing for |
699 | * the [options] section so we can get all default and path options set. | |
700 | * Next, we go back and parse everything but [options]. */ | |
701 | ||
bda208f8 | 702 | /* call the real parseconfig function with a null section & db argument */ |
73c74355 | 703 | pm_printf(PM_LOG_DEBUG, "parseconfig: options pass\n"); |
dd95c96b DM |
704 | if((ret = _parseconfig(file, 1, §ion, NULL))) { |
705 | free(section); | |
73c74355 DM |
706 | return ret; |
707 | } | |
dd95c96b | 708 | free(section); |
4fdcf50d | 709 | if((ret = setup_libalpm())) { |
73c74355 DM |
710 | return ret; |
711 | } | |
712 | /* second pass, repo section parsing */ | |
dd95c96b | 713 | section = NULL; |
73c74355 | 714 | pm_printf(PM_LOG_DEBUG, "parseconfig: repo pass\n"); |
dd95c96b DM |
715 | return _parseconfig(file, 0, §ion, NULL); |
716 | free(section); | |
bda208f8 DM |
717 | } |
718 | ||
d04baaba | 719 | /* vim: set ts=2 sw=2 noet: */ |