A few final changes for the 3.0.6 release
[pacman.git] / lib / libalpm / handle.c
bloba6bb5bc8a7aef87a718c6b4ec12cf4f1506d85c2
1 /*
2 * handle.c
3 *
4 * Copyright (c) 2002-2006 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>
7 *
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, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 * USA.
24 #include "config.h"
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <limits.h>
30 #include <sys/types.h>
31 #include <syslog.h>
32 #include <libintl.h>
33 #include <time.h>
35 /* libalpm */
36 #include "handle.h"
37 #include "alpm_list.h"
38 #include "util.h"
39 #include "log.h"
40 #include "error.h"
41 #include "trans.h"
42 #include "alpm.h"
43 #include "server.h"
45 pmhandle_t *_alpm_handle_new()
47 pmhandle_t *handle;
49 handle = (pmhandle_t *)malloc(sizeof(pmhandle_t));
50 if(handle == NULL) {
51 _alpm_log(PM_LOG_ERROR, _("malloc failure: could not allocate %d bytes"), sizeof(pmhandle_t));
52 RET_ERR(PM_ERR_MEMORY, NULL);
55 memset(handle, 0, sizeof(pmhandle_t));
56 handle->lckfd = -1;
58 #ifndef CYGWIN
59 /* see if we're root or not */
60 handle->uid = geteuid();
61 //#ifndef FAKEROOT
62 // if(!handle->uid && getenv("FAKEROOTKEY")) {
63 // /* fakeroot doesn't count, we're non-root */
64 // handle->uid = 99;
65 // }
66 //#endif
68 // /* see if we're root or not (fakeroot does not count) */
69 //#ifndef FAKEROOT
70 // if(handle->uid == 0 && !getenv("FAKEROOTKEY")) {
71 // /* } make vim indent work - stupid ifdef's */
72 //#else
73 // if(handle->uid == 0) {
74 //#endif
75 // handle->access = PM_ACCESS_RW;
76 // } else {
77 // handle->access = PM_ACCESS_RO;
78 // }
79 //#else
80 handle->access = PM_ACCESS_RW;
81 #endif
83 handle->root = strdup(PM_ROOT);
84 handle->dbpath = strdup(PM_DBPATH);
85 handle->cachedir = strdup(PM_CACHEDIR);
86 handle->logmask = PM_LOG_ERROR | PM_LOG_WARNING;
88 return(handle);
91 int _alpm_handle_free(pmhandle_t *handle)
93 ALPM_LOG_FUNC;
95 ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1));
97 /* close logfiles */
98 if(handle->logfd) {
99 fclose(handle->logfd);
100 handle->logfd = NULL;
102 if(handle->usesyslog) {
103 handle->usesyslog = 0;
104 closelog();
107 /* free memory */
108 FREETRANS(handle->trans);
109 FREE(handle->root);
110 FREE(handle->dbpath);
111 FREE(handle->cachedir);
112 FREE(handle->logfile);
113 FREE(handle->xfercommand);
114 FREELIST(handle->dbs_sync);
115 FREELIST(handle->noupgrade);
116 FREELIST(handle->noextract);
117 FREELIST(handle->ignorepkg);
118 FREELIST(handle->holdpkg);
119 FREE(handle);
121 return(0);
124 alpm_cb_log alpm_option_get_logcb() { return (handle ? handle->logcb : NULL); }
125 alpm_cb_download alpm_option_get_dlcb() { return (handle ? handle->dlcb : NULL); }
126 unsigned short SYMEXPORT alpm_option_get_logmask() { return handle->logmask; }
127 const char SYMEXPORT *alpm_option_get_root() { return handle->root; }
128 const char SYMEXPORT *alpm_option_get_dbpath() { return handle->dbpath; }
129 const char SYMEXPORT *alpm_option_get_cachedir() { return handle->cachedir; }
130 const char *alpm_option_get_logfile() { return handle->logfile; }
131 unsigned short alpm_option_get_usesyslog() { return handle->usesyslog; }
132 alpm_list_t *alpm_option_get_noupgrades() { return handle->noupgrade; }
133 alpm_list_t *alpm_option_get_noextracts() { return handle->noextract; }
134 alpm_list_t *alpm_option_get_ignorepkgs() { return handle->ignorepkg; }
135 alpm_list_t *alpm_option_get_holdpkgs() { return handle->holdpkg; }
136 time_t alpm_option_get_upgradedelay() { return handle->upgradedelay; }
137 const char *alpm_option_get_xfercommand() { return handle->xfercommand; }
138 unsigned short alpm_option_get_nopassiveftp() { return handle->nopassiveftp; }
139 unsigned short SYMEXPORT alpm_option_get_chomp() { return handle->chomp; }
140 unsigned short alpm_option_get_usecolor() { return handle->use_color; }
142 pmdb_t SYMEXPORT *alpm_option_get_localdb() { return handle->db_local; }
143 alpm_list_t SYMEXPORT *alpm_option_get_syncdbs()
145 return handle->dbs_sync;
148 void SYMEXPORT alpm_option_set_logcb(alpm_cb_log cb) { handle->logcb = cb; }
150 void SYMEXPORT alpm_option_set_dlcb(alpm_cb_download cb) { handle->dlcb = cb; }
152 void SYMEXPORT alpm_option_set_logmask(unsigned short mask) { handle->logmask = mask; }
154 void SYMEXPORT alpm_option_set_root(const char *root)
156 if(handle->root) FREE(handle->root);
157 /* According to the man page, realpath is safe to use IFF the second arg is
158 * NULL. */
159 char *realroot = realpath(root, NULL);
160 if(realroot) {
161 root = realroot;
162 } else {
163 _alpm_log(PM_LOG_ERROR, _("cannot canonicalize specified root path '%s'"), root);
166 if(root) {
167 /* verify root ends in a '/' */
168 int rootlen = strlen(realroot);
169 if(realroot[rootlen-1] != '/') {
170 rootlen += 1;
172 handle->root = calloc(rootlen+1, sizeof(char));
173 strncpy(handle->root, realroot, rootlen);
174 handle->root[rootlen-1] = '/';
175 _alpm_log(PM_LOG_DEBUG, _("option 'root' = %s"), handle->root);
178 if(realroot) {
179 free(realroot);
183 void SYMEXPORT alpm_option_set_dbpath(const char *dbpath)
185 if(handle->dbpath) FREE(handle->dbpath);
186 if(dbpath) {
187 /* verify dbpath ends in a '/' */
188 int dbpathlen = strlen(dbpath);
189 if(dbpath[dbpathlen-1] != '/') {
190 dbpathlen += 1;
192 handle->dbpath = calloc(dbpathlen+1, sizeof(char));
193 strncpy(handle->dbpath, dbpath, dbpathlen);
194 handle->dbpath[dbpathlen-1] = '/';
195 _alpm_log(PM_LOG_DEBUG, _("option 'dbpath' = %s"), handle->dbpath);
199 void SYMEXPORT alpm_option_set_cachedir(const char *cachedir)
201 if(handle->cachedir) FREE(handle->cachedir);
202 if(cachedir) {
203 /* verify cachedir ends in a '/' */
204 int cachedirlen = strlen(cachedir);
205 if(cachedir[cachedirlen-1] != '/') {
206 cachedirlen += 1;
208 handle->cachedir = calloc(cachedirlen+1, sizeof(char));
209 strncpy(handle->cachedir, cachedir, cachedirlen);
210 handle->cachedir[cachedirlen-1] = '/';
211 _alpm_log(PM_LOG_DEBUG, _("option 'cachedir' = %s"), handle->cachedir);
215 void alpm_option_set_logfile(const char *logfile)
217 ALPM_LOG_FUNC;
219 if(handle->logfile) {
220 FREE(handle->logfile);
221 if(handle->logfd) {
222 fclose(handle->logfd);
223 handle->logfd = NULL;
226 if(logfile) {
227 handle->logfile = strdup(logfile);
228 handle->logfd = fopen(logfile, "a");
232 void alpm_option_set_usesyslog(unsigned short usesyslog)
234 handle->usesyslog = usesyslog;
237 void alpm_option_add_noupgrade(char *pkg)
239 handle->noupgrade = alpm_list_add(handle->noupgrade, strdup(pkg));
242 void alpm_option_set_noupgrades(alpm_list_t *noupgrade)
244 if(handle->noupgrade) FREELIST(handle->noupgrade);
245 if(noupgrade) handle->noupgrade = noupgrade;
248 void alpm_option_add_noextract(char *pkg)
250 handle->noextract = alpm_list_add(handle->noextract, strdup(pkg));
252 void alpm_option_set_noextracts(alpm_list_t *noextract)
254 if(handle->noextract) FREELIST(handle->noextract);
255 if(noextract) handle->noextract = noextract;
258 void SYMEXPORT alpm_option_add_ignorepkg(char *pkg)
260 handle->ignorepkg = alpm_list_add(handle->ignorepkg, strdup(pkg));
262 void alpm_option_set_ignorepkgs(alpm_list_t *ignorepkgs)
264 if(handle->ignorepkg) FREELIST(handle->ignorepkg);
265 if(ignorepkgs) handle->ignorepkg = ignorepkgs;
268 void alpm_option_add_holdpkg(char *pkg)
270 handle->holdpkg = alpm_list_add(handle->holdpkg, strdup(pkg));
272 void alpm_option_set_holdpkgs(alpm_list_t *holdpkgs)
274 if(handle->holdpkg) FREELIST(handle->holdpkg);
275 if(holdpkgs) handle->holdpkg = holdpkgs;
278 void alpm_option_set_upgradedelay(time_t delay)
280 handle->upgradedelay = delay;
283 void alpm_option_set_xfercommand(const char *cmd)
285 if(handle->xfercommand) FREE(handle->xfercommand);
286 if(cmd) handle->xfercommand = strdup(cmd);
289 void alpm_option_set_nopassiveftp(unsigned short nopasv)
291 handle->nopassiveftp = nopasv;
294 void alpm_option_set_chomp(unsigned short chomp) { handle->chomp = chomp; }
296 void alpm_option_set_usecolor(unsigned short usecolor)
298 handle->use_color = usecolor;
301 /* vim: set ts=2 sw=2 noet: */