source: mainline/uspace/app/fdisk/fdisk.c@ 9c4cf0d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9c4cf0d was 4627314, checked in by Jiri Svoboda <jiri@…>, 10 years ago

Slightly more graceful handling of missing devices in fdisk.

  • Property mode set to 100644
File size: 20.2 KB
RevLine 
[e96047c]1/*
2 * Copyright (c) 2015 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 fdisk
30 * @brief Disk management tool
31 * @{
32 */
33/**
34 * @file
35 */
36
37#include <errno.h>
38#include <nchoice.h>
39#include <stdbool.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <fdisk.h>
43
44static bool quit = false;
[22fb7ab]45static fdisk_t *fdisk;
[e96047c]46
47/** Device menu actions */
48typedef enum {
49 /** Create label */
50 devac_create_label,
51 /** Delete label */
52 devac_delete_label,
[ea0ff6b]53 /** Erase disk */
54 devac_erase_disk,
[b7a4d06]55 /** Create (primary) partition */
56 devac_create_pri_part,
57 /** Create extended partition */
58 devac_create_ext_part,
59 /** Create logical partition */
60 devac_create_log_part,
[e96047c]61 /** Delete partition */
62 devac_delete_part,
63 /** Exit */
64 devac_exit
65} devac_t;
66
[2dab624]67static int fdsk_pcnt_fs_format(vol_part_cnt_t pcnt, vol_fstype_t fstype,
68 char **rstr)
69{
70 int rc;
71 char *s;
72
73 switch (pcnt) {
74 case vpc_empty:
75 s = str_dup("Empty");
76 if (s == NULL)
77 return ENOMEM;
78 break;
79 case vpc_fs:
80 rc = fdisk_fstype_format(fstype, &s);
81 if (rc != EOK)
82 return ENOMEM;
83 break;
84 case vpc_unknown:
85 s = str_dup("Unknown");
86 if (s == NULL)
87 return ENOMEM;
88 break;
89 }
90
91 *rstr = s;
92 return EOK;
93}
94
[ef9dac04]95/** Confirm user selection. */
96static int fdsk_confirm(const char *msg, bool *rconfirm)
97{
98 tinput_t *tinput = NULL;
99 char *answer;
100 int rc;
101
102 tinput = tinput_new();
103 if (tinput == NULL) {
104 rc = ENOMEM;
105 goto error;
106 }
107
108 rc = tinput_set_prompt(tinput, "y/n> ");
109 if (rc != EOK)
110 goto error;
111
112 while (true) {
113 printf("%s\n", msg);
114
115 rc = tinput_read(tinput, &answer);
116 if (rc == ENOENT) {
117 *rconfirm = false;
118 free(answer);
119 break;
120 }
121
122 if (rc != EOK)
123 goto error;
124
125 if (str_cmp(answer, "y") == 0) {
126 *rconfirm = true;
127 free(answer);
128 break;
129 } else if (str_cmp(answer, "n") == 0) {
130 *rconfirm = false;
131 free(answer);
132 break;
133 }
134 }
135
136 tinput_destroy(tinput);
137 return EOK;
138error:
139 if (tinput != NULL)
140 tinput_destroy(tinput);
141 return rc;
142}
143
[e96047c]144/** Device selection */
145static int fdsk_dev_sel_choice(service_id_t *rsvcid)
146{
147 fdisk_dev_list_t *devlist = NULL;
148 fdisk_dev_info_t *info;
149 nchoice_t *choice = NULL;
150 char *svcname = NULL;
151 fdisk_cap_t cap;
[8227d63]152 fdisk_dev_info_t *sdev;
[e96047c]153 char *scap = NULL;
154 char *dtext = NULL;
155 service_id_t svcid;
156 void *sel;
[8227d63]157 int ndevs;
[e96047c]158 int rc;
159
160 rc = nchoice_create(&choice);
161 if (rc != EOK) {
162 assert(rc == ENOMEM);
163 printf("Out of memory.\n");
164 goto error;
165 }
166
167 rc = nchoice_set_prompt(choice, "Select device");
168 if (rc != EOK) {
169 assert(rc == ENOMEM);
170 printf("Out of memory.\n");
171 goto error;
172 }
173
[22fb7ab]174 rc = fdisk_dev_list_get(fdisk, &devlist);
[e96047c]175 if (rc != EOK) {
176 printf("Error getting device list.\n");
177 goto error;
178 }
179
180 info = fdisk_dev_first(devlist);
[8227d63]181 ndevs = 0;
[e96047c]182 while (info != NULL) {
[8227d63]183 rc = fdisk_dev_info_get_svcname(info, &svcname);
[e96047c]184 if (rc != EOK) {
[4627314]185 fdisk_dev_info_get_svcid(info, &svcid);
186 printf("Error getting device service name "
187 "(service ID %zu).\n", svcid);
188 info = fdisk_dev_next(info);
189 continue;
[e96047c]190 }
191
[8227d63]192 rc = fdisk_dev_info_capacity(info, &cap);
[e96047c]193 if (rc != EOK) {
[4627314]194 printf("Error getting device capacity "
195 "(device %s).\n", svcname);
196 info = fdisk_dev_next(info);
197 continue;
[e96047c]198 }
199
[9854a8f]200 fdisk_cap_simplify(&cap);
201
[e96047c]202 rc = fdisk_cap_format(&cap, &scap);
203 if (rc != EOK) {
204 assert(rc == ENOMEM);
205 printf("Out of memory.\n");
206 goto error;
207 }
208
209 rc = asprintf(&dtext, "%s (%s)", svcname, scap);
210 if (rc < 0) {
211 rc = ENOMEM;
212 printf("Out of memory.\n");
213 goto error;
214 }
215
216 free(svcname);
217 svcname = NULL;
218 free(scap);
219 scap = NULL;
220
[68b5dd11]221 rc = nchoice_add(choice, dtext, info, 0);
[e96047c]222 if (rc != EOK) {
223 assert(rc == ENOMEM);
224 printf("Out of memory.\n");
225 goto error;
226 }
227
[4627314]228 ++ndevs;
229
[e96047c]230 free(dtext);
231 dtext = NULL;
232
233 info = fdisk_dev_next(info);
234 }
235
[8227d63]236 if (ndevs == 0) {
237 printf("No disk devices found.\n");
238 rc = ENOENT;
239 goto error;
240 }
241
[68b5dd11]242 rc = nchoice_add(choice, "Exit", NULL, 0);
[8227d63]243 if (rc != EOK) {
244 assert(rc == ENOMEM);
245 printf("Out of memory.\n");
246 goto error;
247 }
248
[e96047c]249 rc = nchoice_get(choice, &sel);
250 if (rc != EOK) {
251 printf("Error getting user selection.\n");
252 return rc;
253 }
254
[8227d63]255 sdev = (fdisk_dev_info_t *)sel;
256 if (sdev != NULL) {
257 fdisk_dev_info_get_svcid(sdev, &svcid);
258 } else {
259 svcid = 0;
260 }
[e96047c]261
[8227d63]262 fdisk_dev_list_free(devlist);
[e96047c]263
264 nchoice_destroy(choice);
265 *rsvcid = svcid;
266 return EOK;
267error:
268 if (devlist != NULL)
269 fdisk_dev_list_free(devlist);
270 if (choice != NULL)
271 nchoice_destroy(choice);
272 free(dtext);
273 free(svcname);
274 free(scap);
275 return rc;
276}
277
278static int fdsk_create_label(fdisk_dev_t *dev)
279{
280 nchoice_t *choice = NULL;
281 void *sel;
[8227d63]282 char *sltype = NULL;
283 int i;
[e96047c]284 int rc;
285
286 rc = nchoice_create(&choice);
287 if (rc != EOK) {
288 assert(rc == ENOMEM);
289 printf("Out of memory.\n");
290 goto error;
291 }
292
293 rc = nchoice_set_prompt(choice, "Select label type");
294 if (rc != EOK) {
295 assert(rc == ENOMEM);
296 printf("Out of memory.\n");
297 goto error;
298 }
299
[372df8f]300 for (i = LT_FIRST; i < LT_LIMIT; i++) {
[8227d63]301 rc = fdisk_ltype_format(i, &sltype);
302 if (rc != EOK)
303 goto error;
304
[68b5dd11]305 rc = nchoice_add(choice, sltype, (void *)(uintptr_t)i,
306 i == LT_DEFAULT);
[8227d63]307 if (rc != EOK) {
308 assert(rc == ENOMEM);
309 printf("Out of memory.\n");
310 goto error;
311 }
312
313 free(sltype);
314 sltype = NULL;
315 }
316
317 rc = nchoice_get(choice, &sel);
318 if (rc != EOK) {
319 printf("Error getting user selection.\n");
320 goto error;
321 }
322
[22fb7ab]323 rc = fdisk_label_create(dev, (label_type_t)sel);
[8227d63]324 if (rc != EOK) {
325 printf("Error creating label.\n");
326 goto error;
327 }
328
329 nchoice_destroy(choice);
330 return EOK;
331error:
332 free(sltype);
333 if (choice != NULL)
334 nchoice_destroy(choice);
335 return rc;
336}
337
338static int fdsk_delete_label(fdisk_dev_t *dev)
339{
[ef9dac04]340 bool confirm;
[8227d63]341 int rc;
342
[ef9dac04]343 rc = fdsk_confirm("Warning. Any data on disk will be lost. "
344 "Really delete label?", &confirm);
345 if (rc != EOK) {
346 printf("Error getting user confirmation.\n");
347 return rc;
348 }
349
350 if (!confirm)
351 return EOK;
352
[8227d63]353 rc = fdisk_label_destroy(dev);
354 if (rc != EOK) {
355 printf("Error deleting label.\n");
356 return rc;
357 }
358
359 return EOK;
360}
361
[ea0ff6b]362static int fdsk_erase_disk(fdisk_dev_t *dev)
363{
[ef9dac04]364 bool confirm;
[ea0ff6b]365 int rc;
366
[ef9dac04]367 rc = fdsk_confirm("Warning. Any data on disk will be lost. "
368 "Really erase disk?", &confirm);
369 if (rc != EOK) {
370 printf("Error getting user confirmation.\n");
371 return rc;
372 }
373
374 if (!confirm)
375 return EOK;
376
[ea0ff6b]377 rc = fdisk_dev_erase(dev);
378 if (rc != EOK) {
379 printf("Error erasing disk.\n");
380 return rc;
381 }
382
383 return EOK;
384}
385
[4b6635a7]386static int fdsk_select_fstype(vol_fstype_t *fstype)
[8227d63]387{
388 nchoice_t *choice = NULL;
389 void *sel;
390 char *sfstype;
391 int i;
392 int rc;
393
394 rc = nchoice_create(&choice);
[e96047c]395 if (rc != EOK) {
396 assert(rc == ENOMEM);
397 printf("Out of memory.\n");
398 goto error;
399 }
400
[8227d63]401 rc = nchoice_set_prompt(choice, "Select file system type");
[e96047c]402 if (rc != EOK) {
403 assert(rc == ENOMEM);
404 printf("Out of memory.\n");
405 goto error;
406 }
407
[4b6635a7]408 for (i = 0; i < VOL_FSTYPE_LIMIT; i++) {
[8227d63]409 rc = fdisk_fstype_format(i, &sfstype);
410 if (rc != EOK)
411 goto error;
412
[68b5dd11]413 rc = nchoice_add(choice, sfstype, (void *)(uintptr_t)i,
414 i == VOL_FSTYPE_DEFAULT);
[8227d63]415 if (rc != EOK) {
416 assert(rc == ENOMEM);
417 printf("Out of memory.\n");
418 goto error;
419 }
420
421 free(sfstype);
422 sfstype = NULL;
423 }
424
[e96047c]425 rc = nchoice_get(choice, &sel);
426 if (rc != EOK) {
427 printf("Error getting user selection.\n");
428 goto error;
429 }
430
[8227d63]431 nchoice_destroy(choice);
[4b6635a7]432 *fstype = (vol_fstype_t)sel;
[8227d63]433 return EOK;
434error:
435 free(sfstype);
436 if (choice != NULL)
437 nchoice_destroy(choice);
438 return rc;
439}
440
[b7a4d06]441static int fdsk_create_part(fdisk_dev_t *dev, label_pkind_t pkind)
[8227d63]442{
443 int rc;
444 fdisk_part_spec_t pspec;
445 fdisk_cap_t cap;
[b598460a]446 fdisk_cap_t mcap;
[4b6635a7]447 vol_fstype_t fstype = 0;
[8227d63]448 tinput_t *tinput = NULL;
[b598460a]449 fdisk_spc_t spc;
[8227d63]450 char *scap;
[b598460a]451 char *smcap = NULL;
452
453 if (pkind == lpk_logical)
454 spc = spc_log;
455 else
456 spc = spc_pri;
457
458 rc = fdisk_part_get_max_avail(dev, spc, &mcap);
459 if (rc != EOK) {
460 rc = EIO;
461 goto error;
462 }
463
[03661d19]464 fdisk_cap_simplify(&mcap);
465
[b598460a]466 rc = fdisk_cap_format(&mcap, &smcap);
467 if (rc != EOK) {
468 rc = ENOMEM;
469 goto error;
470 }
[8227d63]471
472 tinput = tinput_new();
473 if (tinput == NULL) {
474 rc = ENOMEM;
475 goto error;
476 }
477
478 rc = tinput_set_prompt(tinput, "?> ");
479 if (rc != EOK)
480 goto error;
481
482 while (true) {
483 printf("Enter capacity of new partition.\n");
[b598460a]484 rc = tinput_read_i(tinput, smcap, &scap);
[8227d63]485 if (rc != EOK)
486 goto error;
487
488 rc = fdisk_cap_parse(scap, &cap);
489 if (rc == EOK)
490 break;
491 }
492
493 tinput_destroy(tinput);
[78d50bd]494 tinput = NULL;
[b598460a]495 free(smcap);
496 smcap = NULL;
[8227d63]497
[b7a4d06]498 if (pkind != lpk_extended) {
499 rc = fdsk_select_fstype(&fstype);
500 if (rc != EOK)
501 goto error;
502 }
[8227d63]503
504 fdisk_pspec_init(&pspec);
505 pspec.capacity = cap;
[b7a4d06]506 pspec.pkind = pkind;
[8227d63]507 pspec.fstype = fstype;
508
509 rc = fdisk_part_create(dev, &pspec, NULL);
[e96047c]510 if (rc != EOK) {
[8227d63]511 printf("Error creating partition.\n");
[e96047c]512 goto error;
513 }
514
[8227d63]515 return EOK;
516error:
[b598460a]517 free(smcap);
[8227d63]518 if (tinput != NULL)
519 tinput_destroy(tinput);
520 return rc;
521}
522
523static int fdsk_delete_part(fdisk_dev_t *dev)
524{
525 nchoice_t *choice = NULL;
526 fdisk_part_t *part;
527 fdisk_part_info_t pinfo;
528 char *scap = NULL;
[21f1543]529 char *spkind = NULL;
[8227d63]530 char *sfstype = NULL;
531 char *sdesc = NULL;
[ef9dac04]532 bool confirm;
[8227d63]533 void *sel;
534 int rc;
535
536 rc = nchoice_create(&choice);
537 if (rc != EOK) {
538 assert(rc == ENOMEM);
539 printf("Out of memory.\n");
540 goto error;
541 }
542
543 rc = nchoice_set_prompt(choice, "Select partition to delete");
544 if (rc != EOK) {
545 assert(rc == ENOMEM);
546 printf("Out of memory.\n");
547 goto error;
548 }
549
550 part = fdisk_part_first(dev);
551 while (part != NULL) {
552 rc = fdisk_part_get_info(part, &pinfo);
553 if (rc != EOK) {
554 printf("Error getting partition information.\n");
555 goto error;
556 }
557
[9854a8f]558 fdisk_cap_simplify(&pinfo.capacity);
559
[8227d63]560 rc = fdisk_cap_format(&pinfo.capacity, &scap);
561 if (rc != EOK) {
562 printf("Out of memory.\n");
563 goto error;
564 }
565
[21f1543]566 rc = fdisk_pkind_format(pinfo.pkind, &spkind);
[8227d63]567 if (rc != EOK) {
[21f1543]568 printf("\nOut of memory.\n");
[8227d63]569 goto error;
570 }
571
[21f1543]572 if (pinfo.pkind != lpk_extended) {
[2dab624]573 rc = fdsk_pcnt_fs_format(pinfo.pcnt, pinfo.fstype, &sfstype);
[21f1543]574 if (rc != EOK) {
575 printf("Out of memory.\n");
576 goto error;
577 }
578
579 rc = asprintf(&sdesc, "%s, %s, %s", scap, spkind, sfstype);
580 if (rc < 0) {
581 rc = ENOMEM;
582 goto error;
583 }
584
585 } else {
586 rc = asprintf(&sdesc, "%s, %s", scap, spkind);
587 if (rc < 0) {
588 rc = ENOMEM;
589 goto error;
590 }
[8227d63]591 }
592
[68b5dd11]593 rc = nchoice_add(choice, sdesc, (void *)part, 0);
[8227d63]594 if (rc != EOK) {
595 assert(rc == ENOMEM);
596 printf("Out of memory.\n");
597 goto error;
598 }
599
600 free(scap);
601 scap = NULL;
[21f1543]602 free(spkind);
603 spkind = NULL;
[8227d63]604 free(sfstype);
605 sfstype = NULL;
606 free(sdesc);
607 sdesc = NULL;
608
609 part = fdisk_part_next(part);
610 }
611
[21f1543]612 rc = nchoice_add(choice, "Cancel", NULL, 0);
613 if (rc != EOK) {
614 assert(rc == ENOMEM);
615 printf("Out of memory.\n");
616 goto error;
617 }
618
[8227d63]619 rc = nchoice_get(choice, &sel);
[ef9dac04]620 if (rc == ENOENT)
621 return EOK;
[8227d63]622 if (rc != EOK) {
623 printf("Error getting user selection.\n");
624 goto error;
625 }
626
627
[e96047c]628 nchoice_destroy(choice);
[ef9dac04]629 choice = NULL;
630
631 if (sel == NULL)
632 return EOK;
633
634 rc = fdsk_confirm("Warning. Any data in partition will be lost. "
635 "Really delete partition?", &confirm);
636 if (rc != EOK) {
637 printf("Error getting user confirmation.\n");
638 goto error;
639 }
640
641 if (!confirm)
642 return EOK;
643
644 rc = fdisk_part_destroy((fdisk_part_t *)sel);
645 if (rc != EOK) {
646 printf("Error deleting partition.\n");
647 return rc;
648 }
649
[e96047c]650 return EOK;
651error:
[8227d63]652 free(scap);
[21f1543]653 free(spkind);
[8227d63]654 free(sfstype);
655 free(sdesc);
656
[e96047c]657 if (choice != NULL)
658 nchoice_destroy(choice);
659 return rc;
660}
661
662/** Device menu */
663static int fdsk_dev_menu(fdisk_dev_t *dev)
664{
665 nchoice_t *choice = NULL;
666 fdisk_label_info_t linfo;
[8227d63]667 fdisk_part_t *part;
668 fdisk_part_info_t pinfo;
669 fdisk_cap_t cap;
[852664b9]670 fdisk_cap_t mcap;
[ea0ff6b]671 fdisk_dev_flags_t dflags;
[e96047c]672 char *sltype = NULL;
[8227d63]673 char *sdcap = NULL;
674 char *scap = NULL;
[852664b9]675 char *smcap = NULL;
[8227d63]676 char *sfstype = NULL;
677 char *svcname = NULL;
[b7a4d06]678 char *spkind;
[e96047c]679 int rc;
[8227d63]680 int npart;
[e96047c]681 void *sel;
682
683 rc = nchoice_create(&choice);
684 if (rc != EOK) {
685 assert(rc == ENOMEM);
686 printf("Out of memory.\n");
687 goto error;
688 }
689
690 rc = nchoice_set_prompt(choice, "Select action");
691 if (rc != EOK) {
692 assert(rc == ENOMEM);
693 printf("Out of memory.\n");
694 goto error;
695 }
696
[8227d63]697 rc = fdisk_dev_capacity(dev, &cap);
698 if (rc != EOK) {
699 printf("Error getting device capacity.\n");
700 goto error;
701 }
702
[9854a8f]703 fdisk_cap_simplify(&cap);
704
[8227d63]705 rc = fdisk_cap_format(&cap, &sdcap);
706 if (rc != EOK) {
707 printf("Out of memory.\n");
708 goto error;
709 }
710
711 rc = fdisk_dev_get_svcname(dev, &svcname);
712 if (rc != EOK) {
713 printf("Error getting device service name.\n");
714 goto error;
715 }
716
[ea0ff6b]717 fdisk_dev_get_flags(dev, &dflags);
718
[22fb7ab]719 printf("Device: %s, %s\n", sdcap, svcname);
720 free(sdcap);
721 sdcap = NULL;
722
[e96047c]723 rc = fdisk_label_get_info(dev, &linfo);
724 if (rc != EOK) {
725 printf("Error getting label information.\n");
726 goto error;
727 }
728
[0ecfc62]729 switch (linfo.ltype) {
730 case lt_none:
731 printf("Disk contains no label.\n");
[22fb7ab]732 break;
[0ecfc62]733 default:
[22fb7ab]734 rc = fdisk_ltype_format(linfo.ltype, &sltype);
735 if (rc != EOK) {
736 assert(rc == ENOMEM);
737 printf("Out of memory.\n");
738 goto error;
739 }
[e96047c]740
[22fb7ab]741 printf("Label type: %s\n", sltype);
742 free(sltype);
743 sltype = NULL;
744 break;
745 }
[8227d63]746
747 part = fdisk_part_first(dev);
748 npart = 0;
749 while (part != NULL) {
750 ++npart;
751 rc = fdisk_part_get_info(part, &pinfo);
752 if (rc != EOK) {
753 printf("Error getting partition information.\n");
754 goto error;
755 }
756
[9854a8f]757 fdisk_cap_simplify(&pinfo.capacity);
758
[8227d63]759 rc = fdisk_cap_format(&pinfo.capacity, &scap);
760 if (rc != EOK) {
761 printf("Out of memory.\n");
762 goto error;
763 }
764
[2dab624]765 rc = fdsk_pcnt_fs_format(pinfo.pcnt, pinfo.fstype, &sfstype);
[8227d63]766 if (rc != EOK) {
767 printf("Out of memory.\n");
768 goto error;
769 }
770
[edebb4a1]771 if (linfo.ltype == lt_none)
772 printf("Entire disk: %s", scap);
773 else
774 printf("Partition %d: %s", npart, scap);
775
[b7a4d06]776 if ((linfo.flags & lf_ext_supp) != 0) {
777 rc = fdisk_pkind_format(pinfo.pkind, &spkind);
778 if (rc != EOK) {
779 printf("\nOut of memory.\n");
780 goto error;
781 }
782
783 printf(", %s", spkind);
784 free(spkind);
785 }
786
[4b6635a7]787 if (pinfo.pkind != lpk_extended) {
[2dab624]788 printf(", %s", sfstype);
[4b6635a7]789 }
790
[b7a4d06]791 printf("\n");
792
[8227d63]793 free(scap);
794 scap = NULL;
795 free(sfstype);
796 sfstype = NULL;
797
798 part = fdisk_part_next(part);
799 }
[e96047c]800
[852664b9]801 /* Display available space */
802 if ((linfo.flags & lf_can_create_pri) != 0) {
803 rc = fdisk_part_get_max_avail(dev, spc_pri, &mcap);
804 if (rc != EOK) {
805 rc = EIO;
806 goto error;
807 }
808
[9854a8f]809 fdisk_cap_simplify(&mcap);
810
[852664b9]811 rc = fdisk_cap_format(&mcap, &smcap);
812 if (rc != EOK) {
813 rc = ENOMEM;
814 goto error;
815 }
816
817 if ((linfo.flags & lf_ext_supp) != 0)
818 printf("Maximum free primary block: %s\n", smcap);
819 else
820 printf("Maximum free block: %s\n", smcap);
821
822 free(smcap);
823 smcap = NULL;
824
825 rc = fdisk_part_get_tot_avail(dev, spc_pri, &mcap);
826 if (rc != EOK) {
827 rc = EIO;
828 goto error;
829 }
830
[9854a8f]831 fdisk_cap_simplify(&mcap);
832
[852664b9]833 rc = fdisk_cap_format(&mcap, &smcap);
834 if (rc != EOK) {
835 rc = ENOMEM;
836 goto error;
837 }
838
839 if ((linfo.flags & lf_ext_supp) != 0)
840 printf("Total free primary space: %s\n", smcap);
841 else
842 printf("Total free space: %s\n", smcap);
843
844 free(smcap);
845 smcap = NULL;
846 }
847
848 /* Display available space */
849 if ((linfo.flags & lf_can_create_log) != 0) {
850 rc = fdisk_part_get_max_avail(dev, spc_log, &mcap);
851 if (rc != EOK) {
852 rc = EIO;
853 goto error;
854 }
855
[9854a8f]856 fdisk_cap_simplify(&mcap);
857
[852664b9]858 rc = fdisk_cap_format(&mcap, &smcap);
859 if (rc != EOK) {
860 rc = ENOMEM;
861 goto error;
862 }
863
864 printf("Maximum free logical block: %s\n", smcap);
865 free(smcap);
866 smcap = NULL;
867
868 rc = fdisk_part_get_tot_avail(dev, spc_log, &mcap);
869 if (rc != EOK) {
870 rc = EIO;
871 goto error;
872 }
873
[9854a8f]874 fdisk_cap_simplify(&mcap);
875
[852664b9]876 rc = fdisk_cap_format(&mcap, &smcap);
877 if (rc != EOK) {
878 rc = ENOMEM;
879 goto error;
880 }
881
882 printf("Total free logical space: %s\n", smcap);
883 free(smcap);
884 smcap = NULL;
885 }
886
[e96047c]887 rc = nchoice_set_prompt(choice, "Select action");
888 if (rc != EOK) {
889 assert(rc == ENOMEM);
890 printf("Out of memory.\n");
891 goto error;
892 }
893
[0ecfc62]894 if ((linfo.flags & lf_ext_supp) != 0) {
895 if ((linfo.flags & lf_can_create_pri) != 0) {
896 rc = nchoice_add(choice, "Create primary "
897 "partition",
[68b5dd11]898 (void *)devac_create_pri_part, 0);
[0ecfc62]899 if (rc != EOK) {
900 assert(rc == ENOMEM);
901 printf("Out of memory.\n");
902 goto error;
[b7a4d06]903 }
[0ecfc62]904 }
[b7a4d06]905
[0ecfc62]906 if ((linfo.flags & lf_can_create_ext) != 0) {
907 rc = nchoice_add(choice, "Create extended "
908 "partition",
[68b5dd11]909 (void *)devac_create_ext_part, 0);
[0ecfc62]910 if (rc != EOK) {
911 assert(rc == ENOMEM);
912 printf("Out of memory.\n");
913 goto error;
[b7a4d06]914 }
[0ecfc62]915 }
[b7a4d06]916
[0ecfc62]917 if ((linfo.flags & lf_can_create_log) != 0) {
918 rc = nchoice_add(choice, "Create logical "
919 "partition",
[68b5dd11]920 (void *)devac_create_log_part, 0);
[0ecfc62]921 if (rc != EOK) {
922 assert(rc == ENOMEM);
923 printf("Out of memory.\n");
924 goto error;
[b7a4d06]925 }
[0ecfc62]926 }
927 } else { /* (linfo.flags & lf_ext_supp) == 0 */
928 if ((linfo.flags & lf_can_create_pri) != 0) {
929 rc = nchoice_add(choice, "Create partition",
[68b5dd11]930 (void *)devac_create_pri_part, 0);
[0ecfc62]931 if (rc != EOK) {
932 assert(rc == ENOMEM);
[b7a4d06]933 printf("Out of memory.\n");
934 goto error;
935 }
[8227d63]936 }
937 }
938
[edebb4a1]939 if ((linfo.flags & lf_can_delete_part) != 0) {
[8227d63]940 rc = nchoice_add(choice, "Delete partition",
[68b5dd11]941 (void *)devac_delete_part, 0);
[8227d63]942 if (rc != EOK) {
943 assert(rc == ENOMEM);
944 printf("Out of memory.\n");
945 goto error;
946 }
947 }
948
[ea0ff6b]949 if ((dflags & fdf_can_create_label) != 0) {
[e96047c]950 rc = nchoice_add(choice, "Create label",
[68b5dd11]951 (void *)devac_create_label, 0);
[e96047c]952 if (rc != EOK) {
953 assert(rc == ENOMEM);
954 printf("Out of memory.\n");
955 goto error;
956 }
[ea0ff6b]957 }
958
959 if ((dflags & fdf_can_delete_label) != 0) {
[e96047c]960 rc = nchoice_add(choice, "Delete label",
[68b5dd11]961 (void *)devac_delete_label, 0);
[e96047c]962 if (rc != EOK) {
963 assert(rc == ENOMEM);
964 printf("Out of memory.\n");
965 goto error;
966 }
967 }
968
[ea0ff6b]969 if ((dflags & fdf_can_erase_dev) != 0) {
970 rc = nchoice_add(choice, "Erase disk",
[68b5dd11]971 (void *)devac_erase_disk, 0);
[ea0ff6b]972 if (rc != EOK) {
973 assert(rc == ENOMEM);
974 printf("Out of memory.\n");
975 goto error;
976 }
977 }
978
[68b5dd11]979 rc = nchoice_add(choice, "Exit", (void *)devac_exit, 0);
[e96047c]980 if (rc != EOK) {
981 assert(rc == ENOMEM);
982 printf("Out of memory.\n");
983 goto error;
984 }
985
986 rc = nchoice_get(choice, &sel);
987 if (rc != EOK) {
988 printf("Error getting user selection.\n");
989 return rc;
990 }
991
992 switch ((devac_t)sel) {
993 case devac_create_label:
[22fb7ab]994 (void) fdsk_create_label(dev);
[e96047c]995 break;
996 case devac_delete_label:
[22fb7ab]997 (void) fdsk_delete_label(dev);
[e96047c]998 break;
[ea0ff6b]999 case devac_erase_disk:
1000 (void) fdsk_erase_disk(dev);
1001 break;
[b7a4d06]1002 case devac_create_pri_part:
1003 (void) fdsk_create_part(dev, lpk_primary);
1004 break;
1005 case devac_create_ext_part:
1006 (void) fdsk_create_part(dev, lpk_extended);
1007 break;
1008 case devac_create_log_part:
1009 (void) fdsk_create_part(dev, lpk_logical);
[e96047c]1010 break;
1011 case devac_delete_part:
[22fb7ab]1012 (void) fdsk_delete_part(dev);
[e96047c]1013 break;
1014 case devac_exit:
1015 quit = true;
1016 break;
1017 }
1018
1019 nchoice_destroy(choice);
1020 return EOK;
1021error:
[8227d63]1022 free(sdcap);
1023 free(scap);
[852664b9]1024 free(smcap);
[8227d63]1025 free(sfstype);
1026 free(svcname);
[e96047c]1027 if (choice != NULL)
1028 nchoice_destroy(choice);
1029 return rc;
1030}
1031
1032int main(int argc, char *argv[])
1033{
1034 service_id_t svcid;
1035 fdisk_dev_t *dev;
1036 int rc;
1037
[22fb7ab]1038 rc = fdisk_create(&fdisk);
1039 if (rc != EOK) {
1040 printf("Error initializing Fdisk.\n");
1041 return 1;
1042 }
1043
[e96047c]1044 rc = fdsk_dev_sel_choice(&svcid);
1045 if (rc != EOK)
1046 return 1;
1047
[8227d63]1048 if (svcid == 0)
1049 return 0;
1050
[22fb7ab]1051 rc = fdisk_dev_open(fdisk, svcid, &dev);
[e96047c]1052 if (rc != EOK) {
1053 printf("Error opening device.\n");
1054 return 1;
1055 }
1056
1057 while (!quit) {
1058 rc = fdsk_dev_menu(dev);
1059 if (rc != EOK) {
1060 fdisk_dev_close(dev);
1061 return 1;
1062 }
1063 }
1064
1065 fdisk_dev_close(dev);
[22fb7ab]1066 fdisk_destroy(fdisk);
[e96047c]1067
1068 return 0;
1069}
1070
1071
1072/** @}
1073 */
Note: See TracBrowser for help on using the repository browser.