GCC Code Coverage Report


Directory: src/
File: src/storage.c
Date: 2023-08-28 07:33:56
Exec Total Coverage
Lines: 33 44 75.0%
Branches: 8 18 44.4%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2022 Egor Tensin <Egor.Tensin@gmail.com>
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 "storage.h"
9 #include "log.h"
10 #include "process.h"
11 #include "run_queue.h"
12 #include "storage_sqlite.h"
13
14 #include <stddef.h>
15
16 typedef void (*storage_settings_destroy_t)(const struct storage_settings *);
17 typedef int (*storage_create_t)(struct storage *, const struct storage_settings *);
18 typedef void (*storage_destroy_t)(struct storage *);
19
20 typedef int (*storage_run_create_t)(struct storage *, const char *repo_url, const char *rev);
21 typedef int (*storage_run_finished_t)(struct storage *, int repo_id, const struct proc_output *);
22 typedef int (*storage_get_run_queue_t)(struct storage *, struct run_queue *);
23
24 struct storage_api {
25 storage_settings_destroy_t destroy_settings;
26 storage_create_t create;
27 storage_destroy_t destroy;
28
29 storage_run_create_t run_create;
30 storage_run_finished_t run_finished;
31 storage_get_run_queue_t get_run_queue;
32 };
33
34 static const struct storage_api apis[] = {
35 {
36 storage_sqlite_settings_destroy,
37 storage_sqlite_create,
38 storage_sqlite_destroy,
39
40 storage_sqlite_run_create,
41 storage_sqlite_run_finished,
42 storage_sqlite_get_run_queue,
43 },
44 };
45
46 18476 static size_t numof_apis(void)
47 {
48 18476 return sizeof(apis) / sizeof(apis[0]);
49 }
50
51 18476 static const struct storage_api *get_api(enum storage_type type)
52 {
53 if (type < 0)
54 goto invalid_type;
55
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18476 times.
18476 if ((size_t)type > numof_apis())
56 goto invalid_type;
57
58 18476 return &apis[type];
59
60 invalid_type:
61 log_err("Unsupported storage type: %d\n", type);
62 return NULL;
63 }
64
65 29 void storage_settings_destroy(const struct storage_settings *settings)
66 {
67 29 const struct storage_api *api = get_api(settings->type);
68
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (!api)
69 return;
70 29 api->destroy_settings(settings);
71 }
72
73 29 int storage_create(struct storage *storage, const struct storage_settings *settings)
74 {
75 29 int ret = 0;
76 29 const struct storage_api *api = get_api(settings->type);
77
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (!api)
78 return -1;
79 29 ret = api->create(storage, settings);
80
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (ret < 0)
81 return ret;
82 29 storage->type = settings->type;
83 29 return ret;
84 }
85
86 29 void storage_destroy(struct storage *storage)
87 {
88 29 const struct storage_api *api = get_api(storage->type);
89
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (!api)
90 return;
91 29 api->destroy(storage);
92 }
93
94 9180 int storage_run_create(struct storage *storage, const char *repo_url, const char *rev)
95 {
96 9180 const struct storage_api *api = get_api(storage->type);
97
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (!api)
98 return -1;
99 9180 return api->run_create(storage, repo_url, rev);
100 }
101
102 9180 int storage_run_finished(struct storage *storage, int run_id, const struct proc_output *output)
103 {
104 9180 const struct storage_api *api = get_api(storage->type);
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (!api)
106 return -1;
107 9180 return api->run_finished(storage, run_id, output);
108 }
109
110 29 int storage_get_run_queue(struct storage *storage, struct run_queue *queue)
111 {
112 29 const struct storage_api *api = get_api(storage->type);
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (!api)
114 return -1;
115 29 return api->get_run_queue(storage, queue);
116 }
117