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/>.
28 #include <sys/types.h>
36 #include "alpm_list.h"
44 /* global var for handle (private to libalpm) */
45 pmhandle_t
*handle
= NULL
;
47 pmhandle_t
*_alpm_handle_new()
53 CALLOC(handle
, 1, sizeof(pmhandle_t
), RET_ERR(PM_ERR_MEMORY
, NULL
));
56 handle
->logstream
= NULL
;
58 /* see if we're root or not */
59 handle
->uid
= geteuid();
61 handle
->dbpath
= NULL
;
62 handle
->cachedirs
= NULL
;
63 handle
->lockfile
= NULL
;
64 handle
->logfile
= NULL
;
70 void _alpm_handle_free(pmhandle_t
*handle
)
79 if(handle
->logstream
) {
80 fclose(handle
->logstream
);
81 handle
->logstream
= NULL
;
83 if(handle
->usesyslog
) {
84 handle
->usesyslog
= 0;
89 _alpm_trans_free(handle
->trans
);
92 FREELIST(handle
->cachedirs
);
93 FREE(handle
->logfile
);
94 FREE(handle
->lockfile
);
95 FREE(handle
->xfercommand
);
96 FREELIST(handle
->dbs_sync
);
97 FREELIST(handle
->noupgrade
);
98 FREELIST(handle
->noextract
);
99 FREELIST(handle
->ignorepkg
);
100 FREELIST(handle
->holdpkg
);
101 FREELIST(handle
->ignoregrp
);
105 alpm_cb_log SYMEXPORT
alpm_option_get_logcb()
107 if (handle
== NULL
) {
108 pm_errno
= PM_ERR_HANDLE_NULL
;
111 return handle
->logcb
;
114 alpm_cb_download SYMEXPORT
alpm_option_get_dlcb()
116 if (handle
== NULL
) {
117 pm_errno
= PM_ERR_HANDLE_NULL
;
123 const char SYMEXPORT
*alpm_option_get_root()
125 if (handle
== NULL
) {
126 pm_errno
= PM_ERR_HANDLE_NULL
;
132 const char SYMEXPORT
*alpm_option_get_dbpath()
134 if (handle
== NULL
) {
135 pm_errno
= PM_ERR_HANDLE_NULL
;
138 return handle
->dbpath
;
141 alpm_list_t SYMEXPORT
*alpm_option_get_cachedirs()
143 if (handle
== NULL
) {
144 pm_errno
= PM_ERR_HANDLE_NULL
;
147 return handle
->cachedirs
;
150 const char SYMEXPORT
*alpm_option_get_logfile()
152 if (handle
== NULL
) {
153 pm_errno
= PM_ERR_HANDLE_NULL
;
156 return handle
->logfile
;
159 const char SYMEXPORT
*alpm_option_get_lockfile()
161 if (handle
== NULL
) {
162 pm_errno
= PM_ERR_HANDLE_NULL
;
165 return handle
->lockfile
;
168 unsigned short SYMEXPORT
alpm_option_get_usesyslog()
170 if (handle
== NULL
) {
171 pm_errno
= PM_ERR_HANDLE_NULL
;
174 return handle
->usesyslog
;
177 alpm_list_t SYMEXPORT
*alpm_option_get_noupgrades()
179 if (handle
== NULL
) {
180 pm_errno
= PM_ERR_HANDLE_NULL
;
183 return handle
->noupgrade
;
186 alpm_list_t SYMEXPORT
*alpm_option_get_noextracts()
188 if (handle
== NULL
) {
189 pm_errno
= PM_ERR_HANDLE_NULL
;
192 return handle
->noextract
;
195 alpm_list_t SYMEXPORT
*alpm_option_get_ignorepkgs()
197 if (handle
== NULL
) {
198 pm_errno
= PM_ERR_HANDLE_NULL
;
201 return handle
->ignorepkg
;
204 alpm_list_t SYMEXPORT
*alpm_option_get_holdpkgs()
206 if (handle
== NULL
) {
207 pm_errno
= PM_ERR_HANDLE_NULL
;
210 return handle
->holdpkg
;
213 alpm_list_t SYMEXPORT
*alpm_option_get_ignoregrps()
215 if (handle
== NULL
) {
216 pm_errno
= PM_ERR_HANDLE_NULL
;
219 return handle
->ignoregrp
;
222 const char SYMEXPORT
*alpm_option_get_xfercommand()
224 if (handle
== NULL
) {
225 pm_errno
= PM_ERR_HANDLE_NULL
;
228 return handle
->xfercommand
;
231 unsigned short SYMEXPORT
alpm_option_get_nopassiveftp()
233 if (handle
== NULL
) {
234 pm_errno
= PM_ERR_HANDLE_NULL
;
237 return handle
->nopassiveftp
;
240 pmdb_t SYMEXPORT
*alpm_option_get_localdb()
242 if (handle
== NULL
) {
243 pm_errno
= PM_ERR_HANDLE_NULL
;
246 return handle
->db_local
;
249 alpm_list_t SYMEXPORT
*alpm_option_get_syncdbs()
251 if (handle
== NULL
) {
252 pm_errno
= PM_ERR_HANDLE_NULL
;
255 return handle
->dbs_sync
;
258 void SYMEXPORT
alpm_option_set_logcb(alpm_cb_log cb
)
260 if (handle
== NULL
) {
261 pm_errno
= PM_ERR_HANDLE_NULL
;
267 void SYMEXPORT
alpm_option_set_dlcb(alpm_cb_download cb
)
269 if (handle
== NULL
) {
270 pm_errno
= PM_ERR_HANDLE_NULL
;
276 int SYMEXPORT
alpm_option_set_root(const char *root
)
285 pm_errno
= PM_ERR_WRONG_ARGS
;
288 if(stat(root
, &st
) == -1 || !S_ISDIR(st
.st_mode
)) {
289 pm_errno
= PM_ERR_NOT_A_DIR
;
293 realroot
= calloc(PATH_MAX
+1, sizeof(char));
294 if(!realpath(root
, realroot
)) {
295 pm_errno
= PM_ERR_NOT_A_DIR
;
299 /* verify root ends in a '/' */
300 rootlen
= strlen(realroot
);
301 if(realroot
[rootlen
-1] != '/') {
307 handle
->root
= calloc(rootlen
+ 1, sizeof(char));
308 strncpy(handle
->root
, realroot
, rootlen
);
309 handle
->root
[rootlen
-1] = '/';
311 _alpm_log(PM_LOG_DEBUG
, "option 'root' = %s\n", handle
->root
);
315 int SYMEXPORT
alpm_option_set_dbpath(const char *dbpath
)
318 size_t dbpathlen
, lockfilelen
;
319 const char *lf
= "db.lck";
324 pm_errno
= PM_ERR_WRONG_ARGS
;
327 if(stat(dbpath
, &st
) == -1 || !S_ISDIR(st
.st_mode
)) {
328 pm_errno
= PM_ERR_NOT_A_DIR
;
331 /* verify dbpath ends in a '/' */
332 dbpathlen
= strlen(dbpath
);
333 if(dbpath
[dbpathlen
-1] != '/') {
337 FREE(handle
->dbpath
);
339 handle
->dbpath
= calloc(dbpathlen
+1, sizeof(char));
340 strncpy(handle
->dbpath
, dbpath
, dbpathlen
);
341 handle
->dbpath
[dbpathlen
-1] = '/';
342 _alpm_log(PM_LOG_DEBUG
, "option 'dbpath' = %s\n", handle
->dbpath
);
344 if(handle
->lockfile
) {
345 FREE(handle
->lockfile
);
347 lockfilelen
= strlen(handle
->dbpath
) + strlen(lf
) + 1;
348 handle
->lockfile
= calloc(lockfilelen
, sizeof(char));
349 snprintf(handle
->lockfile
, lockfilelen
, "%s%s", handle
->dbpath
, lf
);
350 _alpm_log(PM_LOG_DEBUG
, "option 'lockfile' = %s\n", handle
->lockfile
);
354 int SYMEXPORT
alpm_option_add_cachedir(const char *cachedir
)
362 pm_errno
= PM_ERR_WRONG_ARGS
;
365 /* don't stat the cachedir yet, as it may not even be needed. we can
366 * fail later if it is needed and the path is invalid. */
368 /* verify cachedir ends in a '/' */
369 cachedirlen
= strlen(cachedir
);
370 if(cachedir
[cachedirlen
-1] != '/') {
373 newcachedir
= calloc(cachedirlen
+ 1, sizeof(char));
374 strncpy(newcachedir
, cachedir
, cachedirlen
);
375 newcachedir
[cachedirlen
-1] = '/';
376 handle
->cachedirs
= alpm_list_add(handle
->cachedirs
, newcachedir
);
377 _alpm_log(PM_LOG_DEBUG
, "option 'cachedir' = %s\n", newcachedir
);
381 void SYMEXPORT
alpm_option_set_cachedirs(alpm_list_t
*cachedirs
)
383 if(handle
->cachedirs
) FREELIST(handle
->cachedirs
);
384 if(cachedirs
) handle
->cachedirs
= cachedirs
;
387 int SYMEXPORT
alpm_option_remove_cachedir(const char *cachedir
)
392 /* verify cachedir ends in a '/' */
393 cachedirlen
= strlen(cachedir
);
394 if(cachedir
[cachedirlen
-1] != '/') {
397 newcachedir
= calloc(cachedirlen
+ 1, sizeof(char));
398 strncpy(newcachedir
, cachedir
, cachedirlen
);
399 newcachedir
[cachedirlen
-1] = '/';
400 handle
->cachedirs
= alpm_list_remove(handle
->cachedirs
, newcachedir
,
401 _alpm_str_cmp
, &vdata
);
410 int SYMEXPORT
alpm_option_set_logfile(const char *logfile
)
412 char *oldlogfile
= handle
->logfile
;
417 pm_errno
= PM_ERR_WRONG_ARGS
;
421 handle
->logfile
= strdup(logfile
);
423 /* free the old logfile path string, and close the stream so logaction
424 * will reopen a new stream on the new logfile */
428 if(handle
->logstream
) {
429 fclose(handle
->logstream
);
430 handle
->logstream
= NULL
;
432 _alpm_log(PM_LOG_DEBUG
, "option 'logfile' = %s\n", handle
->logfile
);
436 void SYMEXPORT
alpm_option_set_usesyslog(unsigned short usesyslog
)
438 handle
->usesyslog
= usesyslog
;
441 void SYMEXPORT
alpm_option_add_noupgrade(const char *pkg
)
443 handle
->noupgrade
= alpm_list_add(handle
->noupgrade
, strdup(pkg
));
446 void SYMEXPORT
alpm_option_set_noupgrades(alpm_list_t
*noupgrade
)
448 if(handle
->noupgrade
) FREELIST(handle
->noupgrade
);
449 if(noupgrade
) handle
->noupgrade
= noupgrade
;
452 int SYMEXPORT
alpm_option_remove_noupgrade(const char *pkg
)
455 handle
->noupgrade
= alpm_list_remove(handle
->noupgrade
, pkg
,
456 _alpm_str_cmp
, &vdata
);
464 void SYMEXPORT
alpm_option_add_noextract(const char *pkg
)
466 handle
->noextract
= alpm_list_add(handle
->noextract
, strdup(pkg
));
469 void SYMEXPORT
alpm_option_set_noextracts(alpm_list_t
*noextract
)
471 if(handle
->noextract
) FREELIST(handle
->noextract
);
472 if(noextract
) handle
->noextract
= noextract
;
475 int SYMEXPORT
alpm_option_remove_noextract(const char *pkg
)
478 handle
->noextract
= alpm_list_remove(handle
->noextract
, pkg
,
479 _alpm_str_cmp
, &vdata
);
487 void SYMEXPORT
alpm_option_add_ignorepkg(const char *pkg
)
489 handle
->ignorepkg
= alpm_list_add(handle
->ignorepkg
, strdup(pkg
));
492 void SYMEXPORT
alpm_option_set_ignorepkgs(alpm_list_t
*ignorepkgs
)
494 if(handle
->ignorepkg
) FREELIST(handle
->ignorepkg
);
495 if(ignorepkgs
) handle
->ignorepkg
= ignorepkgs
;
498 int SYMEXPORT
alpm_option_remove_ignorepkg(const char *pkg
)
501 handle
->ignorepkg
= alpm_list_remove(handle
->ignorepkg
, pkg
,
502 _alpm_str_cmp
, &vdata
);
510 void SYMEXPORT
alpm_option_add_holdpkg(const char *pkg
)
512 handle
->holdpkg
= alpm_list_add(handle
->holdpkg
, strdup(pkg
));
515 void SYMEXPORT
alpm_option_set_holdpkgs(alpm_list_t
*holdpkgs
)
517 if(handle
->holdpkg
) FREELIST(handle
->holdpkg
);
518 if(holdpkgs
) handle
->holdpkg
= holdpkgs
;
521 int SYMEXPORT
alpm_option_remove_holdpkg(const char *pkg
)
524 handle
->holdpkg
= alpm_list_remove(handle
->holdpkg
, pkg
,
525 _alpm_str_cmp
, &vdata
);
533 void SYMEXPORT
alpm_option_add_ignoregrp(const char *grp
)
535 handle
->ignoregrp
= alpm_list_add(handle
->ignoregrp
, strdup(grp
));
538 void SYMEXPORT
alpm_option_set_ignoregrps(alpm_list_t
*ignoregrps
)
540 if(handle
->ignoregrp
) FREELIST(handle
->ignoregrp
);
541 if(ignoregrps
) handle
->ignoregrp
= ignoregrps
;
544 int SYMEXPORT
alpm_option_remove_ignoregrp(const char *grp
)
547 handle
->ignoregrp
= alpm_list_remove(handle
->ignoregrp
, grp
,
548 _alpm_str_cmp
, &vdata
);
556 void SYMEXPORT
alpm_option_set_xfercommand(const char *cmd
)
558 if(handle
->xfercommand
) FREE(handle
->xfercommand
);
559 if(cmd
) handle
->xfercommand
= strdup(cmd
);
562 void SYMEXPORT
alpm_option_set_nopassiveftp(unsigned short nopasv
)
564 handle
->nopassiveftp
= nopasv
;
567 void SYMEXPORT
alpm_option_set_usedelta(unsigned short usedelta
)
569 handle
->usedelta
= usedelta
;
572 /* vim: set ts=2 sw=2 noet: */