[arch-general] [PATCH 1/4] Add skeleton for hooks support
This begins work on adding some sort of hook infrastructure to pacman. There has been much discussion and little code that I have seen, so I figured I would go ahead an implement a very basic skeleton for transaction hooks support.
--- lib/libalpm/Makefile.am | 1 + lib/libalpm/{sync.h => hooks.c} | 34 +++++++++++++++++++++++++--------- lib/libalpm/{add.h => hooks.h} | 19 ++++++++----------- 3 files changed, 34 insertions(+), 20 deletions(-)
diff --git a/lib/libalpm/Makefile.am b/lib/libalpm/Makefile.am index 3473a73..b4fdadf 100644 --- a/lib/libalpm/Makefile.am +++ b/lib/libalpm/Makefile.am @@ -44,6 +44,7 @@ libalpm_la_SOURCES = \ sync.h sync.c \ trans.h trans.c \ util.h util.c \ + hooks.h hooks.c \ version.c
libalpm_la_LDFLAGS = -no-undefined -version-info $(LIB_VERSION_INFO) diff --git a/lib/libalpm/sync.h b/lib/libalpm/hooks.c similarity index 65% copy from lib/libalpm/sync.h copy to lib/libalpm/hooks.c index 000a09c..9630c70 100644 --- a/lib/libalpm/sync.h +++ b/lib/libalpm/hooks.c @@ -1,9 +1,10 @@ /* - * sync.h + * hooks.c * * Copyright (c) 2006-2010 Pacman Development Team pacman-dev@archlinux.org * Copyright (c) 2002-2006 by Judd Vinet jvinet@zeroflux.org * 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 * * This program is free software; you can redistribute it and/or modify @@ -19,14 +20,29 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. */ -#ifndef _ALPM_SYNC_H -#define _ALPM_SYNC_H
-#include "alpm.h" - -int _alpm_sync_prepare(pmtrans_t *trans, pmdb_t *db_local, alpm_list_t *dbs_sync, alpm_list_t **data); -int _alpm_sync_commit(pmtrans_t *trans, pmdb_t *db_local, alpm_list_t **data); +#include "config.h"
-#endif /* _ALPM_SYNC_H */ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/statvfs.h> +#include <errno.h>
-/* vim: set ts=2 sw=2 noet: */ +/* libalpm */ +#include "hooks.h" +#include "trans.h" +#include "alpm_list.h" +#include "package.h" +#include "util.h" +#include "log.h" +#include "handle.h" +#include "add.h" +#include "remove.h" +#include "sync.h" +#include "alpm.h" +#include "deps.h" +#include "cache.h" diff --git a/lib/libalpm/add.h b/lib/libalpm/hooks.h similarity index 73% copy from lib/libalpm/add.h copy to lib/libalpm/hooks.h index eb37dc7..d60b1f4 100644 --- a/lib/libalpm/add.h +++ b/lib/libalpm/hooks.h @@ -1,8 +1,11 @@ /* - * add.h + * hooks.h * * Copyright (c) 2006-2010 Pacman Development Team pacman-dev@archlinux.org * Copyright (c) 2002-2006 by Judd Vinet jvinet@zeroflux.org + * Copyright (c) 2005 by Aurelien Foret orelien@chez.com + * Copyright (c) 2005 by Christian Hamar krics@linuxforum.hu + * Copyright (c) 2006 by Miklos Vajna vmiklos@frugalware.org * * 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 @@ -17,15 +20,9 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. */ -#ifndef _ALPM_ADD_H -#define _ALPM_ADD_H +#ifndef _ALPM_HOOKS_H +#define _ALPM_HOOKS_H
-#include "db.h" -#include "alpm_list.h" -#include "trans.h" +#include "alpm.h"
-int _alpm_upgrade_packages(pmtrans_t *trans, pmdb_t *db); - -#endif /* _ALPM_ADD_H */ - -/* vim: set ts=2 sw=2 noet: */ +#endif /*ALPM_HOOKS_H */
If a transaction is interrupted, the hooks need to know so that they can roll back anything that needs to be rolled back. --- lib/libalpm/trans.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
diff --git a/lib/libalpm/trans.c b/lib/libalpm/trans.c index ed26aa6..d212f50 100644 --- a/lib/libalpm/trans.c +++ b/lib/libalpm/trans.c @@ -235,6 +235,7 @@ int SYMEXPORT alpm_trans_interrupt() RET_ERR(PM_ERR_TRANS_TYPE, -1));
trans->state = STATE_INTERRUPTED; + alpm_hook_transact("INTERRUPTED");
return(0); }
We don't need the whole usual pkgbuild, but having one in the git tree sure makes testing simpler.
--- PKGBUILD | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 60 insertions(+), 0 deletions(-)
diff --git a/PKGBUILD b/PKGBUILD new file mode 100644 index 0000000..16cf977 --- /dev/null +++ b/PKGBUILD @@ -0,0 +1,60 @@ +# $Id: PKGBUILD 83376 2010-06-21 12:52:09Z dan $ +# Maintainer: Aaron Griffin aaron@archlinux.org +# Maintainer: Dan McGee dan@archlinux.org + +pkgname=pacman-git +pkgver=$(date +%s) +pkgrel=$(git log --pretty=format:%h |head -n 1) +pkgdesc="A library-based package manager with dependency support" +arch=('i686' 'x86_64') +url="http://www.archlinux.org/pacman/" +license=('GPL') +groups=('base') +depends=('bash' 'libarchive>=2.7.1' 'libfetch>=2.25' 'pacman-mirrorlist') +makedepends=('asciidoc') +optdepends=('fakeroot: for makepkg usage as normal user') +provides=('pacman=9999') +backup=('etc/pacman.conf' 'etc/makepkg.conf') +conflicts=('pacman') +options=(!libtool) + +build() { + cd .. + ./autogen.sh + ./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var --enable-doc + make || return 1 +} + +package() { + cd $srcdir/.. + srcdir="$PWD" + make DESTDIR=$pkgdir install || return 1 + + # install Arch specific stuff + mkdir -p $pkgdir/etc + # set things correctly in the default conf file + case "$CARCH" in + i686) + mycarch="i686" + mychost="i686-pc-linux-gnu" + myflags="-march=i686 " + ;; + x86_64) + mycarch="x86_64" + mychost="x86_64-unknown-linux-gnu" + myflags="-march=x86-64 " + ;; + esac + sed -i $pkgdir/etc/makepkg.conf \ + -e "s|@CARCH[@]|$mycarch|g" \ + -e "s|@CHOST[@]|$mychost|g" \ + -e "s|@CARCHFLAGS[@]|$myflags|g" + + # install completion files + mkdir -p $pkgdir/etc/bash_completion.d/ + install -m644 contrib/bash_completion $pkgdir/etc/bash_completion.d/pacman + mkdir -p $pkgdir/usr/share/zsh/site-functions/ + install -m644 contrib/zsh_completion $pkgdir/usr/share/zsh/site-functions/_pacman +} + +# vim: set ts=2 sw=2 et:
libalpm compliant programs now support running pre-commit and post-commit hooks. Currently, the implementation is as simple as I can make it -- if /etc/pacman.d/hooks.d/transact.sh exists, it will be run with the 'pre-commit' parameter just before a transaction is committed, and it will be run with 'post-commit' after the transaction has been completed.
The intent is for transact.sh to be a wrapper that calls scripts in /etc/pacman.d/hooks.d/transact.d with the arg it is passed, but since this is just for a demo I did not actually include any of that.
I also didn't bother adhering to any of the coding standards, as that will be easy enough to fix up once we have something a bit more fleshed out. --- lib/libalpm/hooks.c | 28 ++++++++++++++++++++++++++++ lib/libalpm/hooks.h | 4 ++++ lib/libalpm/trans.c | 4 +++- 3 files changed, 35 insertions(+), 1 deletions(-)
diff --git a/lib/libalpm/hooks.c b/lib/libalpm/hooks.c index 9630c70..5982ea5 100644 --- a/lib/libalpm/hooks.c +++ b/lib/libalpm/hooks.c @@ -30,6 +30,7 @@ #include <sys/types.h> #include <sys/stat.h> #include <sys/statvfs.h> +#include <sys/wait.h> #include <errno.h>
/* libalpm */ @@ -46,3 +47,30 @@ #include "alpm.h" #include "deps.h" #include "cache.h" + +int alpm_hook_transact(char* phase) { + char filename[PATH_MAX]; + struct stat sbuf; + pid_t childpid, retpid; + int status; + /* ignore transaction hooks when targeting another root filesystem + if (!strcmp(handle->root,"/")) + return 1;*/ + snprintf(filename,PATH_MAX,"%s/transact.sh",ALPM_HOOKS_DIR); + /* if we cannot stat transact.sh, just return */ + if (stat(filename,&sbuf) != 0) + return 1; + fflush(NULL); + childpid=fork(); + if (childpid == -1) { + return 1; + } else if (childpid == 0) { + execl(filename,"transact.sh",phase,(char *)0); + } else { + /* return the exit status of the transact.sh program. */ + do { + retpid = waitpid(childpid,&status,0); + } while (!WIFEXITED(status) && !WIFSIGNALED(status)); + } + return !WEXITSTATUS(status); +} diff --git a/lib/libalpm/hooks.h b/lib/libalpm/hooks.h index d60b1f4..f79d2c4 100644 --- a/lib/libalpm/hooks.h +++ b/lib/libalpm/hooks.h @@ -25,4 +25,8 @@
#include "alpm.h"
+#define ALPM_HOOKS_DIR "/etc/pacman.d/hooks.d" + +int alpm_hook_transact(char*); + #endif /*ALPM_HOOKS_H */ diff --git a/lib/libalpm/trans.c b/lib/libalpm/trans.c index 523f33b..ed26aa6 100644 --- a/lib/libalpm/trans.c +++ b/lib/libalpm/trans.c @@ -195,7 +195,8 @@ int SYMEXPORT alpm_trans_commit(alpm_list_t **data) if(trans->add == NULL && trans->remove == NULL) { return(0); } - + if (! alpm_hook_transact("pre-commit")) + return (-1); trans->state = STATE_COMMITING;
if(trans->add == NULL) { @@ -211,6 +212,7 @@ int SYMEXPORT alpm_trans_commit(alpm_list_t **data) }
trans->state = STATE_COMMITED; + alpm_hook_transact("post-commit");
return(0); }
Am 23.07.2010 23:07, schrieb Victor Lowther:
This begins work on adding some sort of hook infrastructure to pacman. There has been much discussion and little code that I have seen, so I figured I would go ahead an implement a very basic skeleton for transaction hooks support.
You probably want to post that to pacman-dev instead. It might not get a proper audience, and thus lack of review, on arch-general.
On Fri, Jul 23, 2010 at 4:26 PM, Thomas Bächler thomas@archlinux.org wrote:
Am 23.07.2010 23:07, schrieb Victor Lowther:
This begins work on adding some sort of hook infrastructure to pacman. There has been much discussion and little code that I have seen, so I figured I would go ahead an implement a very basic skeleton for transaction hooks support.
You probably want to post that to pacman-dev instead. It might not get a proper audience, and thus lack of review, on arch-general.
Is it closed the way arch-dev-public is?
Am 23.07.2010 23:30, schrieb Victor Lowther:
On Fri, Jul 23, 2010 at 4:26 PM, Thomas Bächler thomas@archlinux.org wrote:
Am 23.07.2010 23:07, schrieb Victor Lowther:
This begins work on adding some sort of hook infrastructure to pacman. There has been much discussion and little code that I have seen, so I figured I would go ahead an implement a very basic skeleton for transaction hooks support.
You probably want to post that to pacman-dev instead. It might not get a proper audience, and thus lack of review, on arch-general.
Is it closed the way arch-dev-public is?
No, you can join just like arch-general.
participants (2)
-
Thomas Bächler
-
Victor Lowther