Directory: | src/ |
---|---|
File: | src/storage.c |
Date: | 2024-04-25 03:45:42 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 37 | 49 | 75.5% |
Branches: | 9 | 20 | 45.0% |
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 "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 | |||
23 | typedef int (*storage_get_runs_t)(struct storage *, struct run_queue *); | ||
24 | typedef storage_get_runs_t storage_get_run_queue_t; | ||
25 | |||
26 | struct storage_api { | ||
27 | storage_settings_destroy_t destroy_settings; | ||
28 | storage_create_t create; | ||
29 | storage_destroy_t destroy; | ||
30 | |||
31 | storage_run_create_t run_create; | ||
32 | storage_run_finished_t run_finished; | ||
33 | |||
34 | storage_get_runs_t get_runs; | ||
35 | storage_get_run_queue_t get_run_queue; | ||
36 | }; | ||
37 | |||
38 | static const struct storage_api apis[] = { | ||
39 | { | ||
40 | storage_sqlite_settings_destroy, | ||
41 | storage_sqlite_create, | ||
42 | storage_sqlite_destroy, | ||
43 | |||
44 | storage_sqlite_run_create, | ||
45 | storage_sqlite_run_finished, | ||
46 | |||
47 | storage_sqlite_get_runs, | ||
48 | storage_sqlite_get_run_queue, | ||
49 | }, | ||
50 | }; | ||
51 | |||
52 | 18502 | static size_t numof_apis(void) | |
53 | { | ||
54 | 18502 | return sizeof(apis) / sizeof(apis[0]); | |
55 | } | ||
56 | |||
57 | 18502 | static const struct storage_api *get_api(enum storage_type type) | |
58 | { | ||
59 | if (type < 0) | ||
60 | goto invalid_type; | ||
61 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 18502 times.
|
18502 | if ((size_t)type > numof_apis()) |
62 | ✗ | goto invalid_type; | |
63 | |||
64 | 18502 | return &apis[type]; | |
65 | |||
66 | ✗ | invalid_type: | |
67 | ✗ | log_err("Unsupported storage type: %d\n", type); | |
68 | ✗ | return NULL; | |
69 | } | ||
70 | |||
71 | 29 | void storage_settings_destroy(const struct storage_settings *settings) | |
72 | { | ||
73 | 29 | const struct storage_api *api = get_api(settings->type); | |
74 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
|
29 | if (!api) |
75 | ✗ | return; | |
76 | 29 | api->destroy_settings(settings); | |
77 | } | ||
78 | |||
79 | 29 | int storage_create(struct storage *storage, const struct storage_settings *settings) | |
80 | { | ||
81 | 29 | int ret = 0; | |
82 | 29 | const struct storage_api *api = get_api(settings->type); | |
83 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
|
29 | if (!api) |
84 | ✗ | return -1; | |
85 | 29 | ret = api->create(storage, settings); | |
86 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
|
29 | if (ret < 0) |
87 | ✗ | return ret; | |
88 | 29 | storage->type = settings->type; | |
89 | 29 | return ret; | |
90 | } | ||
91 | |||
92 | 29 | void storage_destroy(struct storage *storage) | |
93 | { | ||
94 | 29 | const struct storage_api *api = get_api(storage->type); | |
95 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
|
29 | if (!api) |
96 | ✗ | return; | |
97 | 29 | api->destroy(storage); | |
98 | } | ||
99 | |||
100 | 9180 | int storage_run_create(struct storage *storage, const char *repo_url, const char *rev) | |
101 | { | ||
102 | 9180 | const struct storage_api *api = get_api(storage->type); | |
103 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
|
9180 | if (!api) |
104 | ✗ | return -1; | |
105 | 9180 | return api->run_create(storage, repo_url, rev); | |
106 | } | ||
107 | |||
108 | 9180 | int storage_run_finished(struct storage *storage, int run_id, const struct proc_output *output) | |
109 | { | ||
110 | 9180 | const struct storage_api *api = get_api(storage->type); | |
111 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
|
9180 | if (!api) |
112 | ✗ | return -1; | |
113 | 9180 | return api->run_finished(storage, run_id, output); | |
114 | } | ||
115 | |||
116 | 26 | int storage_get_runs(struct storage *storage, struct run_queue *queue) | |
117 | { | ||
118 | 26 | const struct storage_api *api = get_api(storage->type); | |
119 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
|
26 | if (!api) |
120 | ✗ | return -1; | |
121 | 26 | return api->get_runs(storage, queue); | |
122 | } | ||
123 | |||
124 | 29 | int storage_get_run_queue(struct storage *storage, struct run_queue *queue) | |
125 | { | ||
126 | 29 | const struct storage_api *api = get_api(storage->type); | |
127 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
|
29 | if (!api) |
128 | ✗ | return -1; | |
129 | 29 | return api->get_run_queue(storage, queue); | |
130 | } | ||
131 |