Tidy up of the handle struct in libalpm
[pacman-ng.git] / lib / libalpm / handle.c
blobc01dd5515a333202f91f8dc0e4b1279899049542
1 /*
2 * handle.c
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/>.
22 #include "config.h"
24 #include <stdlib.h>
25 #include <string.h>
26 #include <limits.h>
27 #include <sys/types.h>
28 #include <syslog.h>
29 #include <time.h>
30 #include <sys/stat.h>
31 #include <errno.h>
33 /* libalpm */
34 #include "handle.h"
35 #include "alpm_list.h"
36 #include "util.h"
37 #include "log.h"
38 #include "trans.h"
39 #include "alpm.h"
41 /* global var for handle (private to libalpm) */
42 pmhandle_t *handle = NULL;
44 pmhandle_t *_alpm_handle_new()
46 pmhandle_t *handle;
48 ALPM_LOG_FUNC;
50 CALLOC(handle, 1, sizeof(pmhandle_t), RET_ERR(PM_ERR_MEMORY, NULL));
52 handle->lckfd = -1;
53 handle->logstream = NULL;
55 handle->root = NULL;
56 handle->dbpath = NULL;
57 handle->cachedirs = NULL;
58 handle->lockfile = NULL;
59 handle->logfile = NULL;
60 handle->usedelta = 0;
62 return(handle);
65 void _alpm_handle_free(pmhandle_t *handle)
67 ALPM_LOG_FUNC;
69 if(handle == NULL) {
70 return;
73 /* close logfile */
74 if(handle->logstream) {
75 fclose(handle->logstream);
76 handle->logstream= NULL;
78 if(handle->usesyslog) {
79 handle->usesyslog = 0;
80 closelog();
83 /* free memory */
84 _alpm_trans_free(handle->trans);
85 FREE(handle->root);
86 FREE(handle->dbpath);
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);
97 FREE(handle);
100 alpm_cb_log SYMEXPORT alpm_option_get_logcb()
102 if (handle == NULL) {
103 pm_errno = PM_ERR_HANDLE_NULL;
104 return 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;
113 return NULL;
115 return handle->dlcb;
118 const char SYMEXPORT *alpm_option_get_root()
120 if (handle == NULL) {
121 pm_errno = PM_ERR_HANDLE_NULL;
122 return NULL;
124 return handle->root;
127 const char SYMEXPORT *alpm_option_get_dbpath()
129 if (handle == NULL) {
130 pm_errno = PM_ERR_HANDLE_NULL;
131 return 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;
140 return NULL;
142 return handle->cachedirs;
145 const char SYMEXPORT *alpm_option_get_logfile()
147 if (handle == NULL) {
148 pm_errno = PM_ERR_HANDLE_NULL;
149 return NULL;
151 return handle->logfile;
154 const char SYMEXPORT *alpm_option_get_lockfile()
156 if (handle == NULL) {
157 pm_errno = PM_ERR_HANDLE_NULL;
158 return NULL;
160 return handle->lockfile;
163 unsigned short SYMEXPORT alpm_option_get_usesyslog()
165 if (handle == NULL) {
166 pm_errno = PM_ERR_HANDLE_NULL;
167 return -1;
169 return handle->usesyslog;
172 alpm_list_t SYMEXPORT *alpm_option_get_noupgrades()
174 if (handle == NULL) {
175 pm_errno = PM_ERR_HANDLE_NULL;
176 return 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;
185 return 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;
194 return 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;
203 return 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;
212 return NULL;
214 return handle->ignoregrp;
217 const char SYMEXPORT *alpm_option_get_xfercommand()
219 if (handle == NULL) {
220 pm_errno = PM_ERR_HANDLE_NULL;
221 return NULL;
223 return handle->xfercommand;
226 unsigned short SYMEXPORT alpm_option_get_nopassiveftp()
228 if (handle == NULL) {
229 pm_errno = PM_ERR_HANDLE_NULL;
230 return -1;
232 return handle->nopassiveftp;
235 pmdb_t SYMEXPORT *alpm_option_get_localdb()
237 if (handle == NULL) {
238 pm_errno = PM_ERR_HANDLE_NULL;
239 return 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;
248 return 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;
257 return;
259 handle->logcb = cb;
262 void SYMEXPORT alpm_option_set_dlcb(alpm_cb_download cb)
264 if (handle == NULL) {
265 pm_errno = PM_ERR_HANDLE_NULL;
266 return;
268 handle->dlcb = cb;
271 int SYMEXPORT alpm_option_set_root(const char *root)
273 struct stat st;
274 char *realroot;
275 size_t rootlen;
277 ALPM_LOG_FUNC;
279 if(!root) {
280 pm_errno = PM_ERR_WRONG_ARGS;
281 return(-1);
283 if(stat(root, &st) == -1 || !S_ISDIR(st.st_mode)) {
284 pm_errno = PM_ERR_NOT_A_DIR;
285 return(-1);
288 realroot = calloc(PATH_MAX+1, sizeof(char));
289 if(!realpath(root, realroot)) {
290 pm_errno = PM_ERR_NOT_A_DIR;
291 return(-1);
294 /* verify root ends in a '/' */
295 rootlen = strlen(realroot);
296 if(realroot[rootlen-1] != '/') {
297 rootlen += 1;
299 if(handle->root) {
300 FREE(handle->root);
302 handle->root = calloc(rootlen + 1, sizeof(char));
303 strncpy(handle->root, realroot, rootlen);
304 handle->root[rootlen-1] = '/';
305 FREE(realroot);
306 _alpm_log(PM_LOG_DEBUG, "option 'root' = %s\n", handle->root);
307 return(0);
310 int SYMEXPORT alpm_option_set_dbpath(const char *dbpath)
312 struct stat st;
313 size_t dbpathlen, lockfilelen;
314 const char *lf = "db.lck";
316 ALPM_LOG_FUNC;
318 if(!dbpath) {
319 pm_errno = PM_ERR_WRONG_ARGS;
320 return(-1);
322 if(stat(dbpath, &st) == -1 || !S_ISDIR(st.st_mode)) {
323 pm_errno = PM_ERR_NOT_A_DIR;
324 return(-1);
326 /* verify dbpath ends in a '/' */
327 dbpathlen = strlen(dbpath);
328 if(dbpath[dbpathlen-1] != '/') {
329 dbpathlen += 1;
331 if(handle->dbpath) {
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);
346 return(0);
349 int SYMEXPORT alpm_option_add_cachedir(const char *cachedir)
351 char *newcachedir;
352 size_t cachedirlen;
354 ALPM_LOG_FUNC;
356 if(!cachedir) {
357 pm_errno = PM_ERR_WRONG_ARGS;
358 return(-1);
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] != '/') {
366 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);
373 return(0);
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)
384 char *vdata = NULL;
385 char *newcachedir;
386 size_t cachedirlen;
387 /* verify cachedir ends in a '/' */
388 cachedirlen = strlen(cachedir);
389 if(cachedir[cachedirlen-1] != '/') {
390 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);
396 FREE(newcachedir);
397 if(vdata != NULL) {
398 FREE(vdata);
399 return(1);
401 return(0);
404 int SYMEXPORT alpm_option_set_logfile(const char *logfile)
406 char *oldlogfile = handle->logfile;
408 ALPM_LOG_FUNC;
410 if(!logfile) {
411 pm_errno = PM_ERR_WRONG_ARGS;
412 return(-1);
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 */
419 if(oldlogfile) {
420 FREE(oldlogfile);
422 if(handle->logstream) {
423 fclose(handle->logstream);
424 handle->logstream = NULL;
426 _alpm_log(PM_LOG_DEBUG, "option 'logfile' = %s\n", handle->logfile);
427 return(0);
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)
448 char *vdata = NULL;
449 handle->noupgrade = alpm_list_remove_str(handle->noupgrade, pkg, &vdata);
450 if(vdata != NULL) {
451 FREE(vdata);
452 return(1);
454 return(0);
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)
470 char *vdata = NULL;
471 handle->noextract = alpm_list_remove_str(handle->noextract, pkg, &vdata);
472 if(vdata != NULL) {
473 FREE(vdata);
474 return(1);
476 return(0);
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)
492 char *vdata = NULL;
493 handle->ignorepkg = alpm_list_remove_str(handle->ignorepkg, pkg, &vdata);
494 if(vdata != NULL) {
495 FREE(vdata);
496 return(1);
498 return(0);
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)
514 char *vdata = NULL;
515 handle->holdpkg = alpm_list_remove_str(handle->holdpkg, pkg, &vdata);
516 if(vdata != NULL) {
517 FREE(vdata);
518 return(1);
520 return(0);
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)
536 char *vdata = NULL;
537 handle->ignoregrp = alpm_list_remove_str(handle->ignoregrp, grp, &vdata);
538 if(vdata != NULL) {
539 FREE(vdata);
540 return(1);
542 return(0);
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: */