-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplinter_cli_cmd_init.c
More file actions
123 lines (102 loc) · 3.33 KB
/
splinter_cli_cmd_init.c
File metadata and controls
123 lines (102 loc) · 3.33 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
/**
* Copyright 2025 Tim Post
* License: Apache 2 (MIT available upon request to timthepost@protonmail.com)
*
* @file splinter_cli_cmd_init.c
* @brief Implements the CLI 'init' command.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <getopt.h>
#include <stdalign.h>
#include "config.h"
#include "splinter.h"
#include "splinter_cli.h"
static const char *modname = "init";
void help_cmd_init(unsigned int level) {
(void) level;
printf("Usage: %s [store_name] [--slots num_slots] [--maxlen max_val_len]\n", modname);
printf("%s creates a Splinter store to default or specific geometry.\n", modname);
puts("If arguments are omitted, these compiled-in defaults are used:");
printf("\nname: %s\nslots: %lu\nmaxlen: %lu\nalignment: %zu\n",
DEFAULT_BUS,
(unsigned long) DEFAULT_SLOTS,
(unsigned long) DEFAULT_VAL_MAXLEN,
alignof(struct splinter_slot)
);
return;
}
static const struct option long_options[] = {
{ "help", no_argument, NULL, 'h' },
{ "slots", required_argument, NULL, 's' },
{ "maxlen", required_argument, NULL, 'l' },
{ NULL, 0, NULL, 0 }
};
static const char *optstring = "hs:l:";
int cmd_init(int argc, char *argv[]) {
char *buff = NULL, save[64] = { 0 }, store[64] = { 0 };
int rc = 0, opt = 0;
unsigned int prev_conn = 0;
unsigned long max_slots = DEFAULT_SLOTS, max_val = DEFAULT_VAL_MAXLEN;
if (thisuser.store_conn) {
strncpy(save, thisuser.store, 64);
prev_conn = 1;
}
optind = 0;
while ((opt = getopt_long(argc, argv, optstring, long_options, NULL)) != -1) {
switch (opt) {
case 'h':
case '?':
help_cmd_init(1);
rc = 0;
goto restore_conn;
break;
case 'o':
max_slots = strtoul(optarg, &buff, 10);
break;
case 'l':
max_val = strtoul(optarg, &buff, 10);
break;
}
}
if (optind < argc)
snprintf(store, sizeof(store) -1, "%s", argv[optind++]);
if (! store[0])
snprintf(store, sizeof(store) - 1, DEFAULT_BUS);
// Inside cmd_init after parsing options
size_t slot_sz = sizeof(struct splinter_slot);
size_t arena_sz = max_slots * max_val;
size_t total_est = sizeof(struct splinter_header) + (max_slots * slot_sz) + arena_sz;
printf("Initializing store: %s\n", store);
printf(" - Slots: %lu (%zu bytes each, %zu byte alignment)\n",
max_slots,
max_val,
alignof(struct splinter_slot));
printf(" - Value Arena: %lu bytes, SRS: %zu bytes (~%.2f MB)\n",
arena_sz,
total_est,
(double) total_est / 1048576.0);
rc = splinter_create(store, max_slots, max_val);
if (rc < 0)
perror("splinter_create");
splinter_close();
goto restore_conn;
restore_conn:
if (prev_conn) {
rc = splinter_open(save);
if (rc != 0) {
perror("splinter_open");
fprintf(stderr,
"warning: could not re-attach to %s, did something else remove it?",
save);
thisuser.store_conn = 0;
return rc;
} else {
strncpy(thisuser.store, save, 64);
thisuser.store_conn = 1;
}
}
return rc;
}