Merge branch 'maint'
[pacman-ng.git] / lib / libalpm / trans.c
blob5ffb5df2064745478a53eb17f018bf732646fb4b
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>
33 #include <fcntl.h>
35 /* libalpm */
36 #include "trans.h"
37 #include "alpm_list.h"
38 #include "package.h"
39 #include "util.h"
40 #include "log.h"
41 #include "handle.h"
42 #include "remove.h"
43 #include "sync.h"
44 #include "alpm.h"
46 /** \addtogroup alpm_trans Transaction Functions
47 * @brief Functions to manipulate libalpm transactions
48 * @{
51 /* Create a lock file */
52 static int make_lock(pmhandle_t *handle)
54 int fd;
55 char *dir, *ptr;
57 ASSERT(handle->lockfile != NULL, return -1);
59 /* create the dir of the lockfile first */
60 dir = strdup(handle->lockfile);
61 ptr = strrchr(dir, '/');
62 if(ptr) {
63 *ptr = '\0';
65 if(_alpm_makepath(dir)) {
66 FREE(dir);
67 return -1;
69 FREE(dir);
71 do {
72 fd = open(handle->lockfile, O_WRONLY | O_CREAT | O_EXCL, 0000);
73 } while(fd == -1 && errno == EINTR);
74 if(fd > 0) {
75 FILE *f = fdopen(fd, "w");
76 fprintf(f, "%ld\n", (long)getpid());
77 fflush(f);
78 fsync(fd);
79 handle->lckstream = f;
80 return 0;
82 return -1;
85 /* Remove a lock file */
86 static int remove_lock(pmhandle_t *handle)
88 if(handle->lckstream != NULL) {
89 fclose(handle->lckstream);
90 handle->lckstream = NULL;
92 if(unlink(handle->lockfile) == -1 && errno != ENOENT) {
93 return -1;
95 return 0;
98 /** Initialize the transaction. */
99 int SYMEXPORT alpm_trans_init(pmtransflag_t flags,
100 alpm_trans_cb_event event, alpm_trans_cb_conv conv,
101 alpm_trans_cb_progress progress)
103 pmtrans_t *trans;
104 const int required_db_version = 2;
105 int db_version;
107 ALPM_LOG_FUNC;
109 /* Sanity checks */
110 ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1));
111 ASSERT(handle->trans == NULL, RET_ERR(PM_ERR_TRANS_NOT_NULL, -1));
113 /* lock db */
114 if(!(flags & PM_TRANS_FLAG_NOLOCK)) {
115 if(make_lock(handle)) {
116 RET_ERR(PM_ERR_HANDLE_LOCK, -1);
120 trans = _alpm_trans_new();
121 if(trans == NULL) {
122 RET_ERR(PM_ERR_MEMORY, -1);
125 trans->flags = flags;
126 trans->cb_event = event;
127 trans->cb_conv = conv;
128 trans->cb_progress = progress;
129 trans->state = STATE_INITIALIZED;
131 handle->trans = trans;
133 /* check database version */
134 db_version = _alpm_db_version(handle->db_local);
135 if(db_version < required_db_version) {
136 _alpm_log(PM_LOG_ERROR,
137 _("%s database version is too old\n"), handle->db_local->treename);
138 remove_lock(handle);
139 _alpm_trans_free(trans);
140 RET_ERR(PM_ERR_DB_VERSION, -1);
143 return 0;
146 static alpm_list_t *check_arch(alpm_list_t *pkgs)
148 alpm_list_t *i;
149 alpm_list_t *invalid = NULL;
151 const char *arch = alpm_option_get_arch();
152 if(!arch) {
153 return NULL;
155 for(i = pkgs; i; i = i->next) {
156 pmpkg_t *pkg = i->data;
157 const char *pkgarch = alpm_pkg_get_arch(pkg);
158 if(pkgarch && strcmp(pkgarch, arch) && strcmp(pkgarch, "any")) {
159 char *string;
160 const char *pkgname = alpm_pkg_get_name(pkg);
161 const char *pkgver = alpm_pkg_get_version(pkg);
162 size_t len = strlen(pkgname) + strlen(pkgver) + strlen(pkgarch) + 3;
163 MALLOC(string, len, RET_ERR(PM_ERR_MEMORY, invalid));
164 sprintf(string, "%s-%s-%s", pkgname, pkgver, pkgarch);
165 invalid = alpm_list_add(invalid, string);
168 return invalid;
171 /** Prepare a transaction. */
172 int SYMEXPORT alpm_trans_prepare(alpm_list_t **data)
174 pmtrans_t *trans;
176 ALPM_LOG_FUNC;
178 /* Sanity checks */
179 ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1));
180 ASSERT(data != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1));
182 trans = handle->trans;
184 ASSERT(trans != NULL, RET_ERR(PM_ERR_TRANS_NULL, -1));
185 ASSERT(trans->state == STATE_INITIALIZED, RET_ERR(PM_ERR_TRANS_NOT_INITIALIZED, -1));
187 /* If there's nothing to do, return without complaining */
188 if(trans->add == NULL && trans->remove == NULL) {
189 return 0;
192 alpm_list_t *invalid = check_arch(trans->add);
193 if(invalid) {
194 if(data) {
195 *data = invalid;
197 RET_ERR(PM_ERR_PKG_INVALID_ARCH, -1);
200 if(trans->add == NULL) {
201 if(_alpm_remove_prepare(trans, handle->db_local, data) == -1) {
202 /* pm_errno is set by _alpm_remove_prepare() */
203 return -1;
205 } else {
206 if(_alpm_sync_prepare(trans, handle->db_local, handle->dbs_sync, data) == -1) {
207 /* pm_errno is set by _alpm_sync_prepare() */
208 return -1;
212 trans->state = STATE_PREPARED;
214 return 0;
217 /** Commit a transaction. */
218 int SYMEXPORT alpm_trans_commit(alpm_list_t **data)
220 pmtrans_t *trans;
222 ALPM_LOG_FUNC;
224 /* Sanity checks */
225 ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1));
227 trans = handle->trans;
229 ASSERT(trans != NULL, RET_ERR(PM_ERR_TRANS_NULL, -1));
230 ASSERT(trans->state == STATE_PREPARED, RET_ERR(PM_ERR_TRANS_NOT_PREPARED, -1));
232 ASSERT(!(trans->flags & PM_TRANS_FLAG_NOLOCK), RET_ERR(PM_ERR_TRANS_NOT_LOCKED, -1));
234 /* If there's nothing to do, return without complaining */
235 if(trans->add == NULL && trans->remove == NULL) {
236 return 0;
239 trans->state = STATE_COMMITING;
241 if(trans->add == NULL) {
242 if(_alpm_remove_packages(trans, handle->db_local) == -1) {
243 /* pm_errno is set by _alpm_remove_commit() */
244 return -1;
246 } else {
247 if(_alpm_sync_commit(trans, handle->db_local, data) == -1) {
248 /* pm_errno is set by _alpm_sync_commit() */
249 return -1;
253 trans->state = STATE_COMMITED;
255 return 0;
258 /** Interrupt a transaction. */
259 int SYMEXPORT alpm_trans_interrupt(void)
261 pmtrans_t *trans;
263 ALPM_LOG_FUNC;
265 /* Sanity checks */
266 ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1));
268 trans = handle->trans;
269 ASSERT(trans != NULL, RET_ERR(PM_ERR_TRANS_NULL, -1));
270 ASSERT(trans->state == STATE_COMMITING || trans->state == STATE_INTERRUPTED,
271 RET_ERR(PM_ERR_TRANS_TYPE, -1));
273 trans->state = STATE_INTERRUPTED;
275 return 0;
278 /** Release a transaction. */
279 int SYMEXPORT alpm_trans_release(void)
281 pmtrans_t *trans;
283 ALPM_LOG_FUNC;
285 /* Sanity checks */
286 ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1));
288 trans = handle->trans;
289 ASSERT(trans != NULL, RET_ERR(PM_ERR_TRANS_NULL, -1));
290 ASSERT(trans->state != STATE_IDLE, RET_ERR(PM_ERR_TRANS_NULL, -1));
292 int nolock_flag = trans->flags & PM_TRANS_FLAG_NOLOCK;
294 _alpm_trans_free(trans);
295 handle->trans = NULL;
297 /* unlock db */
298 if(!nolock_flag) {
299 if(remove_lock(handle)) {
300 _alpm_log(PM_LOG_WARNING, _("could not remove lock file %s\n"),
301 alpm_option_get_lockfile());
302 alpm_logaction("warning: could not remove lock file %s\n",
303 alpm_option_get_lockfile());
307 return 0;
310 /** @} */
312 pmtrans_t *_alpm_trans_new(void)
314 pmtrans_t *trans;
316 ALPM_LOG_FUNC;
318 CALLOC(trans, 1, sizeof(pmtrans_t), RET_ERR(PM_ERR_MEMORY, NULL));
319 trans->state = STATE_IDLE;
321 return trans;
324 void _alpm_trans_free(pmtrans_t *trans)
326 ALPM_LOG_FUNC;
328 if(trans == NULL) {
329 return;
332 alpm_list_free_inner(trans->add, (alpm_list_fn_free)_alpm_pkg_free_trans);
333 alpm_list_free(trans->add);
334 alpm_list_free_inner(trans->remove, (alpm_list_fn_free)_alpm_pkg_free);
335 alpm_list_free(trans->remove);
337 FREELIST(trans->skip_remove);
339 FREE(trans);
342 /* A cheap grep for text files, returns 1 if a substring
343 * was found in the text file fn, 0 if it wasn't
345 static int grep(const char *fn, const char *needle)
347 FILE *fp;
349 if((fp = fopen(fn, "r")) == NULL) {
350 return 0;
352 while(!feof(fp)) {
353 char line[1024];
354 if(fgets(line, sizeof(line), fp) == NULL) {
355 continue;
357 /* TODO: this will not work if the search string
358 * ends up being split across line reads */
359 if(strstr(line, needle)) {
360 fclose(fp);
361 return 1;
364 fclose(fp);
365 return 0;
368 int _alpm_runscriptlet(const char *root, const char *installfn,
369 const char *script, const char *ver,
370 const char *oldver, pmtrans_t UNUSED *trans)
372 char scriptfn[PATH_MAX];
373 char cmdline[PATH_MAX];
374 char tmpdir[PATH_MAX];
375 char *argv[] = { "sh", "-c", cmdline, NULL };
376 char *scriptpath;
377 int clean_tmpdir = 0;
378 int retval = 0;
380 ALPM_LOG_FUNC;
382 if(access(installfn, R_OK)) {
383 /* not found */
384 _alpm_log(PM_LOG_DEBUG, "scriptlet '%s' not found\n", installfn);
385 return 0;
388 /* creates a directory in $root/tmp/ for copying/extracting the scriptlet */
389 snprintf(tmpdir, PATH_MAX, "%stmp/", root);
390 if(access(tmpdir, F_OK) != 0) {
391 _alpm_makepath_mode(tmpdir, 01777);
393 snprintf(tmpdir, PATH_MAX, "%stmp/alpm_XXXXXX", root);
394 if(mkdtemp(tmpdir) == NULL) {
395 _alpm_log(PM_LOG_ERROR, _("could not create temp directory\n"));
396 return 1;
397 } else {
398 clean_tmpdir = 1;
401 /* either extract or copy the scriptlet */
402 snprintf(scriptfn, PATH_MAX, "%s/.INSTALL", tmpdir);
403 if(strcmp(script, "pre_upgrade") == 0 || strcmp(script, "pre_install") == 0) {
404 if(_alpm_unpack_single(installfn, tmpdir, ".INSTALL")) {
405 retval = 1;
407 } else {
408 if(_alpm_copyfile(installfn, scriptfn)) {
409 _alpm_log(PM_LOG_ERROR, _("could not copy tempfile to %s (%s)\n"), scriptfn, strerror(errno));
410 retval = 1;
413 if(retval == 1) {
414 goto cleanup;
417 /* chop off the root so we can find the tmpdir in the chroot */
418 scriptpath = scriptfn + strlen(root) - 1;
420 if(!grep(scriptfn, script)) {
421 /* script not found in scriptlet file */
422 goto cleanup;
425 if(oldver) {
426 snprintf(cmdline, PATH_MAX, ". %s; %s %s %s",
427 scriptpath, script, ver, oldver);
428 } else {
429 snprintf(cmdline, PATH_MAX, ". %s; %s %s",
430 scriptpath, script, ver);
433 _alpm_log(PM_LOG_DEBUG, "executing \"%s\"\n", cmdline);
435 retval = _alpm_run_chroot(root, "/bin/sh", argv);
437 cleanup:
438 if(clean_tmpdir && _alpm_rmrf(tmpdir)) {
439 _alpm_log(PM_LOG_WARNING, _("could not remove tmpdir %s\n"), tmpdir);
442 return retval;
445 int SYMEXPORT alpm_trans_get_flags()
447 /* Sanity checks */
448 ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1));
449 ASSERT(handle->trans != NULL, RET_ERR(PM_ERR_TRANS_NULL, -1));
451 return handle->trans->flags;
454 alpm_list_t SYMEXPORT * alpm_trans_get_add()
456 /* Sanity checks */
457 ASSERT(handle != NULL, return NULL);
458 ASSERT(handle->trans != NULL, return NULL);
460 return handle->trans->add;
463 alpm_list_t SYMEXPORT * alpm_trans_get_remove()
465 /* Sanity checks */
466 ASSERT(handle != NULL, return NULL);
467 ASSERT(handle->trans != NULL, return NULL);
469 return handle->trans->remove;
471 /* vim: set ts=2 sw=2 noet: */