Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2016, Citrix Systems, Inc.
3 : : *
4 : : * All rights reserved.
5 : : *
6 : : * Redistribution and use in source and binary forms, with or without
7 : : * modification, are permitted provided that the following conditions are met:
8 : : *
9 : : * 1. Redistributions of source code must retain the above copyright
10 : : * notice, this list of conditions and the following disclaimer.
11 : : * 2. Redistributions in binary form must reproduce the above copyright
12 : : * notice, this list of conditions and the following disclaimer in the
13 : : * documentation and/or other materials provided with the distribution.
14 : : * 3. Neither the name of the copyright holder nor the names of its
15 : : * contributors may be used to endorse or promote products derived from
16 : : * this software without specific prior written permission.
17 : : *
18 : : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 : : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 : : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 : : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
22 : : * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 : : * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 : : * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 : : * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 : : * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 : : * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 : : * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 : : */
30 : :
31 : : #ifdef HAVE_CONFIG_H
32 : : #include "config.h"
33 : : #endif
34 : :
35 : : #include <errno.h>
36 : : #include <stdio.h>
37 : : #include <stdlib.h>
38 : : #include <string.h>
39 : :
40 : : #include "libvhd.h"
41 : : #include "vhd-util.h"
42 : :
43 : : #if 1
44 : : #define DFPRINTF(_f, _a...) fprintf(stdout, _f , ##_a)
45 : : #else
46 : : #define DFPRINTF(_f, _a...) ((void)0)
47 : : #endif
48 : :
49 : : typedef int (*vhd_util_func_t) (int, char **);
50 : :
51 : : struct command {
52 : : char *name;
53 : : vhd_util_func_t func;
54 : : };
55 : :
56 : : struct command commands[] = {
57 : : { .name = "create", .func = vhd_util_create },
58 : : { .name = "snapshot", .func = vhd_util_snapshot },
59 : : { .name = "query", .func = vhd_util_query },
60 : : { .name = "read", .func = vhd_util_read },
61 : : { .name = "set", .func = vhd_util_set_field },
62 : : { .name = "repair", .func = vhd_util_repair },
63 : : { .name = "resize", .func = vhd_util_resize },
64 : : { .name = "fill", .func = vhd_util_fill },
65 : : { .name = "coalesce", .func = vhd_util_coalesce },
66 : : { .name = "modify", .func = vhd_util_modify },
67 : : { .name = "scan", .func = vhd_util_scan },
68 : : { .name = "check", .func = vhd_util_check },
69 : : { .name = "revert", .func = vhd_util_revert },
70 : : { .name = "key", .func = vhd_util_key },
71 : : { .name = "copy", .func = vhd_util_copy },
72 : : };
73 : :
74 : : #define print_commands() \
75 : : do { \
76 : : int i, n; \
77 : : n = sizeof(commands) / sizeof(struct command); \
78 : : printf("COMMAND := { "); \
79 : : printf("%s", commands[0].name); \
80 : : for (i = 1; i < n; i++) \
81 : : printf(" | %s", commands[i].name); \
82 : : printf(" }\n"); \
83 : : } while (0)
84 : :
85 : : TEST_FAIL_EXTERN_VARS;
86 : :
87 : : void
88 : 0 : help(void)
89 : : {
90 : : printf("usage: vhd-util COMMAND [OPTIONS]\n");
91 : 0 : print_commands();
92 : 0 : exit(0);
93 : : }
94 : :
95 : : struct command *
96 : 0 : get_command(char *command)
97 : : {
98 : : int i, n;
99 : :
100 : 0 : if (strnlen(command, 25) >= 25)
101 : : return NULL;
102 : :
103 : : n = sizeof(commands) / sizeof (struct command);
104 : :
105 : 0 : for (i = 0; i < n; i++)
106 : 0 : if (!strcmp(command, commands[i].name))
107 : 0 : return &commands[i];
108 : :
109 : : return NULL;
110 : : }
111 : :
112 : : int
113 : 0 : main(int argc, char *argv[])
114 : : {
115 : : char **cargv;
116 : : struct command *cmd;
117 : : int cargc, i, cnt, ret;
118 : :
119 : : #ifdef CORE_DUMP
120 : : #include <sys/resource.h>
121 : : struct rlimit rlim;
122 : : rlim.rlim_cur = RLIM_INFINITY;
123 : : rlim.rlim_max = RLIM_INFINITY;
124 : : if (setrlimit(RLIMIT_CORE, &rlim) < 0)
125 : : fprintf(stderr, "setrlimit failed: %d\n", errno);
126 : : #endif
127 : :
128 : 0 : ret = 0;
129 : :
130 : 0 : if (argc < 2)
131 : 0 : help();
132 : :
133 : 0 : cargc = argc - 1;
134 : 0 : cmd = get_command(argv[1]);
135 : 0 : if (!cmd) {
136 : 0 : fprintf(stderr, "invalid COMMAND %s\n", argv[1]);
137 : 0 : help();
138 : : }
139 : :
140 : 0 : cargv = malloc(sizeof(char *) * cargc);
141 : 0 : if (!cargv)
142 : 0 : exit(ENOMEM);
143 : :
144 : 0 : cnt = 1;
145 : 0 : cargv[0] = cmd->name;
146 : 0 : for (i = 1; i < cargc; i++) {
147 : 0 : char *arg = argv[i + (argc - cargc)];
148 : :
149 : 0 : if (!strcmp(arg, "--debug")) {
150 : 0 : libvhd_set_log_level(1);
151 : 0 : continue;
152 : : }
153 : :
154 : 0 : cargv[cnt++] = arg;
155 : : }
156 : :
157 : : #ifdef ENABLE_FAILURE_TESTING
158 : 0 : for (i = 0; i < NUM_FAIL_TESTS; i++) {
159 : 0 : TEST_FAIL[i] = 0;
160 : 0 : if (getenv(ENV_VAR_FAIL[i]))
161 : 0 : TEST_FAIL[i] = 1;
162 : : }
163 : : #endif // ENABLE_FAILURE_TESTING
164 : :
165 : 0 : ret = cmd->func(cnt, cargv);
166 : :
167 : 0 : free(cargv);
168 : :
169 : 0 : return (ret >= 0 ? ret : -ret);
170 : : }
|