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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9b07fba was 9b07fba, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Fix false warning at -O1.

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