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
RevLine 
[e96047c]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
[6c4eedf]37#include <cap.h>
[e96047c]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
[9c2c7d2]45#define NO_LABEL_CAPTION "(No name)"
46
[e96047c]47static bool quit = false;
[22fb7ab]48static fdisk_t *fdisk;
[e96047c]49
50/** Device menu actions */
51typedef enum {
52 /** Create label */
53 devac_create_label,
54 /** Delete label */
55 devac_delete_label,
[ea0ff6b]56 /** Erase disk */
57 devac_erase_disk,
[b7a4d06]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,
[e96047c]64 /** Delete partition */
65 devac_delete_part,
66 /** Exit */
67 devac_exit
68} devac_t;
69
[2dab624]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
[ef9dac04]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
[e96047c]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;
[6c4eedf]154 cap_spec_t cap;
[8227d63]155 fdisk_dev_info_t *sdev;
[e96047c]156 char *scap = NULL;
157 char *dtext = NULL;
158 service_id_t svcid;
159 void *sel;
[8227d63]160 int ndevs;
[e96047c]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
[22fb7ab]177 rc = fdisk_dev_list_get(fdisk, &devlist);
[e96047c]178 if (rc != EOK) {
179 printf("Error getting device list.\n");
180 goto error;
181 }
182
183 info = fdisk_dev_first(devlist);
[8227d63]184 ndevs = 0;
[e96047c]185 while (info != NULL) {
[8227d63]186 rc = fdisk_dev_info_get_svcname(info, &svcname);
[e96047c]187 if (rc != EOK) {
[4627314]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;
[e96047c]193 }
194
[8227d63]195 rc = fdisk_dev_info_capacity(info, &cap);
[e96047c]196 if (rc != EOK) {
[4627314]197 printf("Error getting device capacity "
198 "(device %s).\n", svcname);
199 info = fdisk_dev_next(info);
200 continue;
[e96047c]201 }
202
[6c4eedf]203 cap_simplify(&cap);
[9854a8f]204
[6c4eedf]205 rc = cap_format(&cap, &scap);
[e96047c]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
[68b5dd11]224 rc = nchoice_add(choice, dtext, info, 0);
[e96047c]225 if (rc != EOK) {
226 assert(rc == ENOMEM);
227 printf("Out of memory.\n");
228 goto error;
229 }
230
[4627314]231 ++ndevs;
232
[e96047c]233 free(dtext);
234 dtext = NULL;
235
236 info = fdisk_dev_next(info);
237 }
238
[8227d63]239 if (ndevs == 0) {
240 printf("No disk devices found.\n");
241 rc = ENOENT;
242 goto error;
243 }
244
[68b5dd11]245 rc = nchoice_add(choice, "Exit", NULL, 0);
[8227d63]246 if (rc != EOK) {
247 assert(rc == ENOMEM);
248 printf("Out of memory.\n");
249 goto error;
250 }
251
[e96047c]252 rc = nchoice_get(choice, &sel);
253 if (rc != EOK) {
254 printf("Error getting user selection.\n");
255 return rc;
256 }
257
[8227d63]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 }
[e96047c]264
[8227d63]265 fdisk_dev_list_free(devlist);
[e96047c]266
267 nchoice_destroy(choice);
268 *rsvcid = svcid;
269 return EOK;
270error:
[9b07fba]271 assert(rc != EOK);
272 *rsvcid = 0;
[e96047c]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;
[8227d63]287 char *sltype = NULL;
288 int i;
[e96047c]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
[372df8f]305 for (i = LT_FIRST; i < LT_LIMIT; i++) {
[8227d63]306 rc = fdisk_ltype_format(i, &sltype);
307 if (rc != EOK)
308 goto error;
309
[68b5dd11]310 rc = nchoice_add(choice, sltype, (void *)(uintptr_t)i,
311 i == LT_DEFAULT);
[8227d63]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
[22fb7ab]328 rc = fdisk_label_create(dev, (label_type_t)sel);
[8227d63]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{
[ef9dac04]345 bool confirm;
[8227d63]346 int rc;
347
[ef9dac04]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
[8227d63]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
[ea0ff6b]367static int fdsk_erase_disk(fdisk_dev_t *dev)
368{
[ef9dac04]369 bool confirm;
[ea0ff6b]370 int rc;
371
[ef9dac04]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
[ea0ff6b]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
[4b6635a7]391static int fdsk_select_fstype(vol_fstype_t *fstype)
[8227d63]392{
393 nchoice_t *choice = NULL;
394 void *sel;
395 char *sfstype;
396 int i;
397 int rc;
398
399 rc = nchoice_create(&choice);
[e96047c]400 if (rc != EOK) {
401 assert(rc == ENOMEM);
402 printf("Out of memory.\n");
403 goto error;
404 }
405
[8227d63]406 rc = nchoice_set_prompt(choice, "Select file system type");
[e96047c]407 if (rc != EOK) {
408 assert(rc == ENOMEM);
409 printf("Out of memory.\n");
410 goto error;
411 }
412
[4b6635a7]413 for (i = 0; i < VOL_FSTYPE_LIMIT; i++) {
[8227d63]414 rc = fdisk_fstype_format(i, &sfstype);
415 if (rc != EOK)
416 goto error;
417
[68b5dd11]418 rc = nchoice_add(choice, sfstype, (void *)(uintptr_t)i,
419 i == VOL_FSTYPE_DEFAULT);
[8227d63]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
[e96047c]430 rc = nchoice_get(choice, &sel);
431 if (rc != EOK) {
432 printf("Error getting user selection.\n");
433 goto error;
434 }
435
[8227d63]436 nchoice_destroy(choice);
[4b6635a7]437 *fstype = (vol_fstype_t)sel;
[8227d63]438 return EOK;
439error:
440 free(sfstype);
441 if (choice != NULL)
442 nchoice_destroy(choice);
443 return rc;
444}
445
[b7a4d06]446static int fdsk_create_part(fdisk_dev_t *dev, label_pkind_t pkind)
[8227d63]447{
448 int rc;
449 fdisk_part_spec_t pspec;
[6c4eedf]450 cap_spec_t cap;
451 cap_spec_t mcap;
[9c2c7d2]452 vol_label_supp_t vlsupp;
[4b6635a7]453 vol_fstype_t fstype = 0;
[8227d63]454 tinput_t *tinput = NULL;
[b598460a]455 fdisk_spc_t spc;
[8227d63]456 char *scap;
[b598460a]457 char *smcap = NULL;
[9c2c7d2]458 char *label = NULL;
[b598460a]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
[6c4eedf]471 cap_simplify(&mcap);
[03661d19]472
[6c4eedf]473 rc = cap_format(&mcap, &smcap);
[b598460a]474 if (rc != EOK) {
475 rc = ENOMEM;
476 goto error;
477 }
[8227d63]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");
[b598460a]491 rc = tinput_read_i(tinput, smcap, &scap);
[8227d63]492 if (rc != EOK)
493 goto error;
494
[6c4eedf]495 rc = cap_parse(scap, &cap);
[8227d63]496 if (rc == EOK)
497 break;
498 }
499
500 tinput_destroy(tinput);
[78d50bd]501 tinput = NULL;
[b598460a]502 free(smcap);
503 smcap = NULL;
[8227d63]504
[b7a4d06]505 if (pkind != lpk_extended) {
506 rc = fdsk_select_fstype(&fstype);
507 if (rc != EOK)
508 goto error;
509 }
[8227d63]510
[9c2c7d2]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");
[f3504c1]525 rc = tinput_read_i(tinput, "New volume", &label);
[9c2c7d2]526 if (rc != EOK)
527 goto error;
528
529 tinput_destroy(tinput);
530 tinput = NULL;
531 }
532
[8227d63]533 fdisk_pspec_init(&pspec);
534 pspec.capacity = cap;
[b7a4d06]535 pspec.pkind = pkind;
[8227d63]536 pspec.fstype = fstype;
[9c2c7d2]537 pspec.label = label;
[8227d63]538
539 rc = fdisk_part_create(dev, &pspec, NULL);
[e96047c]540 if (rc != EOK) {
[8227d63]541 printf("Error creating partition.\n");
[e96047c]542 goto error;
543 }
544
[9c2c7d2]545 free(label);
[8227d63]546 return EOK;
547error:
[b598460a]548 free(smcap);
[9c2c7d2]549 free(label);
[8227d63]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;
[21f1543]561 char *spkind = NULL;
[8227d63]562 char *sfstype = NULL;
563 char *sdesc = NULL;
[9c2c7d2]564 const char *label;
[ef9dac04]565 bool confirm;
[8227d63]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
[6c4eedf]591 cap_simplify(&pinfo.capacity);
[9854a8f]592
[6c4eedf]593 rc = cap_format(&pinfo.capacity, &scap);
[8227d63]594 if (rc != EOK) {
595 printf("Out of memory.\n");
596 goto error;
597 }
598
[21f1543]599 rc = fdisk_pkind_format(pinfo.pkind, &spkind);
[8227d63]600 if (rc != EOK) {
[21f1543]601 printf("\nOut of memory.\n");
[8227d63]602 goto error;
603 }
604
[21f1543]605 if (pinfo.pkind != lpk_extended) {
[2dab624]606 rc = fdsk_pcnt_fs_format(pinfo.pcnt, pinfo.fstype, &sfstype);
[21f1543]607 if (rc != EOK) {
608 printf("Out of memory.\n");
609 goto error;
610 }
611
[9c2c7d2]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);
[21f1543]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 }
[8227d63]630 }
631
[68b5dd11]632 rc = nchoice_add(choice, sdesc, (void *)part, 0);
[8227d63]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;
[21f1543]641 free(spkind);
642 spkind = NULL;
[8227d63]643 free(sfstype);
644 sfstype = NULL;
645 free(sdesc);
646 sdesc = NULL;
647
648 part = fdisk_part_next(part);
649 }
650
[21f1543]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
[8227d63]658 rc = nchoice_get(choice, &sel);
[ef9dac04]659 if (rc == ENOENT)
660 return EOK;
[8227d63]661 if (rc != EOK) {
662 printf("Error getting user selection.\n");
663 goto error;
664 }
665
666
[e96047c]667 nchoice_destroy(choice);
[ef9dac04]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
[e96047c]689 return EOK;
690error:
[8227d63]691 free(scap);
[21f1543]692 free(spkind);
[8227d63]693 free(sfstype);
694 free(sdesc);
695
[e96047c]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;
[8227d63]706 fdisk_part_t *part;
707 fdisk_part_info_t pinfo;
[6c4eedf]708 cap_spec_t cap;
709 cap_spec_t mcap;
[ea0ff6b]710 fdisk_dev_flags_t dflags;
[e96047c]711 char *sltype = NULL;
[8227d63]712 char *sdcap = NULL;
713 char *scap = NULL;
[852664b9]714 char *smcap = NULL;
[8227d63]715 char *sfstype = NULL;
716 char *svcname = NULL;
[b7a4d06]717 char *spkind;
[d858a660]718 const char *label;
[e96047c]719 int rc;
[8227d63]720 int npart;
[e96047c]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
[8227d63]737 rc = fdisk_dev_capacity(dev, &cap);
738 if (rc != EOK) {
739 printf("Error getting device capacity.\n");
740 goto error;
741 }
742
[6c4eedf]743 cap_simplify(&cap);
[9854a8f]744
[6c4eedf]745 rc = cap_format(&cap, &sdcap);
[8227d63]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
[ea0ff6b]757 fdisk_dev_get_flags(dev, &dflags);
758
[d858a660]759 printf("Device: %s (%s)\n", svcname, sdcap);
[22fb7ab]760 free(sdcap);
761 sdcap = NULL;
762
[e96047c]763 rc = fdisk_label_get_info(dev, &linfo);
764 if (rc != EOK) {
765 printf("Error getting label information.\n");
766 goto error;
767 }
768
[0ecfc62]769 switch (linfo.ltype) {
770 case lt_none:
771 printf("Disk contains no label.\n");
[22fb7ab]772 break;
[0ecfc62]773 default:
[22fb7ab]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 }
[e96047c]780
[22fb7ab]781 printf("Label type: %s\n", sltype);
782 free(sltype);
783 sltype = NULL;
784 break;
785 }
[8227d63]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
[6c4eedf]797 cap_simplify(&pinfo.capacity);
[9854a8f]798
[6c4eedf]799 rc = cap_format(&pinfo.capacity, &scap);
[8227d63]800 if (rc != EOK) {
801 printf("Out of memory.\n");
802 goto error;
803 }
804
[2dab624]805 rc = fdsk_pcnt_fs_format(pinfo.pcnt, pinfo.fstype, &sfstype);
[8227d63]806 if (rc != EOK) {
807 printf("Out of memory.\n");
808 goto error;
809 }
810
[d858a660]811 if (str_size(pinfo.label) > 0)
812 label = pinfo.label;
813 else
[9c2c7d2]814 label = "(No name)";
[d858a660]815
[edebb4a1]816 if (linfo.ltype == lt_none)
[d858a660]817 printf("Entire disk: %s %s", label, scap);
[edebb4a1]818 else
[d858a660]819 printf("Partition %d: %s %s", npart, label, scap);
[edebb4a1]820
[b7a4d06]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
[4b6635a7]832 if (pinfo.pkind != lpk_extended) {
[2dab624]833 printf(", %s", sfstype);
[4b6635a7]834 }
835
[b7a4d06]836 printf("\n");
837
[8227d63]838 free(scap);
839 scap = NULL;
840 free(sfstype);
841 sfstype = NULL;
842
843 part = fdisk_part_next(part);
844 }
[e96047c]845
[852664b9]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
[6c4eedf]854 cap_simplify(&mcap);
[9854a8f]855
[6c4eedf]856 rc = cap_format(&mcap, &smcap);
[852664b9]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
[6c4eedf]876 cap_simplify(&mcap);
[9854a8f]877
[6c4eedf]878 rc = cap_format(&mcap, &smcap);
[852664b9]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
[6c4eedf]901 cap_simplify(&mcap);
[9854a8f]902
[6c4eedf]903 rc = cap_format(&mcap, &smcap);
[852664b9]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
[6c4eedf]919 cap_simplify(&mcap);
[9854a8f]920
[6c4eedf]921 rc = cap_format(&mcap, &smcap);
[852664b9]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
[e96047c]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
[0ecfc62]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",
[68b5dd11]943 (void *)devac_create_pri_part, 0);
[0ecfc62]944 if (rc != EOK) {
945 assert(rc == ENOMEM);
946 printf("Out of memory.\n");
947 goto error;
[b7a4d06]948 }
[0ecfc62]949 }
[b7a4d06]950
[0ecfc62]951 if ((linfo.flags & lf_can_create_ext) != 0) {
952 rc = nchoice_add(choice, "Create extended "
953 "partition",
[68b5dd11]954 (void *)devac_create_ext_part, 0);
[0ecfc62]955 if (rc != EOK) {
956 assert(rc == ENOMEM);
957 printf("Out of memory.\n");
958 goto error;
[b7a4d06]959 }
[0ecfc62]960 }
[b7a4d06]961
[0ecfc62]962 if ((linfo.flags & lf_can_create_log) != 0) {
963 rc = nchoice_add(choice, "Create logical "
964 "partition",
[68b5dd11]965 (void *)devac_create_log_part, 0);
[0ecfc62]966 if (rc != EOK) {
967 assert(rc == ENOMEM);
968 printf("Out of memory.\n");
969 goto error;
[b7a4d06]970 }
[0ecfc62]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",
[68b5dd11]975 (void *)devac_create_pri_part, 0);
[0ecfc62]976 if (rc != EOK) {
977 assert(rc == ENOMEM);
[b7a4d06]978 printf("Out of memory.\n");
979 goto error;
980 }
[8227d63]981 }
982 }
983
[edebb4a1]984 if ((linfo.flags & lf_can_delete_part) != 0) {
[8227d63]985 rc = nchoice_add(choice, "Delete partition",
[68b5dd11]986 (void *)devac_delete_part, 0);
[8227d63]987 if (rc != EOK) {
988 assert(rc == ENOMEM);
989 printf("Out of memory.\n");
990 goto error;
991 }
992 }
993
[ea0ff6b]994 if ((dflags & fdf_can_create_label) != 0) {
[e96047c]995 rc = nchoice_add(choice, "Create label",
[68b5dd11]996 (void *)devac_create_label, 0);
[e96047c]997 if (rc != EOK) {
998 assert(rc == ENOMEM);
999 printf("Out of memory.\n");
1000 goto error;
1001 }
[ea0ff6b]1002 }
1003
1004 if ((dflags & fdf_can_delete_label) != 0) {
[e96047c]1005 rc = nchoice_add(choice, "Delete label",
[68b5dd11]1006 (void *)devac_delete_label, 0);
[e96047c]1007 if (rc != EOK) {
1008 assert(rc == ENOMEM);
1009 printf("Out of memory.\n");
1010 goto error;
1011 }
1012 }
1013
[ea0ff6b]1014 if ((dflags & fdf_can_erase_dev) != 0) {
1015 rc = nchoice_add(choice, "Erase disk",
[68b5dd11]1016 (void *)devac_erase_disk, 0);
[ea0ff6b]1017 if (rc != EOK) {
1018 assert(rc == ENOMEM);
1019 printf("Out of memory.\n");
1020 goto error;
1021 }
1022 }
1023
[68b5dd11]1024 rc = nchoice_add(choice, "Exit", (void *)devac_exit, 0);
[e96047c]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:
[22fb7ab]1039 (void) fdsk_create_label(dev);
[e96047c]1040 break;
1041 case devac_delete_label:
[22fb7ab]1042 (void) fdsk_delete_label(dev);
[e96047c]1043 break;
[ea0ff6b]1044 case devac_erase_disk:
1045 (void) fdsk_erase_disk(dev);
1046 break;
[b7a4d06]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);
[e96047c]1055 break;
1056 case devac_delete_part:
[22fb7ab]1057 (void) fdsk_delete_part(dev);
[e96047c]1058 break;
1059 case devac_exit:
1060 quit = true;
1061 break;
1062 }
1063
1064 nchoice_destroy(choice);
1065 return EOK;
1066error:
[8227d63]1067 free(sdcap);
1068 free(scap);
[852664b9]1069 free(smcap);
[8227d63]1070 free(sfstype);
1071 free(svcname);
[e96047c]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
[22fb7ab]1083 rc = fdisk_create(&fdisk);
1084 if (rc != EOK) {
1085 printf("Error initializing Fdisk.\n");
1086 return 1;
1087 }
1088
[e96047c]1089 rc = fdsk_dev_sel_choice(&svcid);
1090 if (rc != EOK)
1091 return 1;
1092
[8227d63]1093 if (svcid == 0)
1094 return 0;
1095
[22fb7ab]1096 rc = fdisk_dev_open(fdisk, svcid, &dev);
[e96047c]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);
[22fb7ab]1111 fdisk_destroy(fdisk);
[e96047c]1112
1113 return 0;
1114}
1115
1116
1117/** @}
1118 */
Note: See TracBrowser for help on using the repository browser.