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>
37 #include "alpm_list.h"
46 /** \addtogroup alpm_trans Transaction Functions
47 * @brief Functions to manipulate libalpm transactions
51 /* Create a lock file */
52 static int make_lock(pmhandle_t
*handle
)
57 ASSERT(handle
->lockfile
!= NULL
, return -1);
59 /* create the dir of the lockfile first */
60 dir
= strdup(handle
->lockfile
);
61 ptr
= strrchr(dir
, '/');
65 if(_alpm_makepath(dir
)) {
72 fd
= open(handle
->lockfile
, O_WRONLY
| O_CREAT
| O_EXCL
, 0000);
73 } while(fd
== -1 && errno
== EINTR
);
75 FILE *f
= fdopen(fd
, "w");
76 fprintf(f
, "%ld\n", (long)getpid());
79 handle
->lckstream
= f
;
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
) {
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
)
104 const int required_db_version
= 2;
110 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
111 ASSERT(handle
->trans
== NULL
, RET_ERR(PM_ERR_TRANS_NOT_NULL
, -1));
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();
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
);
139 _alpm_trans_free(trans
);
140 RET_ERR(PM_ERR_DB_VERSION
, -1);
146 static alpm_list_t
*check_arch(alpm_list_t
*pkgs
)
149 alpm_list_t
*invalid
= NULL
;
151 const char *arch
= alpm_option_get_arch();
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")) {
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
);
171 /** Prepare a transaction. */
172 int SYMEXPORT
alpm_trans_prepare(alpm_list_t
**data
)
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
) {
192 alpm_list_t
*invalid
= check_arch(trans
->add
);
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() */
206 if(_alpm_sync_prepare(trans
, handle
->db_local
, handle
->dbs_sync
, data
) == -1) {
207 /* pm_errno is set by _alpm_sync_prepare() */
212 trans
->state
= STATE_PREPARED
;
217 /** Commit a transaction. */
218 int SYMEXPORT
alpm_trans_commit(alpm_list_t
**data
)
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
) {
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() */
247 if(_alpm_sync_commit(trans
, handle
->db_local
, data
) == -1) {
248 /* pm_errno is set by _alpm_sync_commit() */
253 trans
->state
= STATE_COMMITED
;
258 /** Interrupt a transaction. */
259 int SYMEXPORT
alpm_trans_interrupt(void)
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
;
278 /** Release a transaction. */
279 int SYMEXPORT
alpm_trans_release(void)
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
;
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());
312 pmtrans_t
*_alpm_trans_new(void)
318 CALLOC(trans
, 1, sizeof(pmtrans_t
), RET_ERR(PM_ERR_MEMORY
, NULL
));
319 trans
->state
= STATE_IDLE
;
324 void _alpm_trans_free(pmtrans_t
*trans
)
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
);
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
)
349 if((fp
= fopen(fn
, "r")) == NULL
) {
354 if(fgets(line
, sizeof(line
), fp
) == NULL
) {
357 /* TODO: this will not work if the search string
358 * ends up being split across line reads */
359 if(strstr(line
, needle
)) {
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
};
377 int clean_tmpdir
= 0;
382 if(access(installfn
, R_OK
)) {
384 _alpm_log(PM_LOG_DEBUG
, "scriptlet '%s' not found\n", installfn
);
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"));
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")) {
408 if(_alpm_copyfile(installfn
, scriptfn
)) {
409 _alpm_log(PM_LOG_ERROR
, _("could not copy tempfile to %s (%s)\n"), scriptfn
, strerror(errno
));
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 */
426 snprintf(cmdline
, PATH_MAX
, ". %s; %s %s %s",
427 scriptpath
, script
, ver
, oldver
);
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
);
438 if(clean_tmpdir
&& _alpm_rmrf(tmpdir
)) {
439 _alpm_log(PM_LOG_WARNING
, _("could not remove tmpdir %s\n"), tmpdir
);
445 int SYMEXPORT
alpm_trans_get_flags()
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()
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()
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: */