diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2023-04-29 16:08:37 +0200 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2023-04-29 16:08:37 +0200 |
commit | 35c0333a09cee6c6f7d72e6c2459110a65d51a3d (patch) | |
tree | 1ce971a5d02ad13d7da571bedf7ef715605a3b86 | |
parent | net: use size instead of length for variable names (diff) | |
download | cimple-35c0333a09cee6c6f7d72e6c2459110a65d51a3d.tar.gz cimple-35c0333a09cee6c6f7d72e6c2459110a65d51a3d.zip |
make struct ci_queue_entry opaque
-rw-r--r-- | src/ci_queue.c | 43 | ||||
-rw-r--r-- | src/ci_queue.h | 9 | ||||
-rw-r--r-- | src/msg.c | 14 | ||||
-rw-r--r-- | src/msg.h | 2 | ||||
-rw-r--r-- | src/server.c | 7 | ||||
-rw-r--r-- | src/worker.c | 2 |
6 files changed, 48 insertions, 29 deletions
diff --git a/src/ci_queue.c b/src/ci_queue.c index 97fc257..6429580 100644 --- a/src/ci_queue.c +++ b/src/ci_queue.c @@ -12,14 +12,28 @@ #include <string.h> #include <sys/queue.h> -int ci_queue_entry_create(struct ci_queue_entry **entry, const char *_url, const char *_rev) +struct ci_queue_entry { + char *url; + char *rev; + STAILQ_ENTRY(ci_queue_entry) entries; +}; + +int ci_queue_entry_create(struct ci_queue_entry **_entry, const char *_url, const char *_rev) { + struct ci_queue_entry *entry; char *url, *rev; + *_entry = malloc(sizeof(struct ci_queue_entry)); + if (!*_entry) { + log_errno("malloc"); + goto fail; + } + entry = *_entry; + url = strdup(_url); if (!url) { log_errno("strdup"); - goto fail; + goto free_entry; } rev = strdup(_rev); @@ -28,22 +42,17 @@ int ci_queue_entry_create(struct ci_queue_entry **entry, const char *_url, const goto free_url; } - *entry = malloc(sizeof(struct ci_queue_entry)); - if (!*entry) { - log_errno("malloc"); - goto free_rev; - } - (*entry)->url = url; - (*entry)->rev = rev; + entry->url = url; + entry->rev = rev; return 0; -free_rev: - free(rev); - free_url: free(url); +free_entry: + free(entry); + fail: return -1; } @@ -55,6 +64,16 @@ void ci_queue_entry_destroy(struct ci_queue_entry *entry) free(entry); } +const char *ci_queue_entry_get_url(const struct ci_queue_entry *entry) +{ + return entry->rev; +} + +const char *ci_queue_entry_get_rev(const struct ci_queue_entry *entry) +{ + return entry->rev; +} + void ci_queue_create(struct ci_queue *queue) { STAILQ_INIT(queue); diff --git a/src/ci_queue.h b/src/ci_queue.h index cf8b182..7f3702b 100644 --- a/src/ci_queue.h +++ b/src/ci_queue.h @@ -10,15 +10,14 @@ #include <sys/queue.h> -struct ci_queue_entry { - char *url; - char *rev; - STAILQ_ENTRY(ci_queue_entry) entries; -}; +struct ci_queue_entry; int ci_queue_entry_create(struct ci_queue_entry **, const char *url, const char *rev); void ci_queue_entry_destroy(struct ci_queue_entry *); +const char *ci_queue_entry_get_url(const struct ci_queue_entry *); +const char *ci_queue_entry_get_rev(const struct ci_queue_entry *); + STAILQ_HEAD(ci_queue, ci_queue_entry); void ci_queue_create(struct ci_queue *); @@ -15,13 +15,13 @@ int msg_success(struct msg *msg) { - char *argv[] = {"success", NULL}; + const char *argv[] = {"success", NULL}; return msg_from_argv(msg, argv); } int msg_error(struct msg *msg) { - char *argv[] = {"error", NULL}; + const char *argv[] = {"error", NULL}; return msg_from_argv(msg, argv); } @@ -35,9 +35,9 @@ int msg_is_error(const struct msg *msg) return msg->argc == 1 && !strcmp(msg->argv[0], "error"); } -static int msg_copy_argv(struct msg *msg, char **argv) +static int msg_copy_argv(struct msg *msg, const char **argv) { - msg->argv = calloc(msg->argc, sizeof(char *)); + msg->argv = calloc(msg->argc, sizeof(const char *)); if (!msg->argv) { log_errno("calloc"); @@ -72,7 +72,7 @@ struct msg *msg_copy(const struct msg *src) } dest->argc = src->argc; - ret = msg_copy_argv(dest, src->argv); + ret = msg_copy_argv(dest, (const char **)src->argv); if (ret < 0) goto free; @@ -91,11 +91,11 @@ void msg_free(const struct msg *msg) free(msg->argv); } -int msg_from_argv(struct msg *msg, char **argv) +int msg_from_argv(struct msg *msg, const char **argv) { int argc = 0; - for (char **s = argv; *s; ++s) + for (const char **s = argv; *s; ++s) ++argc; msg->argc = argc; @@ -22,7 +22,7 @@ int msg_is_error(const struct msg *); struct msg *msg_copy(const struct msg *); void msg_free(const struct msg *); -int msg_from_argv(struct msg *, char **argv); +int msg_from_argv(struct msg *, const char **argv); int msg_recv(int fd, struct msg *); int msg_send(int fd, const struct msg *); diff --git a/src/server.c b/src/server.c index f35b781..58833a1 100644 --- a/src/server.c +++ b/src/server.c @@ -114,7 +114,8 @@ static int worker_ci_run(int fd, const struct ci_queue_entry *ci_run) struct msg request, response; int ret = 0; - char *argv[] = {CMD_CI_RUN, ci_run->url, ci_run->rev, NULL}; + const char *argv[] = {CMD_CI_RUN, ci_queue_entry_get_url(ci_run), + ci_queue_entry_get_rev(ci_run), NULL}; ret = msg_from_argv(&request, argv); if (ret < 0) @@ -163,7 +164,7 @@ static int worker_dequeue_run(struct server *server, struct ci_queue_entry **ci_ } *ci_run = ci_queue_pop(&server->ci_queue); - log("Removed a CI run for repository %s from the queue\n", (*ci_run)->url); + log("Removed a CI run for repository %s from the queue\n", ci_queue_entry_get_url(*ci_run)); goto unlock; unlock: @@ -183,7 +184,7 @@ static int worker_requeue_run(struct server *server, struct ci_queue_entry *ci_r } ci_queue_push_head(&server->ci_queue, ci_run); - log("Requeued a CI run for repository %s\n", ci_run->url); + log("Requeued a CI run for repository %s\n", ci_queue_entry_get_url(ci_run)); ret = pthread_cond_signal(&server->server_cv); if (ret) { diff --git a/src/worker.c b/src/worker.c index 474be1c..cf57ad6 100644 --- a/src/worker.c +++ b/src/worker.c @@ -71,7 +71,7 @@ void worker_destroy(struct worker *worker) static int msg_send_new_worker(const struct worker *worker) { - static char *argv[] = {CMD_NEW_WORKER, NULL}; + static const char *argv[] = {CMD_NEW_WORKER, NULL}; struct msg msg; int ret = 0; |