summaryrefslogtreecommitdiff
path: root/src/commit.c
diff options
context:
space:
mode:
authorHenrik Riomar <henrik.riomar@gmail.com>2017-02-03 00:37:23 +0100
committerTimo Teräs <timo.teras@iki.fi>2017-02-15 13:44:04 +0200
commit349c61c9612a328f3a80f301d37aa8c14adcb43a (patch)
treefb779b62735e2cb47ed9f1d763bc1451ed19bcd9 /src/commit.c
parent28a9dcda568c575c569ffa4b68775034b655230a (diff)
downloadapk-tools-349c61c9612a328f3a80f301d37aa8c14adcb43a.tar.gz
apk-tools-349c61c9612a328f3a80f301d37aa8c14adcb43a.tar.bz2
apk-tools-349c61c9612a328f3a80f301d37aa8c14adcb43a.tar.xz
apk-tools-349c61c9612a328f3a80f301d37aa8c14adcb43a.zip
add support for pre and post commit hooks
This allows for instance integration of etckeeper [TT: Reorganized code a bit, and modified to use single directory commit_hooks.d with argument for script of stage.]
Diffstat (limited to 'src/commit.c')
-rw-r--r--src/commit.c39
1 files changed, 38 insertions, 1 deletions
diff --git a/src/commit.c b/src/commit.c
index b87311d..ae43474 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -216,6 +216,40 @@ static void run_triggers(struct apk_database *db, struct apk_changeset *changese
}
}
+#define PRE_COMMIT_HOOK 0
+#define POST_COMMIT_HOOK 1
+
+struct apk_commit_hook {
+ struct apk_database *db;
+ int type;
+};
+
+static int run_commit_hook(void *ctx, int dirfd, const char *file)
+{
+ static char *const commit_hook_str[] = { "pre-commit", "post-commit" };
+ struct apk_commit_hook *hook = (struct apk_commit_hook *) ctx;
+ struct apk_database *db = hook->db;
+ char fn[PATH_MAX], *argv[] = { fn, (char *) commit_hook_str[hook->type], NULL };
+
+ if ((apk_flags & (APK_NO_SCRIPTS | APK_SIMULATE)) != 0)
+ return 0;
+
+ snprintf(fn, sizeof(fn), "etc/apk/commit_hooks.d" "/%s", file);
+ if (apk_verbosity >= 2) apk_message("Executing: %s", fn);
+
+ if (apk_db_run_script(db, fn, argv) < 0 && hook->type == PRE_COMMIT_HOOK)
+ return -1;
+
+ return 0;
+}
+
+static int run_commit_hooks(struct apk_database *db, int type)
+{
+ struct apk_commit_hook hook = { .db = db, .type = type };
+ return apk_dir_foreach_file(openat(db->root_fd, "etc/apk/commit_hooks.d", O_RDONLY | O_CLOEXEC),
+ run_commit_hook, &hook);
+}
+
int apk_solver_commit_changeset(struct apk_database *db,
struct apk_changeset *changeset,
struct apk_dependency_array *world)
@@ -278,6 +312,9 @@ int apk_solver_commit_changeset(struct apk_database *db,
}
}
+ if (run_commit_hooks(db, PRE_COMMIT_HOOK) < 0)
+ return -1;
+
/* Go through changes */
foreach_array_item(change, changeset->changes) {
r = change->old_pkg &&
@@ -308,6 +345,7 @@ int apk_solver_commit_changeset(struct apk_database *db,
all_done:
apk_dependency_array_copy(&db->world, world);
apk_db_write_config(db);
+ run_commit_hooks(db, POST_COMMIT_HOOK);
if (!db->performing_self_upgrade) {
if (errors)
@@ -328,7 +366,6 @@ all_done:
db->installed.stats.packages);
}
}
-
return errors;
}