From ffdc14c109f03325a8573ff7ac5d0b025ff88cd7 Mon Sep 17 00:00:00 2001 From: Ikey Doherty Date: Sun, 22 Oct 2017 02:47:49 +0100 Subject: [PATCH] redirect: Add API for creating and freeing chained LsiRedirects Signed-off-by: Ikey Doherty --- src/redirect/profile.c | 51 +++++++++++++++++++++++++++++++++++++++++ src/redirect/redirect.h | 19 ++++++++++++--- 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/src/redirect/profile.c b/src/redirect/profile.c index dd79c47..8de2f9e 100644 --- a/src/redirect/profile.c +++ b/src/redirect/profile.c @@ -38,6 +38,16 @@ void lsi_redirect_profile_free(LsiRedirectProfile *self) if (!self) { return; } + + /* Free all chains */ + for (unsigned int i = 0; i < LSI_NUM_OPERATIONS; i++) { + LsiRedirect *r = self->op_table[i]; + if (!r) { + continue; + } + lsi_redirect_free(r); + } + free(self->name); free(self); } @@ -63,6 +73,47 @@ void lsi_redirect_profile_insert_rule(LsiRedirectProfile *self, LsiRedirect *red } } +LsiRedirect *lsi_redirect_new_path_replacement(const char *source_path, const char *target_path) +{ + LsiRedirect *ret = NULL; + + ret = calloc(1, sizeof(LsiRedirect)); + if (!ret) { + return NULL; + } + ret->type = LSI_REDIRECT_PATH; + ret->path_source = strdup(source_path); + ret->path_target = strdup(target_path); + + if (!ret->path_source || !ret->path_target) { + lsi_redirect_free(ret); + return NULL; + } + return ret; +} + +void lsi_redirect_free(LsiRedirect *self) +{ + if (!self) { + return; + } + + /* Pass it along */ + if (self->next) { + lsi_redirect_free(self->next); + } + + switch (self->type) { + case LSI_REDIRECT_PATH: + default: + free(self->path_source); + free(self->path_target); + break; + } + + free(self); +} + /* * Editor modelines - https://www.wireshark.org/tools/modelines.html * diff --git a/src/redirect/redirect.h b/src/redirect/redirect.h index aa4a62d..bd7e8c8 100644 --- a/src/redirect/redirect.h +++ b/src/redirect/redirect.h @@ -30,9 +30,8 @@ typedef struct LsiRedirect { union { /* Path replacement */ struct { - const char *path_source; - const char *path_target; - bool path_relative; + char *path_source; + char *path_target; }; }; struct LsiRedirect *next; @@ -79,6 +78,20 @@ void lsi_redirect_profile_free(LsiRedirectProfile *profile); */ void lsi_redirect_profile_insert_rule(LsiRedirectProfile *self, LsiRedirect *redirect); +/* LsiRedirect APIs follow */ + +/** + * Construct a new LsiRedirect to perform path replacements + */ +LsiRedirect *lsi_redirect_new_path_replacement(const char *source_path, const char *target_path); + +/** + * Attempt to free this redirect + * + * @param redirect Pointer to allocated LsiRedirect + */ +void lsi_redirect_free(LsiRedirect *self); + /* * Editor modelines - https://www.wireshark.org/tools/modelines.html *