source: mainline/uspace/app/fdisk/fdisk.c@ 1433ecda

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

Fix cstyle: make ccheck-fix and commit only files where all the changes are good.

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