[pacman-dev] [PATCH 3/4] wip add hooks
Andrew Gregory
andrew.gregory.8 at gmail.com
Sat Jul 4 10:10:24 UTC 2015
---
lib/libalpm/Makefile.am | 2 +
lib/libalpm/alpm.c | 1 +
lib/libalpm/alpm.h | 10 ++
lib/libalpm/handle.c | 59 ++++++++
lib/libalpm/handle.h | 1 +
lib/libalpm/hook.c | 354 ++++++++++++++++++++++++++++++++++++++++++++++++
lib/libalpm/hook.h | 34 +++++
lib/libalpm/trans.c | 6 +
8 files changed, 467 insertions(+)
create mode 100644 lib/libalpm/hook.c
create mode 100644 lib/libalpm/hook.h
diff --git a/lib/libalpm/Makefile.am b/lib/libalpm/Makefile.am
index f66daed..77e68a4 100644
--- a/lib/libalpm/Makefile.am
+++ b/lib/libalpm/Makefile.am
@@ -42,6 +42,8 @@ libalpm_la_SOURCES = \
graph.h graph.c \
group.h group.c \
handle.h handle.c \
+ hook.h hook.c \
+ ini.h ini.c \
libarchive-compat.h \
log.h log.c \
package.h package.c \
diff --git a/lib/libalpm/alpm.c b/lib/libalpm/alpm.c
index 2110286..f2713d4 100644
--- a/lib/libalpm/alpm.c
+++ b/lib/libalpm/alpm.c
@@ -63,6 +63,7 @@ alpm_handle_t SYMEXPORT *alpm_initialize(const char *root, const char *dbpath,
if((myerr = _alpm_set_directory_option(dbpath, &(myhandle->dbpath), 1))) {
goto cleanup;
}
+ myhandle->hookdirs = alpm_list_add(NULL, strdup("/usr/share/alpm/hooks/"));
lockfilelen = strlen(myhandle->dbpath) + strlen(lf) + 1;
myhandle->lockfile = calloc(lockfilelen, sizeof(char));
diff --git a/lib/libalpm/alpm.h b/lib/libalpm/alpm.h
index 06e080b..446005a 100644
--- a/lib/libalpm/alpm.h
+++ b/lib/libalpm/alpm.h
@@ -87,6 +87,7 @@ typedef enum _alpm_errno_t {
ALPM_ERR_TRANS_ABORT,
ALPM_ERR_TRANS_TYPE,
ALPM_ERR_TRANS_NOT_LOCKED,
+ ALPM_ERR_TRANS_HOOK_FAILED,
/* Packages */
ALPM_ERR_PKG_NOT_FOUND,
ALPM_ERR_PKG_IGNORED,
@@ -775,6 +776,15 @@ int alpm_option_add_cachedir(alpm_handle_t *handle, const char *cachedir);
int alpm_option_remove_cachedir(alpm_handle_t *handle, const char *cachedir);
/** @} */
+/** @name Accessors to the list of package hook directories.
+ * @{
+ */
+alpm_list_t *alpm_option_get_hookdirs(alpm_handle_t *handle);
+int alpm_option_set_hookdirs(alpm_handle_t *handle, alpm_list_t *hookdirs);
+int alpm_option_add_hookdir(alpm_handle_t *handle, const char *hookdir);
+int alpm_option_remove_hookdir(alpm_handle_t *handle, const char *hookdir);
+/** @} */
+
/** Returns the logfile name. */
const char *alpm_option_get_logfile(alpm_handle_t *handle);
/** Sets the logfile name. */
diff --git a/lib/libalpm/handle.c b/lib/libalpm/handle.c
index 4915d0b..5a6c298 100644
--- a/lib/libalpm/handle.c
+++ b/lib/libalpm/handle.c
@@ -82,6 +82,7 @@ void _alpm_handle_free(alpm_handle_t *handle)
FREE(handle->root);
FREE(handle->dbpath);
FREELIST(handle->cachedirs);
+ FREELIST(handle->hookdirs);
FREE(handle->logfile);
FREE(handle->lockfile);
FREE(handle->arch);
@@ -206,6 +207,12 @@ const char SYMEXPORT *alpm_option_get_dbpath(alpm_handle_t *handle)
return handle->dbpath;
}
+alpm_list_t SYMEXPORT *alpm_option_get_hookdirs(alpm_handle_t *handle)
+{
+ CHECK_HANDLE(handle, return NULL);
+ return handle->hookdirs;
+}
+
alpm_list_t SYMEXPORT *alpm_option_get_cachedirs(alpm_handle_t *handle)
{
CHECK_HANDLE(handle, return NULL);
@@ -380,6 +387,58 @@ alpm_errno_t _alpm_set_directory_option(const char *value,
return 0;
}
+int SYMEXPORT alpm_option_add_hookdir(alpm_handle_t *handle, const char *hookdir)
+{
+ char *newhookdir;
+
+ CHECK_HANDLE(handle, return -1);
+ ASSERT(hookdir != NULL, RET_ERR(handle, ALPM_ERR_WRONG_ARGS, -1));
+
+ newhookdir = canonicalize_path(hookdir);
+ if(!newhookdir) {
+ RET_ERR(handle, ALPM_ERR_MEMORY, -1);
+ }
+ handle->hookdirs = alpm_list_add(handle->hookdirs, newhookdir);
+ _alpm_log(handle, ALPM_LOG_DEBUG, "option 'hookdir' = %s\n", newhookdir);
+ return 0;
+}
+
+int SYMEXPORT alpm_option_set_hookdirs(alpm_handle_t *handle, alpm_list_t *hookdirs)
+{
+ alpm_list_t *i;
+ CHECK_HANDLE(handle, return -1);
+ if(handle->hookdirs) {
+ FREELIST(handle->hookdirs);
+ }
+ for(i = hookdirs; i; i = i->next) {
+ int ret = alpm_option_add_hookdir(handle, i->data);
+ if(ret) {
+ return ret;
+ }
+ }
+ return 0;
+}
+
+int SYMEXPORT alpm_option_remove_hookdir(alpm_handle_t *handle, const char *hookdir)
+{
+ char *vdata = NULL;
+ char *newhookdir;
+ CHECK_HANDLE(handle, return -1);
+ ASSERT(hookdir != NULL, RET_ERR(handle, ALPM_ERR_WRONG_ARGS, -1));
+
+ newhookdir = canonicalize_path(hookdir);
+ if(!newhookdir) {
+ RET_ERR(handle, ALPM_ERR_MEMORY, -1);
+ }
+ handle->hookdirs = alpm_list_remove_str(handle->hookdirs, newhookdir, &vdata);
+ FREE(newhookdir);
+ if(vdata != NULL) {
+ FREE(vdata);
+ return 1;
+ }
+ return 0;
+}
+
int SYMEXPORT alpm_option_add_cachedir(alpm_handle_t *handle, const char *cachedir)
{
char *newcachedir;
diff --git a/lib/libalpm/handle.h b/lib/libalpm/handle.h
index 5893139..171473f 100644
--- a/lib/libalpm/handle.h
+++ b/lib/libalpm/handle.h
@@ -82,6 +82,7 @@ struct __alpm_handle_t {
char *lockfile; /* Name of the lock file */
char *gpgdir; /* Directory where GnuPG files are stored */
alpm_list_t *cachedirs; /* Paths to pacman cache directories */
+ alpm_list_t *hookdirs; /* Paths to hook directories */
/* package lists */
alpm_list_t *noupgrade; /* List of packages NOT to be upgraded */
diff --git a/lib/libalpm/hook.c b/lib/libalpm/hook.c
new file mode 100644
index 0000000..49a76b4
--- /dev/null
+++ b/lib/libalpm/hook.c
@@ -0,0 +1,354 @@
+/*
+ * hook.c
+ *
+ * Copyright (c) 2015 Pacman Development Team <pacman-dev at archlinux.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
+ * 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
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <string.h>
+#include <errno.h>
+#include <dirent.h>
+#include <fcntl.h>
+
+#include "alpm.h"
+#include "handle.h"
+#include "hook.h"
+#include "ini.h"
+#include "util.h"
+#include "trans.h"
+#include "log.h"
+
+enum alpm_hook_op_t {
+ ALPM_HOOK_OP_SYNC,
+ ALPM_HOOK_OP_REMOVE,
+};
+
+enum alpm_hook_type_t {
+ ALPM_HOOK_TYPE_PACKAGE,
+ ALPM_HOOK_TYPE_FILE,
+};
+
+struct alpm_trigger_t {
+ enum alpm_hook_op_t op;
+ enum alpm_hook_type_t type;
+ alpm_list_t *targets;
+};
+
+struct alpm_hook_t {
+ char *name;
+ alpm_list_t *triggers;
+ alpm_list_t *depends;
+ char *cmd;
+ enum _alpm_hook_when_t when;
+ int abort_on_fail;
+};
+
+struct _alpm_hook_cb_ctx {
+ alpm_handle_t *handle;
+ struct alpm_hook_t *hook;
+};
+
+static void _alpm_trigger_free(struct alpm_trigger_t *trigger)
+{
+ if(trigger) {
+ FREELIST(trigger->targets);
+ free(trigger);
+ }
+}
+
+static void _alpm_hook_free(struct alpm_hook_t *hook)
+{
+ if(hook) {
+ free(hook->name);
+ free(hook->cmd);
+ alpm_list_free_inner(hook->triggers, (alpm_list_fn_free) _alpm_trigger_free);
+ alpm_list_free(hook->triggers);
+ FREELIST(hook->depends);
+ free(hook);
+ }
+}
+
+static int _alpm_hook_parse_cb(const char *file, UNUSED int line,
+ const char *section, char *key, char *value, void *data) {
+ alpm_handle_t *handle = ((struct _alpm_hook_cb_ctx *) data)->handle;
+ struct alpm_hook_t *hook = ((struct _alpm_hook_cb_ctx *) data)->hook;
+
+ if(!section && !key) {
+ _alpm_log(handle, ALPM_LOG_ERROR,
+ _("error while reading file %s: %s\n"), file, strerror(errno));
+ return 1;
+ } else if(!section) {
+ _alpm_log(handle, ALPM_LOG_ERROR,
+ _("error parsing hook file %s: invalid option %s\n"), file, key);
+ return 1;
+ } else if(!key) {
+ /* beginning a new section */
+ if(strcmp(section, "Trigger") == 0) {
+ hook->triggers = alpm_list_add(hook->triggers,
+ calloc(sizeof(struct alpm_trigger_t), 1));
+ } else if(strcmp(section, "Action") == 0) {
+ /* no special processing required */
+ } else {
+ _alpm_log(handle, ALPM_LOG_ERROR,
+ _("error parsing hook file %s: invalid section %s\n"), file, section);
+ return 1;
+ }
+ } else if(strcmp(section, "Trigger") == 0) {
+ struct alpm_trigger_t *t = hook->triggers->prev->data;
+ if(strcmp(key, "Operation") == 0) {
+ if(strcmp(value, "Sync") == 0) {
+ t->op = ALPM_HOOK_OP_SYNC;
+ } else if(strcmp(value, "Remove") == 0) {
+ t->op = ALPM_HOOK_OP_SYNC;
+ } else {
+ _alpm_log(handle, ALPM_LOG_ERROR,
+ _("error parsing hook file %s: invalid value %s\n"), file, value);
+ return 1;
+ }
+ } else if(strcmp(key, "Type") == 0) {
+ if(strcmp(value, "Package") == 0) {
+ t->type = ALPM_HOOK_TYPE_PACKAGE;
+ } else if(strcmp(value, "File") == 0) {
+ t->type = ALPM_HOOK_TYPE_FILE;
+ } else {
+ _alpm_log(handle, ALPM_LOG_ERROR,
+ _("error parsing hook file %s: invalid value %s\n"), file, value);
+ return 1;
+ }
+ } else if(strcmp(key, "Target") == 0) {
+ t->targets = alpm_list_add(t->targets, strdup(value));
+ } else {
+ _alpm_log(handle, ALPM_LOG_ERROR,
+ _("error parsing hook file %s: invalid option %s\n"), file, value);
+ return 1;
+ }
+ } else if(strcmp(section, "Action") == 0) {
+ if(strcmp(key, "When") == 0) {
+ if(strcmp(value, "PreTransaction") == 0) {
+ hook->when = ALPM_HOOK_PRE_TRANSACTION;
+ } else if(strcmp(value, "PostTransaction") == 0) {
+ hook->when = ALPM_HOOK_POST_TRANSACTION;
+ } else {
+ _alpm_log(handle, ALPM_LOG_ERROR,
+ _("error parsing hook file %s: invalid value %s\n"), file, value);
+ return 1;
+ }
+ } else if(strcmp(key, "Depends") == 0) {
+ hook->depends = alpm_list_add(hook->depends, strdup(value));
+ } else if(strcmp(key, "AbortOnFail") == 0) {
+ hook->abort_on_fail = 1;
+ } else if(strcmp(key, "Exec") == 0) {
+ hook->cmd = strdup(value);
+ } else {
+ _alpm_log(handle, ALPM_LOG_ERROR,
+ _("error parsing hook file %s: invalid option %s\n"), file, value);
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
+static int _alpm_hook_trigger_match(alpm_handle_t *handle, struct alpm_trigger_t *t)
+{
+ alpm_list_t *pkgs
+ = t->op == ALPM_HOOK_OP_SYNC
+ ? handle->trans->add
+ : handle->trans->remove;
+ if(t->type == ALPM_HOOK_TYPE_PACKAGE) {
+ while(pkgs) {
+ alpm_pkg_t *pkg = pkgs->data;
+ if(_alpm_fnmatch_patterns(t->targets, pkg->name) == 0) {
+ _alpm_log(handle, ALPM_LOG_DEBUG, "matched package %s\n", pkg->name);
+ return 1;
+ }
+ pkgs = pkgs->next;
+ }
+ } else {
+ /* TODO: optimize this */
+ while(pkgs) {
+ alpm_pkg_t *pkg = pkgs->data;
+ alpm_filelist_t f = pkg->files;
+ size_t i;
+ for(i = 0; i < f.count; i++) {
+ if(_alpm_fnmatch_patterns(t->targets, f.files[i].name) == 0) {
+ _alpm_log(handle, ALPM_LOG_DEBUG, "matched file %s\n", f.files[i].name);
+ return 1;
+ }
+ }
+ pkgs = pkgs->next;
+ }
+ }
+
+ return 0;
+}
+
+static int _alpm_hook_triggered(alpm_handle_t *handle, struct alpm_hook_t *hook)
+{
+ alpm_list_t *i;
+ for(i = hook->triggers; i; i = i->next) {
+ if(_alpm_hook_trigger_match(handle, i->data)) {
+ return 1;
+ }
+ }
+ return 0;
+}
+
+static int _alpm_hook_deps_satisfied(alpm_handle_t *handle,
+ struct alpm_hook_t *hook)
+{
+ alpm_list_t *i;
+ alpm_list_t *pkgs = _alpm_db_get_pkgcache(handle->db_local);
+ for(i = hook->depends; i; i = i->next) {
+ if(!alpm_find_satisfier(pkgs, i->data)) {
+ return 0;
+ }
+ }
+ return 1;
+}
+
+static alpm_list_t *find_hook(alpm_list_t *haystack, const void *needle)
+{
+ while(haystack) {
+ struct alpm_hook_t *h = haystack->data;
+ if(h && strcmp(h->name, needle) == 0) {
+ return haystack;
+ }
+ haystack = haystack->next;
+ }
+ return NULL;
+}
+
+static int _alpm_hook_run_hook(alpm_handle_t *handle, struct alpm_hook_t *hook)
+{
+ char *const argv[] = { hook->cmd, NULL };
+ return _alpm_run_chroot(handle, hook->cmd, argv);
+}
+
+int _alpm_hook_run(alpm_handle_t *handle, enum _alpm_hook_when_t when)
+{
+ alpm_list_t *i, *hooks = NULL;
+ const char *suffix = ".hook";
+ size_t suflen = strlen(suffix);
+ int ret = 0;
+
+ for(i = alpm_list_last(handle->hookdirs); i; i = alpm_list_previous(i)) {
+ int err;
+ char path[PATH_MAX];
+ size_t dirlen;
+ struct dirent entry, *result;
+ DIR *d;
+
+ if(!(d = opendir(i->data))) {
+ if(errno == ENOENT) {
+ continue;
+ } else {
+ _alpm_log(handle, ALPM_LOG_ERROR,
+ _("could not open directory: %s: %s\n"),
+ (char *)i->data, strerror(errno));
+ ret = -1;
+ continue;
+ }
+ }
+
+ strncpy(path, i->data, PATH_MAX);
+ dirlen = strlen(i->data);
+
+ while((err = readdir_r(d, &entry, &result)) == 0 && result) {
+ struct alpm_hook_t *hook;
+ struct _alpm_hook_cb_ctx ctx = { handle, NULL };
+ struct stat buf;
+ size_t name_len = strlen(entry.d_name);
+
+ strncpy(path + dirlen, entry.d_name, PATH_MAX - dirlen);
+
+ if(name_len < suflen
+ || strcmp(entry.d_name + name_len - suflen, suffix) != 0) {
+ _alpm_log(handle, ALPM_LOG_DEBUG, "skipping non-hook file %s\n", path);
+ continue;
+ }
+
+ if(find_hook(hooks, entry.d_name)) {
+ _alpm_log(handle, ALPM_LOG_DEBUG, "skipping overridden hook %s\n", path);
+ continue;
+ }
+
+ if(fstatat(dirfd(d), entry.d_name, &buf, 0) != 0) {
+ _alpm_log(handle, ALPM_LOG_ERROR,
+ _("could not stat file %s: %s\n"), path, strerror(errno));
+ ret = -1;
+ continue;
+ }
+
+ if(S_ISDIR(buf.st_mode)) {
+ _alpm_log(handle, ALPM_LOG_DEBUG, "skipping directory %s\n", path);
+ continue;
+ }
+
+ if(!(hook = calloc(sizeof(struct alpm_hook_t), 1))) {
+ _alpm_log(handle, ALPM_LOG_ERROR,
+ _("could not stat file %s: %s\n"), path, strerror(errno));
+ ret = -1;
+ closedir(d);
+ goto cleanup;
+ }
+
+ ctx.hook = hook;
+ _alpm_log(handle, ALPM_LOG_DEBUG, "parsing hook file %s\n", path);
+ if(parse_ini(path, _alpm_hook_parse_cb, &ctx) != 0) {
+ _alpm_log(handle, ALPM_LOG_DEBUG, "parsing hook file %s failed\n", path);
+ _alpm_hook_free(hook);
+ ret = -1;
+ continue;
+ }
+
+ hook->name = strdup(entry.d_name);
+ hooks = alpm_list_add(hooks, hook);
+ }
+
+ if(err != 0) {
+ _alpm_log(handle, ALPM_LOG_ERROR,
+ _("could not read directory: %s: %s\n"),
+ (char *) i->data, strerror(errno));
+ ret = -1;
+ }
+
+ closedir(d);
+ }
+
+ for(i = hooks; i; i = i->next) {
+ struct alpm_hook_t *hook = i->data;
+ if(hook && hook->when == when && _alpm_hook_triggered(handle, hook)) {
+ _alpm_log(handle, ALPM_LOG_DEBUG, "running hook %s\n", hook->name);
+ if(!_alpm_hook_deps_satisfied(handle, hook) && hook->abort_on_fail) {
+ _alpm_log(handle, ALPM_LOG_ERROR, _("unable to run hook %s: %s\n"),
+ hook->name, _("could not satisfy dependencies"));
+ ret = -1;
+ continue;
+ }
+ if(_alpm_hook_run_hook(handle, hook) != 0 && hook->abort_on_fail) {
+ ret = -1;
+ }
+ }
+ }
+
+cleanup:
+ alpm_list_free_inner(hooks, (alpm_list_fn_free) _alpm_hook_free);
+ alpm_list_free(hooks);
+
+ return ret;
+}
+
+/* vim: set noet: */
diff --git a/lib/libalpm/hook.h b/lib/libalpm/hook.h
new file mode 100644
index 0000000..01de9c1
--- /dev/null
+++ b/lib/libalpm/hook.h
@@ -0,0 +1,34 @@
+/*
+ * hook.h
+ *
+ * Copyright (c) 2015 Pacman Development Team <pacman-dev at archlinux.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
+ * 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
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _ALPM_HOOK_H
+#define _ALPM_HOOK_H
+
+#include "alpm.h"
+
+enum _alpm_hook_when_t {
+ ALPM_HOOK_PRE_TRANSACTION,
+ ALPM_HOOK_POST_TRANSACTION
+};
+
+int _alpm_hook_run(alpm_handle_t *handle, enum _alpm_hook_when_t when);
+
+#endif /* _ALPM_HOOK_H */
+
+/* vim: set noet: */
diff --git a/lib/libalpm/trans.c b/lib/libalpm/trans.c
index 6a26e75..0b225e5 100644
--- a/lib/libalpm/trans.c
+++ b/lib/libalpm/trans.c
@@ -40,6 +40,7 @@
#include "sync.h"
#include "alpm.h"
#include "deps.h"
+#include "hook.h"
/** \addtogroup alpm_trans Transaction Functions
* @brief Functions to manipulate libalpm transactions
@@ -185,6 +186,10 @@ int SYMEXPORT alpm_trans_commit(alpm_handle_t *handle, alpm_list_t **data)
}
}
+ if(_alpm_hook_run(handle, ALPM_HOOK_PRE_TRANSACTION) != 0) {
+ RET_ERR(handle, ALPM_ERR_TRANS_HOOK_FAILED, -1);
+ }
+
trans->state = STATE_COMMITING;
alpm_logaction(handle, ALPM_CALLER_PREFIX, "transaction started\n");
@@ -211,6 +216,7 @@ int SYMEXPORT alpm_trans_commit(alpm_handle_t *handle, alpm_list_t **data)
alpm_logaction(handle, ALPM_CALLER_PREFIX, "transaction interrupted\n");
} else {
alpm_logaction(handle, ALPM_CALLER_PREFIX, "transaction completed\n");
+ _alpm_hook_run(handle, ALPM_HOOK_POST_TRANSACTION);
}
trans->state = STATE_COMMITED;
--
2.4.5
More information about the pacman-dev
mailing list