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/>.
30 #include <sys/types.h>
36 #include "alpm_list.h"
45 /** \addtogroup alpm_trans Transaction Functions
46 * @brief Functions to manipulate libalpm transactions
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
)
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 pmdb_t
*db
= i
->data
;
64 if(!(db
->status
& DB_STATUS_VALID
)) {
65 RET_ERR(handle
, PM_ERR_DB_INVALID
, -1);
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(pmtrans_t
), RET_ERR(handle
, PM_ERR_MEMORY
, -1));
78 trans
->cb_event
= event
;
79 trans
->cb_conv
= conv
;
80 trans
->cb_progress
= progress
;
81 trans
->state
= STATE_INITIALIZED
;
83 handle
->trans
= trans
;
88 static alpm_list_t
*check_arch(alpm_handle_t
*handle
, alpm_list_t
*pkgs
)
91 alpm_list_t
*invalid
= NULL
;
93 const char *arch
= alpm_option_get_arch(handle
);
97 for(i
= pkgs
; i
; i
= i
->next
) {
98 pmpkg_t
*pkg
= i
->data
;
99 const char *pkgarch
= alpm_pkg_get_arch(pkg
);
100 if(pkgarch
&& strcmp(pkgarch
, arch
) && strcmp(pkgarch
, "any")) {
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
);
113 /** Prepare a transaction. */
114 int SYMEXPORT
alpm_trans_prepare(alpm_handle_t
*handle
, alpm_list_t
**data
)
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
) {
132 alpm_list_t
*invalid
= check_arch(handle
, trans
->add
);
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() */
146 if(_alpm_sync_prepare(handle
, data
) == -1) {
147 /* pm_errno is set by _alpm_sync_prepare() */
152 trans
->state
= STATE_PREPARED
;
157 /** Commit a transaction. */
158 int SYMEXPORT
alpm_trans_commit(alpm_handle_t
*handle
, alpm_list_t
**data
)
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
) {
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() */
185 if(_alpm_sync_commit(handle
, data
) == -1) {
186 /* pm_errno is set by _alpm_sync_commit() */
191 trans
->state
= STATE_COMMITED
;
196 /** Interrupt a transaction. */
197 int SYMEXPORT
alpm_trans_interrupt(alpm_handle_t
*handle
)
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
;
214 /** Release a transaction. */
215 int SYMEXPORT
alpm_trans_release(alpm_handle_t
*handle
)
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
;
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
));
246 void _alpm_trans_free(pmtrans_t
*trans
)
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
);
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
)
269 if((fp
= fopen(fn
, "r")) == NULL
) {
274 if(fgets(line
, sizeof(line
), fp
) == NULL
) {
277 /* TODO: this will not work if the search string
278 * ends up being split across line reads */
279 if(strstr(line
, needle
)) {
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
};
296 int clean_tmpdir
= 0;
299 if(access(installfn
, R_OK
)) {
301 _alpm_log(handle
, PM_LOG_DEBUG
, "scriptlet '%s' not found\n", installfn
);
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"));
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")) {
325 if(_alpm_copyfile(installfn
, scriptfn
)) {
326 _alpm_log(handle
, PM_LOG_ERROR
, _("could not copy tempfile to %s (%s)\n"), scriptfn
, strerror(errno
));
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 */
343 snprintf(cmdline
, PATH_MAX
, ". %s; %s %s %s",
344 scriptpath
, script
, ver
, oldver
);
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
);
355 if(clean_tmpdir
&& _alpm_rmrf(tmpdir
)) {
356 _alpm_log(handle
, PM_LOG_WARNING
, _("could not remove tmpdir %s\n"), tmpdir
);
362 pmtransflag_t SYMEXPORT
alpm_trans_get_flags(alpm_handle_t
*handle
)
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
)
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
)
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: */