]> git.proxmox.com Git - ceph.git/blame - ceph/src/pmdk/src/tools/pmempool/pmempool.c
import ceph 16.2.7
[ceph.git] / ceph / src / pmdk / src / tools / pmempool / pmempool.c
CommitLineData
a4b75251
TL
1// SPDX-License-Identifier: BSD-3-Clause
2/* Copyright 2014-2020, Intel Corporation */
3
4/*
5 * pmempool.c -- pmempool main source file
6 */
7
8#include <stdio.h>
9#include <libgen.h>
10#include <string.h>
11#include <unistd.h>
12#include <stdlib.h>
13#include <getopt.h>
14#include <stdbool.h>
15#include "common.h"
16#include "output.h"
17#include "info.h"
18#include "create.h"
19#include "dump.h"
20#include "check.h"
21#include "rm.h"
22#include "convert.h"
23#include "synchronize.h"
24#include "transform.h"
25#include "feature.h"
26#include "set.h"
27#include "pmemcommon.h"
28
29#ifndef _WIN32
30#include "rpmem_common.h"
31#include "rpmem_util.h"
32#endif
33
34#define APPNAME "pmempool"
35
36#define PMEMPOOL_TOOL_LOG_PREFIX "pmempool"
37#define PMEMPOOL_TOOL_LOG_LEVEL_VAR "PMEMPOOL_TOOL_LOG_LEVEL"
38#define PMEMPOOL_TOOL_LOG_FILE_VAR "PMEMPOOL_TOOL_LOG_FILE"
39
40/*
41 * command -- struct for pmempool commands definition
42 */
43struct command {
44 const char *name;
45 const char *brief;
46 int (*func)(const char *, int, char *[]);
47 void (*help)(const char *);
48};
49
50static const struct command *get_command(const char *cmd_str);
51static void print_help(const char *appname);
52
53/*
54 * long_options -- pmempool command line arguments
55 */
56static const struct option long_options[] = {
57 {"version", no_argument, NULL, 'V'},
58 {"help", no_argument, NULL, 'h'},
59 {NULL, 0, NULL, 0 },
60};
61
62/*
63 * help_help -- prints help message for help command
64 */
65static void
66help_help(const char *appname)
67{
68 printf("Usage: %s help <command>\n", appname);
69}
70
71/*
72 * help_func -- prints help message for specified command
73 */
74static int
75help_func(const char *appname, int argc, char *argv[])
76{
77 if (argc > 1) {
78 char *cmd_str = argv[1];
79 const struct command *cmdp = get_command(cmd_str);
80
81 if (cmdp && cmdp->help) {
82 cmdp->help(appname);
83 return 0;
84 } else {
85 outv_err("No help text for '%s' command\n", cmd_str);
86 return -1;
87 }
88 } else {
89 print_help(appname);
90 return -1;
91 }
92}
93
94/*
95 * commands -- definition of all pmempool commands
96 */
97static const struct command commands[] = {
98 {
99 .name = "info",
100 .brief = "print information and statistics about a pool",
101 .func = pmempool_info_func,
102 .help = pmempool_info_help,
103 },
104 {
105 .name = "create",
106 .brief = "create a pool",
107 .func = pmempool_create_func,
108 .help = pmempool_create_help,
109 },
110 {
111 .name = "dump",
112 .brief = "dump user data from a pool",
113 .func = pmempool_dump_func,
114 .help = pmempool_dump_help,
115 },
116 {
117 .name = "check",
118 .brief = "check consistency of a pool",
119 .func = pmempool_check_func,
120 .help = pmempool_check_help,
121 },
122 {
123 .name = "rm",
124 .brief = "remove pool or poolset",
125 .func = pmempool_rm_func,
126 .help = pmempool_rm_help,
127 },
128 {
129 .name = "convert",
130 .brief = "perform pool layout conversion",
131 .func = pmempool_convert_func,
132 .help = pmempool_convert_help,
133 },
134 {
135 .name = "sync",
136 .brief = "synchronize data between replicas",
137 .func = pmempool_sync_func,
138 .help = pmempool_sync_help,
139 },
140 {
141 .name = "transform",
142 .brief = "modify internal structure of a poolset",
143 .func = pmempool_transform_func,
144 .help = pmempool_transform_help,
145 },
146 {
147 .name = "feature",
148 .brief = "toggle / query pool features",
149 .func = pmempool_feature_func,
150 .help = pmempool_feature_help,
151 },
152 {
153 .name = "help",
154 .brief = "print help text about a command",
155 .func = help_func,
156 .help = help_help,
157 },
158};
159
160/*
161 * number of pmempool commands
162 */
163#define COMMANDS_NUMBER (sizeof(commands) / sizeof(commands[0]))
164
165/*
166 * print_version -- prints pmempool version message
167 */
168static void
169print_version(const char *appname)
170{
171 printf("%s %s\n", appname, SRCVERSION);
172}
173
174/*
175 * print_usage -- prints pmempool usage message
176 */
177static void
178print_usage(const char *appname)
179{
180 printf("usage: %s [--version] [--help] <command> [<args>]\n", appname);
181}
182
183/*
184 * print_help -- prints pmempool help message
185 */
186static void
187print_help(const char *appname)
188{
189 print_usage(appname);
190 print_version(appname);
191 printf("\n");
192 printf("Options:\n");
193 printf(" -V, --version display version\n");
194 printf(" -h, --help display this help and exit\n");
195 printf("\n");
196 printf("The available commands are:\n");
197 unsigned i;
198 for (i = 0; i < COMMANDS_NUMBER; i++) {
199 const char *format = (strlen(commands[i].name) / 8)
200 ? "%s\t- %s\n" : "%s\t\t- %s\n";
201 printf(format, commands[i].name, commands[i].brief);
202 }
203 printf("\n");
204 printf("For complete documentation see %s(1) manual page.\n", appname);
205}
206
207/*
208 * get_command -- returns command for specified command name
209 */
210static const struct command *
211get_command(const char *cmd_str)
212{
213 unsigned i;
214 for (i = 0; i < COMMANDS_NUMBER; i++) {
215 if (strcmp(cmd_str, commands[i].name) == 0)
216 return &commands[i];
217 }
218
219 return NULL;
220}
221
222int
223main(int argc, char *argv[])
224{
225 int opt;
226 int option_index;
227 int ret = 0;
228#ifdef _WIN32
229 util_suppress_errmsg();
230 wchar_t **wargv = CommandLineToArgvW(GetCommandLineW(), &argc);
231 for (int i = 0; i < argc; i++) {
232 argv[i] = util_toUTF8(wargv[i]);
233 if (argv[i] == NULL) {
234 for (i--; i >= 0; i--)
235 free(argv[i]);
236 outv_err("Error during arguments conversion\n");
237 return 1;
238 }
239 }
240#endif
241
242 common_init(PMEMPOOL_TOOL_LOG_PREFIX,
243 PMEMPOOL_TOOL_LOG_LEVEL_VAR,
244 PMEMPOOL_TOOL_LOG_FILE_VAR,
245 0 /* major version */,
246 0 /* minor version */);
247
248#ifndef _WIN32
249 util_remote_init();
250 rpmem_util_cmds_init();
251#endif
252
253 if (argc < 2) {
254 print_usage(APPNAME);
255 goto end;
256 }
257
258 while ((opt = getopt_long(2, argv, "Vh",
259 long_options, &option_index)) != -1) {
260 switch (opt) {
261 case 'V':
262 print_version(APPNAME);
263 goto end;
264 case 'h':
265 print_help(APPNAME);
266 goto end;
267 default:
268 print_usage(APPNAME);
269 ret = 1;
270 goto end;
271 }
272 }
273
274 char *cmd_str = argv[optind];
275
276 const struct command *cmdp = get_command(cmd_str);
277
278 if (cmdp) {
279 ret = cmdp->func(APPNAME, argc - 1, argv + 1);
280 } else {
281 outv_err("'%s' -- unknown command\n", cmd_str);
282 ret = 1;
283 }
284
285end:
286
287#ifndef _WIN32
288 util_remote_fini();
289 rpmem_util_cmds_fini();
290#endif
291
292 common_fini();
293
294#ifdef _WIN32
295 for (int i = argc; i > 0; i--)
296 free(argv[i - 1]);
297#endif
298 if (ret)
299 return 1;
300
301 return 0;
302}