Read pkgcache into hash
[pacman-ng.git] / lib / libalpm / handle.c
blob8872ed0ad27c6002493a3c45b93d1e619131ba81
1 /*
2 * handle.c
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/>.
23 #include "config.h"
25 #include <stdlib.h>
26 #include <string.h>
27 #include <limits.h>
28 #include <sys/types.h>
29 #include <syslog.h>
30 #include <time.h>
31 #include <sys/stat.h>
32 #include <errno.h>
34 /* libalpm */
35 #include "handle.h"
36 #include "alpm_list.h"
37 #include "util.h"
38 #include "log.h"
39 #include "trans.h"
40 #include "alpm.h"
42 /* global var for handle (private to libalpm) */
43 pmhandle_t *handle = NULL;
45 pmhandle_t *_alpm_handle_new()
47 pmhandle_t *handle;
49 ALPM_LOG_FUNC;
51 CALLOC(handle, 1, sizeof(pmhandle_t), RET_ERR(PM_ERR_MEMORY, NULL));
52 handle->lckfd = -1;
54 return(handle);
57 void _alpm_handle_free(pmhandle_t *handle)
59 ALPM_LOG_FUNC;
61 if(handle == NULL) {
62 return;
65 /* close logfile */
66 if(handle->logstream) {
67 fclose(handle->logstream);
68 handle->logstream= NULL;
70 if(handle->usesyslog) {
71 handle->usesyslog = 0;
72 closelog();
75 /* free memory */
76 _alpm_trans_free(handle->trans);
77 FREE(handle->root);
78 FREE(handle->dbpath);
79 FREELIST(handle->cachedirs);
80 FREE(handle->logfile);
81 FREE(handle->lockfile);
82 FREE(handle->arch);
83 FREELIST(handle->dbs_sync);
84 FREELIST(handle->noupgrade);
85 FREELIST(handle->noextract);
86 FREELIST(handle->ignorepkg);
87 FREELIST(handle->ignoregrp);
88 FREE(handle);
91 alpm_cb_log SYMEXPORT alpm_option_get_logcb()
93 if (handle == NULL) {
94 pm_errno = PM_ERR_HANDLE_NULL;
95 return NULL;
97 return handle->logcb;
100 alpm_cb_download SYMEXPORT alpm_option_get_dlcb()
102 if (handle == NULL) {
103 pm_errno = PM_ERR_HANDLE_NULL;
104 return NULL;
106 return handle->dlcb;
109 alpm_cb_fetch SYMEXPORT alpm_option_get_fetchcb()
111 if (handle == NULL) {
112 pm_errno = PM_ERR_HANDLE_NULL;
113 return 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;
122 return NULL;
124 return handle->totaldlcb;
127 const char SYMEXPORT *alpm_option_get_root()
129 if (handle == NULL) {
130 pm_errno = PM_ERR_HANDLE_NULL;
131 return NULL;
133 return handle->root;
136 const char SYMEXPORT *alpm_option_get_dbpath()
138 if (handle == NULL) {
139 pm_errno = PM_ERR_HANDLE_NULL;
140 return 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;
149 return NULL;
151 return handle->cachedirs;
154 const char SYMEXPORT *alpm_option_get_logfile()
156 if (handle == NULL) {
157 pm_errno = PM_ERR_HANDLE_NULL;
158 return NULL;
160 return handle->logfile;
163 const char SYMEXPORT *alpm_option_get_lockfile()
165 if (handle == NULL) {
166 pm_errno = PM_ERR_HANDLE_NULL;
167 return NULL;
169 return handle->lockfile;
172 int SYMEXPORT alpm_option_get_usesyslog()
174 if (handle == NULL) {
175 pm_errno = PM_ERR_HANDLE_NULL;
176 return -1;
178 return handle->usesyslog;
181 alpm_list_t SYMEXPORT *alpm_option_get_noupgrades()
183 if (handle == NULL) {
184 pm_errno = PM_ERR_HANDLE_NULL;
185 return 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;
194 return 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;
203 return 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;
212 return NULL;
214 return handle->ignoregrp;
217 const char SYMEXPORT *alpm_option_get_arch()
219 if (handle == NULL) {
220 pm_errno = PM_ERR_HANDLE_NULL;
221 return NULL;
223 return handle->arch;
226 int SYMEXPORT alpm_option_get_usedelta()
228 if (handle == NULL) {
229 pm_errno = PM_ERR_HANDLE_NULL;
230 return -1;
232 return handle->usedelta;
235 int SYMEXPORT alpm_option_get_checkspace()
237 if (handle == NULL) {
238 pm_errno = PM_ERR_HANDLE_NULL;
239 return -1;
241 return handle->checkspace;
244 pmdb_t SYMEXPORT *alpm_option_get_localdb()
246 if (handle == NULL) {
247 pm_errno = PM_ERR_HANDLE_NULL;
248 return 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;
257 return 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;
266 return;
268 handle->logcb = cb;
271 void SYMEXPORT alpm_option_set_dlcb(alpm_cb_download cb)
273 if (handle == NULL) {
274 pm_errno = PM_ERR_HANDLE_NULL;
275 return;
277 handle->dlcb = cb;
280 void SYMEXPORT alpm_option_set_fetchcb(alpm_cb_fetch cb)
282 if (handle == NULL) {
283 pm_errno = PM_ERR_HANDLE_NULL;
284 return;
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;
293 return;
295 handle->totaldlcb = cb;
298 int SYMEXPORT alpm_option_set_root(const char *root)
300 struct stat st;
301 char *realroot;
302 size_t rootlen;
304 ALPM_LOG_FUNC;
306 if(!root) {
307 pm_errno = PM_ERR_WRONG_ARGS;
308 return(-1);
310 if(stat(root, &st) == -1 || !S_ISDIR(st.st_mode)) {
311 pm_errno = PM_ERR_NOT_A_DIR;
312 return(-1);
315 realroot = calloc(PATH_MAX+1, sizeof(char));
316 if(!realpath(root, realroot)) {
317 FREE(realroot);
318 pm_errno = PM_ERR_NOT_A_DIR;
319 return(-1);
322 /* verify root ends in a '/' */
323 rootlen = strlen(realroot);
324 if(realroot[rootlen-1] != '/') {
325 rootlen += 1;
327 if(handle->root) {
328 FREE(handle->root);
330 handle->root = calloc(rootlen + 1, sizeof(char));
331 strncpy(handle->root, realroot, rootlen);
332 handle->root[rootlen-1] = '/';
333 FREE(realroot);
334 _alpm_log(PM_LOG_DEBUG, "option 'root' = %s\n", handle->root);
335 return(0);
338 int SYMEXPORT alpm_option_set_dbpath(const char *dbpath)
340 struct stat st;
341 size_t dbpathlen, lockfilelen;
342 const char *lf = "db.lck";
344 ALPM_LOG_FUNC;
346 if(!dbpath) {
347 pm_errno = PM_ERR_WRONG_ARGS;
348 return(-1);
350 if(stat(dbpath, &st) == -1 || !S_ISDIR(st.st_mode)) {
351 pm_errno = PM_ERR_NOT_A_DIR;
352 return(-1);
354 /* verify dbpath ends in a '/' */
355 dbpathlen = strlen(dbpath);
356 if(dbpath[dbpathlen-1] != '/') {
357 dbpathlen += 1;
359 if(handle->dbpath) {
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);
374 return(0);
377 int SYMEXPORT alpm_option_add_cachedir(const char *cachedir)
379 char *newcachedir;
380 size_t cachedirlen;
382 ALPM_LOG_FUNC;
384 if(!cachedir) {
385 pm_errno = PM_ERR_WRONG_ARGS;
386 return(-1);
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] != '/') {
394 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);
401 return(0);
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)
412 char *vdata = NULL;
413 char *newcachedir;
414 size_t cachedirlen;
415 /* verify cachedir ends in a '/' */
416 cachedirlen = strlen(cachedir);
417 if(cachedir[cachedirlen-1] != '/') {
418 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);
424 FREE(newcachedir);
425 if(vdata != NULL) {
426 FREE(vdata);
427 return(1);
429 return(0);
432 int SYMEXPORT alpm_option_set_logfile(const char *logfile)
434 char *oldlogfile = handle->logfile;
436 ALPM_LOG_FUNC;
438 if(!logfile) {
439 pm_errno = PM_ERR_WRONG_ARGS;
440 return(-1);
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 */
447 if(oldlogfile) {
448 FREE(oldlogfile);
450 if(handle->logstream) {
451 fclose(handle->logstream);
452 handle->logstream = NULL;
454 _alpm_log(PM_LOG_DEBUG, "option 'logfile' = %s\n", handle->logfile);
455 return(0);
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)
476 char *vdata = NULL;
477 handle->noupgrade = alpm_list_remove_str(handle->noupgrade, pkg, &vdata);
478 if(vdata != NULL) {
479 FREE(vdata);
480 return(1);
482 return(0);
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)
498 char *vdata = NULL;
499 handle->noextract = alpm_list_remove_str(handle->noextract, pkg, &vdata);
500 if(vdata != NULL) {
501 FREE(vdata);
502 return(1);
504 return(0);
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)
520 char *vdata = NULL;
521 handle->ignorepkg = alpm_list_remove_str(handle->ignorepkg, pkg, &vdata);
522 if(vdata != NULL) {
523 FREE(vdata);
524 return(1);
526 return(0);
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)
542 char *vdata = NULL;
543 handle->ignoregrp = alpm_list_remove_str(handle->ignoregrp, grp, &vdata);
544 if(vdata != NULL) {
545 FREE(vdata);
546 return(1);
548 return(0);
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: */