Rename pmtrans_t to alpm_trans_t
[pacman-ng.git] / lib / libalpm / trans.c
blobfdcdcdc6b04d96c4800cf005bc88e0c89a16b253
1 /*
2 * trans.c
4 * Copyright (c) 2006-2011 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>
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.
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.
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/>.
24 #include "config.h"
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <sys/types.h>
31 #include <errno.h>
32 #include <limits.h>
34 /* libalpm */
35 #include "trans.h"
36 #include "alpm_list.h"
37 #include "package.h"
38 #include "util.h"
39 #include "log.h"
40 #include "handle.h"
41 #include "remove.h"
42 #include "sync.h"
43 #include "alpm.h"
45 /** \addtogroup alpm_trans Transaction Functions
46 * @brief Functions to manipulate libalpm transactions
47 * @{
50 /** Initialize the transaction. */
51 int SYMEXPORT alpm_trans_init(alpm_handle_t *handle, pmtransflag_t flags,
52 alpm_trans_cb_event event, alpm_trans_cb_conv conv,
53 alpm_trans_cb_progress progress)
55 alpm_trans_t *trans;
56 alpm_list_t *i;
58 /* Sanity checks */
59 CHECK_HANDLE(handle, return -1);
60 ASSERT(handle->trans == NULL, RET_ERR(handle, PM_ERR_TRANS_NOT_NULL, -1));
62 for(i = handle->dbs_sync; i; i = i->next) {
63 const alpm_db_t *db = i->data;
64 if(!(db->status & DB_STATUS_VALID)) {
65 RET_ERR(handle, PM_ERR_DB_INVALID, -1);
69 /* lock db */
70 if(!(flags & PM_TRANS_FLAG_NOLOCK)) {
71 if(_alpm_handle_lock(handle)) {
72 RET_ERR(handle, PM_ERR_HANDLE_LOCK, -1);
76 CALLOC(trans, 1, sizeof(alpm_trans_t), RET_ERR(handle, PM_ERR_MEMORY, -1));
77 trans->flags = flags;
78 trans->cb_event = event;
79 trans->cb_conv = conv;
80 trans->cb_progress = progress;
81 trans->state = STATE_INITIALIZED;
83 handle->trans = trans;
85 return 0;
88 static alpm_list_t *check_arch(alpm_handle_t *handle, alpm_list_t *pkgs)
90 alpm_list_t *i;
91 alpm_list_t *invalid = NULL;
93 const char *arch = alpm_option_get_arch(handle);
94 if(!arch) {
95 return NULL;
97 for(i = pkgs; i; i = i->next) {
98 alpm_pkg_t *pkg = i->data;
99 const char *pkgarch = alpm_pkg_get_arch(pkg);
100 if(pkgarch && strcmp(pkgarch, arch) && strcmp(pkgarch, "any")) {
101 char *string;
102 const char *pkgname = alpm_pkg_get_name(pkg);
103 const char *pkgver = alpm_pkg_get_version(pkg);
104 size_t len = strlen(pkgname) + strlen(pkgver) + strlen(pkgarch) + 3;
105 MALLOC(string, len, RET_ERR(handle, PM_ERR_MEMORY, invalid));
106 sprintf(string, "%s-%s-%s", pkgname, pkgver, pkgarch);
107 invalid = alpm_list_add(invalid, string);
110 return invalid;
113 /** Prepare a transaction. */
114 int SYMEXPORT alpm_trans_prepare(alpm_handle_t *handle, alpm_list_t **data)
116 alpm_trans_t *trans;
118 /* Sanity checks */
119 CHECK_HANDLE(handle, return -1);
120 ASSERT(data != NULL, RET_ERR(handle, PM_ERR_WRONG_ARGS, -1));
122 trans = handle->trans;
124 ASSERT(trans != NULL, RET_ERR(handle, PM_ERR_TRANS_NULL, -1));
125 ASSERT(trans->state == STATE_INITIALIZED, RET_ERR(handle, PM_ERR_TRANS_NOT_INITIALIZED, -1));
127 /* If there's nothing to do, return without complaining */
128 if(trans->add == NULL && trans->remove == NULL) {
129 return 0;
132 alpm_list_t *invalid = check_arch(handle, trans->add);
133 if(invalid) {
134 if(data) {
135 *data = invalid;
137 RET_ERR(handle, PM_ERR_PKG_INVALID_ARCH, -1);
140 if(trans->add == NULL) {
141 if(_alpm_remove_prepare(handle, data) == -1) {
142 /* pm_errno is set by _alpm_remove_prepare() */
143 return -1;
145 } else {
146 if(_alpm_sync_prepare(handle, data) == -1) {
147 /* pm_errno is set by _alpm_sync_prepare() */
148 return -1;
152 trans->state = STATE_PREPARED;
154 return 0;
157 /** Commit a transaction. */
158 int SYMEXPORT alpm_trans_commit(alpm_handle_t *handle, alpm_list_t **data)
160 alpm_trans_t *trans;
162 /* Sanity checks */
163 CHECK_HANDLE(handle, return -1);
165 trans = handle->trans;
167 ASSERT(trans != NULL, RET_ERR(handle, PM_ERR_TRANS_NULL, -1));
168 ASSERT(trans->state == STATE_PREPARED, RET_ERR(handle, PM_ERR_TRANS_NOT_PREPARED, -1));
170 ASSERT(!(trans->flags & PM_TRANS_FLAG_NOLOCK), RET_ERR(handle, PM_ERR_TRANS_NOT_LOCKED, -1));
172 /* If there's nothing to do, return without complaining */
173 if(trans->add == NULL && trans->remove == NULL) {
174 return 0;
177 trans->state = STATE_COMMITING;
179 if(trans->add == NULL) {
180 if(_alpm_remove_packages(handle) == -1) {
181 /* pm_errno is set by _alpm_remove_commit() */
182 return -1;
184 } else {
185 if(_alpm_sync_commit(handle, data) == -1) {
186 /* pm_errno is set by _alpm_sync_commit() */
187 return -1;
191 trans->state = STATE_COMMITED;
193 return 0;
196 /** Interrupt a transaction. */
197 int SYMEXPORT alpm_trans_interrupt(alpm_handle_t *handle)
199 alpm_trans_t *trans;
201 /* Sanity checks */
202 CHECK_HANDLE(handle, return -1);
204 trans = handle->trans;
205 ASSERT(trans != NULL, RET_ERR(handle, PM_ERR_TRANS_NULL, -1));
206 ASSERT(trans->state == STATE_COMMITING || trans->state == STATE_INTERRUPTED,
207 RET_ERR(handle, PM_ERR_TRANS_TYPE, -1));
209 trans->state = STATE_INTERRUPTED;
211 return 0;
214 /** Release a transaction. */
215 int SYMEXPORT alpm_trans_release(alpm_handle_t *handle)
217 alpm_trans_t *trans;
219 /* Sanity checks */
220 CHECK_HANDLE(handle, return -1);
222 trans = handle->trans;
223 ASSERT(trans != NULL, RET_ERR(handle, PM_ERR_TRANS_NULL, -1));
224 ASSERT(trans->state != STATE_IDLE, RET_ERR(handle, PM_ERR_TRANS_NULL, -1));
226 int nolock_flag = trans->flags & PM_TRANS_FLAG_NOLOCK;
228 _alpm_trans_free(trans);
229 handle->trans = NULL;
231 /* unlock db */
232 if(!nolock_flag) {
233 if(_alpm_handle_unlock(handle)) {
234 _alpm_log(handle, PM_LOG_WARNING, _("could not remove lock file %s\n"),
235 alpm_option_get_lockfile(handle));
236 alpm_logaction(handle, "warning: could not remove lock file %s\n",
237 alpm_option_get_lockfile(handle));
241 return 0;
244 /** @} */
246 void _alpm_trans_free(alpm_trans_t *trans)
248 if(trans == NULL) {
249 return;
252 alpm_list_free_inner(trans->add, (alpm_list_fn_free)_alpm_pkg_free_trans);
253 alpm_list_free(trans->add);
254 alpm_list_free_inner(trans->remove, (alpm_list_fn_free)_alpm_pkg_free);
255 alpm_list_free(trans->remove);
257 FREELIST(trans->skip_remove);
259 FREE(trans);
262 /* A cheap grep for text files, returns 1 if a substring
263 * was found in the text file fn, 0 if it wasn't
265 static int grep(const char *fn, const char *needle)
267 FILE *fp;
269 if((fp = fopen(fn, "r")) == NULL) {
270 return 0;
272 while(!feof(fp)) {
273 char line[1024];
274 if(fgets(line, sizeof(line), fp) == NULL) {
275 continue;
277 /* TODO: this will not work if the search string
278 * ends up being split across line reads */
279 if(strstr(line, needle)) {
280 fclose(fp);
281 return 1;
284 fclose(fp);
285 return 0;
288 int _alpm_runscriptlet(alpm_handle_t *handle, const char *installfn,
289 const char *script, const char *ver, const char *oldver)
291 char scriptfn[PATH_MAX];
292 char cmdline[PATH_MAX];
293 char tmpdir[PATH_MAX];
294 char *argv[] = { "sh", "-c", cmdline, NULL };
295 char *scriptpath;
296 int clean_tmpdir = 0;
297 int retval = 0;
299 if(access(installfn, R_OK)) {
300 /* not found */
301 _alpm_log(handle, PM_LOG_DEBUG, "scriptlet '%s' not found\n", installfn);
302 return 0;
305 /* creates a directory in $root/tmp/ for copying/extracting the scriptlet */
306 snprintf(tmpdir, PATH_MAX, "%stmp/", handle->root);
307 if(access(tmpdir, F_OK) != 0) {
308 _alpm_makepath_mode(tmpdir, 01777);
310 snprintf(tmpdir, PATH_MAX, "%stmp/alpm_XXXXXX", handle->root);
311 if(mkdtemp(tmpdir) == NULL) {
312 _alpm_log(handle, PM_LOG_ERROR, _("could not create temp directory\n"));
313 return 1;
314 } else {
315 clean_tmpdir = 1;
318 /* either extract or copy the scriptlet */
319 snprintf(scriptfn, PATH_MAX, "%s/.INSTALL", tmpdir);
320 if(strcmp(script, "pre_upgrade") == 0 || strcmp(script, "pre_install") == 0) {
321 if(_alpm_unpack_single(handle, installfn, tmpdir, ".INSTALL")) {
322 retval = 1;
324 } else {
325 if(_alpm_copyfile(installfn, scriptfn)) {
326 _alpm_log(handle, PM_LOG_ERROR, _("could not copy tempfile to %s (%s)\n"), scriptfn, strerror(errno));
327 retval = 1;
330 if(retval == 1) {
331 goto cleanup;
334 /* chop off the root so we can find the tmpdir in the chroot */
335 scriptpath = scriptfn + strlen(handle->root) - 1;
337 if(!grep(scriptfn, script)) {
338 /* script not found in scriptlet file */
339 goto cleanup;
342 if(oldver) {
343 snprintf(cmdline, PATH_MAX, ". %s; %s %s %s",
344 scriptpath, script, ver, oldver);
345 } else {
346 snprintf(cmdline, PATH_MAX, ". %s; %s %s",
347 scriptpath, script, ver);
350 _alpm_log(handle, PM_LOG_DEBUG, "executing \"%s\"\n", cmdline);
352 retval = _alpm_run_chroot(handle, "/bin/sh", argv);
354 cleanup:
355 if(clean_tmpdir && _alpm_rmrf(tmpdir)) {
356 _alpm_log(handle, PM_LOG_WARNING, _("could not remove tmpdir %s\n"), tmpdir);
359 return retval;
362 pmtransflag_t SYMEXPORT alpm_trans_get_flags(alpm_handle_t *handle)
364 /* Sanity checks */
365 CHECK_HANDLE(handle, return -1);
366 ASSERT(handle->trans != NULL, RET_ERR(handle, PM_ERR_TRANS_NULL, -1));
368 return handle->trans->flags;
371 alpm_list_t SYMEXPORT *alpm_trans_get_add(alpm_handle_t *handle)
373 /* Sanity checks */
374 CHECK_HANDLE(handle, return NULL);
375 ASSERT(handle->trans != NULL, RET_ERR(handle, PM_ERR_TRANS_NULL, NULL));
377 return handle->trans->add;
380 alpm_list_t SYMEXPORT *alpm_trans_get_remove(alpm_handle_t *handle)
382 /* Sanity checks */
383 CHECK_HANDLE(handle, return NULL);
384 ASSERT(handle->trans != NULL, RET_ERR(handle, PM_ERR_TRANS_NULL, NULL));
386 return handle->trans->remove;
388 /* vim: set ts=2 sw=2 noet: */