4 * Copyright (c) 2002-2007 by Judd Vinet <jvinet@zeroflux.org>
5 * Copyright (c) 2005 by Aurelien Foret <orelien@chez.com>
6 * Copyright (c) 2005, 2006 by Miklos Vajna <vmiklos@frugalware.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include <sys/types.h>
35 #include "alpm_list.h"
41 /* global var for handle (private to libalpm) */
42 pmhandle_t
*handle
= NULL
;
44 pmhandle_t
*_alpm_handle_new()
50 CALLOC(handle
, 1, sizeof(pmhandle_t
), RET_ERR(PM_ERR_MEMORY
, NULL
));
53 handle
->logstream
= NULL
;
56 handle
->dbpath
= NULL
;
57 handle
->cachedirs
= NULL
;
58 handle
->lockfile
= NULL
;
59 handle
->logfile
= NULL
;
65 void _alpm_handle_free(pmhandle_t
*handle
)
74 if(handle
->logstream
) {
75 fclose(handle
->logstream
);
76 handle
->logstream
= NULL
;
78 if(handle
->usesyslog
) {
79 handle
->usesyslog
= 0;
84 _alpm_trans_free(handle
->trans
);
87 FREELIST(handle
->cachedirs
);
88 FREE(handle
->logfile
);
89 FREE(handle
->lockfile
);
90 FREE(handle
->xfercommand
);
91 FREELIST(handle
->dbs_sync
);
92 FREELIST(handle
->noupgrade
);
93 FREELIST(handle
->noextract
);
94 FREELIST(handle
->ignorepkg
);
95 FREELIST(handle
->holdpkg
);
96 FREELIST(handle
->ignoregrp
);
100 alpm_cb_log SYMEXPORT
alpm_option_get_logcb()
102 if (handle
== NULL
) {
103 pm_errno
= PM_ERR_HANDLE_NULL
;
106 return handle
->logcb
;
109 alpm_cb_download SYMEXPORT
alpm_option_get_dlcb()
111 if (handle
== NULL
) {
112 pm_errno
= PM_ERR_HANDLE_NULL
;
118 const char SYMEXPORT
*alpm_option_get_root()
120 if (handle
== NULL
) {
121 pm_errno
= PM_ERR_HANDLE_NULL
;
127 const char SYMEXPORT
*alpm_option_get_dbpath()
129 if (handle
== NULL
) {
130 pm_errno
= PM_ERR_HANDLE_NULL
;
133 return handle
->dbpath
;
136 alpm_list_t SYMEXPORT
*alpm_option_get_cachedirs()
138 if (handle
== NULL
) {
139 pm_errno
= PM_ERR_HANDLE_NULL
;
142 return handle
->cachedirs
;
145 const char SYMEXPORT
*alpm_option_get_logfile()
147 if (handle
== NULL
) {
148 pm_errno
= PM_ERR_HANDLE_NULL
;
151 return handle
->logfile
;
154 const char SYMEXPORT
*alpm_option_get_lockfile()
156 if (handle
== NULL
) {
157 pm_errno
= PM_ERR_HANDLE_NULL
;
160 return handle
->lockfile
;
163 unsigned short SYMEXPORT
alpm_option_get_usesyslog()
165 if (handle
== NULL
) {
166 pm_errno
= PM_ERR_HANDLE_NULL
;
169 return handle
->usesyslog
;
172 alpm_list_t SYMEXPORT
*alpm_option_get_noupgrades()
174 if (handle
== NULL
) {
175 pm_errno
= PM_ERR_HANDLE_NULL
;
178 return handle
->noupgrade
;
181 alpm_list_t SYMEXPORT
*alpm_option_get_noextracts()
183 if (handle
== NULL
) {
184 pm_errno
= PM_ERR_HANDLE_NULL
;
187 return handle
->noextract
;
190 alpm_list_t SYMEXPORT
*alpm_option_get_ignorepkgs()
192 if (handle
== NULL
) {
193 pm_errno
= PM_ERR_HANDLE_NULL
;
196 return handle
->ignorepkg
;
199 alpm_list_t SYMEXPORT
*alpm_option_get_holdpkgs()
201 if (handle
== NULL
) {
202 pm_errno
= PM_ERR_HANDLE_NULL
;
205 return handle
->holdpkg
;
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_xfercommand()
219 if (handle
== NULL
) {
220 pm_errno
= PM_ERR_HANDLE_NULL
;
223 return handle
->xfercommand
;
226 unsigned short SYMEXPORT
alpm_option_get_nopassiveftp()
228 if (handle
== NULL
) {
229 pm_errno
= PM_ERR_HANDLE_NULL
;
232 return handle
->nopassiveftp
;
235 pmdb_t SYMEXPORT
*alpm_option_get_localdb()
237 if (handle
== NULL
) {
238 pm_errno
= PM_ERR_HANDLE_NULL
;
241 return handle
->db_local
;
244 alpm_list_t SYMEXPORT
*alpm_option_get_syncdbs()
246 if (handle
== NULL
) {
247 pm_errno
= PM_ERR_HANDLE_NULL
;
250 return handle
->dbs_sync
;
253 void SYMEXPORT
alpm_option_set_logcb(alpm_cb_log cb
)
255 if (handle
== NULL
) {
256 pm_errno
= PM_ERR_HANDLE_NULL
;
262 void SYMEXPORT
alpm_option_set_dlcb(alpm_cb_download cb
)
264 if (handle
== NULL
) {
265 pm_errno
= PM_ERR_HANDLE_NULL
;
271 int SYMEXPORT
alpm_option_set_root(const char *root
)
280 pm_errno
= PM_ERR_WRONG_ARGS
;
283 if(stat(root
, &st
) == -1 || !S_ISDIR(st
.st_mode
)) {
284 pm_errno
= PM_ERR_NOT_A_DIR
;
288 realroot
= calloc(PATH_MAX
+1, sizeof(char));
289 if(!realpath(root
, realroot
)) {
290 pm_errno
= PM_ERR_NOT_A_DIR
;
294 /* verify root ends in a '/' */
295 rootlen
= strlen(realroot
);
296 if(realroot
[rootlen
-1] != '/') {
302 handle
->root
= calloc(rootlen
+ 1, sizeof(char));
303 strncpy(handle
->root
, realroot
, rootlen
);
304 handle
->root
[rootlen
-1] = '/';
306 _alpm_log(PM_LOG_DEBUG
, "option 'root' = %s\n", handle
->root
);
310 int SYMEXPORT
alpm_option_set_dbpath(const char *dbpath
)
313 size_t dbpathlen
, lockfilelen
;
314 const char *lf
= "db.lck";
319 pm_errno
= PM_ERR_WRONG_ARGS
;
322 if(stat(dbpath
, &st
) == -1 || !S_ISDIR(st
.st_mode
)) {
323 pm_errno
= PM_ERR_NOT_A_DIR
;
326 /* verify dbpath ends in a '/' */
327 dbpathlen
= strlen(dbpath
);
328 if(dbpath
[dbpathlen
-1] != '/') {
332 FREE(handle
->dbpath
);
334 handle
->dbpath
= calloc(dbpathlen
+1, sizeof(char));
335 strncpy(handle
->dbpath
, dbpath
, dbpathlen
);
336 handle
->dbpath
[dbpathlen
-1] = '/';
337 _alpm_log(PM_LOG_DEBUG
, "option 'dbpath' = %s\n", handle
->dbpath
);
339 if(handle
->lockfile
) {
340 FREE(handle
->lockfile
);
342 lockfilelen
= strlen(handle
->dbpath
) + strlen(lf
) + 1;
343 handle
->lockfile
= calloc(lockfilelen
, sizeof(char));
344 snprintf(handle
->lockfile
, lockfilelen
, "%s%s", handle
->dbpath
, lf
);
345 _alpm_log(PM_LOG_DEBUG
, "option 'lockfile' = %s\n", handle
->lockfile
);
349 int SYMEXPORT
alpm_option_add_cachedir(const char *cachedir
)
357 pm_errno
= PM_ERR_WRONG_ARGS
;
360 /* don't stat the cachedir yet, as it may not even be needed. we can
361 * fail later if it is needed and the path is invalid. */
363 /* verify cachedir ends in a '/' */
364 cachedirlen
= strlen(cachedir
);
365 if(cachedir
[cachedirlen
-1] != '/') {
368 newcachedir
= calloc(cachedirlen
+ 1, sizeof(char));
369 strncpy(newcachedir
, cachedir
, cachedirlen
);
370 newcachedir
[cachedirlen
-1] = '/';
371 handle
->cachedirs
= alpm_list_add(handle
->cachedirs
, newcachedir
);
372 _alpm_log(PM_LOG_DEBUG
, "option 'cachedir' = %s\n", newcachedir
);
376 void SYMEXPORT
alpm_option_set_cachedirs(alpm_list_t
*cachedirs
)
378 if(handle
->cachedirs
) FREELIST(handle
->cachedirs
);
379 if(cachedirs
) handle
->cachedirs
= cachedirs
;
382 int SYMEXPORT
alpm_option_remove_cachedir(const char *cachedir
)
387 /* verify cachedir ends in a '/' */
388 cachedirlen
= strlen(cachedir
);
389 if(cachedir
[cachedirlen
-1] != '/') {
392 newcachedir
= calloc(cachedirlen
+ 1, sizeof(char));
393 strncpy(newcachedir
, cachedir
, cachedirlen
);
394 newcachedir
[cachedirlen
-1] = '/';
395 handle
->cachedirs
= alpm_list_remove_str(handle
->cachedirs
, newcachedir
, &vdata
);
404 int SYMEXPORT
alpm_option_set_logfile(const char *logfile
)
406 char *oldlogfile
= handle
->logfile
;
411 pm_errno
= PM_ERR_WRONG_ARGS
;
415 handle
->logfile
= strdup(logfile
);
417 /* free the old logfile path string, and close the stream so logaction
418 * will reopen a new stream on the new logfile */
422 if(handle
->logstream
) {
423 fclose(handle
->logstream
);
424 handle
->logstream
= NULL
;
426 _alpm_log(PM_LOG_DEBUG
, "option 'logfile' = %s\n", handle
->logfile
);
430 void SYMEXPORT
alpm_option_set_usesyslog(unsigned short usesyslog
)
432 handle
->usesyslog
= usesyslog
;
435 void SYMEXPORT
alpm_option_add_noupgrade(const char *pkg
)
437 handle
->noupgrade
= alpm_list_add(handle
->noupgrade
, strdup(pkg
));
440 void SYMEXPORT
alpm_option_set_noupgrades(alpm_list_t
*noupgrade
)
442 if(handle
->noupgrade
) FREELIST(handle
->noupgrade
);
443 if(noupgrade
) handle
->noupgrade
= noupgrade
;
446 int SYMEXPORT
alpm_option_remove_noupgrade(const char *pkg
)
449 handle
->noupgrade
= alpm_list_remove_str(handle
->noupgrade
, pkg
, &vdata
);
457 void SYMEXPORT
alpm_option_add_noextract(const char *pkg
)
459 handle
->noextract
= alpm_list_add(handle
->noextract
, strdup(pkg
));
462 void SYMEXPORT
alpm_option_set_noextracts(alpm_list_t
*noextract
)
464 if(handle
->noextract
) FREELIST(handle
->noextract
);
465 if(noextract
) handle
->noextract
= noextract
;
468 int SYMEXPORT
alpm_option_remove_noextract(const char *pkg
)
471 handle
->noextract
= alpm_list_remove_str(handle
->noextract
, pkg
, &vdata
);
479 void SYMEXPORT
alpm_option_add_ignorepkg(const char *pkg
)
481 handle
->ignorepkg
= alpm_list_add(handle
->ignorepkg
, strdup(pkg
));
484 void SYMEXPORT
alpm_option_set_ignorepkgs(alpm_list_t
*ignorepkgs
)
486 if(handle
->ignorepkg
) FREELIST(handle
->ignorepkg
);
487 if(ignorepkgs
) handle
->ignorepkg
= ignorepkgs
;
490 int SYMEXPORT
alpm_option_remove_ignorepkg(const char *pkg
)
493 handle
->ignorepkg
= alpm_list_remove_str(handle
->ignorepkg
, pkg
, &vdata
);
501 void SYMEXPORT
alpm_option_add_holdpkg(const char *pkg
)
503 handle
->holdpkg
= alpm_list_add(handle
->holdpkg
, strdup(pkg
));
506 void SYMEXPORT
alpm_option_set_holdpkgs(alpm_list_t
*holdpkgs
)
508 if(handle
->holdpkg
) FREELIST(handle
->holdpkg
);
509 if(holdpkgs
) handle
->holdpkg
= holdpkgs
;
512 int SYMEXPORT
alpm_option_remove_holdpkg(const char *pkg
)
515 handle
->holdpkg
= alpm_list_remove_str(handle
->holdpkg
, pkg
, &vdata
);
523 void SYMEXPORT
alpm_option_add_ignoregrp(const char *grp
)
525 handle
->ignoregrp
= alpm_list_add(handle
->ignoregrp
, strdup(grp
));
528 void SYMEXPORT
alpm_option_set_ignoregrps(alpm_list_t
*ignoregrps
)
530 if(handle
->ignoregrp
) FREELIST(handle
->ignoregrp
);
531 if(ignoregrps
) handle
->ignoregrp
= ignoregrps
;
534 int SYMEXPORT
alpm_option_remove_ignoregrp(const char *grp
)
537 handle
->ignoregrp
= alpm_list_remove_str(handle
->ignoregrp
, grp
, &vdata
);
545 void SYMEXPORT
alpm_option_set_xfercommand(const char *cmd
)
547 if(handle
->xfercommand
) FREE(handle
->xfercommand
);
548 if(cmd
) handle
->xfercommand
= strdup(cmd
);
551 void SYMEXPORT
alpm_option_set_nopassiveftp(unsigned short nopasv
)
553 handle
->nopassiveftp
= nopasv
;
556 void SYMEXPORT
alpm_option_set_usedelta(unsigned short usedelta
)
558 handle
->usedelta
= usedelta
;
561 /* vim: set ts=2 sw=2 noet: */