source: mainline/uspace/app/fdisk/fdisk.c@ 6c4eedf

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

Move capacity specification to libc.

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