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/>.
28 #include <sys/types.h>
34 #include "alpm_list.h"
40 /* global var for handle (private to libalpm) */
41 pmhandle_t
*handle
= NULL
;
43 pmhandle_t
*_alpm_handle_new()
47 CALLOC(handle
, 1, sizeof(pmhandle_t
), RET_ERR(PM_ERR_MEMORY
, NULL
));
49 handle
->sigverify
= PM_PGP_VERIFY_OPTIONAL
;
54 void _alpm_handle_free(pmhandle_t
*handle
)
61 if(handle
->logstream
) {
62 fclose(handle
->logstream
);
63 handle
->logstream
= NULL
;
65 if(handle
->usesyslog
) {
66 handle
->usesyslog
= 0;
71 /* release curl handle */
72 curl_easy_cleanup(handle
->curl
);
76 _alpm_trans_free(handle
->trans
);
79 FREELIST(handle
->cachedirs
);
80 FREE(handle
->logfile
);
81 FREE(handle
->lockfile
);
83 FREE(handle
->signaturedir
);
84 FREELIST(handle
->dbs_sync
);
85 FREELIST(handle
->noupgrade
);
86 FREELIST(handle
->noextract
);
87 FREELIST(handle
->ignorepkg
);
88 FREELIST(handle
->ignoregrp
);
93 alpm_cb_log SYMEXPORT
alpm_option_get_logcb()
96 pm_errno
= PM_ERR_HANDLE_NULL
;
102 alpm_cb_download SYMEXPORT
alpm_option_get_dlcb()
105 pm_errno
= PM_ERR_HANDLE_NULL
;
111 alpm_cb_fetch SYMEXPORT
alpm_option_get_fetchcb()
114 pm_errno
= PM_ERR_HANDLE_NULL
;
117 return handle
->fetchcb
;
120 alpm_cb_totaldl SYMEXPORT
alpm_option_get_totaldlcb()
123 pm_errno
= PM_ERR_HANDLE_NULL
;
126 return handle
->totaldlcb
;
129 const char SYMEXPORT
*alpm_option_get_root()
132 pm_errno
= PM_ERR_HANDLE_NULL
;
138 const char SYMEXPORT
*alpm_option_get_dbpath()
141 pm_errno
= PM_ERR_HANDLE_NULL
;
144 return handle
->dbpath
;
147 alpm_list_t SYMEXPORT
*alpm_option_get_cachedirs()
150 pm_errno
= PM_ERR_HANDLE_NULL
;
153 return handle
->cachedirs
;
156 const char SYMEXPORT
*alpm_option_get_logfile()
159 pm_errno
= PM_ERR_HANDLE_NULL
;
162 return handle
->logfile
;
165 const char SYMEXPORT
*alpm_option_get_lockfile()
168 pm_errno
= PM_ERR_HANDLE_NULL
;
171 return handle
->lockfile
;
174 const char SYMEXPORT
*alpm_option_get_signaturedir()
177 pm_errno
= PM_ERR_HANDLE_NULL
;
180 return handle
->signaturedir
;
183 int SYMEXPORT
alpm_option_get_usesyslog()
186 pm_errno
= PM_ERR_HANDLE_NULL
;
189 return handle
->usesyslog
;
192 alpm_list_t SYMEXPORT
*alpm_option_get_noupgrades()
195 pm_errno
= PM_ERR_HANDLE_NULL
;
198 return handle
->noupgrade
;
201 alpm_list_t SYMEXPORT
*alpm_option_get_noextracts()
204 pm_errno
= PM_ERR_HANDLE_NULL
;
207 return handle
->noextract
;
210 alpm_list_t SYMEXPORT
*alpm_option_get_ignorepkgs()
213 pm_errno
= PM_ERR_HANDLE_NULL
;
216 return handle
->ignorepkg
;
219 alpm_list_t SYMEXPORT
*alpm_option_get_ignoregrps()
222 pm_errno
= PM_ERR_HANDLE_NULL
;
225 return handle
->ignoregrp
;
228 const char SYMEXPORT
*alpm_option_get_arch()
231 pm_errno
= PM_ERR_HANDLE_NULL
;
237 int SYMEXPORT
alpm_option_get_usedelta()
240 pm_errno
= PM_ERR_HANDLE_NULL
;
243 return handle
->usedelta
;
246 int SYMEXPORT
alpm_option_get_checkspace()
249 pm_errno
= PM_ERR_HANDLE_NULL
;
252 return handle
->checkspace
;
255 pmdb_t SYMEXPORT
*alpm_option_get_localdb()
258 pm_errno
= PM_ERR_HANDLE_NULL
;
261 return handle
->db_local
;
264 alpm_list_t SYMEXPORT
*alpm_option_get_syncdbs()
267 pm_errno
= PM_ERR_HANDLE_NULL
;
270 return handle
->dbs_sync
;
273 int SYMEXPORT
alpm_option_set_logcb(alpm_cb_log cb
)
275 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
280 int SYMEXPORT
alpm_option_set_dlcb(alpm_cb_download cb
)
282 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
287 int SYMEXPORT
alpm_option_set_fetchcb(alpm_cb_fetch cb
)
289 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
290 handle
->fetchcb
= cb
;
294 int SYMEXPORT
alpm_option_set_totaldlcb(alpm_cb_totaldl cb
)
296 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
297 handle
->totaldlcb
= cb
;
301 static char *canonicalize_path(const char *path
) {
305 /* verify path ends in a '/' */
307 if(path
[len
- 1] != '/') {
310 new_path
= calloc(len
+ 1, sizeof(char));
311 strncpy(new_path
, path
, len
);
312 new_path
[len
- 1] = '/';
316 int _alpm_set_directory_option(const char *value
,
317 char **storage
, int must_exist
)
325 pm_errno
= PM_ERR_WRONG_ARGS
;
329 if(stat(path
, &st
) == -1 || !S_ISDIR(st
.st_mode
)) {
330 pm_errno
= PM_ERR_NOT_A_DIR
;
333 real
= calloc(PATH_MAX
+ 1, sizeof(char));
334 if(!realpath(path
, real
)) {
336 pm_errno
= PM_ERR_NOT_A_DIR
;
345 *storage
= canonicalize_path(path
);
350 int SYMEXPORT
alpm_option_set_root(const char *root
)
352 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
354 if(_alpm_set_directory_option(root
, &(handle
->root
), 1)) {
357 _alpm_log(PM_LOG_DEBUG
, "option 'root' = %s\n", handle
->root
);
361 int SYMEXPORT
alpm_option_set_dbpath(const char *dbpath
)
363 const char *lf
= "db.lck";
366 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
368 if(_alpm_set_directory_option(dbpath
, &(handle
->dbpath
), 1)) {
371 _alpm_log(PM_LOG_DEBUG
, "option 'dbpath' = %s\n", handle
->dbpath
);
373 if(handle
->lockfile
) {
374 FREE(handle
->lockfile
);
376 lockfilelen
= strlen(handle
->dbpath
) + strlen(lf
) + 1;
377 handle
->lockfile
= calloc(lockfilelen
, sizeof(char));
378 snprintf(handle
->lockfile
, lockfilelen
, "%s%s", handle
->dbpath
, lf
);
379 _alpm_log(PM_LOG_DEBUG
, "option 'lockfile' = %s\n", handle
->lockfile
);
383 int SYMEXPORT
alpm_option_add_cachedir(const char *cachedir
)
387 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
389 pm_errno
= PM_ERR_WRONG_ARGS
;
392 /* don't stat the cachedir yet, as it may not even be needed. we can
393 * fail later if it is needed and the path is invalid. */
395 newcachedir
= canonicalize_path(cachedir
);
396 handle
->cachedirs
= alpm_list_add(handle
->cachedirs
, newcachedir
);
397 _alpm_log(PM_LOG_DEBUG
, "backend option 'cachedir' = %s\n", newcachedir
);
401 int SYMEXPORT
alpm_option_set_cachedirs(alpm_list_t
*cachedirs
)
403 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
404 if(handle
->cachedirs
) FREELIST(handle
->cachedirs
);
405 handle
->cachedirs
= alpm_list_strdup(cachedirs
);
409 int SYMEXPORT
alpm_option_remove_cachedir(const char *cachedir
)
414 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
415 /* verify cachedir ends in a '/' */
416 cachedirlen
= strlen(cachedir
);
417 if(cachedir
[cachedirlen
-1] != '/') {
420 newcachedir
= calloc(cachedirlen
+ 1, sizeof(char));
421 strncpy(newcachedir
, cachedir
, cachedirlen
);
422 newcachedir
[cachedirlen
-1] = '/';
423 handle
->cachedirs
= alpm_list_remove_str(handle
->cachedirs
, newcachedir
, &vdata
);
432 int SYMEXPORT
alpm_option_set_logfile(const char *logfile
)
434 char *oldlogfile
= handle
->logfile
;
436 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
438 pm_errno
= PM_ERR_WRONG_ARGS
;
442 handle
->logfile
= strdup(logfile
);
444 /* free the old logfile path string, and close the stream so logaction
445 * will reopen a new stream on the new logfile */
449 if(handle
->logstream
) {
450 fclose(handle
->logstream
);
451 handle
->logstream
= NULL
;
453 _alpm_log(PM_LOG_DEBUG
, "option 'logfile' = %s\n", handle
->logfile
);
457 int SYMEXPORT
alpm_option_set_signaturedir(const char *signaturedir
)
460 pm_errno
= PM_ERR_WRONG_ARGS
;
464 if(handle
->signaturedir
) {
465 FREE(handle
->signaturedir
);
467 handle
->signaturedir
= strdup(signaturedir
);
469 _alpm_log(PM_LOG_DEBUG
, "option 'signaturedir' = %s\n", handle
->signaturedir
);
473 int SYMEXPORT
alpm_option_set_usesyslog(int usesyslog
)
475 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
476 handle
->usesyslog
= usesyslog
;
480 int SYMEXPORT
alpm_option_add_noupgrade(const char *pkg
)
482 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
483 handle
->noupgrade
= alpm_list_add(handle
->noupgrade
, strdup(pkg
));
487 int SYMEXPORT
alpm_option_set_noupgrades(alpm_list_t
*noupgrade
)
489 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
490 if(handle
->noupgrade
) FREELIST(handle
->noupgrade
);
491 handle
->noupgrade
= alpm_list_strdup(noupgrade
);
495 int SYMEXPORT
alpm_option_remove_noupgrade(const char *pkg
)
498 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
499 handle
->noupgrade
= alpm_list_remove_str(handle
->noupgrade
, pkg
, &vdata
);
507 int SYMEXPORT
alpm_option_add_noextract(const char *pkg
)
509 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
510 handle
->noextract
= alpm_list_add(handle
->noextract
, strdup(pkg
));
514 int SYMEXPORT
alpm_option_set_noextracts(alpm_list_t
*noextract
)
516 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
517 if(handle
->noextract
) FREELIST(handle
->noextract
);
518 handle
->noextract
= alpm_list_strdup(noextract
);
522 int SYMEXPORT
alpm_option_remove_noextract(const char *pkg
)
525 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
526 handle
->noextract
= alpm_list_remove_str(handle
->noextract
, pkg
, &vdata
);
534 int SYMEXPORT
alpm_option_add_ignorepkg(const char *pkg
)
536 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
537 handle
->ignorepkg
= alpm_list_add(handle
->ignorepkg
, strdup(pkg
));
541 int SYMEXPORT
alpm_option_set_ignorepkgs(alpm_list_t
*ignorepkgs
)
543 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
544 if(handle
->ignorepkg
) FREELIST(handle
->ignorepkg
);
545 handle
->ignorepkg
= alpm_list_strdup(ignorepkgs
);
549 int SYMEXPORT
alpm_option_remove_ignorepkg(const char *pkg
)
552 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
553 handle
->ignorepkg
= alpm_list_remove_str(handle
->ignorepkg
, pkg
, &vdata
);
561 int SYMEXPORT
alpm_option_add_ignoregrp(const char *grp
)
563 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
564 handle
->ignoregrp
= alpm_list_add(handle
->ignoregrp
, strdup(grp
));
568 int SYMEXPORT
alpm_option_set_ignoregrps(alpm_list_t
*ignoregrps
)
570 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
571 if(handle
->ignoregrp
) FREELIST(handle
->ignoregrp
);
572 handle
->ignoregrp
= alpm_list_strdup(ignoregrps
);
576 int SYMEXPORT
alpm_option_remove_ignoregrp(const char *grp
)
579 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
580 handle
->ignoregrp
= alpm_list_remove_str(handle
->ignoregrp
, grp
, &vdata
);
588 int SYMEXPORT
alpm_option_set_arch(const char *arch
)
590 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
591 if(handle
->arch
) FREE(handle
->arch
);
593 handle
->arch
= strdup(arch
);
600 int SYMEXPORT
alpm_option_set_usedelta(int usedelta
)
602 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
603 handle
->usedelta
= usedelta
;
607 int SYMEXPORT
alpm_option_set_checkspace(int checkspace
)
609 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
610 handle
->checkspace
= checkspace
;
614 int SYMEXPORT
alpm_option_set_default_sigverify(pgp_verify_t level
)
616 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
617 ASSERT(level
!= PM_PGP_VERIFY_UNKNOWN
, RET_ERR(PM_ERR_WRONG_ARGS
, -1));
618 handle
->sigverify
= level
;
622 pgp_verify_t SYMEXPORT
alpm_option_get_default_sigverify()
624 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, PM_PGP_VERIFY_UNKNOWN
));
625 return handle
->sigverify
;
628 /* vim: set ts=2 sw=2 noet: */