-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplinter_cli_cmd_export.c
More file actions
182 lines (151 loc) · 5.04 KB
/
splinter_cli_cmd_export.c
File metadata and controls
182 lines (151 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* Copyright 2025 Tim Post
* License: Apache 2 (MIT available upon request to timthepost@protonmail.com)
*
* @file splinter_cli_cmd_export.c
* @brief Implements the CLI 'export' command.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "splinter_cli.h"
#include "3rdparty/grawk.h"
static const char *modname = "export";
static splinter_header_snapshot_t snap = {0};
void help_cmd_export(unsigned int level) {
(void) level;
printf("%s exports the store in various formats to standard output.\n", modname);
printf("Usage: %s [format (default=json)] [max_lines (default=0/unlimited)]\n", modname);
printf("Format can be one of: json (more coming soon)\n");
return;
}
static int compare_slots_by_epoch(const void *a, const void *b) {
const splinter_slot_snapshot_t *slot_a = (const splinter_slot_snapshot_t *)a;
const splinter_slot_snapshot_t *slot_b = (const splinter_slot_snapshot_t *)b;
// Descending order: b - a
if (slot_b->epoch > slot_a->epoch) return 1;
if (slot_b->epoch < slot_a->epoch) return -1;
return 0;
}
/**
* @brief Prints slot snapshots in JSON format
* @param slots Sorted array of slot snapshots
* @param slot_count Number of valid slots in the array
* @param snap Pointer to bus header snapshot
*/
static void print_json(const splinter_slot_snapshot_t *slots, size_t slot_count,
const splinter_header_snapshot_t *snap) {
size_t i;
printf("{\n");
printf(" \"store\": {\n");
printf(" \"total_slots\": %u,\n", snap->slots);
printf(" \"active_keys\": %zu\n", slot_count);
printf(" },\n");
printf(" \"keys\": [\n");
for (i = 0; i < slot_count; i++) {
// Skip empty/invalid entries
if (slots[i].epoch == 0) {
continue;
}
printf(" {\n");
printf(" \"key\": \"%s\",\n", slots[i].key);
printf(" \"type\": \"%s\",\n", cli_show_key_type(slots[i].type_flag));
printf(" \"epoch\": %lu,\n", slots[i].epoch);
printf(" \"value_length\": %u\n", slots[i].val_len);
// Add comma unless this is the last entry
if (i < slot_count - 1 && slots[i + 1].epoch > 0) {
printf(" },\n");
} else {
printf(" }\n");
}
}
printf(" ]\n");
printf("}\n");
}
int cmd_export(int argc, char *argv[]) {
grawk_t *g = NULL;
awk_pat_t *filter = NULL;
grawk_opts_t opts = {
.ignore_case = 0,
.invert_match = 0,
.quiet = 1
};
splinter_slot_snapshot_t *slots = NULL;
char **keynames = NULL;
size_t entry_count = 0;
size_t max_keys = 0;
int rc = -1, i, x = 0;
if (argc > 2) {
help_cmd_list(1);
return -1;
}
// Get header snapshot to determine allocation size
splinter_get_header_snapshot(&snap);
max_keys = snap.slots;
if (max_keys == 0) {
fprintf(stderr, "%s: no slots available in current store.\n", modname);
return -1;
}
keynames = (char **)calloc(max_keys, sizeof(char *));
if (keynames == NULL) {
fprintf(stderr, "%s: unable to allocate memory for key names.\n", modname);
errno = ENOMEM;
return -1;
}
slots = (splinter_slot_snapshot_t *)calloc(max_keys, sizeof(splinter_slot_snapshot_t));
if (slots == NULL) {
fprintf(stderr, "%s: unable to allocate memory for slot snapshots.\n", modname);
errno = ENOMEM;
free(keynames);
return -1;
}
rc = splinter_list(keynames, max_keys, &entry_count);
if (rc == 0) {
g = grawk_init();
if (g == NULL) {
fprintf(stderr, "%s: unable to allocate memory to filter keys.\n", modname);
errno = ENOMEM;
rc = -1;
goto cleanup;
}
grawk_set_options(g, &opts);
if (argc == 2) {
filter = grawk_build_pattern(argv[1]);
grawk_set_pattern(g, filter);
}
for (i = 0; keynames[i]; i++) {
if (keynames[i][0] != '\0') {
// if there's no filter, just add it
if (filter == NULL) {
splinter_get_slot_snapshot(keynames[i], &slots[x]);
x++;
} else {
// only add if filter matches
if (grawk_match(g, keynames[i])) {
splinter_get_slot_snapshot(keynames[i], &slots[x]);
x++;
}
}
}
}
qsort(slots, x, sizeof(splinter_slot_snapshot_t), compare_slots_by_epoch);
// TODO: Other formats / arguments
print_json(slots, x, &snap);
// Empty line is intentional (and uniform throughout commands)
puts("");
rc = 0;
}
cleanup:
// Free grawk resources
if (g != NULL) {
grawk_free(g);
}
if (slots != NULL) {
free(slots);
}
if (keynames != NULL) {
free(keynames);
}
return rc;
}