handle.c

Go to the documentation of this file.
00001 /*
00002  *  handle.c
00003  *
00004  *  Copyright (c) 2002-2007 by Judd Vinet <jvinet@zeroflux.org>
00005  *  Copyright (c) 2005 by Aurelien Foret <orelien@chez.com>
00006  *  Copyright (c) 2005, 2006 by Miklos Vajna <vmiklos@frugalware.org>
00007  *
00008  *  This program is free software; you can redistribute it and/or modify
00009  *  it under the terms of the GNU General Public License as published by
00010  *  the Free Software Foundation; either version 2 of the License, or
00011  *  (at your option) any later version.
00012  *
00013  *  This program is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  *  GNU General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU General Public License
00019  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 #include "config.h"
00023 
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <unistd.h>
00027 #include <limits.h>
00028 #include <sys/types.h>
00029 #include <syslog.h>
00030 #include <time.h>
00031 #include <sys/stat.h>
00032 #include <errno.h>
00033 
00034 /* libalpm */
00035 #include "handle.h"
00036 #include "alpm_list.h"
00037 #include "util.h"
00038 #include "log.h"
00039 #include "error.h"
00040 #include "trans.h"
00041 #include "alpm.h"
00042 #include "server.h"
00043 
00044 /* global var for handle (private to libalpm) */
00045 pmhandle_t *handle = NULL;
00046 
00047 pmhandle_t *_alpm_handle_new()
00048 {
00049     pmhandle_t *handle;
00050 
00051     ALPM_LOG_FUNC;
00052 
00053     CALLOC(handle, 1, sizeof(pmhandle_t), RET_ERR(PM_ERR_MEMORY, NULL));
00054 
00055     handle->lckfd = -1;
00056     handle->logstream = NULL;
00057 
00058     /* see if we're root or not */
00059     handle->uid = geteuid();
00060     handle->root = NULL;
00061     handle->dbpath = NULL;
00062     handle->cachedirs = NULL;
00063     handle->lockfile = NULL;
00064     handle->logfile = NULL;
00065     handle->usedelta = 0;
00066 
00067     return(handle);
00068 }
00069 
00070 void _alpm_handle_free(pmhandle_t *handle)
00071 {
00072     ALPM_LOG_FUNC;
00073 
00074     if(handle == NULL) {
00075         return;
00076     }
00077 
00078     /* close logfile */
00079     if(handle->logstream) {
00080         fclose(handle->logstream);
00081         handle->logstream= NULL;
00082     }
00083     if(handle->usesyslog) {
00084         handle->usesyslog = 0;
00085         closelog();
00086     }
00087 
00088     /* free memory */
00089     _alpm_trans_free(handle->trans);
00090     FREE(handle->root);
00091     FREE(handle->dbpath);
00092     FREELIST(handle->cachedirs);
00093     FREE(handle->logfile);
00094     FREE(handle->lockfile);
00095     FREE(handle->xfercommand);
00096     FREELIST(handle->dbs_sync);
00097     FREELIST(handle->noupgrade);
00098     FREELIST(handle->noextract);
00099     FREELIST(handle->ignorepkg);
00100     FREELIST(handle->holdpkg);
00101     FREELIST(handle->ignoregrp);
00102     FREE(handle);
00103 }
00104 
00105 alpm_cb_log SYMEXPORT alpm_option_get_logcb()
00106 {
00107     if (handle == NULL) {
00108         pm_errno = PM_ERR_HANDLE_NULL;
00109         return NULL;
00110     }
00111     return handle->logcb;
00112 }
00113 
00114 alpm_cb_download SYMEXPORT alpm_option_get_dlcb()
00115 {
00116     if (handle == NULL) {
00117         pm_errno = PM_ERR_HANDLE_NULL;
00118         return NULL;
00119     }
00120     return handle->dlcb;
00121 }
00122 
00123 const char SYMEXPORT *alpm_option_get_root()
00124 {
00125     if (handle == NULL) {
00126         pm_errno = PM_ERR_HANDLE_NULL;
00127         return NULL;
00128     }
00129     return handle->root;
00130 }
00131 
00132 const char SYMEXPORT *alpm_option_get_dbpath()
00133 {
00134     if (handle == NULL) {
00135         pm_errno = PM_ERR_HANDLE_NULL;
00136         return NULL;
00137     }
00138     return handle->dbpath;
00139 }
00140 
00141 alpm_list_t SYMEXPORT *alpm_option_get_cachedirs()
00142 {
00143     if (handle == NULL) {
00144         pm_errno = PM_ERR_HANDLE_NULL;
00145         return NULL;
00146     }
00147     return handle->cachedirs;
00148 }
00149 
00150 const char SYMEXPORT *alpm_option_get_logfile()
00151 {
00152     if (handle == NULL) {
00153         pm_errno = PM_ERR_HANDLE_NULL;
00154         return NULL;
00155     }
00156     return handle->logfile;
00157 }
00158 
00159 const char SYMEXPORT *alpm_option_get_lockfile()
00160 {
00161     if (handle == NULL) {
00162         pm_errno = PM_ERR_HANDLE_NULL;
00163         return NULL;
00164     }
00165     return handle->lockfile;
00166 }
00167 
00168 unsigned short SYMEXPORT alpm_option_get_usesyslog()
00169 {
00170     if (handle == NULL) {
00171         pm_errno = PM_ERR_HANDLE_NULL;
00172         return -1;
00173     }
00174     return handle->usesyslog;
00175 }
00176 
00177 alpm_list_t SYMEXPORT *alpm_option_get_noupgrades()
00178 {
00179     if (handle == NULL) {
00180         pm_errno = PM_ERR_HANDLE_NULL;
00181         return NULL;
00182     }
00183     return handle->noupgrade;
00184 }
00185 
00186 alpm_list_t SYMEXPORT *alpm_option_get_noextracts()
00187 {
00188     if (handle == NULL) {
00189         pm_errno = PM_ERR_HANDLE_NULL;
00190         return NULL;
00191     }
00192     return handle->noextract;
00193 }
00194 
00195 alpm_list_t SYMEXPORT *alpm_option_get_ignorepkgs()
00196 {
00197     if (handle == NULL) {
00198         pm_errno = PM_ERR_HANDLE_NULL;
00199         return NULL;
00200     }
00201     return handle->ignorepkg;
00202 }
00203 
00204 alpm_list_t SYMEXPORT *alpm_option_get_holdpkgs()
00205 {
00206     if (handle == NULL) {
00207         pm_errno = PM_ERR_HANDLE_NULL;
00208         return NULL;
00209     }
00210     return handle->holdpkg;
00211 }
00212 
00213 alpm_list_t SYMEXPORT *alpm_option_get_ignoregrps()
00214 {
00215     if (handle == NULL) {
00216         pm_errno = PM_ERR_HANDLE_NULL;
00217         return NULL;
00218     }
00219     return handle->ignoregrp;
00220 }
00221 
00222 const char SYMEXPORT *alpm_option_get_xfercommand()
00223 {
00224     if (handle == NULL) {
00225         pm_errno = PM_ERR_HANDLE_NULL;
00226         return NULL;
00227     }
00228     return handle->xfercommand;
00229 }
00230 
00231 unsigned short SYMEXPORT alpm_option_get_nopassiveftp()
00232 {
00233     if (handle == NULL) {
00234         pm_errno = PM_ERR_HANDLE_NULL;
00235         return -1;
00236     }
00237     return handle->nopassiveftp;
00238 }
00239 
00240 pmdb_t SYMEXPORT *alpm_option_get_localdb()
00241 {
00242     if (handle == NULL) {
00243         pm_errno = PM_ERR_HANDLE_NULL;
00244         return NULL;
00245     }
00246     return handle->db_local;
00247 }
00248 
00249 alpm_list_t SYMEXPORT *alpm_option_get_syncdbs()
00250 {
00251     if (handle == NULL) {
00252         pm_errno = PM_ERR_HANDLE_NULL;
00253         return NULL;
00254     }
00255     return handle->dbs_sync;
00256 }
00257 
00258 void SYMEXPORT alpm_option_set_logcb(alpm_cb_log cb)
00259 {
00260     if (handle == NULL) {
00261         pm_errno = PM_ERR_HANDLE_NULL;
00262         return;
00263     }
00264     handle->logcb = cb;
00265 }
00266 
00267 void SYMEXPORT alpm_option_set_dlcb(alpm_cb_download cb)
00268 {
00269     if (handle == NULL) {
00270         pm_errno = PM_ERR_HANDLE_NULL;
00271         return;
00272     }
00273     handle->dlcb = cb;
00274 }
00275 
00276 int SYMEXPORT alpm_option_set_root(const char *root)
00277 {
00278     struct stat st;
00279     char *realroot;
00280     size_t rootlen;
00281 
00282     ALPM_LOG_FUNC;
00283 
00284     if(!root) {
00285         pm_errno = PM_ERR_WRONG_ARGS;
00286         return(-1);
00287     }
00288     if(stat(root, &st) == -1 || !S_ISDIR(st.st_mode)) {
00289         pm_errno = PM_ERR_NOT_A_DIR;
00290         return(-1);
00291     }
00292 
00293     realroot = calloc(PATH_MAX+1, sizeof(char));
00294     if(!realpath(root, realroot)) {
00295         pm_errno = PM_ERR_NOT_A_DIR;
00296         return(-1);
00297     }
00298 
00299     /* verify root ends in a '/' */
00300     rootlen = strlen(realroot);
00301     if(realroot[rootlen-1] != '/') {
00302         rootlen += 1;
00303     }
00304     if(handle->root) {
00305         FREE(handle->root);
00306     }
00307     handle->root = calloc(rootlen + 1, sizeof(char));
00308     strncpy(handle->root, realroot, rootlen);
00309     handle->root[rootlen-1] = '/';
00310     FREE(realroot);
00311     _alpm_log(PM_LOG_DEBUG, "option 'root' = %s\n", handle->root);
00312     return(0);
00313 }
00314 
00315 int SYMEXPORT alpm_option_set_dbpath(const char *dbpath)
00316 {
00317     struct stat st;
00318     size_t dbpathlen, lockfilelen;
00319     const char *lf = "db.lck";
00320 
00321     ALPM_LOG_FUNC;
00322 
00323     if(!dbpath) {
00324         pm_errno = PM_ERR_WRONG_ARGS;
00325         return(-1);
00326     }
00327     if(stat(dbpath, &st) == -1 || !S_ISDIR(st.st_mode)) {
00328         pm_errno = PM_ERR_NOT_A_DIR;
00329         return(-1);
00330     }
00331     /* verify dbpath ends in a '/' */
00332     dbpathlen = strlen(dbpath);
00333     if(dbpath[dbpathlen-1] != '/') {
00334         dbpathlen += 1;
00335     }
00336     if(handle->dbpath) {
00337         FREE(handle->dbpath);
00338     }
00339     handle->dbpath = calloc(dbpathlen+1, sizeof(char));
00340     strncpy(handle->dbpath, dbpath, dbpathlen);
00341     handle->dbpath[dbpathlen-1] = '/';
00342     _alpm_log(PM_LOG_DEBUG, "option 'dbpath' = %s\n", handle->dbpath);
00343 
00344     if(handle->lockfile) {
00345         FREE(handle->lockfile);
00346     }
00347     lockfilelen = strlen(handle->dbpath) + strlen(lf) + 1;
00348     handle->lockfile = calloc(lockfilelen, sizeof(char));
00349     snprintf(handle->lockfile, lockfilelen, "%s%s", handle->dbpath, lf);
00350     _alpm_log(PM_LOG_DEBUG, "option 'lockfile' = %s\n", handle->lockfile);
00351     return(0);
00352 }
00353 
00354 int SYMEXPORT alpm_option_add_cachedir(const char *cachedir)
00355 {
00356     char *newcachedir;
00357     size_t cachedirlen;
00358 
00359     ALPM_LOG_FUNC;
00360 
00361     if(!cachedir) {
00362         pm_errno = PM_ERR_WRONG_ARGS;
00363         return(-1);
00364     }
00365     /* don't stat the cachedir yet, as it may not even be needed. we can
00366      * fail later if it is needed and the path is invalid. */
00367 
00368     /* verify cachedir ends in a '/' */
00369     cachedirlen = strlen(cachedir);
00370     if(cachedir[cachedirlen-1] != '/') {
00371         cachedirlen += 1;
00372     }
00373     newcachedir = calloc(cachedirlen + 1, sizeof(char));
00374     strncpy(newcachedir, cachedir, cachedirlen);
00375     newcachedir[cachedirlen-1] = '/';
00376     handle->cachedirs = alpm_list_add(handle->cachedirs, newcachedir);
00377     _alpm_log(PM_LOG_DEBUG, "option 'cachedir' = %s\n", newcachedir);
00378     return(0);
00379 }
00380 
00381 void SYMEXPORT alpm_option_set_cachedirs(alpm_list_t *cachedirs)
00382 {
00383     if(handle->cachedirs) FREELIST(handle->cachedirs);
00384     if(cachedirs) handle->cachedirs = cachedirs;
00385 }
00386 
00387 int SYMEXPORT alpm_option_remove_cachedir(const char *cachedir)
00388 {
00389     void *vdata = NULL;
00390     char *newcachedir;
00391     size_t cachedirlen;
00392     /* verify cachedir ends in a '/' */
00393     cachedirlen = strlen(cachedir);
00394     if(cachedir[cachedirlen-1] != '/') {
00395         cachedirlen += 1;
00396     }
00397     newcachedir = calloc(cachedirlen + 1, sizeof(char));
00398     strncpy(newcachedir, cachedir, cachedirlen);
00399     newcachedir[cachedirlen-1] = '/';
00400     handle->cachedirs = alpm_list_remove(handle->cachedirs, newcachedir,
00401         _alpm_str_cmp, &vdata);
00402     FREE(newcachedir);
00403     if(vdata != NULL) {
00404         FREE(vdata);
00405         return(1);
00406     }
00407     return(0);
00408 }
00409 
00410 int SYMEXPORT alpm_option_set_logfile(const char *logfile)
00411 {
00412     char *oldlogfile = handle->logfile;
00413 
00414     ALPM_LOG_FUNC;
00415 
00416     if(!logfile) {
00417         pm_errno = PM_ERR_WRONG_ARGS;
00418         return(-1);
00419     }
00420 
00421     handle->logfile = strdup(logfile);
00422 
00423     /* free the old logfile path string, and close the stream so logaction
00424      * will reopen a new stream on the new logfile */
00425     if(oldlogfile) {
00426         FREE(oldlogfile);
00427     }
00428     if(handle->logstream) {
00429         fclose(handle->logstream);
00430     }
00431     _alpm_log(PM_LOG_DEBUG, "option 'logfile' = %s\n", handle->logfile);
00432     return(0);
00433 }
00434 
00435 void SYMEXPORT alpm_option_set_usesyslog(unsigned short usesyslog)
00436 {
00437     handle->usesyslog = usesyslog;
00438 }
00439 
00440 void SYMEXPORT alpm_option_add_noupgrade(const char *pkg)
00441 {
00442     handle->noupgrade = alpm_list_add(handle->noupgrade, strdup(pkg));
00443 }
00444 
00445 void SYMEXPORT alpm_option_set_noupgrades(alpm_list_t *noupgrade)
00446 {
00447     if(handle->noupgrade) FREELIST(handle->noupgrade);
00448     if(noupgrade) handle->noupgrade = noupgrade;
00449 }
00450 
00451 int SYMEXPORT alpm_option_remove_noupgrade(const char *pkg)
00452 {
00453     void *vdata = NULL;
00454     handle->noupgrade = alpm_list_remove(handle->noupgrade, pkg,
00455         _alpm_str_cmp, &vdata);
00456     if(vdata != NULL) {
00457         FREE(vdata);
00458         return(1);
00459     }
00460     return(0);
00461 }
00462 
00463 void SYMEXPORT alpm_option_add_noextract(const char *pkg)
00464 {
00465     handle->noextract = alpm_list_add(handle->noextract, strdup(pkg));
00466 }
00467 
00468 void SYMEXPORT alpm_option_set_noextracts(alpm_list_t *noextract)
00469 {
00470     if(handle->noextract) FREELIST(handle->noextract);
00471     if(noextract) handle->noextract = noextract;
00472 }
00473 
00474 int SYMEXPORT alpm_option_remove_noextract(const char *pkg)
00475 {
00476     void *vdata = NULL;
00477     handle->noextract = alpm_list_remove(handle->noextract, pkg,
00478         _alpm_str_cmp, &vdata);
00479     if(vdata != NULL) {
00480         FREE(vdata);
00481         return(1);
00482     }
00483     return(0);
00484 }
00485 
00486 void SYMEXPORT alpm_option_add_ignorepkg(const char *pkg)
00487 {
00488     handle->ignorepkg = alpm_list_add(handle->ignorepkg, strdup(pkg));
00489 }
00490 
00491 void SYMEXPORT alpm_option_set_ignorepkgs(alpm_list_t *ignorepkgs)
00492 {
00493     if(handle->ignorepkg) FREELIST(handle->ignorepkg);
00494     if(ignorepkgs) handle->ignorepkg = ignorepkgs;
00495 }
00496 
00497 int SYMEXPORT alpm_option_remove_ignorepkg(const char *pkg)
00498 {
00499     void *vdata = NULL;
00500     handle->ignorepkg = alpm_list_remove(handle->ignorepkg, pkg,
00501         _alpm_str_cmp, &vdata);
00502     if(vdata != NULL) {
00503         FREE(vdata);
00504         return(1);
00505     }
00506     return(0);
00507 }
00508 
00509 void SYMEXPORT alpm_option_add_holdpkg(const char *pkg)
00510 {
00511     handle->holdpkg = alpm_list_add(handle->holdpkg, strdup(pkg));
00512 }
00513 
00514 void SYMEXPORT alpm_option_set_holdpkgs(alpm_list_t *holdpkgs)
00515 {
00516     if(handle->holdpkg) FREELIST(handle->holdpkg);
00517     if(holdpkgs) handle->holdpkg = holdpkgs;
00518 }
00519 
00520 int SYMEXPORT alpm_option_remove_holdpkg(const char *pkg)
00521 {
00522     void *vdata = NULL;
00523     handle->holdpkg = alpm_list_remove(handle->holdpkg, pkg,
00524         _alpm_str_cmp, &vdata);
00525     if(vdata != NULL) {
00526         FREE(vdata);
00527         return(1);
00528     }
00529     return(0);
00530 }
00531 
00532 void SYMEXPORT alpm_option_add_ignoregrp(const char *grp)
00533 {
00534     handle->ignoregrp = alpm_list_add(handle->ignoregrp, strdup(grp));
00535 }
00536 
00537 void SYMEXPORT alpm_option_set_ignoregrps(alpm_list_t *ignoregrps)
00538 {
00539     if(handle->ignoregrp) FREELIST(handle->ignoregrp);
00540     if(ignoregrps) handle->ignoregrp = ignoregrps;
00541 }
00542 
00543 int SYMEXPORT alpm_option_remove_ignoregrp(const char *grp)
00544 {
00545     void *vdata = NULL;
00546     handle->ignoregrp = alpm_list_remove(handle->ignoregrp, grp,
00547         _alpm_str_cmp, &vdata);
00548     if(vdata != NULL) {
00549         FREE(vdata);
00550         return(1);
00551     }
00552     return(0);
00553 }
00554 
00555 void SYMEXPORT alpm_option_set_xfercommand(const char *cmd)
00556 {
00557     if(handle->xfercommand) FREE(handle->xfercommand);
00558     if(cmd) handle->xfercommand = strdup(cmd);
00559 }
00560 
00561 void SYMEXPORT alpm_option_set_nopassiveftp(unsigned short nopasv)
00562 {
00563     handle->nopassiveftp = nopasv;
00564 }
00565 
00566 void SYMEXPORT alpm_option_set_usedelta(unsigned short usedelta)
00567 {
00568     handle->usedelta = usedelta;
00569 }
00570 
00571 /* vim: set ts=2 sw=2 noet: */

Generated on Mon Jan 14 23:53:40 2008 for libalpm by  doxygen 1.5.4