GCC Code Coverage Report


Directory: src/
File: src/run_queue.c
Date: 2024-04-25 03:45:42
Exec Total Coverage
Lines: 81 110 73.6%
Branches: 17 36 47.2%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2022 Egor Tensin <egor@tensin.name>
3 * This file is part of the "cimple" project.
4 * For details, see https://github.com/egor-tensin/cimple.
5 * Distributed under the MIT License.
6 */
7
8 #include "run_queue.h"
9 #include "json.h"
10 #include "log.h"
11
12 #include <json-c/json_object.h>
13
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/queue.h>
17
18 struct run {
19 int id;
20 char *repo_url;
21 char *repo_rev;
22 int status;
23 int exit_code;
24
25 SIMPLEQ_ENTRY(run) entries;
26 };
27
28 36720 int run_new(struct run **_entry, int id, const char *_repo_url, const char *_repo_rev,
29 enum run_status status, int exit_code)
30 {
31 36720 struct run *entry = malloc(sizeof(struct run));
32
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36720 times.
36720 if (!entry) {
33 log_errno("malloc");
34 goto fail;
35 }
36
37 36720 char *repo_url = strdup(_repo_url);
38
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36720 times.
36720 if (!repo_url) {
39 log_errno("strdup");
40 goto free_entry;
41 }
42
43 36720 char *repo_rev = strdup(_repo_rev);
44
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36720 times.
36720 if (!repo_rev) {
45 log_errno("strdup");
46 goto free_repo_url;
47 }
48
49 36720 entry->id = id;
50 36720 entry->repo_url = repo_url;
51 36720 entry->repo_rev = repo_rev;
52 36720 entry->status = status;
53 36720 entry->exit_code = exit_code;
54
55 36720 *_entry = entry;
56 36720 return 0;
57
58 free_repo_url:
59 free(repo_url);
60
61 free_entry:
62 free(entry);
63
64 fail:
65 return -1;
66 }
67
68 36720 void run_destroy(struct run *entry)
69 {
70 36720 free(entry->repo_rev);
71 36720 free(entry->repo_url);
72 36720 free(entry);
73 36720 }
74
75 18360 int run_queued(struct run **entry, const char *repo_url, const char *repo_rev)
76 {
77 18360 return run_new(entry, -1, repo_url, repo_rev, RUN_STATUS_CREATED, -1);
78 }
79
80 9180 int run_created(struct run **entry, int id, const char *repo_url, const char *repo_rev)
81 {
82 9180 return run_new(entry, id, repo_url, repo_rev, RUN_STATUS_CREATED, -1);
83 }
84
85 9180 int run_to_json(const struct run *entry, struct json_object **_json)
86 {
87 9180 struct json_object *json = NULL;
88 9180 int ret = 0;
89
90 9180 ret = libjson_new_object(&json);
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (ret < 0)
92 return -1;
93 9180 ret = libjson_set_int_const_key(json, "id", entry->id);
94
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (ret < 0)
95 goto free;
96 9180 ret = libjson_set_int_const_key(json, "exit_code", entry->exit_code);
97
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (ret < 0)
98 goto free;
99 9180 ret = libjson_set_string_const_key(json, "repo_url", entry->repo_url);
100
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (ret < 0)
101 goto free;
102 9180 ret = libjson_set_string_const_key(json, "repo_rev", entry->repo_rev);
103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (ret < 0)
104 goto free;
105
106 9180 *_json = json;
107 9180 return ret;
108
109 free:
110 libjson_free(json);
111
112 return ret;
113 }
114
115 45900 int run_get_id(const struct run *entry)
116 {
117 45900 return entry->id;
118 }
119
120 64260 const char *run_get_repo_url(const struct run *entry)
121 {
122 64260 return entry->repo_url;
123 }
124
125 36720 const char *run_get_repo_rev(const struct run *entry)
126 {
127 36720 return entry->repo_rev;
128 }
129
130 9180 void run_set_id(struct run *entry, int id)
131 {
132 9180 entry->id = id;
133 9180 }
134
135 55 void run_queue_create(struct run_queue *queue)
136 {
137 55 SIMPLEQ_INIT(queue);
138 55 }
139
140 55 void run_queue_destroy(struct run_queue *queue)
141 {
142 55 struct run *entry1 = SIMPLEQ_FIRST(queue);
143
2/2
✓ Branch 0 taken 9180 times.
✓ Branch 1 taken 55 times.
9235 while (entry1) {
144 9180 struct run *entry2 = SIMPLEQ_NEXT(entry1, entries);
145 9180 run_destroy(entry1);
146 9180 entry1 = entry2;
147 }
148 55 SIMPLEQ_INIT(queue);
149 55 }
150
151 26 int run_queue_to_json(const struct run_queue *queue, struct json_object **_json)
152 {
153 26 struct json_object *json = NULL;
154 26 int ret = 0;
155
156 26 ret = libjson_new_array(&json);
157
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (ret < 0)
158 return ret;
159
160 26 struct run *entry = NULL;
161
2/2
✓ Branch 0 taken 9180 times.
✓ Branch 1 taken 26 times.
9206 SIMPLEQ_FOREACH(entry, queue, entries)
162 {
163 9180 struct json_object *entry_json = NULL;
164 9180 ret = run_to_json(entry, &entry_json);
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (ret < 0)
166 goto free;
167
168 9180 ret = libjson_append(json, entry_json);
169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (ret < 0) {
170 libjson_free(entry_json);
171 goto free;
172 }
173 }
174
175 26 *_json = json;
176 26 return ret;
177
178 free:
179 libjson_free(json);
180
181 return ret;
182 }
183
184 27472 int run_queue_is_empty(const struct run_queue *queue)
185 {
186 27472 return SIMPLEQ_EMPTY(queue);
187 }
188
189 void run_queue_add_first(struct run_queue *queue, struct run *entry)
190 {
191 SIMPLEQ_INSERT_HEAD(queue, entry, entries);
192 }
193
194 18360 void run_queue_add_last(struct run_queue *queue, struct run *entry)
195 {
196 18360 SIMPLEQ_INSERT_TAIL(queue, entry, entries);
197 18360 }
198
199 9180 struct run *run_queue_remove_first(struct run_queue *queue)
200 {
201 9180 struct run *entry = SIMPLEQ_FIRST(queue);
202
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 9102 times.
9180 SIMPLEQ_REMOVE_HEAD(queue, entries);
203 9180 return entry;
204 }
205