source: mainline/uspace/app/fdisk/fdisk.c@ 64ffd83

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

Configuring mount point for (newly created) paritions.

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