|
| 1 | +/* |
| 2 | + Copyright 2025 Bloomberg Finance L.P. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <assert.h> |
| 18 | +#include <comdb2systblInt.h> |
| 19 | +#include <ezsystables.h> |
| 20 | +#include <bdb_api.h> |
| 21 | + |
| 22 | +typedef struct systable_unused_files { |
| 23 | + char *filename; |
| 24 | + int64_t reflog; |
| 25 | +} systable_unused_files_t; |
| 26 | + |
| 27 | +typedef struct getunusedfiles { |
| 28 | + int count; |
| 29 | + int alloc; |
| 30 | + systable_unused_files_t *records; |
| 31 | +} getunusedfiles_t; |
| 32 | + |
| 33 | +static int collect(void *args, int reflog, char *filename) |
| 34 | +{ |
| 35 | + getunusedfiles_t *a = (getunusedfiles_t *)args; |
| 36 | + systable_unused_files_t *u; |
| 37 | + a->count++; |
| 38 | + if (a->count >= a->alloc) { |
| 39 | + if (a->alloc == 0) a->alloc = 16; |
| 40 | + else a->alloc = a->alloc * 2; |
| 41 | + a->records = realloc(a->records, a->alloc * sizeof(systable_unused_files_t)); |
| 42 | + if (!a->records) { |
| 43 | + logmsg(LOGMSG_FATAL, "%s: realloc failed\n", __func__); |
| 44 | + abort(); |
| 45 | + } |
| 46 | + } |
| 47 | + u = &a->records[a->count - 1]; |
| 48 | + u->filename = strdup(filename); |
| 49 | + if (!u->filename) { |
| 50 | + logmsg(LOGMSG_FATAL, "%s: strdup failed\n", __func__); |
| 51 | + abort(); |
| 52 | + } |
| 53 | + u->reflog = reflog; |
| 54 | + return 0; |
| 55 | +} |
| 56 | + |
| 57 | +static int get_unused_files(void **data, int *records) |
| 58 | +{ |
| 59 | + getunusedfiles_t a = {0}; |
| 60 | + oldfile_hash_collect(collect, &a); |
| 61 | + *data = a.records; |
| 62 | + *records = a.count; |
| 63 | + return 0; |
| 64 | +} |
| 65 | + |
| 66 | +static void free_unused_files(void *p, int n) |
| 67 | +{ |
| 68 | + systable_unused_files_t *a, *begin = p; |
| 69 | + systable_unused_files_t *end = begin + n; |
| 70 | + for (a = begin; a < end; ++a) { |
| 71 | + free(a->filename); |
| 72 | + } |
| 73 | + free(p); |
| 74 | +} |
| 75 | + |
| 76 | +sqlite3_module systblUnusedFilesModule = { |
| 77 | + .access_flag = CDB2_ALLOW_USER |
| 78 | +}; |
| 79 | + |
| 80 | +int systblUnusedFilesInit(sqlite3 *db) { |
| 81 | + return create_system_table(db, "comdb2_unused_files", &systblUnusedFilesModule, |
| 82 | + get_unused_files, free_unused_files, sizeof(systable_unused_files_t), |
| 83 | + CDB2_CSTRING, "filename", -1, offsetof(systable_unused_files_t, filename), |
| 84 | + CDB2_INTEGER, "reflog", -1, offsetof(systable_unused_files_t, reflog), |
| 85 | + SYSTABLE_END_OF_FIELDS); |
| 86 | +} |
0 commit comments