source: mainline/uspace/app/fdisk/fdisk.c@ ef9dac04

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

Confirm destructive actions.

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