]>
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) { | |
ca43fdd9 | 46 | pm_fprintf(stderr, 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); |
7af0ab1c | 55 | newconfig->siglevel = ALPM_SIG_USE_DEFAULT; |
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) { | |
ca43fdd9 | 162 | pm_printf(ALPM_LOG_ERROR, _("could not get current working directory\n")); |
bda208f8 DM |
163 | } else { |
164 | restore_cwd = 1; | |
165 | } | |
166 | ||
167 | /* cwd to the download directory */ | |
168 | if(chdir(localpath)) { | |
ca43fdd9 | 169 | pm_printf(ALPM_LOG_WARNING, _("could not chdir to download directory %s\n"), localpath); |
bda208f8 DM |
170 | ret = -1; |
171 | goto cleanup; | |
172 | } | |
173 | /* execute the parsed command via /bin/sh -c */ | |
ca43fdd9 | 174 | pm_printf(ALPM_LOG_DEBUG, "running command: %s\n", parsedcmd); |
bda208f8 DM |
175 | retval = system(parsedcmd); |
176 | ||
177 | if(retval == -1) { | |
ca43fdd9 | 178 | pm_printf(ALPM_LOG_WARNING, _("running XferCommand: fork failed!\n")); |
bda208f8 DM |
179 | ret = -1; |
180 | } else if(retval != 0) { | |
181 | /* download failed */ | |
ca43fdd9 | 182 | pm_printf(ALPM_LOG_DEBUG, "XferCommand command returned non-zero status " |
bda208f8 DM |
183 | "code (%d)\n", retval); |
184 | ret = -1; | |
185 | } else { | |
186 | /* download was successful */ | |
07a12927 | 187 | ret = 0; |
bda208f8 | 188 | if(usepart) { |
07a12927 DM |
189 | if(rename(tempfile, destfile)) { |
190 | pm_printf(ALPM_LOG_ERROR, _("could not rename %s to %s (%s)\n"), | |
191 | tempfile, destfile, strerror(errno)); | |
192 | ret = -1; | |
193 | } | |
bda208f8 | 194 | } |
bda208f8 DM |
195 | } |
196 | ||
197 | cleanup: | |
198 | /* restore the old cwd if we have it */ | |
199 | if(restore_cwd && chdir(cwd) != 0) { | |
ca43fdd9 | 200 | pm_printf(ALPM_LOG_ERROR, _("could not change directory to %s (%s)\n"), |
bda208f8 DM |
201 | cwd, strerror(errno)); |
202 | } | |
203 | ||
204 | if(ret == -1) { | |
205 | /* hack to let an user the time to cancel a download */ | |
206 | sleep(2); | |
207 | } | |
208 | free(destfile); | |
209 | free(tempfile); | |
210 | free(parsedcmd); | |
211 | ||
212 | return ret; | |
213 | } | |
214 | ||
215 | ||
216 | int config_set_arch(const char *arch) | |
217 | { | |
218 | if(strcmp(arch, "auto") == 0) { | |
219 | struct utsname un; | |
220 | uname(&un); | |
4fdcf50d | 221 | config->arch = strdup(un.machine); |
bda208f8 | 222 | } else { |
4fdcf50d | 223 | config->arch = strdup(arch); |
bda208f8 | 224 | } |
ca43fdd9 | 225 | pm_printf(ALPM_LOG_DEBUG, "config: arch: %s\n", config->arch); |
4fdcf50d | 226 | return 0; |
bda208f8 DM |
227 | } |
228 | ||
b03b06cf DM |
229 | /** |
230 | * Parse a signature verification level line. | |
231 | * @param values the list of parsed option values | |
232 | * @param storage location to store the derived signature level; any existing | |
233 | * value here is used as a starting point | |
234 | * @return 0 on success, 1 on any parsing error | |
235 | */ | |
236 | static int process_siglevel(alpm_list_t *values, alpm_siglevel_t *storage) | |
0fbdfd02 | 237 | { |
b03b06cf DM |
238 | alpm_siglevel_t level = *storage; |
239 | alpm_list_t *i; | |
240 | int ret = 0; | |
241 | ||
242 | /* Collapse the option names into a single bitmasked value */ | |
243 | for(i = values; i; i = alpm_list_next(i)) { | |
244 | const char *original = i->data, *value; | |
245 | int package = 0, database = 0; | |
246 | ||
247 | if (strncmp(original, "Package", strlen("Package")) == 0) { | |
248 | /* only packages are affected, don't flip flags for databases */ | |
249 | value = original + strlen("Package"); | |
250 | package = 1; | |
251 | } else if (strncmp(original, "Database", strlen("Database")) == 0) { | |
252 | /* only databases are affected, don't flip flags for packages */ | |
253 | value = original + strlen("Database"); | |
254 | database = 1; | |
255 | } else { | |
256 | /* no prefix, so anything found will affect both packages and dbs */ | |
257 | value = original; | |
258 | package = database = 1; | |
259 | } | |
260 | ||
261 | /* now parse out and store actual flag if it is valid */ | |
262 | if(strcmp(value, "Never") == 0) { | |
263 | if(package) { | |
264 | level &= ~ALPM_SIG_PACKAGE; | |
265 | } | |
266 | if(database) { | |
267 | level &= ~ALPM_SIG_DATABASE; | |
268 | } | |
269 | } else if(strcmp(value, "Optional") == 0) { | |
270 | if(package) { | |
271 | level |= ALPM_SIG_PACKAGE; | |
272 | level |= ALPM_SIG_PACKAGE_OPTIONAL; | |
273 | } | |
274 | if(database) { | |
275 | level |= ALPM_SIG_DATABASE; | |
276 | level |= ALPM_SIG_DATABASE_OPTIONAL; | |
277 | } | |
278 | } else if(strcmp(value, "Required") == 0) { | |
279 | if(package) { | |
280 | level |= ALPM_SIG_PACKAGE; | |
281 | level &= ~ALPM_SIG_PACKAGE_OPTIONAL; | |
282 | } | |
283 | if(database) { | |
284 | level |= ALPM_SIG_DATABASE; | |
285 | level &= ~ALPM_SIG_DATABASE_OPTIONAL; | |
286 | } | |
287 | } else if(strcmp(value, "TrustedOnly") == 0) { | |
288 | if(package) { | |
289 | level &= ~ALPM_SIG_PACKAGE_MARGINAL_OK; | |
290 | level &= ~ALPM_SIG_PACKAGE_UNKNOWN_OK; | |
291 | } | |
292 | if(database) { | |
293 | level &= ~ALPM_SIG_DATABASE_MARGINAL_OK; | |
294 | level &= ~ALPM_SIG_DATABASE_UNKNOWN_OK; | |
295 | } | |
296 | } else if(strcmp(value, "TrustAll") == 0) { | |
297 | if(package) { | |
298 | level |= ALPM_SIG_PACKAGE_MARGINAL_OK; | |
299 | level |= ALPM_SIG_PACKAGE_UNKNOWN_OK; | |
300 | } | |
301 | if(database) { | |
302 | level |= ALPM_SIG_DATABASE_MARGINAL_OK; | |
303 | level |= ALPM_SIG_DATABASE_UNKNOWN_OK; | |
304 | } | |
305 | } else { | |
306 | pm_printf(ALPM_LOG_ERROR, _("invalid value for 'SigLevel' : '%s'\n"), | |
307 | original); | |
308 | ret = 1; | |
309 | } | |
310 | level &= ~ALPM_SIG_USE_DEFAULT; | |
0fbdfd02 | 311 | } |
b03b06cf DM |
312 | |
313 | if(!ret) { | |
314 | *storage = level; | |
315 | } | |
316 | return ret; | |
0fbdfd02 | 317 | } |
bda208f8 | 318 | |
4fdcf50d DM |
319 | static int process_cleanmethods(alpm_list_t *values) { |
320 | alpm_list_t *i; | |
321 | for(i = values; i; i = alpm_list_next(i)) { | |
322 | const char *value = i->data; | |
323 | if(strcmp(value, "KeepInstalled") == 0) { | |
324 | config->cleanmethod |= PM_CLEAN_KEEPINST; | |
325 | } else if(strcmp(value, "KeepCurrent") == 0) { | |
326 | config->cleanmethod |= PM_CLEAN_KEEPCUR; | |
327 | } else { | |
ca43fdd9 | 328 | pm_printf(ALPM_LOG_ERROR, _("invalid value for 'CleanMethod' : '%s'\n"), |
4fdcf50d DM |
329 | value); |
330 | return 1; | |
331 | } | |
bda208f8 DM |
332 | } |
333 | return 0; | |
334 | } | |
335 | ||
336 | /** Add repeating options such as NoExtract, NoUpgrade, etc to libalpm | |
337 | * settings. Refactored out of the parseconfig code since all of them did | |
338 | * the exact same thing and duplicated code. | |
339 | * @param ptr a pointer to the start of the multiple options | |
340 | * @param option the string (friendly) name of the option, used for messages | |
4fdcf50d | 341 | * @param list the list to add the option to |
bda208f8 DM |
342 | */ |
343 | static void setrepeatingoption(char *ptr, const char *option, | |
4fdcf50d | 344 | alpm_list_t **list) |
bda208f8 DM |
345 | { |
346 | char *q; | |
347 | ||
348 | while((q = strchr(ptr, ' '))) { | |
349 | *q = '\0'; | |
4fdcf50d | 350 | *list = alpm_list_add(*list, strdup(ptr)); |
ca43fdd9 | 351 | pm_printf(ALPM_LOG_DEBUG, "config: %s: %s\n", option, ptr); |
bda208f8 DM |
352 | ptr = q; |
353 | ptr++; | |
354 | } | |
4fdcf50d | 355 | *list = alpm_list_add(*list, strdup(ptr)); |
ca43fdd9 | 356 | pm_printf(ALPM_LOG_DEBUG, "config: %s: %s\n", option, ptr); |
bda208f8 DM |
357 | } |
358 | ||
359 | static int _parse_options(const char *key, char *value, | |
360 | const char *file, int linenum) | |
361 | { | |
362 | if(value == NULL) { | |
363 | /* options without settings */ | |
364 | if(strcmp(key, "UseSyslog") == 0) { | |
4fdcf50d | 365 | config->usesyslog = 1; |
ca43fdd9 | 366 | pm_printf(ALPM_LOG_DEBUG, "config: usesyslog\n"); |
bda208f8 DM |
367 | } else if(strcmp(key, "ILoveCandy") == 0) { |
368 | config->chomp = 1; | |
ca43fdd9 | 369 | pm_printf(ALPM_LOG_DEBUG, "config: chomp\n"); |
bda208f8 DM |
370 | } else if(strcmp(key, "VerbosePkgLists") == 0) { |
371 | config->verbosepkglists = 1; | |
ca43fdd9 | 372 | pm_printf(ALPM_LOG_DEBUG, "config: verbosepkglists\n"); |
bda208f8 | 373 | } else if(strcmp(key, "UseDelta") == 0) { |
4fdcf50d | 374 | config->usedelta = 1; |
ca43fdd9 | 375 | pm_printf(ALPM_LOG_DEBUG, "config: usedelta\n"); |
bda208f8 DM |
376 | } else if(strcmp(key, "TotalDownload") == 0) { |
377 | config->totaldownload = 1; | |
ca43fdd9 | 378 | pm_printf(ALPM_LOG_DEBUG, "config: totaldownload\n"); |
bda208f8 | 379 | } else if(strcmp(key, "CheckSpace") == 0) { |
4fdcf50d | 380 | config->checkspace = 1; |
bda208f8 | 381 | } else { |
ca43fdd9 | 382 | pm_printf(ALPM_LOG_WARNING, |
bda208f8 DM |
383 | _("config file %s, line %d: directive '%s' in section '%s' not recognized.\n"), |
384 | file, linenum, key, "options"); | |
385 | } | |
386 | } else { | |
387 | /* options with settings */ | |
388 | if(strcmp(key, "NoUpgrade") == 0) { | |
4fdcf50d | 389 | setrepeatingoption(value, "NoUpgrade", &(config->noupgrade)); |
bda208f8 | 390 | } else if(strcmp(key, "NoExtract") == 0) { |
4fdcf50d | 391 | setrepeatingoption(value, "NoExtract", &(config->noextract)); |
bda208f8 | 392 | } else if(strcmp(key, "IgnorePkg") == 0) { |
4fdcf50d | 393 | setrepeatingoption(value, "IgnorePkg", &(config->ignorepkg)); |
bda208f8 | 394 | } else if(strcmp(key, "IgnoreGroup") == 0) { |
4fdcf50d | 395 | setrepeatingoption(value, "IgnoreGroup", &(config->ignoregrp)); |
bda208f8 | 396 | } else if(strcmp(key, "HoldPkg") == 0) { |
4fdcf50d | 397 | setrepeatingoption(value, "HoldPkg", &(config->holdpkg)); |
bda208f8 | 398 | } else if(strcmp(key, "SyncFirst") == 0) { |
4fdcf50d DM |
399 | setrepeatingoption(value, "SyncFirst", &(config->syncfirst)); |
400 | } else if(strcmp(key, "CacheDir") == 0) { | |
401 | setrepeatingoption(value, "CacheDir", &(config->cachedirs)); | |
bda208f8 | 402 | } else if(strcmp(key, "Architecture") == 0) { |
4fdcf50d | 403 | if(!config->arch) { |
bda208f8 DM |
404 | config_set_arch(value); |
405 | } | |
406 | } else if(strcmp(key, "DBPath") == 0) { | |
407 | /* don't overwrite a path specified on the command line */ | |
408 | if(!config->dbpath) { | |
409 | config->dbpath = strdup(value); | |
ca43fdd9 | 410 | pm_printf(ALPM_LOG_DEBUG, "config: dbpath: %s\n", value); |
bda208f8 | 411 | } |
bda208f8 DM |
412 | } else if(strcmp(key, "RootDir") == 0) { |
413 | /* don't overwrite a path specified on the command line */ | |
414 | if(!config->rootdir) { | |
415 | config->rootdir = strdup(value); | |
ca43fdd9 | 416 | pm_printf(ALPM_LOG_DEBUG, "config: rootdir: %s\n", value); |
bda208f8 DM |
417 | } |
418 | } else if(strcmp(key, "GPGDir") == 0) { | |
419 | if(!config->gpgdir) { | |
420 | config->gpgdir = strdup(value); | |
ca43fdd9 | 421 | pm_printf(ALPM_LOG_DEBUG, "config: gpgdir: %s\n", value); |
bda208f8 DM |
422 | } |
423 | } else if(strcmp(key, "LogFile") == 0) { | |
424 | if(!config->logfile) { | |
425 | config->logfile = strdup(value); | |
ca43fdd9 | 426 | pm_printf(ALPM_LOG_DEBUG, "config: logfile: %s\n", value); |
bda208f8 DM |
427 | } |
428 | } else if(strcmp(key, "XferCommand") == 0) { | |
429 | config->xfercommand = strdup(value); | |
ca43fdd9 | 430 | pm_printf(ALPM_LOG_DEBUG, "config: xfercommand: %s\n", value); |
bda208f8 | 431 | } else if(strcmp(key, "CleanMethod") == 0) { |
4fdcf50d DM |
432 | alpm_list_t *methods = NULL; |
433 | setrepeatingoption(value, "CleanMethod", &methods); | |
434 | if(process_cleanmethods(methods)) { | |
435 | FREELIST(methods); | |
436 | return 1; | |
437 | } | |
438 | FREELIST(methods); | |
b03b06cf DM |
439 | } else if(strcmp(key, "SigLevel") == 0) { |
440 | alpm_list_t *values = NULL; | |
441 | setrepeatingoption(value, "SigLevel", &values); | |
442 | if(process_siglevel(values, &(config->siglevel))) { | |
443 | FREELIST(values); | |
bda208f8 DM |
444 | return 1; |
445 | } | |
b03b06cf | 446 | FREELIST(values); |
bda208f8 | 447 | } else { |
ca43fdd9 | 448 | pm_printf(ALPM_LOG_WARNING, |
bda208f8 DM |
449 | _("config file %s, line %d: directive '%s' in section '%s' not recognized.\n"), |
450 | file, linenum, key, "options"); | |
451 | } | |
452 | ||
453 | } | |
454 | return 0; | |
455 | } | |
456 | ||
939d5a95 | 457 | static int _add_mirror(alpm_db_t *db, char *value) |
bda208f8 DM |
458 | { |
459 | const char *dbname = alpm_db_get_name(db); | |
460 | /* let's attempt a replacement for the current repo */ | |
461 | char *temp = strreplace(value, "$repo", dbname); | |
462 | /* let's attempt a replacement for the arch */ | |
4fdcf50d | 463 | const char *arch = config->arch; |
bda208f8 DM |
464 | char *server; |
465 | if(arch) { | |
466 | server = strreplace(temp, "$arch", arch); | |
467 | free(temp); | |
468 | } else { | |
469 | if(strstr(temp, "$arch")) { | |
470 | free(temp); | |
ca43fdd9 | 471 | pm_printf(ALPM_LOG_ERROR, _("The mirror '%s' contains the $arch" |
bda208f8 DM |
472 | " variable, but no Architecture is defined.\n"), value); |
473 | return 1; | |
474 | } | |
475 | server = temp; | |
476 | } | |
477 | ||
42ab639b | 478 | if(alpm_db_add_server(db, server) != 0) { |
bda208f8 | 479 | /* pm_errno is set by alpm_db_setserver */ |
ca43fdd9 | 480 | pm_printf(ALPM_LOG_ERROR, _("could not add server URL to database '%s': %s (%s)\n"), |
e2aa9526 | 481 | dbname, server, alpm_strerror(alpm_errno(config->handle))); |
bda208f8 DM |
482 | free(server); |
483 | return 1; | |
484 | } | |
485 | ||
486 | free(server); | |
487 | return 0; | |
488 | } | |
489 | ||
4fdcf50d | 490 | /** Sets up libalpm global stuff in one go. Called after the command line |
bda208f8 DM |
491 | * and inital config file parsing. Once this is complete, we can see if any |
492 | * paths were defined. If a rootdir was defined and nothing else, we want all | |
493 | * of our paths to live under the rootdir that was specified. Safe to call | |
494 | * multiple times (will only do anything the first time). | |
495 | */ | |
4fdcf50d | 496 | static int setup_libalpm(void) |
bda208f8 | 497 | { |
73c74355 | 498 | int ret = 0; |
ddad4009 | 499 | enum _alpm_errno_t err; |
64c1cf79 | 500 | alpm_handle_t *handle; |
4fdcf50d | 501 | |
ca43fdd9 | 502 | pm_printf(ALPM_LOG_DEBUG, "setup_libalpm called\n"); |
73c74355 | 503 | |
73c74355 DM |
504 | /* Configure root path first. If it is set and dbpath/logfile were not |
505 | * set, then set those as well to reside under the root. */ | |
506 | if(config->rootdir) { | |
507 | char path[PATH_MAX]; | |
73c74355 | 508 | if(!config->dbpath) { |
4fdcf50d | 509 | snprintf(path, PATH_MAX, "%s/%s", config->rootdir, DBPATH + 1); |
73c74355 | 510 | config->dbpath = strdup(path); |
bda208f8 | 511 | } |
73c74355 | 512 | if(!config->logfile) { |
4fdcf50d | 513 | snprintf(path, PATH_MAX, "%s/%s", config->rootdir, LOGFILE + 1); |
73c74355 | 514 | config->logfile = strdup(path); |
bda208f8 | 515 | } |
4fdcf50d DM |
516 | } else { |
517 | config->rootdir = strdup(ROOTDIR); | |
518 | if(!config->dbpath) { | |
519 | config->dbpath = strdup(DBPATH); | |
bda208f8 | 520 | } |
73c74355 | 521 | } |
4fdcf50d DM |
522 | |
523 | /* initialize library */ | |
17a6ac56 DM |
524 | handle = alpm_initialize(config->rootdir, config->dbpath, &err); |
525 | if(!handle) { | |
ca43fdd9 | 526 | pm_printf(ALPM_LOG_ERROR, _("failed to initialize alpm library (%s)\n"), |
4fdcf50d | 527 | alpm_strerror(err)); |
afc96f2a | 528 | if(err == ALPM_ERR_DB_VERSION) { |
ca43fdd9 | 529 | pm_printf(ALPM_LOG_ERROR, _(" try running pacman-db-upgrade\n")); |
1150d9e1 | 530 | } |
4fdcf50d DM |
531 | return -1; |
532 | } | |
17a6ac56 | 533 | config->handle = handle; |
4fdcf50d | 534 | |
17a6ac56 DM |
535 | alpm_option_set_logcb(handle, cb_log); |
536 | alpm_option_set_dlcb(handle, cb_dl_progress); | |
37da18ae DM |
537 | alpm_option_set_eventcb(handle, cb_trans_evt); |
538 | alpm_option_set_convcb(handle, cb_trans_conv); | |
539 | alpm_option_set_progresscb(handle, cb_trans_progress); | |
4fdcf50d DM |
540 | |
541 | config->logfile = config->logfile ? config->logfile : strdup(LOGFILE); | |
17a6ac56 | 542 | ret = alpm_option_set_logfile(handle, config->logfile); |
4fdcf50d | 543 | if(ret != 0) { |
ca43fdd9 | 544 | pm_printf(ALPM_LOG_ERROR, _("problem setting logfile '%s' (%s)\n"), |
db3b86e7 | 545 | config->logfile, alpm_strerror(alpm_errno(handle))); |
4fdcf50d | 546 | return ret; |
73c74355 | 547 | } |
bda208f8 | 548 | |
73c74355 DM |
549 | /* Set GnuPG's home directory. This is not relative to rootdir, even if |
550 | * rootdir is defined. Reasoning: gpgdir contains configuration data. */ | |
4fdcf50d | 551 | config->gpgdir = config->gpgdir ? config->gpgdir : strdup(GPGDIR); |
1cd6515a | 552 | ret = alpm_option_set_gpgdir(handle, config->gpgdir); |
4fdcf50d | 553 | if(ret != 0) { |
ca43fdd9 | 554 | pm_printf(ALPM_LOG_ERROR, _("problem setting gpgdir '%s' (%s)\n"), |
db3b86e7 | 555 | config->gpgdir, alpm_strerror(alpm_errno(handle))); |
4fdcf50d | 556 | return ret; |
bda208f8 | 557 | } |
73c74355 DM |
558 | |
559 | /* add a default cachedir if one wasn't specified */ | |
4fdcf50d | 560 | if(config->cachedirs == NULL) { |
17a6ac56 | 561 | alpm_option_add_cachedir(handle, CACHEDIR); |
4fdcf50d | 562 | } else { |
17a6ac56 | 563 | alpm_option_set_cachedirs(handle, config->cachedirs); |
4fdcf50d DM |
564 | } |
565 | ||
7af0ab1c DM |
566 | if(config->siglevel != ALPM_SIG_USE_DEFAULT) { |
567 | alpm_option_set_default_siglevel(handle, config->siglevel); | |
4fdcf50d | 568 | } |
a8a43950 DM |
569 | /* retrieve the set or default siglevel from the backend at this point; |
570 | * this way all future DB settings base their setting off this value */ | |
571 | config->siglevel = alpm_option_get_default_siglevel(handle); | |
4fdcf50d DM |
572 | |
573 | if(config->xfercommand) { | |
17a6ac56 | 574 | alpm_option_set_fetchcb(handle, download_with_xfercommand); |
73c74355 | 575 | } |
4fdcf50d DM |
576 | |
577 | if(config->totaldownload) { | |
17a6ac56 | 578 | alpm_option_set_totaldlcb(handle, cb_dl_total); |
4fdcf50d DM |
579 | } |
580 | ||
17a6ac56 DM |
581 | alpm_option_set_arch(handle, config->arch); |
582 | alpm_option_set_checkspace(handle, config->checkspace); | |
583 | alpm_option_set_usesyslog(handle, config->usesyslog); | |
584 | alpm_option_set_usedelta(handle, config->usedelta); | |
4fdcf50d | 585 | |
17a6ac56 | 586 | alpm_option_set_ignorepkgs(handle, config->ignorepkg); |
f1bb56ce | 587 | alpm_option_set_ignoregroups(handle, config->ignoregrp); |
17a6ac56 DM |
588 | alpm_option_set_noupgrades(handle, config->noupgrade); |
589 | alpm_option_set_noextracts(handle, config->noextract); | |
4fdcf50d | 590 | |
73c74355 | 591 | return 0; |
bda208f8 DM |
592 | } |
593 | ||
a7755309 DM |
594 | /** |
595 | * Allows parsing in advance of an entire config section before we start | |
596 | * calling library methods. | |
597 | */ | |
598 | struct section_t { | |
599 | /* useful for all sections */ | |
600 | char *name; | |
601 | int is_options; | |
602 | /* db section option gathering */ | |
7af0ab1c | 603 | alpm_siglevel_t siglevel; |
a7755309 DM |
604 | alpm_list_t *servers; |
605 | }; | |
606 | ||
607 | /** | |
608 | * Wrap up a section once we have reached the end of it. This should be called | |
609 | * when a subsequent section is encountered, or when we have reached the end of | |
610 | * the root config file. Once called, all existing saved config pieces on the | |
611 | * section struct are freed. | |
612 | * @param section the current parsed and saved section data | |
613 | * @param parse_options whether we are parsing options or repo data | |
614 | * @return 0 on success, 1 on failure | |
615 | */ | |
616 | static int finish_section(struct section_t *section, int parse_options) | |
617 | { | |
618 | int ret = 0; | |
619 | alpm_list_t *i; | |
939d5a95 | 620 | alpm_db_t *db; |
a7755309 | 621 | |
ca43fdd9 | 622 | pm_printf(ALPM_LOG_DEBUG, "config: finish section '%s'\n", section->name); |
a7755309 DM |
623 | |
624 | /* parsing options (or nothing)- nothing to do except free the pieces */ | |
625 | if(!section->name || parse_options || section->is_options) { | |
626 | goto cleanup; | |
627 | } | |
628 | ||
629 | /* if we are not looking at options sections only, register a db */ | |
7af0ab1c | 630 | db = alpm_db_register_sync(config->handle, section->name, section->siglevel); |
a7755309 | 631 | if(db == NULL) { |
ca43fdd9 | 632 | pm_printf(ALPM_LOG_ERROR, _("could not register '%s' database (%s)\n"), |
a7755309 DM |
633 | section->name, alpm_strerror(alpm_errno(config->handle))); |
634 | ret = 1; | |
635 | goto cleanup; | |
636 | } | |
637 | ||
a7755309 DM |
638 | for(i = section->servers; i; i = alpm_list_next(i)) { |
639 | char *value = alpm_list_getdata(i); | |
640 | if(_add_mirror(db, value) != 0) { | |
ca43fdd9 | 641 | pm_printf(ALPM_LOG_ERROR, |
a7755309 DM |
642 | _("could not add mirror '%s' to database '%s' (%s)\n"), |
643 | value, section->name, alpm_strerror(alpm_errno(config->handle))); | |
644 | ret = 1; | |
645 | goto cleanup; | |
646 | } | |
647 | free(value); | |
648 | } | |
649 | ||
650 | cleanup: | |
651 | alpm_list_free(section->servers); | |
652 | section->servers = NULL; | |
7af0ab1c | 653 | section->siglevel = ALPM_SIG_USE_DEFAULT; |
a7755309 DM |
654 | free(section->name); |
655 | section->name = NULL; | |
656 | return ret; | |
657 | } | |
bda208f8 | 658 | |
c730ca59 DM |
659 | /** The "real" parseconfig. Each "Include" directive will recall this method so |
660 | * recursion and stack depth are limited to 10 levels. The publicly visible | |
661 | * parseconfig calls this with a NULL section argument so we can recall from | |
662 | * within ourself on an include. | |
663 | * @param file path to the config file | |
a7755309 | 664 | * @param section the current active section |
c730ca59 DM |
665 | * @param parse_options whether to parse and call methods for the options |
666 | * section; if 0, parse and call methods for the repos sections | |
667 | * @param depth the current recursion depth | |
a7755309 DM |
668 | * @return 0 on success, 1 on failure |
669 | */ | |
670 | static int _parseconfig(const char *file, struct section_t *section, | |
c730ca59 | 671 | int parse_options, int depth) |
bda208f8 DM |
672 | { |
673 | FILE *fp = NULL; | |
e826c143 | 674 | char line[PATH_MAX]; |
bda208f8 | 675 | int linenum = 0; |
bda208f8 | 676 | int ret = 0; |
c730ca59 DM |
677 | const int max_depth = 10; |
678 | ||
679 | if(depth >= max_depth) { | |
ca43fdd9 | 680 | pm_printf(ALPM_LOG_ERROR, |
c730ca59 DM |
681 | _("config parsing exceeded max recursion depth of %d.\n"), max_depth); |
682 | ret = 1; | |
683 | goto cleanup; | |
684 | } | |
bda208f8 | 685 | |
ca43fdd9 | 686 | pm_printf(ALPM_LOG_DEBUG, "config: attempting to read file %s\n", file); |
bda208f8 DM |
687 | fp = fopen(file, "r"); |
688 | if(fp == NULL) { | |
ca43fdd9 | 689 | pm_printf(ALPM_LOG_ERROR, _("config file %s could not be read.\n"), file); |
c730ca59 DM |
690 | ret = 1; |
691 | goto cleanup; | |
bda208f8 DM |
692 | } |
693 | ||
bda208f8 | 694 | while(fgets(line, PATH_MAX, fp)) { |
c730ca59 DM |
695 | char *key, *value, *ptr; |
696 | size_t line_len; | |
73c74355 | 697 | |
bda208f8 DM |
698 | linenum++; |
699 | strtrim(line); | |
c730ca59 | 700 | line_len = strlen(line); |
bda208f8 DM |
701 | |
702 | /* ignore whole line and end of line comments */ | |
c730ca59 | 703 | if(line_len == 0 || line[0] == '#') { |
bda208f8 DM |
704 | continue; |
705 | } | |
706 | if((ptr = strchr(line, '#'))) { | |
707 | *ptr = '\0'; | |
708 | } | |
709 | ||
c730ca59 | 710 | if(line[0] == '[' && line[line_len - 1] == ']') { |
dd95c96b | 711 | char *name; |
c730ca59 DM |
712 | /* only possibility here is a line == '[]' */ |
713 | if(line_len <= 2) { | |
ca43fdd9 | 714 | pm_printf(ALPM_LOG_ERROR, _("config file %s, line %d: bad section name.\n"), |
bda208f8 DM |
715 | file, linenum); |
716 | ret = 1; | |
717 | goto cleanup; | |
718 | } | |
c730ca59 DM |
719 | /* new config section, skip the '[' */ |
720 | name = strdup(line + 1); | |
721 | name[line_len - 2] = '\0'; | |
a7755309 DM |
722 | /* we're at a new section; perform any post-actions for the prior */ |
723 | if(finish_section(section, parse_options)) { | |
724 | ret = 1; | |
725 | goto cleanup; | |
dd95c96b | 726 | } |
ca43fdd9 | 727 | pm_printf(ALPM_LOG_DEBUG, "config: new section '%s'\n", name); |
a7755309 DM |
728 | section->name = name; |
729 | section->is_options = (strcmp(name, "options") == 0); | |
bda208f8 DM |
730 | continue; |
731 | } | |
732 | ||
733 | /* directive */ | |
bda208f8 DM |
734 | /* strsep modifies the 'line' string: 'key \0 value' */ |
735 | key = line; | |
736 | value = line; | |
737 | strsep(&value, "="); | |
738 | strtrim(key); | |
739 | strtrim(value); | |
740 | ||
741 | if(key == NULL) { | |
ca43fdd9 | 742 | pm_printf(ALPM_LOG_ERROR, _("config file %s, line %d: syntax error in config file- missing key.\n"), |
bda208f8 DM |
743 | file, linenum); |
744 | ret = 1; | |
745 | goto cleanup; | |
746 | } | |
747 | /* For each directive, compare to the camelcase string. */ | |
a7755309 | 748 | if(section->name == NULL) { |
ca43fdd9 | 749 | pm_printf(ALPM_LOG_ERROR, _("config file %s, line %d: All directives must belong to a section.\n"), |
bda208f8 DM |
750 | file, linenum); |
751 | ret = 1; | |
752 | goto cleanup; | |
753 | } | |
754 | /* Include is allowed in both options and repo sections */ | |
755 | if(strcmp(key, "Include") == 0) { | |
73c74355 DM |
756 | glob_t globbuf; |
757 | int globret; | |
758 | size_t gindex; | |
759 | ||
bda208f8 | 760 | if(value == NULL) { |
ca43fdd9 | 761 | pm_printf(ALPM_LOG_ERROR, _("config file %s, line %d: directive '%s' needs a value\n"), |
bda208f8 DM |
762 | file, linenum, key); |
763 | ret = 1; | |
764 | goto cleanup; | |
765 | } | |
766 | /* Ignore include failures... assume non-critical */ | |
bda208f8 DM |
767 | globret = glob(value, GLOB_NOCHECK, NULL, &globbuf); |
768 | switch(globret) { | |
769 | case GLOB_NOSPACE: | |
ca43fdd9 | 770 | pm_printf(ALPM_LOG_DEBUG, |
bda208f8 DM |
771 | "config file %s, line %d: include globbing out of space\n", |
772 | file, linenum); | |
773 | break; | |
774 | case GLOB_ABORTED: | |
ca43fdd9 | 775 | pm_printf(ALPM_LOG_DEBUG, |
bda208f8 DM |
776 | "config file %s, line %d: include globbing read error for %s\n", |
777 | file, linenum, value); | |
778 | break; | |
779 | case GLOB_NOMATCH: | |
ca43fdd9 | 780 | pm_printf(ALPM_LOG_DEBUG, |
bda208f8 DM |
781 | "config file %s, line %d: no include found for %s\n", |
782 | file, linenum, value); | |
783 | break; | |
784 | default: | |
73c74355 | 785 | for(gindex = 0; gindex < globbuf.gl_pathc; gindex++) { |
ca43fdd9 | 786 | pm_printf(ALPM_LOG_DEBUG, "config file %s, line %d: including %s\n", |
bda208f8 | 787 | file, linenum, globbuf.gl_pathv[gindex]); |
a7755309 | 788 | _parseconfig(globbuf.gl_pathv[gindex], section, parse_options, depth + 1); |
bda208f8 DM |
789 | } |
790 | break; | |
791 | } | |
792 | globfree(&globbuf); | |
793 | continue; | |
794 | } | |
a7755309 | 795 | if(parse_options && section->is_options) { |
bda208f8 DM |
796 | /* we are either in options ... */ |
797 | if((ret = _parse_options(key, value, file, linenum)) != 0) { | |
798 | goto cleanup; | |
799 | } | |
a7755309 | 800 | } else if (!parse_options && !section->is_options) { |
bda208f8 DM |
801 | /* ... or in a repo section */ |
802 | if(strcmp(key, "Server") == 0) { | |
803 | if(value == NULL) { | |
ca43fdd9 | 804 | pm_printf(ALPM_LOG_ERROR, _("config file %s, line %d: directive '%s' needs a value\n"), |
bda208f8 DM |
805 | file, linenum, key); |
806 | ret = 1; | |
807 | goto cleanup; | |
808 | } | |
a7755309 | 809 | section->servers = alpm_list_add(section->servers, strdup(value)); |
b03b06cf DM |
810 | } else if(strcmp(key, "SigLevel") == 0) { |
811 | alpm_list_t *values = NULL; | |
812 | setrepeatingoption(value, "SigLevel", &values); | |
813 | if(values) { | |
814 | if(section->siglevel == ALPM_SIG_USE_DEFAULT) { | |
815 | section->siglevel = config->siglevel; | |
816 | } | |
817 | if(process_siglevel(values, &(section->siglevel))) { | |
818 | FREELIST(values); | |
819 | ret = 1; | |
820 | goto cleanup; | |
821 | } | |
822 | FREELIST(values); | |
bda208f8 | 823 | } |
bda208f8 | 824 | } else { |
ca43fdd9 | 825 | pm_printf(ALPM_LOG_WARNING, |
bda208f8 | 826 | _("config file %s, line %d: directive '%s' in section '%s' not recognized.\n"), |
a7755309 | 827 | file, linenum, key, section->name); |
bda208f8 DM |
828 | } |
829 | } | |
bda208f8 DM |
830 | } |
831 | ||
a7755309 DM |
832 | if(depth == 0) { |
833 | ret = finish_section(section, parse_options); | |
834 | } | |
835 | ||
bda208f8 | 836 | cleanup: |
36474af4 FP |
837 | if (fp) { |
838 | fclose(fp); | |
839 | } | |
ca43fdd9 | 840 | pm_printf(ALPM_LOG_DEBUG, "config: finished parsing %s\n", file); |
bda208f8 DM |
841 | return ret; |
842 | } | |
843 | ||
844 | /** Parse a configuration file. | |
c730ca59 | 845 | * @param file path to the config file |
bda208f8 DM |
846 | * @return 0 on success, non-zero on error |
847 | */ | |
848 | int parseconfig(const char *file) | |
849 | { | |
73c74355 | 850 | int ret; |
a7755309 DM |
851 | struct section_t section; |
852 | memset(§ion, 0, sizeof(struct section_t)); | |
7af0ab1c | 853 | section.siglevel = ALPM_SIG_USE_DEFAULT; |
73c74355 DM |
854 | /* the config parse is a two-pass affair. We first parse the entire thing for |
855 | * the [options] section so we can get all default and path options set. | |
856 | * Next, we go back and parse everything but [options]. */ | |
857 | ||
bda208f8 | 858 | /* call the real parseconfig function with a null section & db argument */ |
ca43fdd9 | 859 | pm_printf(ALPM_LOG_DEBUG, "parseconfig: options pass\n"); |
a7755309 | 860 | if((ret = _parseconfig(file, §ion, 1, 0))) { |
73c74355 DM |
861 | return ret; |
862 | } | |
4fdcf50d | 863 | if((ret = setup_libalpm())) { |
73c74355 DM |
864 | return ret; |
865 | } | |
866 | /* second pass, repo section parsing */ | |
ca43fdd9 | 867 | pm_printf(ALPM_LOG_DEBUG, "parseconfig: repo pass\n"); |
a7755309 | 868 | return _parseconfig(file, §ion, 0, 0); |
bda208f8 DM |
869 | } |
870 | ||
d04baaba | 871 | /* vim: set ts=2 sw=2 noet: */ |