[pacman-dev] [PATCH 1/5] pmrule.py: add FILE_CONTENTS rule
Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com> --- To be used in NeedsTargets test. test/pacman/README | 1 + test/pacman/pmrule.py | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/test/pacman/README b/test/pacman/README index 6c601b2..a5fdeaa 100644 --- a/test/pacman/README +++ b/test/pacman/README @@ -303,6 +303,7 @@ its DEPENDS field. . FILE rules + FILE_CONTENTS=path/to/file|contents FILE_EXIST=path/to/file FILE_EMPTY=path/to/file FILE_MODIFIED=path/to/file diff --git a/test/pacman/pmrule.py b/test/pacman/pmrule.py index 3bcac86..f8991fd 100644 --- a/test/pacman/pmrule.py +++ b/test/pacman/pmrule.py @@ -120,6 +120,12 @@ def check(self, test): if not (os.path.isfile(filename) and os.path.getsize(filename) == 0): success = 0 + elif case == "CONTENTS": + try: + with open(filename, 'r') as f: + success = f.read() == value + except: + success = 0 elif case == "MODIFIED": for f in test.files: if f.name == key: -- 2.6.2
Newlines clutter tap output and can potentially confuse TAP parsers. Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com> --- test/pacman/tap.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/pacman/tap.py b/test/pacman/tap.py index 520f1e3..a91071b 100644 --- a/test/pacman/tap.py +++ b/test/pacman/tap.py @@ -19,7 +19,7 @@ failed = 0 def _output(msg): - print("%s%s" % (" "*level, msg)) + print("%s%s" % (" "*level, str(msg).replace("\n", "\\n"))) def ok(ok, description=""): global count, failed -- 2.6.2
Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com> --- Fixed whitespace. doc/alpm-hooks.5.txt | 5 +- lib/libalpm/hook.c | 129 +++++++++++++++++++++++++- test/pacman/tests/TESTS | 1 + test/pacman/tests/hook-exec-with-arguments.py | 22 +++++ 4 files changed, 150 insertions(+), 7 deletions(-) create mode 100644 test/pacman/tests/hook-exec-with-arguments.py diff --git a/doc/alpm-hooks.5.txt b/doc/alpm-hooks.5.txt index 2986abf..770f466 100644 --- a/doc/alpm-hooks.5.txt +++ b/doc/alpm-hooks.5.txt @@ -62,8 +62,9 @@ defined the hook will run if the transaction matches *any* of the triggers. ACTIONS ------- -*Exec =* /path/to/executable:: - Executable to run. Required. +*Exec =* <command>:: + Command to run. Command arguments are split on whitespace. Values + containing whitespace should be enclosed in quotes. Required. *When =* PreTransaction|PostTransaction:: When to run the hook. Required. diff --git a/lib/libalpm/hook.c b/lib/libalpm/hook.c index 0191a21..bff072d 100644 --- a/lib/libalpm/hook.c +++ b/lib/libalpm/hook.c @@ -17,6 +17,7 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#include <ctype.h> #include <dirent.h> #include <errno.h> #include <string.h> @@ -49,7 +50,7 @@ struct _alpm_hook_t { char *name; alpm_list_t *triggers; alpm_list_t *depends; - char *cmd; + char **cmd; enum _alpm_hook_when_t when; int abort_on_fail; }; @@ -67,11 +68,22 @@ static void _alpm_trigger_free(struct _alpm_trigger_t *trigger) } } +static void _alpm_wordsplit_free(char **ws) +{ + if(ws) { + char **c; + for(c = ws; *c; c++) { + free(*c); + } + free(ws); + } +} + static void _alpm_hook_free(struct _alpm_hook_t *hook) { if(hook) { free(hook->name); - free(hook->cmd); + _alpm_wordsplit_free(hook->cmd); alpm_list_free_inner(hook->triggers, (alpm_list_fn_free) _alpm_trigger_free); alpm_list_free(hook->triggers); FREELIST(hook->depends); @@ -141,6 +153,107 @@ static int _alpm_hook_validate(alpm_handle_t *handle, return ret; } +static char **_alpm_wordsplit(char *str) +{ + char *c = str, *end; + char **out = NULL, **outsave; + size_t count = 0; + + if(str == NULL) { + errno = EINVAL; + return NULL; + } + + for(c = str; isspace(*c); c++); + while(*c) { + size_t wordlen = 0; + + /* extend our array */ + outsave = out; + if((out = realloc(out, (count + 1) * sizeof(char*))) == NULL) { + out = outsave; + goto error; + } + + /* calculate word length and check for unbalanced quotes */ + for(end = c; *end && !isspace(*end); end++) { + if(*end == '\'' || *end == '"') { + char quote = *end; + while(*(++end) && *end != quote) { + if(*end == '\\' && *(end + 1) == quote) { + end++; + } + wordlen++; + } + if(*end != quote) { + errno = EINVAL; + goto error; + } + } else { + if(*end == '\\' && (end[1] == '\'' || end[1] == '"')) { + end++; /* skip the '\\' */ + } + wordlen++; + } + } + + if(wordlen == (size_t) (end - c)) { + /* no internal quotes or escapes, copy it the easy way */ + if((out[count++] = strndup(c, wordlen)) == NULL) { + goto error; + } + } else { + /* manually copy to remove quotes and escapes */ + char *dest = out[count++] = malloc(wordlen + 1); + if(dest == NULL) { goto error; } + while(c < end) { + if(*c == '\'' || *c == '"') { + char quote = *c; + /* we know there must be a matching end quote, + * no need to check for '\0' */ + for(c++; *c != quote; c++) { + if(*c == '\\' && *(c + 1) == quote) { + c++; + } + *(dest++) = *c; + } + c++; + } else { + if(*c == '\\' && (c[1] == '\'' || c[1] == '"')) { + c++; /* skip the '\\' */ + } + *(dest++) = *(c++); + } + } + *dest = '\0'; + } + + if(*end == '\0') { + break; + } else { + for(c = end + 1; isspace(*c); c++); + } + } + + outsave = out; + if((out = realloc(out, (count + 1) * sizeof(char*))) == NULL) { + out = outsave; + goto error; + } + + out[count++] = NULL; + + return out; + +error: + /* can't use wordsplit_free here because NULL has not been appended */ + while(count) { + free(out[--count]); + } + free(out); + return NULL; +} + static int _alpm_hook_parse_cb(const char *file, int line, const char *section, char *key, char *value, void *data) { @@ -208,7 +321,14 @@ static int _alpm_hook_parse_cb(const char *file, int line, } else if(strcmp(key, "AbortOnFail") == 0) { hook->abort_on_fail = 1; } else if(strcmp(key, "Exec") == 0) { - STRDUP(hook->cmd, value, return 1); + if((hook->cmd = _alpm_wordsplit(value)) == NULL) { + if(errno == EINVAL) { + error(_("hook %s line %d: invalid value %s\n"), file, line, value); + } else { + error(_("hook %s line %d: unable to set option (%s)\n"), + file, line, strerror(errno)); + } + } } else { error(_("hook %s line %d: invalid option %s\n"), file, line, key); } @@ -383,7 +503,6 @@ static alpm_list_t *find_hook(alpm_list_t *haystack, const void *needle) static int _alpm_hook_run_hook(alpm_handle_t *handle, struct _alpm_hook_t *hook) { alpm_list_t *i, *pkgs = _alpm_db_get_pkgcache(handle->db_local); - char *const argv[] = { hook->cmd, NULL }; for(i = hook->depends; i; i = i->next) { if(!alpm_find_satisfier(pkgs, i->data)) { @@ -393,7 +512,7 @@ static int _alpm_hook_run_hook(alpm_handle_t *handle, struct _alpm_hook_t *hook) } } - return _alpm_run_chroot(handle, hook->cmd, argv); + return _alpm_run_chroot(handle, hook->cmd[0], hook->cmd); } int _alpm_hook_run(alpm_handle_t *handle, enum _alpm_hook_when_t when) diff --git a/test/pacman/tests/TESTS b/test/pacman/tests/TESTS index 8ad1b9c..afd2e69 100644 --- a/test/pacman/tests/TESTS +++ b/test/pacman/tests/TESTS @@ -51,6 +51,7 @@ TESTS += test/pacman/tests/fileconflict030.py TESTS += test/pacman/tests/fileconflict031.py TESTS += test/pacman/tests/fileconflict032.py TESTS += test/pacman/tests/hook-abortonfail.py +TESTS += test/pacman/tests/hook-exec-with-arguments.py TESTS += test/pacman/tests/hook-file-change-packages.py TESTS += test/pacman/tests/hook-file-remove-trigger-match.py TESTS += test/pacman/tests/hook-file-upgrade-nomatch.py diff --git a/test/pacman/tests/hook-exec-with-arguments.py b/test/pacman/tests/hook-exec-with-arguments.py new file mode 100644 index 0000000..d3df87b --- /dev/null +++ b/test/pacman/tests/hook-exec-with-arguments.py @@ -0,0 +1,22 @@ +self.description = "Hook with arguments" + +self.add_hook("hook", + """ + [Trigger] + Type = Package + Operation = Install + Target = foo + + [Action] + When = PreTransaction + Exec = bin/sh -c ': > hook-output' + """); + +sp = pmpkg("foo") +self.addpkg2db("sync", sp) + +self.args = "-S foo" + +self.addrule("PACMAN_RETCODE=0") +self.addrule("PKG_EXIST=foo") +self.addrule("FILE_EXIST=hook-output") -- 2.6.2
Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com> --- * split read/write sections into _alpm_chroot_{write_to_child,read_from_child} * fully spell out parent2child/child2parent * replaced pthread_sigmask with sigaction * renamed p2c_cb and p2c_ctx -> stdin_cb and stdin_ctx and updated doxygen * stdin_ctx was left as void* rather than alpm_list_t* because void* is traditional for caller-supplied callback context lib/libalpm/hook.c | 2 +- lib/libalpm/trans.c | 2 +- lib/libalpm/util.c | 199 +++++++++++++++++++++++++++++++++++++++++++++------- lib/libalpm/util.h | 5 +- 4 files changed, 179 insertions(+), 29 deletions(-) diff --git a/lib/libalpm/hook.c b/lib/libalpm/hook.c index bff072d..6d238d8 100644 --- a/lib/libalpm/hook.c +++ b/lib/libalpm/hook.c @@ -512,7 +512,7 @@ static int _alpm_hook_run_hook(alpm_handle_t *handle, struct _alpm_hook_t *hook) } } - return _alpm_run_chroot(handle, hook->cmd[0], hook->cmd); + return _alpm_run_chroot(handle, hook->cmd[0], hook->cmd, NULL, NULL); } int _alpm_hook_run(alpm_handle_t *handle, enum _alpm_hook_when_t when) diff --git a/lib/libalpm/trans.c b/lib/libalpm/trans.c index a6b1aef..06997a0 100644 --- a/lib/libalpm/trans.c +++ b/lib/libalpm/trans.c @@ -391,7 +391,7 @@ int _alpm_runscriptlet(alpm_handle_t *handle, const char *filepath, _alpm_log(handle, ALPM_LOG_DEBUG, "executing \"%s\"\n", cmdline); - retval = _alpm_run_chroot(handle, SCRIPTLET_SHELL, argv); + retval = _alpm_run_chroot(handle, SCRIPTLET_SHELL, argv, NULL, NULL); cleanup: if(scriptfn && unlink(scriptfn)) { diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c index 66a2742..948fd47 100644 --- a/lib/libalpm/util.c +++ b/lib/libalpm/util.c @@ -31,6 +31,7 @@ #include <limits.h> #include <sys/wait.h> #include <fnmatch.h> +#include <poll.h> /* libarchive */ #include <archive.h> @@ -445,16 +446,119 @@ ssize_t _alpm_files_in_directory(alpm_handle_t *handle, const char *path, return files; } +static int _alpm_chroot_write_to_child(alpm_handle_t *handle, int fd, + char *buf, ssize_t *buf_size, ssize_t buf_limit, + _alpm_cb_io out_cb, void *cb_ctx) +{ + ssize_t nwrite; + struct sigaction newaction, oldaction; + + if(*buf_size == 0) { + /* empty buffer, ask the callback for more */ + if((*buf_size = out_cb(buf, buf_limit, cb_ctx)) == 0) { + /* no more to write, close the pipe */ + return -1; + } + } + + /* ignore SIGPIPE in case the pipe has been closed */ + newaction.sa_handler = SIG_IGN; + sigemptyset(&newaction.sa_mask); + newaction.sa_flags = 0; + sigaction(SIGPIPE, &newaction, &oldaction); + + nwrite = write(fd, buf, *buf_size); + + /* restore previous SIGPIPE handler */ + sigaction(SIGPIPE, &oldaction, NULL); + + if(nwrite != -1) { + /* write was successful, remove the written data from the buffer */ + *buf_size -= nwrite; + memmove(buf, buf + nwrite, *buf_size); + } else if(errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) { + /* nothing written, try again later */ + } else { + _alpm_log(handle, ALPM_LOG_ERROR, + _("unable to write to pipe (%s)\n"), strerror(errno)); + return -1; + } + + return 0; +} + +static void _alpm_chroot_process_output(alpm_handle_t *handle, const char *line) +{ + alpm_event_scriptlet_info_t event = { + .type = ALPM_EVENT_SCRIPTLET_INFO, + .line = line + }; + alpm_logaction(handle, "ALPM-SCRIPTLET", "%s", line); + EVENT(handle, &event); +} + +static int _alpm_chroot_read_from_child(alpm_handle_t *handle, int fd, + char *buf, ssize_t *buf_size, ssize_t buf_limit) +{ + ssize_t space = buf_limit - *buf_size - 2; /* reserve 2 for "\n\0" */ + ssize_t nread = read(fd, buf + *buf_size, space); + if(nread > 0) { + char *newline = memchr(buf + *buf_size, '\n', nread); + *buf_size += nread; + if(newline) { + while(newline) { + size_t linelen = newline - buf + 1; + char old = buf[linelen]; + buf[linelen] = '\0'; + _alpm_chroot_process_output(handle, buf); + buf[linelen] = old; + + *buf_size -= linelen; + memmove(buf, buf + linelen, *buf_size); + newline = memchr(buf, '\n', *buf_size); + } + } else if(nread == space) { + /* we didn't read a full line, but we're out of space */ + strcpy(buf + *buf_size, "\n"); + _alpm_chroot_process_output(handle, buf); + *buf_size = 0; + } + } else if(nread == 0) { + /* end-of-file */ + if(*buf_size) { + strcpy(buf + *buf_size, "\n"); + _alpm_chroot_process_output(handle, buf); + } + return -1; + } else if(errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) { + /* nothing read, try again */ + } else { + /* read error */ + if(*buf_size) { + strcpy(buf + *buf_size, "\n"); + _alpm_chroot_process_output(handle, buf); + } + _alpm_log(handle, ALPM_LOG_ERROR, + _("unable to read from pipe (%s)\n"), strerror(errno)); + return -1; + } + return 0; +} + /** Execute a command with arguments in a chroot. * @param handle the context handle * @param cmd command to execute * @param argv arguments to pass to cmd + * @param stdin_cb callback to provide input to the chroot on stdin + * @param stdin_ctx context to be passed to @a stdin_cb * @return 0 on success, 1 on error */ -int _alpm_run_chroot(alpm_handle_t *handle, const char *cmd, char *const argv[]) +int _alpm_run_chroot(alpm_handle_t *handle, const char *cmd, char *const argv[], + _alpm_cb_io stdin_cb, void *stdin_ctx) { pid_t pid; - int pipefd[2], cwdfd; + int child2parent_pipefd[2], parent2child_pipefd[2]; + int cwdfd; int retval = 0; /* save the cwd so we can restore it later */ @@ -476,7 +580,13 @@ int _alpm_run_chroot(alpm_handle_t *handle, const char *cmd, char *const argv[]) /* Flush open fds before fork() to avoid cloning buffers */ fflush(NULL); - if(pipe(pipefd) == -1) { + if(pipe(child2parent_pipefd) == -1) { + _alpm_log(handle, ALPM_LOG_ERROR, _("could not create pipe (%s)\n"), strerror(errno)); + retval = 1; + goto cleanup; + } + + if(stdin_cb && pipe(parent2child_pipefd) == -1) { _alpm_log(handle, ALPM_LOG_ERROR, _("could not create pipe (%s)\n"), strerror(errno)); retval = 1; goto cleanup; @@ -495,10 +605,15 @@ int _alpm_run_chroot(alpm_handle_t *handle, const char *cmd, char *const argv[]) close(0); close(1); close(2); - while(dup2(pipefd[1], 1) == -1 && errno == EINTR); - while(dup2(pipefd[1], 2) == -1 && errno == EINTR); - close(pipefd[0]); - close(pipefd[1]); + while(dup2(child2parent_pipefd[1], 1) == -1 && errno == EINTR); + while(dup2(child2parent_pipefd[1], 2) == -1 && errno == EINTR); + if(stdin_cb) { + while(dup2(parent2child_pipefd[0], 0) == -1 && errno == EINTR); + close(parent2child_pipefd[0]); + close(parent2child_pipefd[1]); + } + close(child2parent_pipefd[0]); + close(child2parent_pipefd[1]); if(cwdfd >= 0) { close(cwdfd); } @@ -521,27 +636,59 @@ int _alpm_run_chroot(alpm_handle_t *handle, const char *cmd, char *const argv[]) } else { /* this code runs for the parent only (wait on the child) */ int status; - FILE *pipe_file; - - close(pipefd[1]); - pipe_file = fdopen(pipefd[0], "r"); - if(pipe_file == NULL) { - close(pipefd[0]); - retval = 1; + char obuf[PIPE_BUF]; /* writes <= PIPE_BUF are guaranteed atomic */ + char ibuf[LINE_MAX]; + ssize_t olen = 0, ilen = 0; + nfds_t nfds = 2; + struct pollfd fds[2], *child2parent = &(fds[0]), *parent2child = &(fds[1]); + + child2parent->fd = child2parent_pipefd[0]; + child2parent->events = POLLIN; + fcntl(child2parent->fd, F_SETFL, O_NONBLOCK); + close(child2parent_pipefd[1]); + + if(stdin_cb) { + parent2child->fd = parent2child_pipefd[1]; + parent2child->events = POLLOUT; + fcntl(parent2child->fd, F_SETFL, O_NONBLOCK); + close(parent2child_pipefd[0]); } else { - while(!feof(pipe_file)) { - char line[PATH_MAX]; - alpm_event_scriptlet_info_t event = { - .type = ALPM_EVENT_SCRIPTLET_INFO, - .line = line - }; - if(safe_fgets(line, PATH_MAX, pipe_file) == NULL) { - break; + parent2child->fd = -1; + parent2child->events = 0; + } + +#define STOP_POLLING(p) do { close(p->fd); p->fd = -1; } while(0) + + while((child2parent->fd != -1 || parent2child->fd != -1) + && poll(fds, nfds, -1) > 0) { + if(child2parent->revents & POLLIN) { + if(_alpm_chroot_read_from_child(handle, child2parent->fd, + ibuf, &ilen, sizeof(ibuf)) != 0) { + /* we encountered end-of-file or an error */ + STOP_POLLING(child2parent); } - alpm_logaction(handle, "ALPM-SCRIPTLET", "%s", line); - EVENT(handle, &event); + } else if(child2parent->revents) { + /* anything but POLLIN indicates an error */ + STOP_POLLING(child2parent); } - fclose(pipe_file); + if(parent2child->revents & POLLOUT) { + if(_alpm_chroot_write_to_child(handle, parent2child->fd, obuf, &olen, + sizeof(obuf), stdin_cb, stdin_ctx) != 0) { + STOP_POLLING(parent2child); + } + } else if(parent2child->revents) { + /* anything but POLLOUT indicates an error */ + STOP_POLLING(parent2child); + } + } + +#undef STOP_POLLING + + if(parent2child->fd != -1) { + close(parent2child->fd); + } + if(child2parent->fd != -1) { + close(child2parent->fd); } while(waitpid(pid, &status, 0) == -1) { @@ -605,7 +752,7 @@ int _alpm_ldconfig(alpm_handle_t *handle) char arg0[32]; char *argv[] = { arg0, NULL }; strcpy(arg0, "ldconfig"); - return _alpm_run_chroot(handle, LDCONFIG, argv); + return _alpm_run_chroot(handle, LDCONFIG, argv, NULL, NULL); } } diff --git a/lib/libalpm/util.h b/lib/libalpm/util.h index 95112cf..c0c9ff0 100644 --- a/lib/libalpm/util.h +++ b/lib/libalpm/util.h @@ -119,7 +119,10 @@ int _alpm_unpack(alpm_handle_t *handle, const char *archive, const char *prefix, ssize_t _alpm_files_in_directory(alpm_handle_t *handle, const char *path, int full_count); -int _alpm_run_chroot(alpm_handle_t *handle, const char *cmd, char *const argv[]); +typedef ssize_t (*_alpm_cb_io)(void *buf, ssize_t len, void *ctx); + +int _alpm_run_chroot(alpm_handle_t *handle, const char *cmd, char *const argv[], + _alpm_cb_io in_cb, void *in_ctx); int _alpm_ldconfig(alpm_handle_t *handle); int _alpm_str_cmp(const void *s1, const void *s2); char *_alpm_filecache_find(alpm_handle_t *handle, const char *filename); -- 2.6.2
Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com> --- * added test * added comment explaining duplicate matches doc/alpm-hooks.5.txt | 5 ++ lib/libalpm/hook.c | 127 +++++++++++++++++++++++++++++----- test/pacman/tests/TESTS | 1 + test/pacman/tests/hook-target-list.py | 40 +++++++++++ 4 files changed, 156 insertions(+), 17 deletions(-) create mode 100644 test/pacman/tests/hook-target-list.py diff --git a/doc/alpm-hooks.5.txt b/doc/alpm-hooks.5.txt index 770f466..3729387 100644 --- a/doc/alpm-hooks.5.txt +++ b/doc/alpm-hooks.5.txt @@ -23,6 +23,7 @@ When = PreTransaction|PostTransaction (Required) Exec = <Command> (Required) Depends = <PkgName> (Optional) AbortOnFail (Optional, PreTransaction only) +NeedsTargets (Optional) -------- DESCRIPTION @@ -77,6 +78,10 @@ ACTIONS Causes the transaction to be aborted if the hook exits non-zero. Only applies to PreTransaction hooks. +*NeedsTargets*:: + Causes the list of matched trigger targets to be passed to the running hook + on 'stdin'. + OVERRIDING HOOKS ---------------- diff --git a/lib/libalpm/hook.c b/lib/libalpm/hook.c index 6d238d8..f5627af 100644 --- a/lib/libalpm/hook.c +++ b/lib/libalpm/hook.c @@ -51,8 +51,9 @@ struct _alpm_hook_t { alpm_list_t *triggers; alpm_list_t *depends; char **cmd; + alpm_list_t *matches; enum _alpm_hook_when_t when; - int abort_on_fail; + int abort_on_fail, needs_targets; }; struct _alpm_hook_cb_ctx { @@ -86,6 +87,7 @@ static void _alpm_hook_free(struct _alpm_hook_t *hook) _alpm_wordsplit_free(hook->cmd); alpm_list_free_inner(hook->triggers, (alpm_list_fn_free) _alpm_trigger_free); alpm_list_free(hook->triggers); + alpm_list_free(hook->matches); FREELIST(hook->depends); free(hook); } @@ -320,6 +322,8 @@ static int _alpm_hook_parse_cb(const char *file, int line, hook->depends = alpm_list_add(hook->depends, val); } else if(strcmp(key, "AbortOnFail") == 0) { hook->abort_on_fail = 1; + } else if(strcmp(key, "NeedsTargets") == 0) { + hook->needs_targets = 1; } else if(strcmp(key, "Exec") == 0) { if((hook->cmd = _alpm_wordsplit(value)) == NULL) { if(errno == EINVAL) { @@ -339,7 +343,8 @@ static int _alpm_hook_parse_cb(const char *file, int line, return 0; } -static int _alpm_hook_trigger_match_file(alpm_handle_t *handle, struct _alpm_trigger_t *t) +static int _alpm_hook_trigger_match_file(alpm_handle_t *handle, + struct _alpm_hook_t *hook, struct _alpm_trigger_t *t) { alpm_list_t *i, *j, *install = NULL, *upgrade = NULL, *remove = NULL; size_t isize = 0, rsize = 0; @@ -423,15 +428,31 @@ static int _alpm_hook_trigger_match_file(alpm_handle_t *handle, struct _alpm_tri || (t->op & ALPM_HOOK_OP_UPGRADE && upgrade) || (t->op & ALPM_HOOK_OP_REMOVE && remove); - alpm_list_free(install); - alpm_list_free(upgrade); - alpm_list_free(remove); + if(hook->needs_targets) { +#define _save_matches(_op, _matches) \ + if(t->op & _op && _matches) { \ + hook->matches = alpm_list_join(hook->matches, _matches); \ + } else { \ + alpm_list_free(_matches); \ + } + _save_matches(ALPM_HOOK_OP_INSTALL, install); + _save_matches(ALPM_HOOK_OP_UPGRADE, upgrade); + _save_matches(ALPM_HOOK_OP_REMOVE, remove); +#undef _save_matches + } else { + alpm_list_free(install); + alpm_list_free(upgrade); + alpm_list_free(remove); + } return ret; } -static int _alpm_hook_trigger_match_pkg(alpm_handle_t *handle, struct _alpm_trigger_t *t) +static int _alpm_hook_trigger_match_pkg(alpm_handle_t *handle, + struct _alpm_hook_t *hook, struct _alpm_trigger_t *t) { + alpm_list_t *install = NULL, *upgrade = NULL, *remove = NULL; + if(t->op & ALPM_HOOK_OP_INSTALL || t->op & ALPM_HOOK_OP_UPGRADE) { alpm_list_t *i; for(i = handle->trans->add; i; i = i->next) { @@ -439,11 +460,19 @@ static int _alpm_hook_trigger_match_pkg(alpm_handle_t *handle, struct _alpm_trig if(_alpm_fnmatch_patterns(t->targets, pkg->name) == 0) { if(alpm_db_get_pkg(handle->db_local, pkg->name)) { if(t->op & ALPM_HOOK_OP_UPGRADE) { - return 1; + if(hook->needs_targets) { + upgrade = alpm_list_add(upgrade, pkg->name); + } else { + return 1; + } } } else { if(t->op & ALPM_HOOK_OP_INSTALL) { - return 1; + if(hook->needs_targets) { + install = alpm_list_add(install, pkg->name); + } else { + return 1; + } } } } @@ -456,31 +485,47 @@ static int _alpm_hook_trigger_match_pkg(alpm_handle_t *handle, struct _alpm_trig alpm_pkg_t *pkg = i->data; if(pkg && _alpm_fnmatch_patterns(t->targets, pkg->name) == 0) { if(!alpm_list_find(handle->trans->add, pkg, _alpm_pkg_cmp)) { - return 1; + if(hook->needs_targets) { + remove = alpm_list_add(remove, pkg->name); + } else { + return 1; + } } } } } - return 0; + /* if we reached this point we either need the target lists or we didn't + * match anything and the following calls will all be no-ops */ + hook->matches = alpm_list_join(hook->matches, install); + hook->matches = alpm_list_join(hook->matches, upgrade); + hook->matches = alpm_list_join(hook->matches, remove); + + return install || upgrade || remove; } -static int _alpm_hook_trigger_match(alpm_handle_t *handle, struct _alpm_trigger_t *t) +static int _alpm_hook_trigger_match(alpm_handle_t *handle, + struct _alpm_hook_t *hook, struct _alpm_trigger_t *t) { return t->type == ALPM_HOOK_TYPE_PACKAGE - ? _alpm_hook_trigger_match_pkg(handle, t) - : _alpm_hook_trigger_match_file(handle, t); + ? _alpm_hook_trigger_match_pkg(handle, hook, t) + : _alpm_hook_trigger_match_file(handle, hook, t); } static int _alpm_hook_triggered(alpm_handle_t *handle, struct _alpm_hook_t *hook) { alpm_list_t *i; + int ret = 0; for(i = hook->triggers; i; i = i->next) { - if(_alpm_hook_trigger_match(handle, i->data)) { - return 1; + if(_alpm_hook_trigger_match(handle, hook, i->data)) { + if(!hook->needs_targets) { + return 1; + } else { + ret = 1; + } } } - return 0; + return ret; } static int _alpm_hook_cmp(struct _alpm_hook_t *h1, struct _alpm_hook_t *h2) @@ -500,6 +545,44 @@ static alpm_list_t *find_hook(alpm_list_t *haystack, const void *needle) return NULL; } +static ssize_t _alpm_hook_feed_targets(char *buf, ssize_t needed, alpm_list_t **pos) +{ + size_t remaining = needed, written = 0;; + size_t len; + + while(*pos && (len = strlen((*pos)->data)) + 1 <= remaining) { + memcpy(buf, (*pos)->data, len); + buf[len++] = '\n'; + *pos = (*pos)->next; + buf += len; + remaining -= len; + written += len; + } + + if(*pos && remaining) { + memcpy(buf, (*pos)->data, remaining); + (*pos)->data = (char*) (*pos)->data + remaining; + written += remaining; + } + + return written; +} + +static alpm_list_t *_alpm_strlist_dedup(alpm_list_t *list) +{ + alpm_list_t *i = list; + while(i) { + alpm_list_t *next = i->next; + while(next && strcmp(i->data, next->data) == 0) { + list = alpm_list_remove_item(list, next); + free(next); + next = i->next; + } + i = next; + } + return list; +} + static int _alpm_hook_run_hook(alpm_handle_t *handle, struct _alpm_hook_t *hook) { alpm_list_t *i, *pkgs = _alpm_db_get_pkgcache(handle->db_local); @@ -512,7 +595,17 @@ static int _alpm_hook_run_hook(alpm_handle_t *handle, struct _alpm_hook_t *hook) } } - return _alpm_run_chroot(handle, hook->cmd[0], hook->cmd, NULL, NULL); + if(hook->needs_targets) { + alpm_list_t *ctx; + hook->matches = alpm_list_msort(hook->matches, + alpm_list_count(hook->matches), (alpm_list_fn_cmp)strcmp); + /* hooks with multiple triggers could have duplicate matches */ + ctx = hook->matches = _alpm_strlist_dedup(hook->matches); + return _alpm_run_chroot(handle, hook->cmd[0], hook->cmd, + (_alpm_cb_io) _alpm_hook_feed_targets, &ctx); + } else { + return _alpm_run_chroot(handle, hook->cmd[0], hook->cmd, NULL, NULL); + } } int _alpm_hook_run(alpm_handle_t *handle, enum _alpm_hook_when_t when) diff --git a/test/pacman/tests/TESTS b/test/pacman/tests/TESTS index afd2e69..e330896 100644 --- a/test/pacman/tests/TESTS +++ b/test/pacman/tests/TESTS @@ -59,6 +59,7 @@ TESTS += test/pacman/tests/hook-invalid-trigger.py TESTS += test/pacman/tests/hook-pkg-install-trigger-match.py TESTS += test/pacman/tests/hook-pkg-remove-trigger-match.py TESTS += test/pacman/tests/hook-pkg-upgrade-trigger-match.py +TESTS += test/pacman/tests/hook-target-list.py TESTS += test/pacman/tests/hook-upgrade-trigger-no-match.py TESTS += test/pacman/tests/ignore001.py TESTS += test/pacman/tests/ignore002.py diff --git a/test/pacman/tests/hook-target-list.py b/test/pacman/tests/hook-target-list.py new file mode 100644 index 0000000..6dd6c4d --- /dev/null +++ b/test/pacman/tests/hook-target-list.py @@ -0,0 +1,40 @@ +self.description = "Hook with NeedsTargets" + +self.add_hook("hook", + """ + [Trigger] + Type = Package + Operation = Install + Target = foo + + # duplicate trigger to check that duplicate targets are removed + [Trigger] + Type = Package + Operation = Install + Target = foo + + [Trigger] + Type = File + Operation = Install + # matches files in 'file/' but not 'file/' itself + Target = file/?* + + [Action] + When = PreTransaction + Exec = bin/sh -c 'while read -r tgt; do printf "%s\\n" "$tgt"; done > var/log/hook-output' + NeedsTargets + """); + +p1 = pmpkg("foo") +p1.files = ["file/foo"] +self.addpkg(p1) + +p2 = pmpkg("bar") +p2.files = ["file/bar"] +self.addpkg(p2) + +self.args = "-U %s %s" % (p1.filename(), p2.filename()) + +self.addrule("PACMAN_RETCODE=0") +self.addrule("PKG_EXIST=foo") +self.addrule("FILE_CONTENTS=var/log/hook-output|file/bar\nfile/foo\nfoo\n") -- 2.6.2
participants (1)
-
Andrew Gregory