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