1 /*
2 * alpm.c
3 *
4 * Copyright (c) 2006-2018 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 by Christian Hamar <krics@linuxforum.hu>
8 * Copyright (c) 2005, 2006 by Miklos Vajna <vmiklos@frugalware.org>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #ifdef HAVE_LIBCURL
25 #include <curl/curl.h>
26 #endif
27
28 /* libalpm */
29 #include "alpm.h"
30 #include "alpm_list.h"
31 #include "handle.h"
32 #include "log.h"
33 #include "util.h"
34
35 /** \addtogroup alpm_interface Interface Functions
36 * @brief Functions to initialize and release libalpm
37 * @{
38 */
39
40 /** Initializes the library.
41 * Creates handle, connects to database and creates lockfile.
42 * This must be called before any other functions are called.
43 * @param root the root path for all filesystem operations
44 * @param dbpath the absolute path to the libalpm database
45 * @param err an optional variable to hold any error return codes
46 * @return a context handle on success, NULL on error, err will be set if provided
47 */
alpm_initialize(const char * root,const char * dbpath,alpm_errno_t * err)48 alpm_handle_t SYMEXPORT *alpm_initialize(const char *root, const char *dbpath,
49 alpm_errno_t *err)
50 {
51 alpm_errno_t myerr;
52 const char *lf = "db.lck";
53 char *hookdir;
54 size_t lockfilelen;
55 alpm_handle_t *myhandle = _alpm_handle_new();
56
57 if(myhandle == NULL) {
58 goto nomem;
59 }
60 if((myerr = _alpm_set_directory_option(root, &(myhandle->root), 1))) {
61 goto cleanup;
62 }
63 if((myerr = _alpm_set_directory_option(dbpath, &(myhandle->dbpath), 1))) {
64 goto cleanup;
65 }
66
67 /* to concatenate myhandle->root (ends with a slash) with SYSHOOKDIR (starts
68 * with a slash) correctly, we skip SYSHOOKDIR[0]; the regular +1 therefore
69 * disappears from the allocation */
70 MALLOC(hookdir, strlen(myhandle->root) + strlen(SYSHOOKDIR), goto nomem);
71 sprintf(hookdir, "%s%s", myhandle->root, SYSHOOKDIR + 1);
72 myhandle->hookdirs = alpm_list_add(NULL, hookdir);
73
74 /* set default database extension */
75 STRDUP(myhandle->dbext, ".db", goto nomem);
76
77 lockfilelen = strlen(myhandle->dbpath) + strlen(lf) + 1;
78 MALLOC(myhandle->lockfile, lockfilelen, goto nomem);
79 snprintf(myhandle->lockfile, lockfilelen, "%s%s", myhandle->dbpath, lf);
80
81 if(_alpm_db_register_local(myhandle) == NULL) {
82 myerr = myhandle->pm_errno;
83 goto cleanup;
84 }
85
86 #ifdef ENABLE_NLS
87 bindtextdomain("libalpm", LOCALEDIR);
88 #endif
89
90 return myhandle;
91
92 nomem:
93 myerr = ALPM_ERR_MEMORY;
94 cleanup:
95 _alpm_handle_free(myhandle);
96 if(err) {
97 *err = myerr;
98 }
99 return NULL;
100 }
101
102 /** Release the library.
103 * Disconnects from the database, removes handle and lockfile
104 * This should be the last alpm call you make.
105 * After this returns, handle should be considered invalid and cannot be reused
106 * in any way.
107 * @param myhandle the context handle
108 * @return 0 on success, -1 on error
109 */
alpm_release(alpm_handle_t * myhandle)110 int SYMEXPORT alpm_release(alpm_handle_t *myhandle)
111 {
112 int ret = 0;
113 alpm_db_t *db;
114
115 CHECK_HANDLE(myhandle, return -1);
116
117 /* close local database */
118 db = myhandle->db_local;
119 if(db) {
120 db->ops->unregister(db);
121 myhandle->db_local = NULL;
122 }
123
124 if(alpm_unregister_all_syncdbs(myhandle) == -1) {
125 ret = -1;
126 }
127
128 _alpm_handle_unlock(myhandle);
129 _alpm_handle_free(myhandle);
130
131 #ifdef HAVE_LIBCURL
132 curl_global_cleanup();
133 #endif
134
135 return ret;
136 }
137
138 /** @} */
139
140 /** @defgroup alpm_misc Miscellaneous Functions
141 * @brief Various libalpm functions
142 */
143
144 /** Get the version of library.
145 * @return the library version, e.g. "6.0.4"
146 * */
alpm_version(void)147 const char SYMEXPORT *alpm_version(void)
148 {
149 return LIB_VERSION;
150 }
151
152 /** Get the capabilities of the library.
153 * @return a bitmask of the capabilities
154 * */
alpm_capabilities(void)155 int SYMEXPORT alpm_capabilities(void)
156 {
157 return 0
158 #ifdef ENABLE_NLS
159 | ALPM_CAPABILITY_NLS
160 #endif
161 #ifdef HAVE_LIBCURL
162 | ALPM_CAPABILITY_DOWNLOADER
163 #endif
164 #ifdef HAVE_LIBGPGME
165 | ALPM_CAPABILITY_SIGNATURES
166 #endif
167 | 0;
168 }
169