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 pmhandle_t
*_alpm_handle_new()
44 CALLOC(handle
, 1, sizeof(pmhandle_t
), return NULL
);
46 handle
->sigverify
= PM_PGP_VERIFY_OPTIONAL
;
51 void _alpm_handle_free(pmhandle_t
*handle
)
58 if(handle
->logstream
) {
59 fclose(handle
->logstream
);
60 handle
->logstream
= NULL
;
62 if(handle
->usesyslog
) {
63 handle
->usesyslog
= 0;
68 /* release curl handle */
69 curl_easy_cleanup(handle
->curl
);
73 _alpm_trans_free(handle
->trans
);
76 FREELIST(handle
->cachedirs
);
77 FREE(handle
->logfile
);
78 FREE(handle
->lockfile
);
81 FREELIST(handle
->dbs_sync
);
82 FREELIST(handle
->noupgrade
);
83 FREELIST(handle
->noextract
);
84 FREELIST(handle
->ignorepkg
);
85 FREELIST(handle
->ignoregrp
);
89 alpm_cb_log SYMEXPORT
alpm_option_get_logcb(pmhandle_t
*handle
)
91 CHECK_HANDLE(handle
, return NULL
);
95 alpm_cb_download SYMEXPORT
alpm_option_get_dlcb(pmhandle_t
*handle
)
97 CHECK_HANDLE(handle
, return NULL
);
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
);
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
);
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);
216 int SYMEXPORT
alpm_option_set_dlcb(pmhandle_t
*handle
, alpm_cb_download cb
)
218 CHECK_HANDLE(handle
, return -1);
223 int SYMEXPORT
alpm_option_set_fetchcb(pmhandle_t
*handle
, alpm_cb_fetch cb
)
225 CHECK_HANDLE(handle
, return -1);
226 handle
->fetchcb
= cb
;
230 int SYMEXPORT
alpm_option_set_totaldlcb(pmhandle_t
*handle
, alpm_cb_totaldl cb
)
232 CHECK_HANDLE(handle
, return -1);
233 handle
->totaldlcb
= cb
;
237 static char *canonicalize_path(const char *path
) {
241 /* verify path ends in a '/' */
243 if(path
[len
- 1] != '/') {
246 new_path
= calloc(len
+ 1, sizeof(char));
247 strncpy(new_path
, path
, len
);
248 new_path
[len
- 1] = '/';
252 enum _pmerrno_t
_alpm_set_directory_option(const char *value
,
253 char **storage
, int must_exist
)
261 return PM_ERR_WRONG_ARGS
;
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
)) {
270 return PM_ERR_NOT_A_DIR
;
278 *storage
= canonicalize_path(path
);
283 int SYMEXPORT
alpm_option_add_cachedir(pmhandle_t
*handle
, const char *cachedir
)
287 CHECK_HANDLE(handle
, return -1);
289 handle
->pm_errno
= PM_ERR_WRONG_ARGS
;
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
);
301 int SYMEXPORT
alpm_option_set_cachedirs(pmhandle_t
*handle
, alpm_list_t
*cachedirs
)
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
);
317 int SYMEXPORT
alpm_option_remove_cachedir(pmhandle_t
*handle
, const char *cachedir
)
322 CHECK_HANDLE(handle
, return -1);
323 /* verify cachedir ends in a '/' */
324 cachedirlen
= strlen(cachedir
);
325 if(cachedir
[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
);
340 int SYMEXPORT
alpm_option_set_logfile(pmhandle_t
*handle
, const char *logfile
)
342 char *oldlogfile
= handle
->logfile
;
344 CHECK_HANDLE(handle
, return -1);
346 handle
->pm_errno
= PM_ERR_WRONG_ARGS
;
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 */
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
);
365 int SYMEXPORT
alpm_option_set_gpgdir(pmhandle_t
*handle
, const char *gpgdir
)
367 CHECK_HANDLE(handle
, return -1);
369 handle
->pm_errno
= PM_ERR_WRONG_ARGS
;
374 FREE(handle
->gpgdir
);
376 handle
->gpgdir
= strdup(gpgdir
);
378 _alpm_log(handle
, PM_LOG_DEBUG
, "option 'gpgdir' = %s\n", handle
->gpgdir
);
382 int SYMEXPORT
alpm_option_set_usesyslog(pmhandle_t
*handle
, int usesyslog
)
384 CHECK_HANDLE(handle
, return -1);
385 handle
->usesyslog
= usesyslog
;
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
));
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
);
404 int SYMEXPORT
alpm_option_remove_noupgrade(pmhandle_t
*handle
, const char *pkg
)
407 CHECK_HANDLE(handle
, return -1);
408 handle
->noupgrade
= alpm_list_remove_str(handle
->noupgrade
, pkg
, &vdata
);
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
));
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
);
431 int SYMEXPORT
alpm_option_remove_noextract(pmhandle_t
*handle
, const char *pkg
)
434 CHECK_HANDLE(handle
, return -1);
435 handle
->noextract
= alpm_list_remove_str(handle
->noextract
, pkg
, &vdata
);
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
));
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
);
458 int SYMEXPORT
alpm_option_remove_ignorepkg(pmhandle_t
*handle
, const char *pkg
)
461 CHECK_HANDLE(handle
, return -1);
462 handle
->ignorepkg
= alpm_list_remove_str(handle
->ignorepkg
, pkg
, &vdata
);
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
));
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
);
485 int SYMEXPORT
alpm_option_remove_ignoregrp(pmhandle_t
*handle
, const char *grp
)
488 CHECK_HANDLE(handle
, return -1);
489 handle
->ignoregrp
= alpm_list_remove_str(handle
->ignoregrp
, grp
, &vdata
);
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
);
502 handle
->arch
= strdup(arch
);
509 int SYMEXPORT
alpm_option_set_usedelta(pmhandle_t
*handle
, int usedelta
)
511 CHECK_HANDLE(handle
, return -1);
512 handle
->usedelta
= usedelta
;
516 int SYMEXPORT
alpm_option_set_checkspace(pmhandle_t
*handle
, int checkspace
)
518 CHECK_HANDLE(handle
, return -1);
519 handle
->checkspace
= checkspace
;
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
;
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: */