2005-03-14 20:51:43 -05:00
|
|
|
/*
|
|
|
|
* trans.c
|
2006-10-15 15:31:03 -04:00
|
|
|
*
|
2016-01-02 22:48:43 -05:00
|
|
|
* Copyright (c) 2006-2016 Pacman Development Team <pacman-dev@archlinux.org>
|
2009-07-01 03:08:33 -04:00
|
|
|
* Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
|
2006-10-15 15:31:03 -04:00
|
|
|
* Copyright (c) 2005 by Aurelien Foret <orelien@chez.com>
|
|
|
|
* Copyright (c) 2005 by Christian Hamar <krics@linuxforum.hu>
|
|
|
|
* Copyright (c) 2005, 2006 by Miklos Vajna <vmiklos@frugalware.org>
|
2007-11-16 21:18:45 -05:00
|
|
|
*
|
2005-03-14 20:51:43 -05:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2007-12-10 23:55:22 -05:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2005-03-14 20:51:43 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2008-06-15 20:15:36 -04:00
|
|
|
#include <unistd.h>
|
2007-04-29 12:47:02 -04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <errno.h>
|
2010-12-17 13:48:09 -05:00
|
|
|
#include <limits.h>
|
2007-03-05 17:13:33 -05:00
|
|
|
|
|
|
|
/* libalpm */
|
|
|
|
#include "trans.h"
|
|
|
|
#include "alpm_list.h"
|
2005-03-14 20:51:43 -05:00
|
|
|
#include "package.h"
|
|
|
|
#include "util.h"
|
2006-03-01 02:51:00 -05:00
|
|
|
#include "log.h"
|
2005-03-14 20:51:43 -05:00
|
|
|
#include "handle.h"
|
|
|
|
#include "remove.h"
|
|
|
|
#include "sync.h"
|
|
|
|
#include "alpm.h"
|
2013-12-11 11:58:28 -05:00
|
|
|
#include "deps.h"
|
2015-10-16 20:28:25 -04:00
|
|
|
#include "hook.h"
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2007-06-03 23:57:38 -04:00
|
|
|
/** \addtogroup alpm_trans Transaction Functions
|
|
|
|
* @brief Functions to manipulate libalpm transactions
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2011-03-06 11:50:01 -05:00
|
|
|
/** Initialize the transaction. */
|
2011-09-01 18:16:56 -04:00
|
|
|
int SYMEXPORT alpm_trans_init(alpm_handle_t *handle, alpm_transflag_t flags)
|
2007-06-03 23:57:38 -04:00
|
|
|
{
|
2011-06-28 09:27:16 -04:00
|
|
|
alpm_trans_t *trans;
|
2009-07-15 13:14:01 -04:00
|
|
|
|
2007-06-03 23:57:38 -04:00
|
|
|
/* Sanity checks */
|
2011-06-14 11:01:08 -04:00
|
|
|
CHECK_HANDLE(handle, return -1);
|
2011-07-01 12:01:39 -04:00
|
|
|
ASSERT(handle->trans == NULL, RET_ERR(handle, ALPM_ERR_TRANS_NOT_NULL, -1));
|
2007-06-03 23:57:38 -04:00
|
|
|
|
|
|
|
/* lock db */
|
2011-07-01 12:01:39 -04:00
|
|
|
if(!(flags & ALPM_TRANS_FLAG_NOLOCK)) {
|
2011-06-24 05:02:58 -04:00
|
|
|
if(_alpm_handle_lock(handle)) {
|
2011-07-01 12:01:39 -04:00
|
|
|
RET_ERR(handle, ALPM_ERR_HANDLE_LOCK, -1);
|
2009-05-16 10:54:21 -04:00
|
|
|
}
|
2007-06-03 23:57:38 -04:00
|
|
|
}
|
|
|
|
|
2011-07-01 12:01:39 -04:00
|
|
|
CALLOC(trans, 1, sizeof(alpm_trans_t), RET_ERR(handle, ALPM_ERR_MEMORY, -1));
|
2009-07-15 13:14:01 -04:00
|
|
|
trans->flags = flags;
|
|
|
|
trans->state = STATE_INITIALIZED;
|
|
|
|
|
2011-06-07 17:06:16 -04:00
|
|
|
handle->trans = trans;
|
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return 0;
|
2007-06-03 23:57:38 -04:00
|
|
|
}
|
|
|
|
|
2011-06-28 00:04:00 -04:00
|
|
|
static alpm_list_t *check_arch(alpm_handle_t *handle, alpm_list_t *pkgs)
|
2009-07-15 13:14:01 -04:00
|
|
|
{
|
|
|
|
alpm_list_t *i;
|
|
|
|
alpm_list_t *invalid = NULL;
|
|
|
|
|
2011-08-19 20:12:21 -04:00
|
|
|
const char *arch = handle->arch;
|
2009-07-15 13:14:01 -04:00
|
|
|
if(!arch) {
|
2011-03-20 20:45:57 -04:00
|
|
|
return NULL;
|
2009-07-15 13:14:01 -04:00
|
|
|
}
|
|
|
|
for(i = pkgs; i; i = i->next) {
|
2011-06-28 09:26:39 -04:00
|
|
|
alpm_pkg_t *pkg = i->data;
|
2009-07-15 13:14:01 -04:00
|
|
|
const char *pkgarch = alpm_pkg_get_arch(pkg);
|
2010-12-10 20:43:59 -05:00
|
|
|
if(pkgarch && strcmp(pkgarch, arch) && strcmp(pkgarch, "any")) {
|
2009-07-15 13:14:01 -04:00
|
|
|
char *string;
|
2011-08-18 00:25:19 -04:00
|
|
|
const char *pkgname = pkg->name;
|
|
|
|
const char *pkgver = pkg->version;
|
2009-07-15 13:14:01 -04:00
|
|
|
size_t len = strlen(pkgname) + strlen(pkgver) + strlen(pkgarch) + 3;
|
2011-07-01 12:01:39 -04:00
|
|
|
MALLOC(string, len, RET_ERR(handle, ALPM_ERR_MEMORY, invalid));
|
2009-07-15 13:14:01 -04:00
|
|
|
sprintf(string, "%s-%s-%s", pkgname, pkgver, pkgarch);
|
|
|
|
invalid = alpm_list_add(invalid, string);
|
|
|
|
}
|
|
|
|
}
|
2011-03-20 20:45:57 -04:00
|
|
|
return invalid;
|
2007-06-03 23:57:38 -04:00
|
|
|
}
|
|
|
|
|
2011-03-06 11:50:01 -05:00
|
|
|
/** Prepare a transaction. */
|
2011-06-28 00:04:00 -04:00
|
|
|
int SYMEXPORT alpm_trans_prepare(alpm_handle_t *handle, alpm_list_t **data)
|
2007-06-03 23:57:38 -04:00
|
|
|
{
|
2011-06-28 09:27:16 -04:00
|
|
|
alpm_trans_t *trans;
|
2009-07-15 13:14:01 -04:00
|
|
|
|
2007-06-03 23:57:38 -04:00
|
|
|
/* Sanity checks */
|
2011-06-14 11:01:08 -04:00
|
|
|
CHECK_HANDLE(handle, return -1);
|
2011-07-01 12:01:39 -04:00
|
|
|
ASSERT(data != NULL, RET_ERR(handle, ALPM_ERR_WRONG_ARGS, -1));
|
2007-06-03 23:57:38 -04:00
|
|
|
|
2009-07-15 13:14:01 -04:00
|
|
|
trans = handle->trans;
|
2007-06-03 23:57:38 -04:00
|
|
|
|
2011-07-01 12:01:39 -04:00
|
|
|
ASSERT(trans != NULL, RET_ERR(handle, ALPM_ERR_TRANS_NULL, -1));
|
|
|
|
ASSERT(trans->state == STATE_INITIALIZED, RET_ERR(handle, ALPM_ERR_TRANS_NOT_INITIALIZED, -1));
|
2009-07-15 13:14:01 -04:00
|
|
|
|
|
|
|
/* If there's nothing to do, return without complaining */
|
|
|
|
if(trans->add == NULL && trans->remove == NULL) {
|
2011-03-20 20:45:57 -04:00
|
|
|
return 0;
|
2009-07-15 13:14:01 -04:00
|
|
|
}
|
|
|
|
|
2011-06-07 14:29:05 -04:00
|
|
|
alpm_list_t *invalid = check_arch(handle, trans->add);
|
2009-07-15 13:14:01 -04:00
|
|
|
if(invalid) {
|
|
|
|
if(data) {
|
|
|
|
*data = invalid;
|
|
|
|
}
|
2011-07-01 12:01:39 -04:00
|
|
|
RET_ERR(handle, ALPM_ERR_PKG_INVALID_ARCH, -1);
|
2009-07-15 13:14:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if(trans->add == NULL) {
|
2011-06-03 13:53:53 -04:00
|
|
|
if(_alpm_remove_prepare(handle, data) == -1) {
|
2009-07-15 13:14:01 -04:00
|
|
|
/* pm_errno is set by _alpm_remove_prepare() */
|
2011-03-20 20:45:57 -04:00
|
|
|
return -1;
|
2009-07-15 13:14:01 -04:00
|
|
|
}
|
|
|
|
} else {
|
2011-06-03 14:18:36 -04:00
|
|
|
if(_alpm_sync_prepare(handle, data) == -1) {
|
2009-07-15 13:14:01 -04:00
|
|
|
/* pm_errno is set by _alpm_sync_prepare() */
|
2011-03-20 20:45:57 -04:00
|
|
|
return -1;
|
2009-07-15 13:14:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-11 11:58:28 -05:00
|
|
|
|
|
|
|
if(!(trans->flags & ALPM_TRANS_FLAG_NODEPS)) {
|
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "sorting by dependencies\n");
|
|
|
|
if(trans->add) {
|
|
|
|
alpm_list_t *add_orig = trans->add;
|
|
|
|
trans->add = _alpm_sortbydeps(handle, add_orig, trans->remove, 0);
|
|
|
|
alpm_list_free(add_orig);
|
|
|
|
}
|
|
|
|
if(trans->remove) {
|
|
|
|
alpm_list_t *rem_orig = trans->remove;
|
|
|
|
trans->remove = _alpm_sortbydeps(handle, rem_orig, NULL, 1);
|
|
|
|
alpm_list_free(rem_orig);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-15 13:14:01 -04:00
|
|
|
trans->state = STATE_PREPARED;
|
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return 0;
|
2007-06-03 23:57:38 -04:00
|
|
|
}
|
|
|
|
|
2011-03-06 11:50:01 -05:00
|
|
|
/** Commit a transaction. */
|
2011-06-28 00:04:00 -04:00
|
|
|
int SYMEXPORT alpm_trans_commit(alpm_handle_t *handle, alpm_list_t **data)
|
2007-06-03 23:57:38 -04:00
|
|
|
{
|
2011-06-28 09:27:16 -04:00
|
|
|
alpm_trans_t *trans;
|
2015-12-13 22:39:58 -05:00
|
|
|
alpm_event_any_t event;
|
2009-07-15 13:14:01 -04:00
|
|
|
|
2007-06-03 23:57:38 -04:00
|
|
|
/* Sanity checks */
|
2011-06-14 11:01:08 -04:00
|
|
|
CHECK_HANDLE(handle, return -1);
|
2007-06-03 23:57:38 -04:00
|
|
|
|
2009-07-15 13:14:01 -04:00
|
|
|
trans = handle->trans;
|
|
|
|
|
2011-07-01 12:01:39 -04:00
|
|
|
ASSERT(trans != NULL, RET_ERR(handle, ALPM_ERR_TRANS_NULL, -1));
|
|
|
|
ASSERT(trans->state == STATE_PREPARED, RET_ERR(handle, ALPM_ERR_TRANS_NOT_PREPARED, -1));
|
2009-07-15 13:14:01 -04:00
|
|
|
|
2011-07-01 12:01:39 -04:00
|
|
|
ASSERT(!(trans->flags & ALPM_TRANS_FLAG_NOLOCK), RET_ERR(handle, ALPM_ERR_TRANS_NOT_LOCKED, -1));
|
2009-07-15 13:14:01 -04:00
|
|
|
|
|
|
|
/* If there's nothing to do, return without complaining */
|
|
|
|
if(trans->add == NULL && trans->remove == NULL) {
|
2011-03-20 20:45:57 -04:00
|
|
|
return 0;
|
2009-07-15 13:14:01 -04:00
|
|
|
}
|
|
|
|
|
2014-10-11 04:08:20 -04:00
|
|
|
if(trans->add) {
|
|
|
|
if(_alpm_sync_load(handle, data) != 0) {
|
|
|
|
/* pm_errno is set by _alpm_sync_load() */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(trans->flags & ALPM_TRANS_FLAG_DOWNLOADONLY) {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-09-14 18:37:40 -04:00
|
|
|
if(_alpm_sync_check(handle, data) != 0) {
|
|
|
|
/* pm_errno is set by _alpm_sync_check() */
|
|
|
|
return -1;
|
|
|
|
}
|
2014-10-11 04:08:20 -04:00
|
|
|
}
|
|
|
|
|
2015-10-16 20:28:25 -04:00
|
|
|
if(_alpm_hook_run(handle, ALPM_HOOK_PRE_TRANSACTION) != 0) {
|
|
|
|
RET_ERR(handle, ALPM_ERR_TRANS_HOOK_FAILED, -1);
|
|
|
|
}
|
|
|
|
|
2009-07-15 13:14:01 -04:00
|
|
|
trans->state = STATE_COMMITING;
|
|
|
|
|
2014-04-06 11:04:18 -04:00
|
|
|
alpm_logaction(handle, ALPM_CALLER_PREFIX, "transaction started\n");
|
2015-12-13 22:39:58 -05:00
|
|
|
event.type = ALPM_EVENT_TRANSACTION_START;
|
|
|
|
EVENT(handle, &event);
|
2014-04-06 11:04:18 -04:00
|
|
|
|
2009-07-15 13:14:01 -04:00
|
|
|
if(trans->add == NULL) {
|
2011-09-19 14:18:42 -04:00
|
|
|
if(_alpm_remove_packages(handle, 1) == -1) {
|
2012-08-12 08:07:10 -04:00
|
|
|
/* pm_errno is set by _alpm_remove_packages() */
|
2015-01-26 13:19:29 -05:00
|
|
|
alpm_errno_t save = handle->pm_errno;
|
2014-04-06 11:04:18 -04:00
|
|
|
alpm_logaction(handle, ALPM_CALLER_PREFIX, "transaction failed\n");
|
2015-01-26 13:19:29 -05:00
|
|
|
handle->pm_errno = save;
|
2011-03-20 20:45:57 -04:00
|
|
|
return -1;
|
2009-07-15 13:14:01 -04:00
|
|
|
}
|
|
|
|
} else {
|
2015-09-14 18:37:40 -04:00
|
|
|
if(_alpm_sync_commit(handle) == -1) {
|
2009-07-15 13:14:01 -04:00
|
|
|
/* pm_errno is set by _alpm_sync_commit() */
|
2015-01-26 13:19:29 -05:00
|
|
|
alpm_errno_t save = handle->pm_errno;
|
2014-04-06 11:04:18 -04:00
|
|
|
alpm_logaction(handle, ALPM_CALLER_PREFIX, "transaction failed\n");
|
2015-01-26 13:19:29 -05:00
|
|
|
handle->pm_errno = save;
|
2011-03-20 20:45:57 -04:00
|
|
|
return -1;
|
2009-07-15 13:14:01 -04:00
|
|
|
}
|
|
|
|
}
|
2007-06-03 23:57:38 -04:00
|
|
|
|
2014-04-06 11:04:18 -04:00
|
|
|
if(trans->state == STATE_INTERRUPTED) {
|
|
|
|
alpm_logaction(handle, ALPM_CALLER_PREFIX, "transaction interrupted\n");
|
|
|
|
} else {
|
2015-12-13 22:39:58 -05:00
|
|
|
event.type = ALPM_EVENT_TRANSACTION_DONE;
|
|
|
|
EVENT(handle, &event);
|
2014-04-06 11:04:18 -04:00
|
|
|
alpm_logaction(handle, ALPM_CALLER_PREFIX, "transaction completed\n");
|
2015-10-16 20:28:25 -04:00
|
|
|
_alpm_hook_run(handle, ALPM_HOOK_POST_TRANSACTION);
|
2014-04-06 11:04:18 -04:00
|
|
|
}
|
|
|
|
|
2009-07-15 13:14:01 -04:00
|
|
|
trans->state = STATE_COMMITED;
|
2009-05-16 10:54:21 -04:00
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return 0;
|
2007-06-03 23:57:38 -04:00
|
|
|
}
|
|
|
|
|
2011-03-06 11:50:01 -05:00
|
|
|
/** Interrupt a transaction. */
|
2011-06-28 00:04:00 -04:00
|
|
|
int SYMEXPORT alpm_trans_interrupt(alpm_handle_t *handle)
|
2007-09-07 18:36:38 -04:00
|
|
|
{
|
2011-06-28 09:27:16 -04:00
|
|
|
alpm_trans_t *trans;
|
2007-09-07 18:36:38 -04:00
|
|
|
|
|
|
|
/* Sanity checks */
|
2011-06-14 11:01:08 -04:00
|
|
|
CHECK_HANDLE(handle, return -1);
|
2007-09-07 18:36:38 -04:00
|
|
|
|
|
|
|
trans = handle->trans;
|
2011-07-01 12:01:39 -04:00
|
|
|
ASSERT(trans != NULL, RET_ERR(handle, ALPM_ERR_TRANS_NULL, -1));
|
2007-09-07 18:36:38 -04:00
|
|
|
ASSERT(trans->state == STATE_COMMITING || trans->state == STATE_INTERRUPTED,
|
2011-07-01 12:01:39 -04:00
|
|
|
RET_ERR(handle, ALPM_ERR_TRANS_TYPE, -1));
|
2007-09-07 18:36:38 -04:00
|
|
|
|
|
|
|
trans->state = STATE_INTERRUPTED;
|
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return 0;
|
2007-09-07 18:36:38 -04:00
|
|
|
}
|
|
|
|
|
2011-03-06 11:50:01 -05:00
|
|
|
/** Release a transaction. */
|
2011-06-28 00:04:00 -04:00
|
|
|
int SYMEXPORT alpm_trans_release(alpm_handle_t *handle)
|
2007-06-03 23:57:38 -04:00
|
|
|
{
|
2011-06-28 09:27:16 -04:00
|
|
|
alpm_trans_t *trans;
|
2007-06-03 23:57:38 -04:00
|
|
|
|
|
|
|
/* Sanity checks */
|
2011-06-14 11:01:08 -04:00
|
|
|
CHECK_HANDLE(handle, return -1);
|
2007-06-03 23:57:38 -04:00
|
|
|
|
|
|
|
trans = handle->trans;
|
2011-07-01 12:01:39 -04:00
|
|
|
ASSERT(trans != NULL, RET_ERR(handle, ALPM_ERR_TRANS_NULL, -1));
|
|
|
|
ASSERT(trans->state != STATE_IDLE, RET_ERR(handle, ALPM_ERR_TRANS_NULL, -1));
|
2007-06-03 23:57:38 -04:00
|
|
|
|
2011-07-01 12:01:39 -04:00
|
|
|
int nolock_flag = trans->flags & ALPM_TRANS_FLAG_NOLOCK;
|
2009-05-16 10:54:21 -04:00
|
|
|
|
2007-06-03 23:57:38 -04:00
|
|
|
_alpm_trans_free(trans);
|
|
|
|
handle->trans = NULL;
|
|
|
|
|
|
|
|
/* unlock db */
|
2009-05-16 10:54:21 -04:00
|
|
|
if(!nolock_flag) {
|
2014-08-01 17:19:50 -04:00
|
|
|
_alpm_handle_unlock(handle);
|
2007-06-03 23:57:38 -04:00
|
|
|
}
|
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return 0;
|
2007-06-03 23:57:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @} */
|
|
|
|
|
2011-06-28 09:27:16 -04:00
|
|
|
void _alpm_trans_free(alpm_trans_t *trans)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
|
|
|
if(trans == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-28 02:53:43 -04:00
|
|
|
alpm_list_free_inner(trans->unresolvable,
|
|
|
|
(alpm_list_fn_free)_alpm_pkg_free_trans);
|
|
|
|
alpm_list_free(trans->unresolvable);
|
2009-07-15 13:14:01 -04:00
|
|
|
alpm_list_free_inner(trans->add, (alpm_list_fn_free)_alpm_pkg_free_trans);
|
|
|
|
alpm_list_free(trans->add);
|
|
|
|
alpm_list_free_inner(trans->remove, (alpm_list_fn_free)_alpm_pkg_free);
|
|
|
|
alpm_list_free(trans->remove);
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2007-02-19 21:14:27 -05:00
|
|
|
FREELIST(trans->skip_remove);
|
2006-01-10 15:28:27 -05:00
|
|
|
|
2006-10-31 01:39:59 -05:00
|
|
|
FREE(trans);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
2007-04-29 12:47:02 -04:00
|
|
|
/* A cheap grep for text files, returns 1 if a substring
|
|
|
|
* was found in the text file fn, 0 if it wasn't
|
|
|
|
*/
|
|
|
|
static int grep(const char *fn, const char *needle)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
if((fp = fopen(fn, "r")) == NULL) {
|
2011-03-20 20:45:57 -04:00
|
|
|
return 0;
|
2007-04-29 12:47:02 -04:00
|
|
|
}
|
|
|
|
while(!feof(fp)) {
|
|
|
|
char line[1024];
|
2014-08-06 16:36:00 -04:00
|
|
|
if(safe_fgets(line, sizeof(line), fp) == NULL) {
|
2007-04-29 12:47:02 -04:00
|
|
|
continue;
|
|
|
|
}
|
2010-07-07 18:12:42 -04:00
|
|
|
/* TODO: this will not work if the search string
|
|
|
|
* ends up being split across line reads */
|
2007-04-29 12:47:02 -04:00
|
|
|
if(strstr(line, needle)) {
|
|
|
|
fclose(fp);
|
2011-03-20 20:45:57 -04:00
|
|
|
return 1;
|
2007-04-29 12:47:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fp);
|
2011-03-20 20:45:57 -04:00
|
|
|
return 0;
|
2007-04-29 12:47:02 -04:00
|
|
|
}
|
|
|
|
|
2011-09-28 04:45:46 -04:00
|
|
|
int _alpm_runscriptlet(alpm_handle_t *handle, const char *filepath,
|
|
|
|
const char *script, const char *ver, const char *oldver, int is_archive)
|
2007-04-29 12:47:02 -04:00
|
|
|
{
|
2012-04-07 14:01:13 -04:00
|
|
|
char arg0[64], arg1[3], cmdline[PATH_MAX];
|
|
|
|
char *argv[] = { arg0, arg1, cmdline, NULL };
|
2011-09-28 04:45:46 -04:00
|
|
|
char *tmpdir, *scriptfn = NULL, *scriptpath;
|
2007-04-29 12:47:02 -04:00
|
|
|
int retval = 0;
|
2011-09-28 04:45:46 -04:00
|
|
|
size_t len;
|
2007-04-29 12:47:02 -04:00
|
|
|
|
2011-09-28 04:45:46 -04:00
|
|
|
if(_alpm_access(handle, NULL, filepath, R_OK) != 0) {
|
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "scriptlet '%s' not found\n", filepath);
|
2011-03-20 20:45:57 -04:00
|
|
|
return 0;
|
2007-04-29 12:47:02 -04:00
|
|
|
}
|
|
|
|
|
2011-09-28 04:45:46 -04:00
|
|
|
if(!is_archive && !grep(filepath, script)) {
|
|
|
|
/* script not found in scriptlet file; we can only short-circuit this early
|
2013-11-08 00:44:40 -05:00
|
|
|
* if it is an actual scriptlet file and not an archive. */
|
2011-09-28 04:45:46 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-07 14:01:13 -04:00
|
|
|
strcpy(arg0, SCRIPTLET_SHELL);
|
|
|
|
strcpy(arg1, "-c");
|
|
|
|
|
2011-09-28 04:45:46 -04:00
|
|
|
/* create a directory in $root/tmp/ for copying/extracting the scriptlet */
|
|
|
|
len = strlen(handle->root) + strlen("tmp/alpm_XXXXXX") + 1;
|
|
|
|
MALLOC(tmpdir, len, RET_ERR(handle, ALPM_ERR_MEMORY, -1));
|
|
|
|
snprintf(tmpdir, len, "%stmp/", handle->root);
|
2008-06-15 20:15:36 -04:00
|
|
|
if(access(tmpdir, F_OK) != 0) {
|
2008-01-13 09:06:25 -05:00
|
|
|
_alpm_makepath_mode(tmpdir, 01777);
|
2007-10-05 12:09:49 -04:00
|
|
|
}
|
2011-09-28 04:45:46 -04:00
|
|
|
snprintf(tmpdir, len, "%stmp/alpm_XXXXXX", handle->root);
|
2007-10-05 12:09:49 -04:00
|
|
|
if(mkdtemp(tmpdir) == NULL) {
|
2011-07-01 12:01:38 -04:00
|
|
|
_alpm_log(handle, ALPM_LOG_ERROR, _("could not create temp directory\n"));
|
2011-09-28 04:45:46 -04:00
|
|
|
free(tmpdir);
|
2011-03-20 20:45:57 -04:00
|
|
|
return 1;
|
2007-10-05 12:09:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* either extract or copy the scriptlet */
|
2011-09-28 04:45:46 -04:00
|
|
|
len += strlen("/.INSTALL");
|
2014-12-23 19:52:15 -05:00
|
|
|
MALLOC(scriptfn, len, free(tmpdir); RET_ERR(handle, ALPM_ERR_MEMORY, -1));
|
2011-09-28 04:45:46 -04:00
|
|
|
snprintf(scriptfn, len, "%s/.INSTALL", tmpdir);
|
|
|
|
if(is_archive) {
|
|
|
|
if(_alpm_unpack_single(handle, filepath, tmpdir, ".INSTALL")) {
|
2007-11-18 17:49:02 -05:00
|
|
|
retval = 1;
|
|
|
|
}
|
2007-04-29 12:47:02 -04:00
|
|
|
} else {
|
2011-09-28 04:45:46 -04:00
|
|
|
if(_alpm_copyfile(filepath, scriptfn)) {
|
2011-07-01 12:01:38 -04:00
|
|
|
_alpm_log(handle, ALPM_LOG_ERROR, _("could not copy tempfile to %s (%s)\n"), scriptfn, strerror(errno));
|
2007-10-05 12:09:49 -04:00
|
|
|
retval = 1;
|
|
|
|
}
|
2007-04-29 12:47:02 -04:00
|
|
|
}
|
2007-11-18 17:49:02 -05:00
|
|
|
if(retval == 1) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2011-09-28 04:45:46 -04:00
|
|
|
if(is_archive && !grep(scriptfn, script)) {
|
|
|
|
/* script not found in extracted scriptlet file */
|
2007-04-29 12:47:02 -04:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2011-09-19 15:28:18 -04:00
|
|
|
/* chop off the root so we can find the tmpdir in the chroot */
|
|
|
|
scriptpath = scriptfn + strlen(handle->root) - 1;
|
|
|
|
|
2007-04-29 12:47:02 -04:00
|
|
|
if(oldver) {
|
2007-11-29 14:58:00 -05:00
|
|
|
snprintf(cmdline, PATH_MAX, ". %s; %s %s %s",
|
2007-04-29 12:47:02 -04:00
|
|
|
scriptpath, script, ver, oldver);
|
|
|
|
} else {
|
2007-11-29 14:58:00 -05:00
|
|
|
snprintf(cmdline, PATH_MAX, ". %s; %s %s",
|
2007-04-29 12:47:02 -04:00
|
|
|
scriptpath, script, ver);
|
|
|
|
}
|
|
|
|
|
2011-07-01 12:01:38 -04:00
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "executing \"%s\"\n", cmdline);
|
2010-08-18 08:07:21 -04:00
|
|
|
|
2015-11-11 19:20:00 -05:00
|
|
|
retval = _alpm_run_chroot(handle, SCRIPTLET_SHELL, argv, NULL, NULL);
|
2007-04-29 12:47:02 -04:00
|
|
|
|
|
|
|
cleanup:
|
2011-09-28 04:45:46 -04:00
|
|
|
if(scriptfn && unlink(scriptfn)) {
|
|
|
|
_alpm_log(handle, ALPM_LOG_WARNING,
|
|
|
|
_("could not remove %s\n"), scriptfn);
|
|
|
|
}
|
|
|
|
if(rmdir(tmpdir)) {
|
|
|
|
_alpm_log(handle, ALPM_LOG_WARNING,
|
|
|
|
_("could not remove tmpdir %s\n"), tmpdir);
|
2007-04-29 12:47:02 -04:00
|
|
|
}
|
|
|
|
|
2011-09-28 04:45:46 -04:00
|
|
|
free(scriptfn);
|
|
|
|
free(tmpdir);
|
2011-03-20 20:45:57 -04:00
|
|
|
return retval;
|
2007-04-29 12:47:02 -04:00
|
|
|
}
|
|
|
|
|
2011-06-28 00:42:24 -04:00
|
|
|
alpm_transflag_t SYMEXPORT alpm_trans_get_flags(alpm_handle_t *handle)
|
2006-11-22 04:03:41 -05:00
|
|
|
{
|
|
|
|
/* Sanity checks */
|
2011-06-14 11:01:08 -04:00
|
|
|
CHECK_HANDLE(handle, return -1);
|
2011-07-01 12:01:39 -04:00
|
|
|
ASSERT(handle->trans != NULL, RET_ERR(handle, ALPM_ERR_TRANS_NULL, -1));
|
2006-11-22 04:03:41 -05:00
|
|
|
|
2009-07-15 13:14:01 -04:00
|
|
|
return handle->trans->flags;
|
2006-11-22 04:03:41 -05:00
|
|
|
}
|
|
|
|
|
2011-06-28 00:04:00 -04:00
|
|
|
alpm_list_t SYMEXPORT *alpm_trans_get_add(alpm_handle_t *handle)
|
2006-11-22 04:03:41 -05:00
|
|
|
{
|
|
|
|
/* Sanity checks */
|
2011-06-14 11:01:08 -04:00
|
|
|
CHECK_HANDLE(handle, return NULL);
|
2011-07-01 12:01:39 -04:00
|
|
|
ASSERT(handle->trans != NULL, RET_ERR(handle, ALPM_ERR_TRANS_NULL, NULL));
|
2006-11-22 04:03:41 -05:00
|
|
|
|
2009-07-15 13:14:01 -04:00
|
|
|
return handle->trans->add;
|
2006-11-22 04:03:41 -05:00
|
|
|
}
|
|
|
|
|
2011-06-28 00:04:00 -04:00
|
|
|
alpm_list_t SYMEXPORT *alpm_trans_get_remove(alpm_handle_t *handle)
|
2006-11-22 04:03:41 -05:00
|
|
|
{
|
|
|
|
/* Sanity checks */
|
2011-06-14 11:01:08 -04:00
|
|
|
CHECK_HANDLE(handle, return NULL);
|
2011-07-01 12:01:39 -04:00
|
|
|
ASSERT(handle->trans != NULL, RET_ERR(handle, ALPM_ERR_TRANS_NULL, NULL));
|
2006-11-22 04:03:41 -05:00
|
|
|
|
2009-07-15 13:14:01 -04:00
|
|
|
return handle->trans->remove;
|
2006-11-22 04:03:41 -05:00
|
|
|
}
|
2014-01-22 18:06:11 -05:00
|
|
|
/* vim: set noet: */
|