Move database 'version' check to registration time
[pacman-ng.git] / lib / libalpm / trans.c
blobb4bdccfba6f045a4e3857413f95b87fd030fec22
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(pmhandle_t *handle, pmtransflag_t flags,
100 alpm_trans_cb_event event, alpm_trans_cb_conv conv,
101 alpm_trans_cb_progress progress)
103 pmtrans_t *trans;
105 /* Sanity checks */
106 CHECK_HANDLE(handle, return -1);
107 ASSERT(handle->trans == NULL, RET_ERR(handle, PM_ERR_TRANS_NOT_NULL, -1));
109 /* lock db */
110 if(!(flags & PM_TRANS_FLAG_NOLOCK)) {
111 if(make_lock(handle)) {
112 RET_ERR(handle, PM_ERR_HANDLE_LOCK, -1);
116 CALLOC(trans, 1, sizeof(pmtrans_t), RET_ERR(handle, PM_ERR_MEMORY, -1));
117 trans->flags = flags;
118 trans->cb_event = event;
119 trans->cb_conv = conv;
120 trans->cb_progress = progress;
121 trans->state = STATE_INITIALIZED;
123 handle->trans = trans;
125 return 0;
128 static alpm_list_t *check_arch(pmhandle_t *handle, alpm_list_t *pkgs)
130 alpm_list_t *i;
131 alpm_list_t *invalid = NULL;
133 const char *arch = alpm_option_get_arch(handle);
134 if(!arch) {
135 return NULL;
137 for(i = pkgs; i; i = i->next) {
138 pmpkg_t *pkg = i->data;
139 const char *pkgarch = alpm_pkg_get_arch(pkg);
140 if(pkgarch && strcmp(pkgarch, arch) && strcmp(pkgarch, "any")) {
141 char *string;
142 const char *pkgname = alpm_pkg_get_name(pkg);
143 const char *pkgver = alpm_pkg_get_version(pkg);
144 size_t len = strlen(pkgname) + strlen(pkgver) + strlen(pkgarch) + 3;
145 MALLOC(string, len, RET_ERR(handle, PM_ERR_MEMORY, invalid));
146 sprintf(string, "%s-%s-%s", pkgname, pkgver, pkgarch);
147 invalid = alpm_list_add(invalid, string);
150 return invalid;
153 /** Prepare a transaction. */
154 int SYMEXPORT alpm_trans_prepare(pmhandle_t *handle, alpm_list_t **data)
156 pmtrans_t *trans;
158 /* Sanity checks */
159 CHECK_HANDLE(handle, return -1);
160 ASSERT(data != NULL, RET_ERR(handle, PM_ERR_WRONG_ARGS, -1));
162 trans = handle->trans;
164 ASSERT(trans != NULL, RET_ERR(handle, PM_ERR_TRANS_NULL, -1));
165 ASSERT(trans->state == STATE_INITIALIZED, RET_ERR(handle, PM_ERR_TRANS_NOT_INITIALIZED, -1));
167 /* If there's nothing to do, return without complaining */
168 if(trans->add == NULL && trans->remove == NULL) {
169 return 0;
172 alpm_list_t *invalid = check_arch(handle, trans->add);
173 if(invalid) {
174 if(data) {
175 *data = invalid;
177 RET_ERR(handle, PM_ERR_PKG_INVALID_ARCH, -1);
180 if(trans->add == NULL) {
181 if(_alpm_remove_prepare(handle, data) == -1) {
182 /* pm_errno is set by _alpm_remove_prepare() */
183 return -1;
185 } else {
186 if(_alpm_sync_prepare(handle, data) == -1) {
187 /* pm_errno is set by _alpm_sync_prepare() */
188 return -1;
192 trans->state = STATE_PREPARED;
194 return 0;
197 /** Commit a transaction. */
198 int SYMEXPORT alpm_trans_commit(pmhandle_t *handle, alpm_list_t **data)
200 pmtrans_t *trans;
202 /* Sanity checks */
203 CHECK_HANDLE(handle, return -1);
205 trans = handle->trans;
207 ASSERT(trans != NULL, RET_ERR(handle, PM_ERR_TRANS_NULL, -1));
208 ASSERT(trans->state == STATE_PREPARED, RET_ERR(handle, PM_ERR_TRANS_NOT_PREPARED, -1));
210 ASSERT(!(trans->flags & PM_TRANS_FLAG_NOLOCK), RET_ERR(handle, PM_ERR_TRANS_NOT_LOCKED, -1));
212 /* If there's nothing to do, return without complaining */
213 if(trans->add == NULL && trans->remove == NULL) {
214 return 0;
217 trans->state = STATE_COMMITING;
219 if(trans->add == NULL) {
220 if(_alpm_remove_packages(handle) == -1) {
221 /* pm_errno is set by _alpm_remove_commit() */
222 return -1;
224 } else {
225 if(_alpm_sync_commit(handle, data) == -1) {
226 /* pm_errno is set by _alpm_sync_commit() */
227 return -1;
231 trans->state = STATE_COMMITED;
233 return 0;
236 /** Interrupt a transaction. */
237 int SYMEXPORT alpm_trans_interrupt(pmhandle_t *handle)
239 pmtrans_t *trans;
241 /* Sanity checks */
242 CHECK_HANDLE(handle, return -1);
244 trans = handle->trans;
245 ASSERT(trans != NULL, RET_ERR(handle, PM_ERR_TRANS_NULL, -1));
246 ASSERT(trans->state == STATE_COMMITING || trans->state == STATE_INTERRUPTED,
247 RET_ERR(handle, PM_ERR_TRANS_TYPE, -1));
249 trans->state = STATE_INTERRUPTED;
251 return 0;
254 /** Release a transaction. */
255 int SYMEXPORT alpm_trans_release(pmhandle_t *handle)
257 pmtrans_t *trans;
259 /* Sanity checks */
260 CHECK_HANDLE(handle, return -1);
262 trans = handle->trans;
263 ASSERT(trans != NULL, RET_ERR(handle, PM_ERR_TRANS_NULL, -1));
264 ASSERT(trans->state != STATE_IDLE, RET_ERR(handle, PM_ERR_TRANS_NULL, -1));
266 int nolock_flag = trans->flags & PM_TRANS_FLAG_NOLOCK;
268 _alpm_trans_free(trans);
269 handle->trans = NULL;
271 /* unlock db */
272 if(!nolock_flag) {
273 if(remove_lock(handle)) {
274 _alpm_log(handle, PM_LOG_WARNING, _("could not remove lock file %s\n"),
275 alpm_option_get_lockfile(handle));
276 alpm_logaction(handle, "warning: could not remove lock file %s\n",
277 alpm_option_get_lockfile(handle));
281 return 0;
284 /** @} */
286 void _alpm_trans_free(pmtrans_t *trans)
288 if(trans == NULL) {
289 return;
292 alpm_list_free_inner(trans->add, (alpm_list_fn_free)_alpm_pkg_free_trans);
293 alpm_list_free(trans->add);
294 alpm_list_free_inner(trans->remove, (alpm_list_fn_free)_alpm_pkg_free);
295 alpm_list_free(trans->remove);
297 FREELIST(trans->skip_remove);
299 FREE(trans);
302 /* A cheap grep for text files, returns 1 if a substring
303 * was found in the text file fn, 0 if it wasn't
305 static int grep(const char *fn, const char *needle)
307 FILE *fp;
309 if((fp = fopen(fn, "r")) == NULL) {
310 return 0;
312 while(!feof(fp)) {
313 char line[1024];
314 if(fgets(line, sizeof(line), fp) == NULL) {
315 continue;
317 /* TODO: this will not work if the search string
318 * ends up being split across line reads */
319 if(strstr(line, needle)) {
320 fclose(fp);
321 return 1;
324 fclose(fp);
325 return 0;
328 int _alpm_runscriptlet(pmhandle_t *handle, const char *installfn,
329 const char *script, const char *ver, const char *oldver)
331 char scriptfn[PATH_MAX];
332 char cmdline[PATH_MAX];
333 char tmpdir[PATH_MAX];
334 char *argv[] = { "sh", "-c", cmdline, NULL };
335 char *scriptpath;
336 int clean_tmpdir = 0;
337 int retval = 0;
339 if(access(installfn, R_OK)) {
340 /* not found */
341 _alpm_log(handle, PM_LOG_DEBUG, "scriptlet '%s' not found\n", installfn);
342 return 0;
345 /* creates a directory in $root/tmp/ for copying/extracting the scriptlet */
346 snprintf(tmpdir, PATH_MAX, "%stmp/", handle->root);
347 if(access(tmpdir, F_OK) != 0) {
348 _alpm_makepath_mode(tmpdir, 01777);
350 snprintf(tmpdir, PATH_MAX, "%stmp/alpm_XXXXXX", handle->root);
351 if(mkdtemp(tmpdir) == NULL) {
352 _alpm_log(handle, PM_LOG_ERROR, _("could not create temp directory\n"));
353 return 1;
354 } else {
355 clean_tmpdir = 1;
358 /* either extract or copy the scriptlet */
359 snprintf(scriptfn, PATH_MAX, "%s/.INSTALL", tmpdir);
360 if(strcmp(script, "pre_upgrade") == 0 || strcmp(script, "pre_install") == 0) {
361 if(_alpm_unpack_single(handle, installfn, tmpdir, ".INSTALL")) {
362 retval = 1;
364 } else {
365 if(_alpm_copyfile(installfn, scriptfn)) {
366 _alpm_log(handle, PM_LOG_ERROR, _("could not copy tempfile to %s (%s)\n"), scriptfn, strerror(errno));
367 retval = 1;
370 if(retval == 1) {
371 goto cleanup;
374 /* chop off the root so we can find the tmpdir in the chroot */
375 scriptpath = scriptfn + strlen(handle->root) - 1;
377 if(!grep(scriptfn, script)) {
378 /* script not found in scriptlet file */
379 goto cleanup;
382 if(oldver) {
383 snprintf(cmdline, PATH_MAX, ". %s; %s %s %s",
384 scriptpath, script, ver, oldver);
385 } else {
386 snprintf(cmdline, PATH_MAX, ". %s; %s %s",
387 scriptpath, script, ver);
390 _alpm_log(handle, PM_LOG_DEBUG, "executing \"%s\"\n", cmdline);
392 retval = _alpm_run_chroot(handle, "/bin/sh", argv);
394 cleanup:
395 if(clean_tmpdir && _alpm_rmrf(tmpdir)) {
396 _alpm_log(handle, PM_LOG_WARNING, _("could not remove tmpdir %s\n"), tmpdir);
399 return retval;
402 pmtransflag_t SYMEXPORT alpm_trans_get_flags(pmhandle_t *handle)
404 /* Sanity checks */
405 CHECK_HANDLE(handle, return -1);
406 ASSERT(handle->trans != NULL, RET_ERR(handle, PM_ERR_TRANS_NULL, -1));
408 return handle->trans->flags;
411 alpm_list_t SYMEXPORT *alpm_trans_get_add(pmhandle_t *handle)
413 /* Sanity checks */
414 CHECK_HANDLE(handle, return NULL);
415 ASSERT(handle->trans != NULL, RET_ERR(handle, PM_ERR_TRANS_NULL, NULL));
417 return handle->trans->add;
420 alpm_list_t SYMEXPORT *alpm_trans_get_remove(pmhandle_t *handle)
422 /* Sanity checks */
423 CHECK_HANDLE(handle, return NULL);
424 ASSERT(handle->trans != NULL, RET_ERR(handle, PM_ERR_TRANS_NULL, NULL));
426 return handle->trans->remove;
428 /* vim: set ts=2 sw=2 noet: */