Move important information up in -Si output
[pacman-ng.git] / lib / libalpm / trans.c
blob08f70dd7ea605d4fe321f393060b9760e533eeb4
1 /*
2 * trans.c
4 * Copyright (c) 2006-2012 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 <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <errno.h>
30 #include <limits.h>
32 /* libalpm */
33 #include "trans.h"
34 #include "alpm_list.h"
35 #include "package.h"
36 #include "util.h"
37 #include "log.h"
38 #include "handle.h"
39 #include "remove.h"
40 #include "sync.h"
41 #include "alpm.h"
43 /** \addtogroup alpm_trans Transaction Functions
44 * @brief Functions to manipulate libalpm transactions
45 * @{
48 /** Initialize the transaction. */
49 int SYMEXPORT alpm_trans_init(alpm_handle_t *handle, alpm_transflag_t flags)
51 alpm_trans_t *trans;
53 /* Sanity checks */
54 CHECK_HANDLE(handle, return -1);
55 ASSERT(handle->trans == NULL, RET_ERR(handle, ALPM_ERR_TRANS_NOT_NULL, -1));
57 /* lock db */
58 if(!(flags & ALPM_TRANS_FLAG_NOLOCK)) {
59 if(_alpm_handle_lock(handle)) {
60 RET_ERR(handle, ALPM_ERR_HANDLE_LOCK, -1);
64 CALLOC(trans, 1, sizeof(alpm_trans_t), RET_ERR(handle, ALPM_ERR_MEMORY, -1));
65 trans->flags = flags;
66 trans->state = STATE_INITIALIZED;
68 handle->trans = trans;
70 return 0;
73 static alpm_list_t *check_arch(alpm_handle_t *handle, alpm_list_t *pkgs)
75 alpm_list_t *i;
76 alpm_list_t *invalid = NULL;
78 const char *arch = handle->arch;
79 if(!arch) {
80 return NULL;
82 for(i = pkgs; i; i = i->next) {
83 alpm_pkg_t *pkg = i->data;
84 const char *pkgarch = alpm_pkg_get_arch(pkg);
85 if(pkgarch && strcmp(pkgarch, arch) && strcmp(pkgarch, "any")) {
86 char *string;
87 const char *pkgname = pkg->name;
88 const char *pkgver = pkg->version;
89 size_t len = strlen(pkgname) + strlen(pkgver) + strlen(pkgarch) + 3;
90 MALLOC(string, len, RET_ERR(handle, ALPM_ERR_MEMORY, invalid));
91 sprintf(string, "%s-%s-%s", pkgname, pkgver, pkgarch);
92 invalid = alpm_list_add(invalid, string);
95 return invalid;
98 /** Prepare a transaction. */
99 int SYMEXPORT alpm_trans_prepare(alpm_handle_t *handle, alpm_list_t **data)
101 alpm_trans_t *trans;
103 /* Sanity checks */
104 CHECK_HANDLE(handle, return -1);
105 ASSERT(data != NULL, RET_ERR(handle, ALPM_ERR_WRONG_ARGS, -1));
107 trans = handle->trans;
109 ASSERT(trans != NULL, RET_ERR(handle, ALPM_ERR_TRANS_NULL, -1));
110 ASSERT(trans->state == STATE_INITIALIZED, RET_ERR(handle, ALPM_ERR_TRANS_NOT_INITIALIZED, -1));
112 /* If there's nothing to do, return without complaining */
113 if(trans->add == NULL && trans->remove == NULL) {
114 return 0;
117 alpm_list_t *invalid = check_arch(handle, trans->add);
118 if(invalid) {
119 if(data) {
120 *data = invalid;
122 RET_ERR(handle, ALPM_ERR_PKG_INVALID_ARCH, -1);
125 if(trans->add == NULL) {
126 if(_alpm_remove_prepare(handle, data) == -1) {
127 /* pm_errno is set by _alpm_remove_prepare() */
128 return -1;
130 } else {
131 if(_alpm_sync_prepare(handle, data) == -1) {
132 /* pm_errno is set by _alpm_sync_prepare() */
133 return -1;
137 trans->state = STATE_PREPARED;
139 return 0;
142 /** Commit a transaction. */
143 int SYMEXPORT alpm_trans_commit(alpm_handle_t *handle, alpm_list_t **data)
145 alpm_trans_t *trans;
147 /* Sanity checks */
148 CHECK_HANDLE(handle, return -1);
150 trans = handle->trans;
152 ASSERT(trans != NULL, RET_ERR(handle, ALPM_ERR_TRANS_NULL, -1));
153 ASSERT(trans->state == STATE_PREPARED, RET_ERR(handle, ALPM_ERR_TRANS_NOT_PREPARED, -1));
155 ASSERT(!(trans->flags & ALPM_TRANS_FLAG_NOLOCK), RET_ERR(handle, ALPM_ERR_TRANS_NOT_LOCKED, -1));
157 /* If there's nothing to do, return without complaining */
158 if(trans->add == NULL && trans->remove == NULL) {
159 return 0;
162 trans->state = STATE_COMMITING;
164 if(trans->add == NULL) {
165 if(_alpm_remove_packages(handle, 1) == -1) {
166 /* pm_errno is set by _alpm_remove_commit() */
167 return -1;
169 } else {
170 if(_alpm_sync_commit(handle, data) == -1) {
171 /* pm_errno is set by _alpm_sync_commit() */
172 return -1;
176 trans->state = STATE_COMMITED;
178 return 0;
181 /** Interrupt a transaction. */
182 int SYMEXPORT alpm_trans_interrupt(alpm_handle_t *handle)
184 alpm_trans_t *trans;
186 /* Sanity checks */
187 CHECK_HANDLE(handle, return -1);
189 trans = handle->trans;
190 ASSERT(trans != NULL, RET_ERR(handle, ALPM_ERR_TRANS_NULL, -1));
191 ASSERT(trans->state == STATE_COMMITING || trans->state == STATE_INTERRUPTED,
192 RET_ERR(handle, ALPM_ERR_TRANS_TYPE, -1));
194 trans->state = STATE_INTERRUPTED;
196 return 0;
199 /** Release a transaction. */
200 int SYMEXPORT alpm_trans_release(alpm_handle_t *handle)
202 alpm_trans_t *trans;
204 /* Sanity checks */
205 CHECK_HANDLE(handle, return -1);
207 trans = handle->trans;
208 ASSERT(trans != NULL, RET_ERR(handle, ALPM_ERR_TRANS_NULL, -1));
209 ASSERT(trans->state != STATE_IDLE, RET_ERR(handle, ALPM_ERR_TRANS_NULL, -1));
211 int nolock_flag = trans->flags & ALPM_TRANS_FLAG_NOLOCK;
213 _alpm_trans_free(trans);
214 handle->trans = NULL;
216 /* unlock db */
217 if(!nolock_flag) {
218 if(_alpm_handle_unlock(handle)) {
219 _alpm_log(handle, ALPM_LOG_WARNING, _("could not remove lock file %s\n"),
220 handle->lockfile);
221 alpm_logaction(handle, "warning: could not remove lock file %s\n",
222 handle->lockfile);
226 return 0;
229 /** @} */
231 void _alpm_trans_free(alpm_trans_t *trans)
233 if(trans == NULL) {
234 return;
237 alpm_list_free_inner(trans->unresolvable,
238 (alpm_list_fn_free)_alpm_pkg_free_trans);
239 alpm_list_free(trans->unresolvable);
240 alpm_list_free_inner(trans->add, (alpm_list_fn_free)_alpm_pkg_free_trans);
241 alpm_list_free(trans->add);
242 alpm_list_free_inner(trans->remove, (alpm_list_fn_free)_alpm_pkg_free);
243 alpm_list_free(trans->remove);
245 FREELIST(trans->skip_remove);
247 FREE(trans);
250 /* A cheap grep for text files, returns 1 if a substring
251 * was found in the text file fn, 0 if it wasn't
253 static int grep(const char *fn, const char *needle)
255 FILE *fp;
257 if((fp = fopen(fn, "r")) == NULL) {
258 return 0;
260 while(!feof(fp)) {
261 char line[1024];
262 if(fgets(line, sizeof(line), fp) == NULL) {
263 continue;
265 /* TODO: this will not work if the search string
266 * ends up being split across line reads */
267 if(strstr(line, needle)) {
268 fclose(fp);
269 return 1;
272 fclose(fp);
273 return 0;
276 int _alpm_runscriptlet(alpm_handle_t *handle, const char *filepath,
277 const char *script, const char *ver, const char *oldver, int is_archive)
279 char arg0[64], arg1[3], cmdline[PATH_MAX];
280 char *argv[] = { arg0, arg1, cmdline, NULL };
281 char *tmpdir, *scriptfn = NULL, *scriptpath;
282 int retval = 0;
283 size_t len;
285 if(_alpm_access(handle, NULL, filepath, R_OK) != 0) {
286 _alpm_log(handle, ALPM_LOG_DEBUG, "scriptlet '%s' not found\n", filepath);
287 return 0;
290 if(!is_archive && !grep(filepath, script)) {
291 /* script not found in scriptlet file; we can only short-circuit this early
292 * if it is an actual scriptlet file and not an archive. */
293 return 0;
296 strcpy(arg0, SCRIPTLET_SHELL);
297 strcpy(arg1, "-c");
299 /* create a directory in $root/tmp/ for copying/extracting the scriptlet */
300 len = strlen(handle->root) + strlen("tmp/alpm_XXXXXX") + 1;
301 MALLOC(tmpdir, len, RET_ERR(handle, ALPM_ERR_MEMORY, -1));
302 snprintf(tmpdir, len, "%stmp/", handle->root);
303 if(access(tmpdir, F_OK) != 0) {
304 _alpm_makepath_mode(tmpdir, 01777);
306 snprintf(tmpdir, len, "%stmp/alpm_XXXXXX", handle->root);
307 if(mkdtemp(tmpdir) == NULL) {
308 _alpm_log(handle, ALPM_LOG_ERROR, _("could not create temp directory\n"));
309 free(tmpdir);
310 return 1;
313 /* either extract or copy the scriptlet */
314 len += strlen("/.INSTALL");
315 MALLOC(scriptfn, len, RET_ERR(handle, ALPM_ERR_MEMORY, -1));
316 snprintf(scriptfn, len, "%s/.INSTALL", tmpdir);
317 if(is_archive) {
318 if(_alpm_unpack_single(handle, filepath, tmpdir, ".INSTALL")) {
319 retval = 1;
321 } else {
322 if(_alpm_copyfile(filepath, scriptfn)) {
323 _alpm_log(handle, ALPM_LOG_ERROR, _("could not copy tempfile to %s (%s)\n"), scriptfn, strerror(errno));
324 retval = 1;
327 if(retval == 1) {
328 goto cleanup;
331 if(is_archive && !grep(scriptfn, script)) {
332 /* script not found in extracted scriptlet file */
333 goto cleanup;
336 /* chop off the root so we can find the tmpdir in the chroot */
337 scriptpath = scriptfn + strlen(handle->root) - 1;
339 if(oldver) {
340 snprintf(cmdline, PATH_MAX, ". %s; %s %s %s",
341 scriptpath, script, ver, oldver);
342 } else {
343 snprintf(cmdline, PATH_MAX, ". %s; %s %s",
344 scriptpath, script, ver);
347 _alpm_log(handle, ALPM_LOG_DEBUG, "executing \"%s\"\n", cmdline);
349 retval = _alpm_run_chroot(handle, SCRIPTLET_SHELL, argv);
351 cleanup:
352 if(scriptfn && unlink(scriptfn)) {
353 _alpm_log(handle, ALPM_LOG_WARNING,
354 _("could not remove %s\n"), scriptfn);
356 if(rmdir(tmpdir)) {
357 _alpm_log(handle, ALPM_LOG_WARNING,
358 _("could not remove tmpdir %s\n"), tmpdir);
361 free(scriptfn);
362 free(tmpdir);
363 return retval;
366 alpm_transflag_t SYMEXPORT alpm_trans_get_flags(alpm_handle_t *handle)
368 /* Sanity checks */
369 CHECK_HANDLE(handle, return -1);
370 ASSERT(handle->trans != NULL, RET_ERR(handle, ALPM_ERR_TRANS_NULL, -1));
372 return handle->trans->flags;
375 alpm_list_t SYMEXPORT *alpm_trans_get_add(alpm_handle_t *handle)
377 /* Sanity checks */
378 CHECK_HANDLE(handle, return NULL);
379 ASSERT(handle->trans != NULL, RET_ERR(handle, ALPM_ERR_TRANS_NULL, NULL));
381 return handle->trans->add;
384 alpm_list_t SYMEXPORT *alpm_trans_get_remove(alpm_handle_t *handle)
386 /* Sanity checks */
387 CHECK_HANDLE(handle, return NULL);
388 ASSERT(handle->trans != NULL, RET_ERR(handle, ALPM_ERR_TRANS_NULL, NULL));
390 return handle->trans->remove;
392 /* vim: set ts=2 sw=2 noet: */