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