Set handle->logstream to null after fclose()
[pacman-ng.git] / lib / libalpm / handle.c
blob368222850efd7f78cf722a2b7a7e9a09942134ae
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 <unistd.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 "error.h"
40 #include "trans.h"
41 #include "alpm.h"
42 #include "server.h"
44 /* global var for handle (private to libalpm) */
45 pmhandle_t *handle = NULL;
47 pmhandle_t *_alpm_handle_new()
49 pmhandle_t *handle;
51 ALPM_LOG_FUNC;
53 CALLOC(handle, 1, sizeof(pmhandle_t), RET_ERR(PM_ERR_MEMORY, NULL));
55 handle->lckfd = -1;
56 handle->logstream = NULL;
58 /* see if we're root or not */
59 handle->uid = geteuid();
60 handle->root = NULL;
61 handle->dbpath = NULL;
62 handle->cachedirs = NULL;
63 handle->lockfile = NULL;
64 handle->logfile = NULL;
65 handle->usedelta = 0;
67 return(handle);
70 void _alpm_handle_free(pmhandle_t *handle)
72 ALPM_LOG_FUNC;
74 if(handle == NULL) {
75 return;
78 /* close logfile */
79 if(handle->logstream) {
80 fclose(handle->logstream);
81 handle->logstream= NULL;
83 if(handle->usesyslog) {
84 handle->usesyslog = 0;
85 closelog();
88 /* free memory */
89 _alpm_trans_free(handle->trans);
90 FREE(handle->root);
91 FREE(handle->dbpath);
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);
102 FREE(handle);
105 alpm_cb_log SYMEXPORT alpm_option_get_logcb()
107 if (handle == NULL) {
108 pm_errno = PM_ERR_HANDLE_NULL;
109 return 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;
118 return NULL;
120 return handle->dlcb;
123 const char SYMEXPORT *alpm_option_get_root()
125 if (handle == NULL) {
126 pm_errno = PM_ERR_HANDLE_NULL;
127 return NULL;
129 return handle->root;
132 const char SYMEXPORT *alpm_option_get_dbpath()
134 if (handle == NULL) {
135 pm_errno = PM_ERR_HANDLE_NULL;
136 return 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;
145 return NULL;
147 return handle->cachedirs;
150 const char SYMEXPORT *alpm_option_get_logfile()
152 if (handle == NULL) {
153 pm_errno = PM_ERR_HANDLE_NULL;
154 return NULL;
156 return handle->logfile;
159 const char SYMEXPORT *alpm_option_get_lockfile()
161 if (handle == NULL) {
162 pm_errno = PM_ERR_HANDLE_NULL;
163 return NULL;
165 return handle->lockfile;
168 unsigned short SYMEXPORT alpm_option_get_usesyslog()
170 if (handle == NULL) {
171 pm_errno = PM_ERR_HANDLE_NULL;
172 return -1;
174 return handle->usesyslog;
177 alpm_list_t SYMEXPORT *alpm_option_get_noupgrades()
179 if (handle == NULL) {
180 pm_errno = PM_ERR_HANDLE_NULL;
181 return 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;
190 return 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;
199 return 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;
208 return 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;
217 return NULL;
219 return handle->ignoregrp;
222 const char SYMEXPORT *alpm_option_get_xfercommand()
224 if (handle == NULL) {
225 pm_errno = PM_ERR_HANDLE_NULL;
226 return NULL;
228 return handle->xfercommand;
231 unsigned short SYMEXPORT alpm_option_get_nopassiveftp()
233 if (handle == NULL) {
234 pm_errno = PM_ERR_HANDLE_NULL;
235 return -1;
237 return handle->nopassiveftp;
240 pmdb_t SYMEXPORT *alpm_option_get_localdb()
242 if (handle == NULL) {
243 pm_errno = PM_ERR_HANDLE_NULL;
244 return 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;
253 return 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;
262 return;
264 handle->logcb = cb;
267 void SYMEXPORT alpm_option_set_dlcb(alpm_cb_download cb)
269 if (handle == NULL) {
270 pm_errno = PM_ERR_HANDLE_NULL;
271 return;
273 handle->dlcb = cb;
276 int SYMEXPORT alpm_option_set_root(const char *root)
278 struct stat st;
279 char *realroot;
280 size_t rootlen;
282 ALPM_LOG_FUNC;
284 if(!root) {
285 pm_errno = PM_ERR_WRONG_ARGS;
286 return(-1);
288 if(stat(root, &st) == -1 || !S_ISDIR(st.st_mode)) {
289 pm_errno = PM_ERR_NOT_A_DIR;
290 return(-1);
293 realroot = calloc(PATH_MAX+1, sizeof(char));
294 if(!realpath(root, realroot)) {
295 pm_errno = PM_ERR_NOT_A_DIR;
296 return(-1);
299 /* verify root ends in a '/' */
300 rootlen = strlen(realroot);
301 if(realroot[rootlen-1] != '/') {
302 rootlen += 1;
304 if(handle->root) {
305 FREE(handle->root);
307 handle->root = calloc(rootlen + 1, sizeof(char));
308 strncpy(handle->root, realroot, rootlen);
309 handle->root[rootlen-1] = '/';
310 FREE(realroot);
311 _alpm_log(PM_LOG_DEBUG, "option 'root' = %s\n", handle->root);
312 return(0);
315 int SYMEXPORT alpm_option_set_dbpath(const char *dbpath)
317 struct stat st;
318 size_t dbpathlen, lockfilelen;
319 const char *lf = "db.lck";
321 ALPM_LOG_FUNC;
323 if(!dbpath) {
324 pm_errno = PM_ERR_WRONG_ARGS;
325 return(-1);
327 if(stat(dbpath, &st) == -1 || !S_ISDIR(st.st_mode)) {
328 pm_errno = PM_ERR_NOT_A_DIR;
329 return(-1);
331 /* verify dbpath ends in a '/' */
332 dbpathlen = strlen(dbpath);
333 if(dbpath[dbpathlen-1] != '/') {
334 dbpathlen += 1;
336 if(handle->dbpath) {
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);
351 return(0);
354 int SYMEXPORT alpm_option_add_cachedir(const char *cachedir)
356 char *newcachedir;
357 size_t cachedirlen;
359 ALPM_LOG_FUNC;
361 if(!cachedir) {
362 pm_errno = PM_ERR_WRONG_ARGS;
363 return(-1);
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] != '/') {
371 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);
378 return(0);
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)
389 void *vdata = NULL;
390 char *newcachedir;
391 size_t cachedirlen;
392 /* verify cachedir ends in a '/' */
393 cachedirlen = strlen(cachedir);
394 if(cachedir[cachedirlen-1] != '/') {
395 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);
402 FREE(newcachedir);
403 if(vdata != NULL) {
404 FREE(vdata);
405 return(1);
407 return(0);
410 int SYMEXPORT alpm_option_set_logfile(const char *logfile)
412 char *oldlogfile = handle->logfile;
414 ALPM_LOG_FUNC;
416 if(!logfile) {
417 pm_errno = PM_ERR_WRONG_ARGS;
418 return(-1);
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 */
425 if(oldlogfile) {
426 FREE(oldlogfile);
428 if(handle->logstream) {
429 fclose(handle->logstream);
430 handle->logstream = NULL;
432 _alpm_log(PM_LOG_DEBUG, "option 'logfile' = %s\n", handle->logfile);
433 return(0);
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)
454 void *vdata = NULL;
455 handle->noupgrade = alpm_list_remove(handle->noupgrade, pkg,
456 _alpm_str_cmp, &vdata);
457 if(vdata != NULL) {
458 FREE(vdata);
459 return(1);
461 return(0);
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)
477 void *vdata = NULL;
478 handle->noextract = alpm_list_remove(handle->noextract, pkg,
479 _alpm_str_cmp, &vdata);
480 if(vdata != NULL) {
481 FREE(vdata);
482 return(1);
484 return(0);
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)
500 void *vdata = NULL;
501 handle->ignorepkg = alpm_list_remove(handle->ignorepkg, pkg,
502 _alpm_str_cmp, &vdata);
503 if(vdata != NULL) {
504 FREE(vdata);
505 return(1);
507 return(0);
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)
523 void *vdata = NULL;
524 handle->holdpkg = alpm_list_remove(handle->holdpkg, pkg,
525 _alpm_str_cmp, &vdata);
526 if(vdata != NULL) {
527 FREE(vdata);
528 return(1);
530 return(0);
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)
546 void *vdata = NULL;
547 handle->ignoregrp = alpm_list_remove(handle->ignoregrp, grp,
548 _alpm_str_cmp, &vdata);
549 if(vdata != NULL) {
550 FREE(vdata);
551 return(1);
553 return(0);
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: */