source: mainline/uspace/app/vol/vol.c@ 21a65cca

topic/simplify-dev-export
Last change on this file since 21a65cca was b82985e, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Inserting volume by path.

  • Property mode set to 100644
File size: 8.5 KB
RevLine 
[db9c889]1/*
2 * Copyright (c) 2017 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - 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 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup vol
30 * @{
31 */
32/** @file Volume administration (interface to volsrv).
33 */
34
35#include <errno.h>
36#include <io/table.h>
37#include <loc.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <str.h>
41#include <vfs/vfs.h>
42#include <vol.h>
43
44#define NAME "vol"
45
46typedef enum {
47 vcmd_eject,
[f0f8787]48 vcmd_insert,
[b82985e]49 vcmd_insert_by_path,
[db9c889]50 vcmd_help,
51 vcmd_list,
[63c1dd5]52 vcmd_cfglist,
[db9c889]53} vol_cmd_t;
54
[72c72d4]55/** Find volume by current mount point. */
56static errno_t vol_cmd_part_by_mp(vol_t *vol, const char *mp,
57 service_id_t *rid)
58{
59 vol_part_info_t vinfo;
60 service_id_t *part_ids = NULL;
61 char *canon_mp_buf = NULL;
62 char *canon_mp;
63 size_t nparts;
64 size_t i;
65 errno_t rc;
66
67 canon_mp_buf = str_dup(mp);
68 if (canon_mp_buf == NULL) {
69 printf("Out of memory.\n");
70 rc = ENOMEM;
71 goto out;
72 }
73
74 canon_mp = vfs_absolutize(canon_mp_buf, NULL);
75 if (canon_mp == NULL) {
76 printf("Invalid volume path '%s'.\n", mp);
77 rc = EINVAL;
78 goto out;
79 }
80
81 rc = vol_get_parts(vol, &part_ids, &nparts);
82 if (rc != EOK) {
83 printf("Error getting list of volumes.\n");
84 goto out;
85 }
86
87 for (i = 0; i < nparts; i++) {
88 rc = vol_part_info(vol, part_ids[i], &vinfo);
89 if (rc != EOK) {
90 printf("Error getting volume information.\n");
91 rc = EIO;
92 goto out;
93 }
94
95 if (str_cmp(vinfo.cur_mp, canon_mp) == 0) {
96 *rid = part_ids[i];
97 rc = EOK;
98 goto out;
99 }
100 }
101
102 rc = ENOENT;
103out:
104 free(part_ids);
105 free(canon_mp_buf);
106 return rc;
107}
108
109static errno_t vol_cmd_eject(const char *volspec)
110{
111 vol_t *vol = NULL;
112 service_id_t part_id;
113 errno_t rc;
114
115 rc = vol_create(&vol);
116 if (rc != EOK) {
117 printf("Error contacting volume service.\n");
118 goto out;
119 }
120
121 rc = vol_cmd_part_by_mp(vol, volspec, &part_id);
122 if (rc != EOK) {
123 printf("Error looking up volume '%s'.\n", volspec);
124 goto out;
125 }
126
127 rc = vol_part_eject(vol, part_id);
128 if (rc != EOK) {
129 printf("Error ejecting volume.\n");
130 goto out;
131 }
132
133 rc = EOK;
134out:
135 vol_destroy(vol);
136 return rc;
137}
138
[f0f8787]139static errno_t vol_cmd_insert(const char *volspec)
140{
141 vol_t *vol = NULL;
142 service_id_t svc_id;
143 errno_t rc;
144
145 rc = loc_service_get_id(volspec, &svc_id, 0);
146 if (rc != EOK) {
147 printf("Error looking up service '%s'.\n", volspec);
148 goto out;
149 }
150
151 rc = vol_create(&vol);
152 if (rc != EOK) {
153 printf("Error contacting volume service.\n");
154 goto out;
155 }
156
157 rc = vol_part_insert(vol, svc_id);
158 if (rc != EOK) {
159 printf("Error inserting volume.\n");
160 goto out;
161 }
162
163 rc = EOK;
164out:
165 vol_destroy(vol);
166 return rc;
167}
168
[b82985e]169static errno_t vol_cmd_insert_by_path(const char *volspec)
170{
171 vol_t *vol = NULL;
172 errno_t rc;
173
174 rc = vol_create(&vol);
175 if (rc != EOK) {
176 printf("Error contacting volume service.\n");
177 goto out;
178 }
179
180 rc = vol_part_insert_by_path(vol, volspec);
181 if (rc != EOK) {
182 printf("Error inserting volume.\n");
183 goto out;
184 }
185
186 rc = EOK;
187out:
188 vol_destroy(vol);
189 return rc;
190}
191
[72c72d4]192static errno_t vol_cmd_list(void)
[db9c889]193{
194 vol_t *vol = NULL;
195 vol_part_info_t vinfo;
196 service_id_t *part_ids = NULL;
197 char *svc_name;
198 char *sfstype;
199 size_t nparts;
200 size_t i;
201 table_t *table = NULL;
[72c72d4]202 errno_t rc;
[db9c889]203
204 rc = vol_create(&vol);
205 if (rc != EOK) {
206 printf("Error contacting volume service.\n");
207 goto out;
208 }
209
210 rc = vol_get_parts(vol, &part_ids, &nparts);
211 if (rc != EOK) {
212 printf("Error getting list of volumes.\n");
213 goto out;
214 }
215
216 rc = table_create(&table);
217 if (rc != EOK) {
218 printf("Out of memory.\n");
219 goto out;
220 }
221
222 table_header_row(table);
223 table_printf(table, "Volume Name\t" "Resource\t" "Content\t" "Auto\t"
224 "Mounted at\n");
225
226 for (i = 0; i < nparts; i++) {
227 rc = vol_part_info(vol, part_ids[i], &vinfo);
228 if (rc != EOK) {
229 printf("Error getting volume information.\n");
230 return EIO;
231 }
232
233 rc = loc_service_get_name(part_ids[i], &svc_name);
234 if (rc != EOK) {
235 printf("Error getting service name.\n");
236 return EIO;
237 }
238
239 rc = vol_pcnt_fs_format(vinfo.pcnt, vinfo.fstype, &sfstype);
240 if (rc != EOK) {
241 printf("Out of memory.\n");
242 free(svc_name);
243 return ENOMEM;
244 }
245
[1a9174e]246 table_printf(table, "%s\t" "%s\t" "%s\t" "%s\t" "%s\n",
247 vinfo.label, svc_name, sfstype,
248 vinfo.cur_mp_auto ? "Yes" : "", vinfo.cur_mp);
[db9c889]249
250 free(svc_name);
251 free(sfstype);
252 svc_name = NULL;
253 }
254
255 rc = table_print_out(table, stdout);
256 if (rc != EOK)
257 printf("Error printing table.\n");
258out:
259 table_destroy(table);
260 vol_destroy(vol);
261 free(part_ids);
262
263 return rc;
264}
265
[63c1dd5]266/** List volume configuration entries.
267 *
268 * @return EOK on success or an error code
269 */
270static errno_t vol_cmd_cfglist(void)
271{
272 vol_t *vol = NULL;
273 vol_info_t vinfo;
274 volume_id_t *volume_ids = NULL;
275 size_t nvols;
276 size_t i;
277 table_t *table = NULL;
278 errno_t rc;
279
280 rc = vol_create(&vol);
281 if (rc != EOK) {
282 printf("Error contacting volume service.\n");
283 goto out;
284 }
285
286 rc = vol_get_volumes(vol, &volume_ids, &nvols);
287 if (rc != EOK) {
288 printf("Error getting list of volumes.\n");
289 goto out;
290 }
291
292 rc = table_create(&table);
293 if (rc != EOK) {
294 printf("Out of memory.\n");
295 goto out;
296 }
297
298 table_header_row(table);
299 table_printf(table, "Volume Name\t" "Path\n");
300
301 for (i = 0; i < nvols; i++) {
302 rc = vol_info(vol, volume_ids[i], &vinfo);
303 if (rc != EOK) {
304 printf("Error getting volume information.\n");
305 return EIO;
306 }
307
308 table_printf(table, "%s\t" "%s\n", vinfo.label, vinfo.path);
309 }
310
311 rc = table_print_out(table, stdout);
312 if (rc != EOK)
313 printf("Error printing table.\n");
314out:
315 table_destroy(table);
316 vol_destroy(vol);
317 free(volume_ids);
318
319 return rc;
320}
321
[db9c889]322static void print_syntax(void)
323{
324 printf("Syntax:\n");
[63c1dd5]325 printf(" %s List present volumes\n", NAME);
326 printf(" %s -c List volume configuration entries\n", NAME);
[db9c889]327 printf(" %s -h Print help\n", NAME);
[f0f8787]328 printf(" %s eject <mp> Eject volume mounted in a directory\n", NAME);
329 printf(" %s insert <svc> Insert volume based on service identifier\n", NAME);
[b82985e]330 printf(" %s insert -p <mp> Insert volume based on filesystem path\n", NAME);
[db9c889]331}
332
333int main(int argc, char *argv[])
334{
335 char *cmd;
[f0f8787]336 char *volspec;
[db9c889]337 vol_cmd_t vcmd;
338 int i;
[2d56e2e]339 errno_t rc = EINVAL;
[db9c889]340
341 if (argc < 2) {
342 vcmd = vcmd_list;
343 i = 1;
344 } else {
345 cmd = argv[1];
346 i = 2;
347 if (str_cmp(cmd, "-h") == 0) {
348 vcmd = vcmd_help;
[63c1dd5]349 } else if (str_cmp(cmd, "-c") == 0) {
350 vcmd = vcmd_cfglist;
[db9c889]351 } else if (str_cmp(cmd, "eject") == 0) {
352 vcmd = vcmd_eject;
353 if (argc <= i) {
354 printf("Parameter missing.\n");
355 goto syntax_error;
356 }
357 volspec = argv[i++];
[f0f8787]358 } else if (str_cmp(cmd, "insert") == 0) {
[b82985e]359 if (str_cmp(argv[i], "-p") == 0) {
360 vcmd = vcmd_insert_by_path;
361 ++i;
362 } else {
363 vcmd = vcmd_insert;
364 }
365
[f0f8787]366 if (argc <= i) {
367 printf("Parameter missing.\n");
368 goto syntax_error;
369 }
370 volspec = argv[i++];
[db9c889]371 } else {
372 printf("Invalid sub-command '%s'.\n", cmd);
373 goto syntax_error;
374 }
375 }
376
377 if (argc > i) {
378 printf("Unexpected argument '%s'.\n", argv[i]);
379 goto syntax_error;
380 }
381
382 switch (vcmd) {
383 case vcmd_eject:
[72c72d4]384 rc = vol_cmd_eject(volspec);
[db9c889]385 break;
[f0f8787]386 case vcmd_insert:
387 rc = vol_cmd_insert(volspec);
388 break;
[b82985e]389 case vcmd_insert_by_path:
390 rc = vol_cmd_insert_by_path(volspec);
391 break;
[db9c889]392 case vcmd_help:
393 print_syntax();
394 rc = EOK;
395 break;
396 case vcmd_list:
397 rc = vol_cmd_list();
398 break;
[63c1dd5]399 case vcmd_cfglist:
400 rc = vol_cmd_cfglist();
401 break;
[db9c889]402 }
403
404 if (rc != EOK)
405 return 1;
406
407 return 0;
408
409syntax_error:
410 printf("Use %s -h to get help.\n", NAME);
411 return 1;
412}
413
414/** @}
415 */
Note: See TracBrowser for help on using the repository browser.