-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplinter_cli_cmd_init.c
More file actions
111 lines (95 loc) · 3.15 KB
/
splinter_cli_cmd_init.c
File metadata and controls
111 lines (95 loc) · 3.15 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
/**
* 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 <stdalign.h>
#include "config.h"
#include "splinter.h"
#include "splinter_cli.h"
#include "argparse.h"
static const char *modname = "init";
static const char *const usages[] = {
"init [options] [[--] args]",
"init [options]",
NULL,
};
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;
}
int cmd_init(int argc, char *argv[])
{
char save[64] = {0}, store[64] = {0};
int rc = 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;
}
struct argparse_option options[] = {
OPT_HELP(),
OPT_INTEGER('s', "slots", &max_slots, "Maximum Slots", NULL, 0, 0),
OPT_INTEGER('l', "length", &max_val, "Maximum Value Length", NULL, 0, 0),
OPT_END(),
};
struct argparse argparse;
argparse_init(&argparse, options, usages, 0);
argparse_describe(&argparse, "\nInitialize a store", "\nCreates a new store to the specified geometry.");
argc = argparse_parse(&argparse, argc, (const char **)argv);
if (argc != 0)
snprintf(store, sizeof(store) - 1, "%s", argv[argc-1]);
if (!store[0])
snprintf(store, sizeof(store) - 1, DEFAULT_BUS);
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;
}