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>
36 #include "alpm_list.h"
42 /* global var for handle (private to libalpm) */
43 pmhandle_t
*handle
= NULL
;
45 pmhandle_t
*_alpm_handle_new()
51 CALLOC(handle
, 1, sizeof(pmhandle_t
), RET_ERR(PM_ERR_MEMORY
, NULL
));
57 void _alpm_handle_free(pmhandle_t
*handle
)
66 if(handle
->logstream
) {
67 fclose(handle
->logstream
);
68 handle
->logstream
= NULL
;
70 if(handle
->usesyslog
) {
71 handle
->usesyslog
= 0;
76 _alpm_trans_free(handle
->trans
);
79 FREELIST(handle
->cachedirs
);
80 FREE(handle
->logfile
);
81 FREE(handle
->lockfile
);
83 FREELIST(handle
->dbs_sync
);
84 FREELIST(handle
->noupgrade
);
85 FREELIST(handle
->noextract
);
86 FREELIST(handle
->ignorepkg
);
87 FREELIST(handle
->ignoregrp
);
91 alpm_cb_log SYMEXPORT
alpm_option_get_logcb()
94 pm_errno
= PM_ERR_HANDLE_NULL
;
100 alpm_cb_download SYMEXPORT
alpm_option_get_dlcb()
102 if (handle
== NULL
) {
103 pm_errno
= PM_ERR_HANDLE_NULL
;
109 alpm_cb_fetch SYMEXPORT
alpm_option_get_fetchcb()
111 if (handle
== NULL
) {
112 pm_errno
= PM_ERR_HANDLE_NULL
;
115 return handle
->fetchcb
;
118 alpm_cb_totaldl SYMEXPORT
alpm_option_get_totaldlcb()
120 if (handle
== NULL
) {
121 pm_errno
= PM_ERR_HANDLE_NULL
;
124 return handle
->totaldlcb
;
127 const char SYMEXPORT
*alpm_option_get_root()
129 if (handle
== NULL
) {
130 pm_errno
= PM_ERR_HANDLE_NULL
;
136 const char SYMEXPORT
*alpm_option_get_dbpath()
138 if (handle
== NULL
) {
139 pm_errno
= PM_ERR_HANDLE_NULL
;
142 return handle
->dbpath
;
145 alpm_list_t SYMEXPORT
*alpm_option_get_cachedirs()
147 if (handle
== NULL
) {
148 pm_errno
= PM_ERR_HANDLE_NULL
;
151 return handle
->cachedirs
;
154 const char SYMEXPORT
*alpm_option_get_logfile()
156 if (handle
== NULL
) {
157 pm_errno
= PM_ERR_HANDLE_NULL
;
160 return handle
->logfile
;
163 const char SYMEXPORT
*alpm_option_get_lockfile()
165 if (handle
== NULL
) {
166 pm_errno
= PM_ERR_HANDLE_NULL
;
169 return handle
->lockfile
;
172 int SYMEXPORT
alpm_option_get_usesyslog()
174 if (handle
== NULL
) {
175 pm_errno
= PM_ERR_HANDLE_NULL
;
178 return handle
->usesyslog
;
181 alpm_list_t SYMEXPORT
*alpm_option_get_noupgrades()
183 if (handle
== NULL
) {
184 pm_errno
= PM_ERR_HANDLE_NULL
;
187 return handle
->noupgrade
;
190 alpm_list_t SYMEXPORT
*alpm_option_get_noextracts()
192 if (handle
== NULL
) {
193 pm_errno
= PM_ERR_HANDLE_NULL
;
196 return handle
->noextract
;
199 alpm_list_t SYMEXPORT
*alpm_option_get_ignorepkgs()
201 if (handle
== NULL
) {
202 pm_errno
= PM_ERR_HANDLE_NULL
;
205 return handle
->ignorepkg
;
208 alpm_list_t SYMEXPORT
*alpm_option_get_ignoregrps()
210 if (handle
== NULL
) {
211 pm_errno
= PM_ERR_HANDLE_NULL
;
214 return handle
->ignoregrp
;
217 const char SYMEXPORT
*alpm_option_get_arch()
219 if (handle
== NULL
) {
220 pm_errno
= PM_ERR_HANDLE_NULL
;
226 int SYMEXPORT
alpm_option_get_usedelta()
228 if (handle
== NULL
) {
229 pm_errno
= PM_ERR_HANDLE_NULL
;
232 return handle
->usedelta
;
235 int SYMEXPORT
alpm_option_get_checkspace()
237 if (handle
== NULL
) {
238 pm_errno
= PM_ERR_HANDLE_NULL
;
241 return handle
->checkspace
;
244 pmdb_t SYMEXPORT
*alpm_option_get_localdb()
246 if (handle
== NULL
) {
247 pm_errno
= PM_ERR_HANDLE_NULL
;
250 return handle
->db_local
;
253 alpm_list_t SYMEXPORT
*alpm_option_get_syncdbs()
255 if (handle
== NULL
) {
256 pm_errno
= PM_ERR_HANDLE_NULL
;
259 return handle
->dbs_sync
;
262 void SYMEXPORT
alpm_option_set_logcb(alpm_cb_log cb
)
264 if (handle
== NULL
) {
265 pm_errno
= PM_ERR_HANDLE_NULL
;
271 void SYMEXPORT
alpm_option_set_dlcb(alpm_cb_download cb
)
273 if (handle
== NULL
) {
274 pm_errno
= PM_ERR_HANDLE_NULL
;
280 void SYMEXPORT
alpm_option_set_fetchcb(alpm_cb_fetch cb
)
282 if (handle
== NULL
) {
283 pm_errno
= PM_ERR_HANDLE_NULL
;
286 handle
->fetchcb
= cb
;
289 void SYMEXPORT
alpm_option_set_totaldlcb(alpm_cb_totaldl cb
)
291 if (handle
== NULL
) {
292 pm_errno
= PM_ERR_HANDLE_NULL
;
295 handle
->totaldlcb
= cb
;
298 int SYMEXPORT
alpm_option_set_root(const char *root
)
307 pm_errno
= PM_ERR_WRONG_ARGS
;
310 if(stat(root
, &st
) == -1 || !S_ISDIR(st
.st_mode
)) {
311 pm_errno
= PM_ERR_NOT_A_DIR
;
315 realroot
= calloc(PATH_MAX
+1, sizeof(char));
316 if(!realpath(root
, realroot
)) {
318 pm_errno
= PM_ERR_NOT_A_DIR
;
322 /* verify root ends in a '/' */
323 rootlen
= strlen(realroot
);
324 if(realroot
[rootlen
-1] != '/') {
330 handle
->root
= calloc(rootlen
+ 1, sizeof(char));
331 strncpy(handle
->root
, realroot
, rootlen
);
332 handle
->root
[rootlen
-1] = '/';
334 _alpm_log(PM_LOG_DEBUG
, "option 'root' = %s\n", handle
->root
);
338 int SYMEXPORT
alpm_option_set_dbpath(const char *dbpath
)
341 size_t dbpathlen
, lockfilelen
;
342 const char *lf
= "db.lck";
347 pm_errno
= PM_ERR_WRONG_ARGS
;
350 if(stat(dbpath
, &st
) == -1 || !S_ISDIR(st
.st_mode
)) {
351 pm_errno
= PM_ERR_NOT_A_DIR
;
354 /* verify dbpath ends in a '/' */
355 dbpathlen
= strlen(dbpath
);
356 if(dbpath
[dbpathlen
-1] != '/') {
360 FREE(handle
->dbpath
);
362 handle
->dbpath
= calloc(dbpathlen
+1, sizeof(char));
363 strncpy(handle
->dbpath
, dbpath
, dbpathlen
);
364 handle
->dbpath
[dbpathlen
-1] = '/';
365 _alpm_log(PM_LOG_DEBUG
, "option 'dbpath' = %s\n", handle
->dbpath
);
367 if(handle
->lockfile
) {
368 FREE(handle
->lockfile
);
370 lockfilelen
= strlen(handle
->dbpath
) + strlen(lf
) + 1;
371 handle
->lockfile
= calloc(lockfilelen
, sizeof(char));
372 snprintf(handle
->lockfile
, lockfilelen
, "%s%s", handle
->dbpath
, lf
);
373 _alpm_log(PM_LOG_DEBUG
, "option 'lockfile' = %s\n", handle
->lockfile
);
377 int SYMEXPORT
alpm_option_add_cachedir(const char *cachedir
)
385 pm_errno
= PM_ERR_WRONG_ARGS
;
388 /* don't stat the cachedir yet, as it may not even be needed. we can
389 * fail later if it is needed and the path is invalid. */
391 /* verify cachedir ends in a '/' */
392 cachedirlen
= strlen(cachedir
);
393 if(cachedir
[cachedirlen
-1] != '/') {
396 newcachedir
= calloc(cachedirlen
+ 1, sizeof(char));
397 strncpy(newcachedir
, cachedir
, cachedirlen
);
398 newcachedir
[cachedirlen
-1] = '/';
399 handle
->cachedirs
= alpm_list_add(handle
->cachedirs
, newcachedir
);
400 _alpm_log(PM_LOG_DEBUG
, "option 'cachedir' = %s\n", newcachedir
);
404 void SYMEXPORT
alpm_option_set_cachedirs(alpm_list_t
*cachedirs
)
406 if(handle
->cachedirs
) FREELIST(handle
->cachedirs
);
407 if(cachedirs
) handle
->cachedirs
= cachedirs
;
410 int SYMEXPORT
alpm_option_remove_cachedir(const char *cachedir
)
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
;
439 pm_errno
= PM_ERR_WRONG_ARGS
;
443 handle
->logfile
= strdup(logfile
);
445 /* free the old logfile path string, and close the stream so logaction
446 * will reopen a new stream on the new logfile */
450 if(handle
->logstream
) {
451 fclose(handle
->logstream
);
452 handle
->logstream
= NULL
;
454 _alpm_log(PM_LOG_DEBUG
, "option 'logfile' = %s\n", handle
->logfile
);
458 void SYMEXPORT
alpm_option_set_usesyslog(int usesyslog
)
460 handle
->usesyslog
= usesyslog
;
463 void SYMEXPORT
alpm_option_add_noupgrade(const char *pkg
)
465 handle
->noupgrade
= alpm_list_add(handle
->noupgrade
, strdup(pkg
));
468 void SYMEXPORT
alpm_option_set_noupgrades(alpm_list_t
*noupgrade
)
470 if(handle
->noupgrade
) FREELIST(handle
->noupgrade
);
471 if(noupgrade
) handle
->noupgrade
= noupgrade
;
474 int SYMEXPORT
alpm_option_remove_noupgrade(const char *pkg
)
477 handle
->noupgrade
= alpm_list_remove_str(handle
->noupgrade
, pkg
, &vdata
);
485 void SYMEXPORT
alpm_option_add_noextract(const char *pkg
)
487 handle
->noextract
= alpm_list_add(handle
->noextract
, strdup(pkg
));
490 void SYMEXPORT
alpm_option_set_noextracts(alpm_list_t
*noextract
)
492 if(handle
->noextract
) FREELIST(handle
->noextract
);
493 if(noextract
) handle
->noextract
= noextract
;
496 int SYMEXPORT
alpm_option_remove_noextract(const char *pkg
)
499 handle
->noextract
= alpm_list_remove_str(handle
->noextract
, pkg
, &vdata
);
507 void SYMEXPORT
alpm_option_add_ignorepkg(const char *pkg
)
509 handle
->ignorepkg
= alpm_list_add(handle
->ignorepkg
, strdup(pkg
));
512 void SYMEXPORT
alpm_option_set_ignorepkgs(alpm_list_t
*ignorepkgs
)
514 if(handle
->ignorepkg
) FREELIST(handle
->ignorepkg
);
515 if(ignorepkgs
) handle
->ignorepkg
= ignorepkgs
;
518 int SYMEXPORT
alpm_option_remove_ignorepkg(const char *pkg
)
521 handle
->ignorepkg
= alpm_list_remove_str(handle
->ignorepkg
, pkg
, &vdata
);
529 void SYMEXPORT
alpm_option_add_ignoregrp(const char *grp
)
531 handle
->ignoregrp
= alpm_list_add(handle
->ignoregrp
, strdup(grp
));
534 void SYMEXPORT
alpm_option_set_ignoregrps(alpm_list_t
*ignoregrps
)
536 if(handle
->ignoregrp
) FREELIST(handle
->ignoregrp
);
537 if(ignoregrps
) handle
->ignoregrp
= ignoregrps
;
540 int SYMEXPORT
alpm_option_remove_ignoregrp(const char *grp
)
543 handle
->ignoregrp
= alpm_list_remove_str(handle
->ignoregrp
, grp
, &vdata
);
551 void SYMEXPORT
alpm_option_set_arch(const char *arch
)
553 if(handle
->arch
) FREE(handle
->arch
);
554 if(arch
) handle
->arch
= strdup(arch
);
557 void SYMEXPORT
alpm_option_set_usedelta(int usedelta
)
559 handle
->usedelta
= usedelta
;
562 void SYMEXPORT
alpm_option_set_checkspace(int checkspace
)
564 handle
->checkspace
= checkspace
;
567 /* vim: set ts=2 sw=2 noet: */