source: mainline/uspace/app/fdisk/fdisk.c@ 03661d19

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

Handle simplified capacity entry.

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