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/>.
28 #include <sys/types.h>
34 #include "alpm_list.h"
43 /** \addtogroup alpm_trans Transaction Functions
44 * @brief Functions to manipulate libalpm transactions
48 /** Initialize the transaction. */
49 int SYMEXPORT
alpm_trans_init(alpm_handle_t
*handle
, alpm_transflag_t flags
)
54 CHECK_HANDLE(handle
, return -1);
55 ASSERT(handle
->trans
== NULL
, RET_ERR(handle
, ALPM_ERR_TRANS_NOT_NULL
, -1));
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));
66 trans
->state
= STATE_INITIALIZED
;
68 handle
->trans
= trans
;
73 static alpm_list_t
*check_arch(alpm_handle_t
*handle
, alpm_list_t
*pkgs
)
76 alpm_list_t
*invalid
= NULL
;
78 const char *arch
= handle
->arch
;
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")) {
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
);
98 /** Prepare a transaction. */
99 int SYMEXPORT
alpm_trans_prepare(alpm_handle_t
*handle
, alpm_list_t
**data
)
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
) {
117 alpm_list_t
*invalid
= check_arch(handle
, trans
->add
);
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() */
131 if(_alpm_sync_prepare(handle
, data
) == -1) {
132 /* pm_errno is set by _alpm_sync_prepare() */
137 trans
->state
= STATE_PREPARED
;
142 /** Commit a transaction. */
143 int SYMEXPORT
alpm_trans_commit(alpm_handle_t
*handle
, alpm_list_t
**data
)
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
) {
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() */
170 if(_alpm_sync_commit(handle
, data
) == -1) {
171 /* pm_errno is set by _alpm_sync_commit() */
176 trans
->state
= STATE_COMMITED
;
181 /** Interrupt a transaction. */
182 int SYMEXPORT
alpm_trans_interrupt(alpm_handle_t
*handle
)
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
;
199 /** Release a transaction. */
200 int SYMEXPORT
alpm_trans_release(alpm_handle_t
*handle
)
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
;
218 if(_alpm_handle_unlock(handle
)) {
219 _alpm_log(handle
, ALPM_LOG_WARNING
, _("could not remove lock file %s\n"),
221 alpm_logaction(handle
, "warning: could not remove lock file %s\n",
231 void _alpm_trans_free(alpm_trans_t
*trans
)
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
);
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
)
257 if((fp
= fopen(fn
, "r")) == NULL
) {
262 if(fgets(line
, sizeof(line
), fp
) == NULL
) {
265 /* TODO: this will not work if the search string
266 * ends up being split across line reads */
267 if(strstr(line
, needle
)) {
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
;
285 if(_alpm_access(handle
, NULL
, filepath
, R_OK
) != 0) {
286 _alpm_log(handle
, ALPM_LOG_DEBUG
, "scriptlet '%s' not found\n", filepath
);
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. */
296 strcpy(arg0
, SCRIPTLET_SHELL
);
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"));
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
);
318 if(_alpm_unpack_single(handle
, filepath
, tmpdir
, ".INSTALL")) {
322 if(_alpm_copyfile(filepath
, scriptfn
)) {
323 _alpm_log(handle
, ALPM_LOG_ERROR
, _("could not copy tempfile to %s (%s)\n"), scriptfn
, strerror(errno
));
331 if(is_archive
&& !grep(scriptfn
, script
)) {
332 /* script not found in extracted scriptlet file */
336 /* chop off the root so we can find the tmpdir in the chroot */
337 scriptpath
= scriptfn
+ strlen(handle
->root
) - 1;
340 snprintf(cmdline
, PATH_MAX
, ". %s; %s %s %s",
341 scriptpath
, script
, ver
, oldver
);
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
);
352 if(scriptfn
&& unlink(scriptfn
)) {
353 _alpm_log(handle
, ALPM_LOG_WARNING
,
354 _("could not remove %s\n"), scriptfn
);
357 _alpm_log(handle
, ALPM_LOG_WARNING
,
358 _("could not remove tmpdir %s\n"), tmpdir
);
366 alpm_transflag_t SYMEXPORT
alpm_trans_get_flags(alpm_handle_t
*handle
)
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
)
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
)
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: */