API: change 'signaturedir' to 'gpgdir'
[pacman-ng.git] / lib / libalpm / handle.c
bloba516003ddd5c507e05e181b14fd3e9ece03c96cf
1 /*
2 * handle.c
4 * Copyright (c) 2006-2011 Pacman Development Team <pacman-dev@archlinux.org>
5 * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
6 * Copyright (c) 2005 by Aurelien Foret <orelien@chez.com>
7 * Copyright (c) 2005, 2006 by Miklos Vajna <vmiklos@frugalware.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "config.h"
25 #include <stdlib.h>
26 #include <string.h>
27 #include <limits.h>
28 #include <sys/types.h>
29 #include <syslog.h>
30 #include <sys/stat.h>
32 /* libalpm */
33 #include "handle.h"
34 #include "alpm_list.h"
35 #include "util.h"
36 #include "log.h"
37 #include "trans.h"
38 #include "alpm.h"
40 pmhandle_t *_alpm_handle_new()
42 pmhandle_t *handle;
44 CALLOC(handle, 1, sizeof(pmhandle_t), return NULL);
46 handle->sigverify = PM_PGP_VERIFY_OPTIONAL;
48 return handle;
51 void _alpm_handle_free(pmhandle_t *handle)
53 if(handle == NULL) {
54 return;
57 /* close logfile */
58 if(handle->logstream) {
59 fclose(handle->logstream);
60 handle->logstream= NULL;
62 if(handle->usesyslog) {
63 handle->usesyslog = 0;
64 closelog();
67 #ifdef HAVE_LIBCURL
68 /* release curl handle */
69 curl_easy_cleanup(handle->curl);
70 #endif
72 /* free memory */
73 _alpm_trans_free(handle->trans);
74 FREE(handle->root);
75 FREE(handle->dbpath);
76 FREELIST(handle->cachedirs);
77 FREE(handle->logfile);
78 FREE(handle->lockfile);
79 FREE(handle->arch);
80 FREE(handle->gpgdir);
81 FREELIST(handle->dbs_sync);
82 FREELIST(handle->noupgrade);
83 FREELIST(handle->noextract);
84 FREELIST(handle->ignorepkg);
85 FREELIST(handle->ignoregrp);
86 FREE(handle);
89 alpm_cb_log SYMEXPORT alpm_option_get_logcb(pmhandle_t *handle)
91 CHECK_HANDLE(handle, return NULL);
92 return handle->logcb;
95 alpm_cb_download SYMEXPORT alpm_option_get_dlcb(pmhandle_t *handle)
97 CHECK_HANDLE(handle, return NULL);
98 return handle->dlcb;
101 alpm_cb_fetch SYMEXPORT alpm_option_get_fetchcb(pmhandle_t *handle)
103 CHECK_HANDLE(handle, return NULL);
104 return handle->fetchcb;
107 alpm_cb_totaldl SYMEXPORT alpm_option_get_totaldlcb(pmhandle_t *handle)
109 CHECK_HANDLE(handle, return NULL);
110 return handle->totaldlcb;
113 const char SYMEXPORT *alpm_option_get_root(pmhandle_t *handle)
115 CHECK_HANDLE(handle, return NULL);
116 return handle->root;
119 const char SYMEXPORT *alpm_option_get_dbpath(pmhandle_t *handle)
121 CHECK_HANDLE(handle, return NULL);
122 return handle->dbpath;
125 alpm_list_t SYMEXPORT *alpm_option_get_cachedirs(pmhandle_t *handle)
127 CHECK_HANDLE(handle, return NULL);
128 return handle->cachedirs;
131 const char SYMEXPORT *alpm_option_get_logfile(pmhandle_t *handle)
133 CHECK_HANDLE(handle, return NULL);
134 return handle->logfile;
137 const char SYMEXPORT *alpm_option_get_lockfile(pmhandle_t *handle)
139 CHECK_HANDLE(handle, return NULL);
140 return handle->lockfile;
143 const char SYMEXPORT *alpm_option_get_gpgdir(pmhandle_t *handle)
145 CHECK_HANDLE(handle, return NULL);
146 return handle->gpgdir;
149 int SYMEXPORT alpm_option_get_usesyslog(pmhandle_t *handle)
151 CHECK_HANDLE(handle, return -1);
152 return handle->usesyslog;
155 alpm_list_t SYMEXPORT *alpm_option_get_noupgrades(pmhandle_t *handle)
157 CHECK_HANDLE(handle, return NULL);
158 return handle->noupgrade;
161 alpm_list_t SYMEXPORT *alpm_option_get_noextracts(pmhandle_t *handle)
163 CHECK_HANDLE(handle, return NULL);
164 return handle->noextract;
167 alpm_list_t SYMEXPORT *alpm_option_get_ignorepkgs(pmhandle_t *handle)
169 CHECK_HANDLE(handle, return NULL);
170 return handle->ignorepkg;
173 alpm_list_t SYMEXPORT *alpm_option_get_ignoregrps(pmhandle_t *handle)
175 CHECK_HANDLE(handle, return NULL);
176 return handle->ignoregrp;
179 const char SYMEXPORT *alpm_option_get_arch(pmhandle_t *handle)
181 CHECK_HANDLE(handle, return NULL);
182 return handle->arch;
185 int SYMEXPORT alpm_option_get_usedelta(pmhandle_t *handle)
187 CHECK_HANDLE(handle, return -1);
188 return handle->usedelta;
191 int SYMEXPORT alpm_option_get_checkspace(pmhandle_t *handle)
193 CHECK_HANDLE(handle, return -1);
194 return handle->checkspace;
197 pmdb_t SYMEXPORT *alpm_option_get_localdb(pmhandle_t *handle)
199 CHECK_HANDLE(handle, return NULL);
200 return handle->db_local;
203 alpm_list_t SYMEXPORT *alpm_option_get_syncdbs(pmhandle_t *handle)
205 CHECK_HANDLE(handle, return NULL);
206 return handle->dbs_sync;
209 int SYMEXPORT alpm_option_set_logcb(pmhandle_t *handle, alpm_cb_log cb)
211 CHECK_HANDLE(handle, return -1);
212 handle->logcb = cb;
213 return 0;
216 int SYMEXPORT alpm_option_set_dlcb(pmhandle_t *handle, alpm_cb_download cb)
218 CHECK_HANDLE(handle, return -1);
219 handle->dlcb = cb;
220 return 0;
223 int SYMEXPORT alpm_option_set_fetchcb(pmhandle_t *handle, alpm_cb_fetch cb)
225 CHECK_HANDLE(handle, return -1);
226 handle->fetchcb = cb;
227 return 0;
230 int SYMEXPORT alpm_option_set_totaldlcb(pmhandle_t *handle, alpm_cb_totaldl cb)
232 CHECK_HANDLE(handle, return -1);
233 handle->totaldlcb = cb;
234 return 0;
237 static char *canonicalize_path(const char *path) {
238 char *new_path;
239 size_t len;
241 /* verify path ends in a '/' */
242 len = strlen(path);
243 if(path[len - 1] != '/') {
244 len += 1;
246 new_path = calloc(len + 1, sizeof(char));
247 strncpy(new_path, path, len);
248 new_path[len - 1] = '/';
249 return new_path;
252 enum _pmerrno_t _alpm_set_directory_option(const char *value,
253 char **storage, int must_exist)
255 struct stat st;
256 char *real = NULL;
257 const char *path;
259 path = value;
260 if(!path) {
261 return PM_ERR_WRONG_ARGS;
263 if(must_exist) {
264 if(stat(path, &st) == -1 || !S_ISDIR(st.st_mode)) {
265 return PM_ERR_NOT_A_DIR;
267 real = calloc(PATH_MAX, sizeof(char));
268 if(!realpath(path, real)) {
269 free(real);
270 return PM_ERR_NOT_A_DIR;
272 path = real;
275 if(*storage) {
276 FREE(*storage);
278 *storage = canonicalize_path(path);
279 free(real);
280 return 0;
283 int SYMEXPORT alpm_option_add_cachedir(pmhandle_t *handle, const char *cachedir)
285 char *newcachedir;
287 CHECK_HANDLE(handle, return -1);
288 if(!cachedir) {
289 handle->pm_errno = PM_ERR_WRONG_ARGS;
290 return -1;
292 /* don't stat the cachedir yet, as it may not even be needed. we can
293 * fail later if it is needed and the path is invalid. */
295 newcachedir = canonicalize_path(cachedir);
296 handle->cachedirs = alpm_list_add(handle->cachedirs, newcachedir);
297 _alpm_log(handle, PM_LOG_DEBUG, "option 'cachedir' = %s\n", newcachedir);
298 return 0;
301 int SYMEXPORT alpm_option_set_cachedirs(pmhandle_t *handle, alpm_list_t *cachedirs)
303 alpm_list_t *i;
304 CHECK_HANDLE(handle, return -1);
305 if(handle->cachedirs) {
306 FREELIST(handle->cachedirs);
308 for(i = cachedirs; i; i = i->next) {
309 int ret = alpm_option_add_cachedir(handle, i->data);
310 if(ret) {
311 return ret;
314 return 0;
317 int SYMEXPORT alpm_option_remove_cachedir(pmhandle_t *handle, const char *cachedir)
319 char *vdata = NULL;
320 char *newcachedir;
321 size_t cachedirlen;
322 CHECK_HANDLE(handle, return -1);
323 /* verify cachedir ends in a '/' */
324 cachedirlen = strlen(cachedir);
325 if(cachedir[cachedirlen-1] != '/') {
326 cachedirlen += 1;
328 newcachedir = calloc(cachedirlen + 1, sizeof(char));
329 strncpy(newcachedir, cachedir, cachedirlen);
330 newcachedir[cachedirlen-1] = '/';
331 handle->cachedirs = alpm_list_remove_str(handle->cachedirs, newcachedir, &vdata);
332 FREE(newcachedir);
333 if(vdata != NULL) {
334 FREE(vdata);
335 return 1;
337 return 0;
340 int SYMEXPORT alpm_option_set_logfile(pmhandle_t *handle, const char *logfile)
342 char *oldlogfile = handle->logfile;
344 CHECK_HANDLE(handle, return -1);
345 if(!logfile) {
346 handle->pm_errno = PM_ERR_WRONG_ARGS;
347 return -1;
350 handle->logfile = strdup(logfile);
352 /* free the old logfile path string, and close the stream so logaction
353 * will reopen a new stream on the new logfile */
354 if(oldlogfile) {
355 FREE(oldlogfile);
357 if(handle->logstream) {
358 fclose(handle->logstream);
359 handle->logstream = NULL;
361 _alpm_log(handle, PM_LOG_DEBUG, "option 'logfile' = %s\n", handle->logfile);
362 return 0;
365 int SYMEXPORT alpm_option_set_gpgdir(pmhandle_t *handle, const char *gpgdir)
367 CHECK_HANDLE(handle, return -1);
368 if(!gpgdir) {
369 handle->pm_errno = PM_ERR_WRONG_ARGS;
370 return -1;
373 if(handle->gpgdir) {
374 FREE(handle->gpgdir);
376 handle->gpgdir = strdup(gpgdir);
378 _alpm_log(handle, PM_LOG_DEBUG, "option 'gpgdir' = %s\n", handle->gpgdir);
379 return 0;
382 int SYMEXPORT alpm_option_set_usesyslog(pmhandle_t *handle, int usesyslog)
384 CHECK_HANDLE(handle, return -1);
385 handle->usesyslog = usesyslog;
386 return 0;
389 int SYMEXPORT alpm_option_add_noupgrade(pmhandle_t *handle, const char *pkg)
391 CHECK_HANDLE(handle, return -1);
392 handle->noupgrade = alpm_list_add(handle->noupgrade, strdup(pkg));
393 return 0;
396 int SYMEXPORT alpm_option_set_noupgrades(pmhandle_t *handle, alpm_list_t *noupgrade)
398 CHECK_HANDLE(handle, return -1);
399 if(handle->noupgrade) FREELIST(handle->noupgrade);
400 handle->noupgrade = alpm_list_strdup(noupgrade);
401 return 0;
404 int SYMEXPORT alpm_option_remove_noupgrade(pmhandle_t *handle, const char *pkg)
406 char *vdata = NULL;
407 CHECK_HANDLE(handle, return -1);
408 handle->noupgrade = alpm_list_remove_str(handle->noupgrade, pkg, &vdata);
409 if(vdata != NULL) {
410 FREE(vdata);
411 return 1;
413 return 0;
416 int SYMEXPORT alpm_option_add_noextract(pmhandle_t *handle, const char *pkg)
418 CHECK_HANDLE(handle, return -1);
419 handle->noextract = alpm_list_add(handle->noextract, strdup(pkg));
420 return 0;
423 int SYMEXPORT alpm_option_set_noextracts(pmhandle_t *handle, alpm_list_t *noextract)
425 CHECK_HANDLE(handle, return -1);
426 if(handle->noextract) FREELIST(handle->noextract);
427 handle->noextract = alpm_list_strdup(noextract);
428 return 0;
431 int SYMEXPORT alpm_option_remove_noextract(pmhandle_t *handle, const char *pkg)
433 char *vdata = NULL;
434 CHECK_HANDLE(handle, return -1);
435 handle->noextract = alpm_list_remove_str(handle->noextract, pkg, &vdata);
436 if(vdata != NULL) {
437 FREE(vdata);
438 return 1;
440 return 0;
443 int SYMEXPORT alpm_option_add_ignorepkg(pmhandle_t *handle, const char *pkg)
445 CHECK_HANDLE(handle, return -1);
446 handle->ignorepkg = alpm_list_add(handle->ignorepkg, strdup(pkg));
447 return 0;
450 int SYMEXPORT alpm_option_set_ignorepkgs(pmhandle_t *handle, alpm_list_t *ignorepkgs)
452 CHECK_HANDLE(handle, return -1);
453 if(handle->ignorepkg) FREELIST(handle->ignorepkg);
454 handle->ignorepkg = alpm_list_strdup(ignorepkgs);
455 return 0;
458 int SYMEXPORT alpm_option_remove_ignorepkg(pmhandle_t *handle, const char *pkg)
460 char *vdata = NULL;
461 CHECK_HANDLE(handle, return -1);
462 handle->ignorepkg = alpm_list_remove_str(handle->ignorepkg, pkg, &vdata);
463 if(vdata != NULL) {
464 FREE(vdata);
465 return 1;
467 return 0;
470 int SYMEXPORT alpm_option_add_ignoregrp(pmhandle_t *handle, const char *grp)
472 CHECK_HANDLE(handle, return -1);
473 handle->ignoregrp = alpm_list_add(handle->ignoregrp, strdup(grp));
474 return 0;
477 int SYMEXPORT alpm_option_set_ignoregrps(pmhandle_t *handle, alpm_list_t *ignoregrps)
479 CHECK_HANDLE(handle, return -1);
480 if(handle->ignoregrp) FREELIST(handle->ignoregrp);
481 handle->ignoregrp = alpm_list_strdup(ignoregrps);
482 return 0;
485 int SYMEXPORT alpm_option_remove_ignoregrp(pmhandle_t *handle, const char *grp)
487 char *vdata = NULL;
488 CHECK_HANDLE(handle, return -1);
489 handle->ignoregrp = alpm_list_remove_str(handle->ignoregrp, grp, &vdata);
490 if(vdata != NULL) {
491 FREE(vdata);
492 return 1;
494 return 0;
497 int SYMEXPORT alpm_option_set_arch(pmhandle_t *handle, const char *arch)
499 CHECK_HANDLE(handle, return -1);
500 if(handle->arch) FREE(handle->arch);
501 if(arch) {
502 handle->arch = strdup(arch);
503 } else {
504 handle->arch = NULL;
506 return 0;
509 int SYMEXPORT alpm_option_set_usedelta(pmhandle_t *handle, int usedelta)
511 CHECK_HANDLE(handle, return -1);
512 handle->usedelta = usedelta;
513 return 0;
516 int SYMEXPORT alpm_option_set_checkspace(pmhandle_t *handle, int checkspace)
518 CHECK_HANDLE(handle, return -1);
519 handle->checkspace = checkspace;
520 return 0;
523 int SYMEXPORT alpm_option_set_default_sigverify(pmhandle_t *handle, pgp_verify_t level)
525 CHECK_HANDLE(handle, return -1);
526 ASSERT(level != PM_PGP_VERIFY_UNKNOWN, RET_ERR(handle, PM_ERR_WRONG_ARGS, -1));
527 handle->sigverify = level;
528 return 0;
531 pgp_verify_t SYMEXPORT alpm_option_get_default_sigverify(pmhandle_t *handle)
533 CHECK_HANDLE(handle, return PM_PGP_VERIFY_UNKNOWN);
534 return handle->sigverify;
537 /* vim: set ts=2 sw=2 noet: */