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