libalpm
Arch Linux Package Manager Library
alpm.c
Go to the documentation of this file.
00001 /*
00002  *  alpm.c
00003  *
00004  *  Copyright (c) 2006-2011 Pacman Development Team <pacman-dev@archlinux.org>
00005  *  Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
00006  *  Copyright (c) 2005 by Aurelien Foret <orelien@chez.com>
00007  *  Copyright (c) 2005 by Christian Hamar <krics@linuxforum.hu>
00008  *  Copyright (c) 2005, 2006 by Miklos Vajna <vmiklos@frugalware.org>
00009  *
00010  *  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU General Public License for more details.
00019  *
00020  *  You should have received a copy of the GNU General Public License
00021  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00022  */
00023 
00024 #ifdef HAVE_LIBCURL
00025 #include <curl/curl.h>
00026 #endif
00027 
00028 /* libalpm */
00029 #include "alpm.h"
00030 #include "alpm_list.h"
00031 #include "handle.h"
00032 #include "log.h"
00033 #include "util.h"
00034 
00035 /** \addtogroup alpm_interface Interface Functions
00036  * @brief Functions to initialize and release libalpm
00037  * @{
00038  */
00039 
00040 /** Initializes the library.  This must be called before any other
00041  * functions are called.
00042  * @param root the root path for all filesystem operations
00043  * @param dbpath the absolute path to the libalpm database
00044  * @param err an optional variable to hold any error return codes
00045  * @return a context handle on success, NULL on error, err will be set if provided
00046  */
00047 alpm_handle_t SYMEXPORT *alpm_initialize(const char *root, const char *dbpath,
00048         alpm_errno_t *err)
00049 {
00050     alpm_errno_t myerr;
00051     const char *lf = "db.lck";
00052     size_t lockfilelen;
00053     alpm_handle_t *myhandle = _alpm_handle_new();
00054 
00055     if(myhandle == NULL) {
00056         myerr = ALPM_ERR_MEMORY;
00057         goto cleanup;
00058     }
00059     if((myerr = _alpm_set_directory_option(root, &(myhandle->root), 1))) {
00060         goto cleanup;
00061     }
00062     if((myerr = _alpm_set_directory_option(dbpath, &(myhandle->dbpath), 1))) {
00063         goto cleanup;
00064     }
00065 
00066     lockfilelen = strlen(myhandle->dbpath) + strlen(lf) + 1;
00067     myhandle->lockfile = calloc(lockfilelen, sizeof(char));
00068     snprintf(myhandle->lockfile, lockfilelen, "%s%s", myhandle->dbpath, lf);
00069 
00070     if(_alpm_db_register_local(myhandle) == NULL) {
00071         myerr = myhandle->pm_errno;
00072         goto cleanup;
00073     }
00074 
00075 #ifdef ENABLE_NLS
00076     bindtextdomain("libalpm", LOCALEDIR);
00077 #endif
00078 
00079     return myhandle;
00080 
00081 cleanup:
00082     _alpm_handle_free(myhandle);
00083     if(err && myerr) {
00084         *err = myerr;
00085     }
00086     return NULL;
00087 }
00088 
00089 /** Release the library.  This should be the last alpm call you make.
00090  * After this returns, handle should be considered invalid and cannot be reused
00091  * in any way.
00092  * @param myhandle the context handle
00093  * @return 0 on success, -1 on error
00094  */
00095 int SYMEXPORT alpm_release(alpm_handle_t *myhandle)
00096 {
00097     int ret = 0;
00098     alpm_db_t *db;
00099 
00100     CHECK_HANDLE(myhandle, return -1);
00101 
00102     /* close local database */
00103     db = myhandle->db_local;
00104     if(db) {
00105         db->ops->unregister(db);
00106         myhandle->db_local = NULL;
00107     }
00108 
00109     if(alpm_db_unregister_all(myhandle) == -1) {
00110         ret = -1;
00111     }
00112 
00113     _alpm_handle_unlock(myhandle);
00114     _alpm_handle_free(myhandle);
00115 
00116 #ifdef HAVE_LIBCURL
00117     curl_global_cleanup();
00118 #endif
00119 
00120     return ret;
00121 }
00122 
00123 /** @} */
00124 
00125 /** @defgroup alpm_misc Miscellaneous Functions
00126  * @brief Various libalpm functions
00127  */
00128 
00129 /** Get the version of library.
00130  * @return the library version, e.g. "6.0.4"
00131  * */
00132 const char SYMEXPORT *alpm_version(void)
00133 {
00134     return LIB_VERSION;
00135 }
00136 
00137 /** Get the capabilities of the library.
00138  * @return a bitmask of the capabilities
00139  * */
00140 enum alpm_caps SYMEXPORT alpm_capabilities(void)
00141 {
00142     return 0
00143 #ifdef ENABLE_NLS
00144         | ALPM_CAPABILITY_NLS
00145 #endif
00146 #ifdef HAVE_LIBCURL
00147         | ALPM_CAPABILITY_DOWNLOADER
00148 #endif
00149 #ifdef HAVE_LIBGPGME
00150         | ALPM_CAPABILITY_SIGNATURES
00151 #endif
00152         | 0;
00153 }
00154 
00155 /* vim: set ts=2 sw=2 noet: */