[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>
|
---|
[1d6dd2a] | 44 | #include <str.h>
|
---|
[e96047c] | 45 |
|
---|
[9c2c7d2] | 46 | #define NO_LABEL_CAPTION "(No name)"
|
---|
| 47 |
|
---|
[e96047c] | 48 | static bool quit = false;
|
---|
[22fb7ab] | 49 | static fdisk_t *fdisk;
|
---|
[e96047c] | 50 |
|
---|
| 51 | /** Device menu actions */
|
---|
| 52 | typedef enum {
|
---|
| 53 | /** Create label */
|
---|
| 54 | devac_create_label,
|
---|
| 55 | /** Delete label */
|
---|
| 56 | devac_delete_label,
|
---|
[ea0ff6b] | 57 | /** Erase disk */
|
---|
| 58 | devac_erase_disk,
|
---|
[b7a4d06] | 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,
|
---|
[e96047c] | 65 | /** Delete partition */
|
---|
| 66 | devac_delete_part,
|
---|
| 67 | /** Exit */
|
---|
| 68 | devac_exit
|
---|
| 69 | } devac_t;
|
---|
| 70 |
|
---|
[b7fd2a0] | 71 | static errno_t fdsk_pcnt_fs_format(vol_part_cnt_t pcnt, vol_fstype_t fstype,
|
---|
[2dab624] | 72 | char **rstr)
|
---|
| 73 | {
|
---|
[b7fd2a0] | 74 | errno_t rc;
|
---|
[2dab624] | 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 |
|
---|
[ef9dac04] | 99 | /** Confirm user selection. */
|
---|
[b7fd2a0] | 100 | static errno_t fdsk_confirm(const char *msg, bool *rconfirm)
|
---|
[ef9dac04] | 101 | {
|
---|
| 102 | tinput_t *tinput = NULL;
|
---|
| 103 | char *answer;
|
---|
[b7fd2a0] | 104 | errno_t rc;
|
---|
[ef9dac04] | 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;
|
---|
| 142 | error:
|
---|
| 143 | if (tinput != NULL)
|
---|
| 144 | tinput_destroy(tinput);
|
---|
| 145 | return rc;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
[e96047c] | 148 | /** Device selection */
|
---|
[b7fd2a0] | 149 | static errno_t fdsk_dev_sel_choice(service_id_t *rsvcid)
|
---|
[e96047c] | 150 | {
|
---|
| 151 | fdisk_dev_list_t *devlist = NULL;
|
---|
| 152 | fdisk_dev_info_t *info;
|
---|
| 153 | nchoice_t *choice = NULL;
|
---|
| 154 | char *svcname = NULL;
|
---|
[6c4eedf] | 155 | cap_spec_t cap;
|
---|
[8227d63] | 156 | fdisk_dev_info_t *sdev;
|
---|
[e96047c] | 157 | char *scap = NULL;
|
---|
| 158 | char *dtext = NULL;
|
---|
| 159 | service_id_t svcid;
|
---|
| 160 | void *sel;
|
---|
[8227d63] | 161 | int ndevs;
|
---|
[b7fd2a0] | 162 | errno_t rc;
|
---|
[e96047c] | 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 |
|
---|
[22fb7ab] | 178 | rc = fdisk_dev_list_get(fdisk, &devlist);
|
---|
[e96047c] | 179 | if (rc != EOK) {
|
---|
| 180 | printf("Error getting device list.\n");
|
---|
| 181 | goto error;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | info = fdisk_dev_first(devlist);
|
---|
[8227d63] | 185 | ndevs = 0;
|
---|
[e96047c] | 186 | while (info != NULL) {
|
---|
[8227d63] | 187 | rc = fdisk_dev_info_get_svcname(info, &svcname);
|
---|
[e96047c] | 188 | if (rc != EOK) {
|
---|
[4627314] | 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;
|
---|
[e96047c] | 194 | }
|
---|
| 195 |
|
---|
[8227d63] | 196 | rc = fdisk_dev_info_capacity(info, &cap);
|
---|
[e96047c] | 197 | if (rc != EOK) {
|
---|
[4627314] | 198 | printf("Error getting device capacity "
|
---|
| 199 | "(device %s).\n", svcname);
|
---|
| 200 | info = fdisk_dev_next(info);
|
---|
| 201 | continue;
|
---|
[e96047c] | 202 | }
|
---|
| 203 |
|
---|
[6c4eedf] | 204 | cap_simplify(&cap);
|
---|
[9854a8f] | 205 |
|
---|
[6c4eedf] | 206 | rc = cap_format(&cap, &scap);
|
---|
[e96047c] | 207 | if (rc != EOK) {
|
---|
| 208 | assert(rc == ENOMEM);
|
---|
| 209 | printf("Out of memory.\n");
|
---|
| 210 | goto error;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
[d5c1051] | 213 | int ret = asprintf(&dtext, "%s (%s)", svcname, scap);
|
---|
| 214 | if (ret < 0) {
|
---|
[e96047c] | 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 |
|
---|
[68b5dd11] | 225 | rc = nchoice_add(choice, dtext, info, 0);
|
---|
[e96047c] | 226 | if (rc != EOK) {
|
---|
| 227 | assert(rc == ENOMEM);
|
---|
| 228 | printf("Out of memory.\n");
|
---|
| 229 | goto error;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
[4627314] | 232 | ++ndevs;
|
---|
| 233 |
|
---|
[e96047c] | 234 | free(dtext);
|
---|
| 235 | dtext = NULL;
|
---|
| 236 |
|
---|
| 237 | info = fdisk_dev_next(info);
|
---|
| 238 | }
|
---|
| 239 |
|
---|
[8227d63] | 240 | if (ndevs == 0) {
|
---|
| 241 | printf("No disk devices found.\n");
|
---|
| 242 | rc = ENOENT;
|
---|
| 243 | goto error;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
[68b5dd11] | 246 | rc = nchoice_add(choice, "Exit", NULL, 0);
|
---|
[8227d63] | 247 | if (rc != EOK) {
|
---|
| 248 | assert(rc == ENOMEM);
|
---|
| 249 | printf("Out of memory.\n");
|
---|
| 250 | goto error;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
[e96047c] | 253 | rc = nchoice_get(choice, &sel);
|
---|
| 254 | if (rc != EOK) {
|
---|
| 255 | printf("Error getting user selection.\n");
|
---|
| 256 | return rc;
|
---|
| 257 | }
|
---|
| 258 |
|
---|
[8227d63] | 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 | }
|
---|
[e96047c] | 265 |
|
---|
[8227d63] | 266 | fdisk_dev_list_free(devlist);
|
---|
[e96047c] | 267 |
|
---|
| 268 | nchoice_destroy(choice);
|
---|
| 269 | *rsvcid = svcid;
|
---|
| 270 | return EOK;
|
---|
| 271 | error:
|
---|
[9b07fba] | 272 | assert(rc != EOK);
|
---|
| 273 | *rsvcid = 0;
|
---|
[e96047c] | 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 |
|
---|
[b7fd2a0] | 284 | static errno_t fdsk_create_label(fdisk_dev_t *dev)
|
---|
[e96047c] | 285 | {
|
---|
| 286 | nchoice_t *choice = NULL;
|
---|
| 287 | void *sel;
|
---|
[8227d63] | 288 | char *sltype = NULL;
|
---|
| 289 | int i;
|
---|
[b7fd2a0] | 290 | errno_t rc;
|
---|
[e96047c] | 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 |
|
---|
[372df8f] | 306 | for (i = LT_FIRST; i < LT_LIMIT; i++) {
|
---|
[8227d63] | 307 | rc = fdisk_ltype_format(i, &sltype);
|
---|
| 308 | if (rc != EOK)
|
---|
| 309 | goto error;
|
---|
| 310 |
|
---|
[68b5dd11] | 311 | rc = nchoice_add(choice, sltype, (void *)(uintptr_t)i,
|
---|
| 312 | i == LT_DEFAULT);
|
---|
[8227d63] | 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 |
|
---|
[22fb7ab] | 329 | rc = fdisk_label_create(dev, (label_type_t)sel);
|
---|
[8227d63] | 330 | if (rc != EOK) {
|
---|
| 331 | printf("Error creating label.\n");
|
---|
| 332 | goto error;
|
---|
| 333 | }
|
---|
| 334 |
|
---|
| 335 | nchoice_destroy(choice);
|
---|
| 336 | return EOK;
|
---|
| 337 | error:
|
---|
| 338 | free(sltype);
|
---|
| 339 | if (choice != NULL)
|
---|
| 340 | nchoice_destroy(choice);
|
---|
| 341 | return rc;
|
---|
| 342 | }
|
---|
| 343 |
|
---|
[b7fd2a0] | 344 | static errno_t fdsk_delete_label(fdisk_dev_t *dev)
|
---|
[8227d63] | 345 | {
|
---|
[ef9dac04] | 346 | bool confirm;
|
---|
[b7fd2a0] | 347 | errno_t rc;
|
---|
[8227d63] | 348 |
|
---|
[ef9dac04] | 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 |
|
---|
[8227d63] | 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 |
|
---|
[b7fd2a0] | 368 | static errno_t fdsk_erase_disk(fdisk_dev_t *dev)
|
---|
[ea0ff6b] | 369 | {
|
---|
[ef9dac04] | 370 | bool confirm;
|
---|
[b7fd2a0] | 371 | errno_t rc;
|
---|
[ea0ff6b] | 372 |
|
---|
[ef9dac04] | 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 |
|
---|
[ea0ff6b] | 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 |
|
---|
[b7fd2a0] | 392 | static errno_t fdsk_select_fstype(vol_fstype_t *fstype)
|
---|
[8227d63] | 393 | {
|
---|
| 394 | nchoice_t *choice = NULL;
|
---|
| 395 | void *sel;
|
---|
| 396 | char *sfstype;
|
---|
| 397 | int i;
|
---|
[b7fd2a0] | 398 | errno_t rc;
|
---|
[8227d63] | 399 |
|
---|
| 400 | rc = nchoice_create(&choice);
|
---|
[e96047c] | 401 | if (rc != EOK) {
|
---|
| 402 | assert(rc == ENOMEM);
|
---|
| 403 | printf("Out of memory.\n");
|
---|
| 404 | goto error;
|
---|
| 405 | }
|
---|
| 406 |
|
---|
[8227d63] | 407 | rc = nchoice_set_prompt(choice, "Select file system type");
|
---|
[e96047c] | 408 | if (rc != EOK) {
|
---|
| 409 | assert(rc == ENOMEM);
|
---|
| 410 | printf("Out of memory.\n");
|
---|
| 411 | goto error;
|
---|
| 412 | }
|
---|
| 413 |
|
---|
[4b6635a7] | 414 | for (i = 0; i < VOL_FSTYPE_LIMIT; i++) {
|
---|
[8227d63] | 415 | rc = fdisk_fstype_format(i, &sfstype);
|
---|
| 416 | if (rc != EOK)
|
---|
| 417 | goto error;
|
---|
| 418 |
|
---|
[68b5dd11] | 419 | rc = nchoice_add(choice, sfstype, (void *)(uintptr_t)i,
|
---|
| 420 | i == VOL_FSTYPE_DEFAULT);
|
---|
[8227d63] | 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 |
|
---|
[e96047c] | 431 | rc = nchoice_get(choice, &sel);
|
---|
| 432 | if (rc != EOK) {
|
---|
| 433 | printf("Error getting user selection.\n");
|
---|
| 434 | goto error;
|
---|
| 435 | }
|
---|
| 436 |
|
---|
[8227d63] | 437 | nchoice_destroy(choice);
|
---|
[4b6635a7] | 438 | *fstype = (vol_fstype_t)sel;
|
---|
[8227d63] | 439 | return EOK;
|
---|
| 440 | error:
|
---|
| 441 | free(sfstype);
|
---|
| 442 | if (choice != NULL)
|
---|
| 443 | nchoice_destroy(choice);
|
---|
| 444 | return rc;
|
---|
| 445 | }
|
---|
| 446 |
|
---|
[b7fd2a0] | 447 | static errno_t fdsk_create_part(fdisk_dev_t *dev, label_pkind_t pkind)
|
---|
[8227d63] | 448 | {
|
---|
[b7fd2a0] | 449 | errno_t rc;
|
---|
[8227d63] | 450 | fdisk_part_spec_t pspec;
|
---|
[6c4eedf] | 451 | cap_spec_t cap;
|
---|
| 452 | cap_spec_t mcap;
|
---|
[9c2c7d2] | 453 | vol_label_supp_t vlsupp;
|
---|
[4b6635a7] | 454 | vol_fstype_t fstype = 0;
|
---|
[8227d63] | 455 | tinput_t *tinput = NULL;
|
---|
[b598460a] | 456 | fdisk_spc_t spc;
|
---|
[8227d63] | 457 | char *scap;
|
---|
[b598460a] | 458 | char *smcap = NULL;
|
---|
[9c2c7d2] | 459 | char *label = NULL;
|
---|
[b598460a] | 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 |
|
---|
[6c4eedf] | 472 | cap_simplify(&mcap);
|
---|
[03661d19] | 473 |
|
---|
[6c4eedf] | 474 | rc = cap_format(&mcap, &smcap);
|
---|
[b598460a] | 475 | if (rc != EOK) {
|
---|
| 476 | rc = ENOMEM;
|
---|
| 477 | goto error;
|
---|
| 478 | }
|
---|
[8227d63] | 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");
|
---|
[b598460a] | 492 | rc = tinput_read_i(tinput, smcap, &scap);
|
---|
[8227d63] | 493 | if (rc != EOK)
|
---|
| 494 | goto error;
|
---|
| 495 |
|
---|
[6c4eedf] | 496 | rc = cap_parse(scap, &cap);
|
---|
[8227d63] | 497 | if (rc == EOK)
|
---|
| 498 | break;
|
---|
| 499 | }
|
---|
| 500 |
|
---|
| 501 | tinput_destroy(tinput);
|
---|
[78d50bd] | 502 | tinput = NULL;
|
---|
[b598460a] | 503 | free(smcap);
|
---|
| 504 | smcap = NULL;
|
---|
[8227d63] | 505 |
|
---|
[b7a4d06] | 506 | if (pkind != lpk_extended) {
|
---|
| 507 | rc = fdsk_select_fstype(&fstype);
|
---|
| 508 | if (rc != EOK)
|
---|
| 509 | goto error;
|
---|
| 510 | }
|
---|
[8227d63] | 511 |
|
---|
[9c2c7d2] | 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");
|
---|
[f3504c1] | 526 | rc = tinput_read_i(tinput, "New volume", &label);
|
---|
[9c2c7d2] | 527 | if (rc != EOK)
|
---|
| 528 | goto error;
|
---|
| 529 |
|
---|
| 530 | tinput_destroy(tinput);
|
---|
| 531 | tinput = NULL;
|
---|
| 532 | }
|
---|
| 533 |
|
---|
[8227d63] | 534 | fdisk_pspec_init(&pspec);
|
---|
| 535 | pspec.capacity = cap;
|
---|
[b7a4d06] | 536 | pspec.pkind = pkind;
|
---|
[8227d63] | 537 | pspec.fstype = fstype;
|
---|
[9c2c7d2] | 538 | pspec.label = label;
|
---|
[8227d63] | 539 |
|
---|
| 540 | rc = fdisk_part_create(dev, &pspec, NULL);
|
---|
[e96047c] | 541 | if (rc != EOK) {
|
---|
[8227d63] | 542 | printf("Error creating partition.\n");
|
---|
[e96047c] | 543 | goto error;
|
---|
| 544 | }
|
---|
| 545 |
|
---|
[9c2c7d2] | 546 | free(label);
|
---|
[8227d63] | 547 | return EOK;
|
---|
| 548 | error:
|
---|
[b598460a] | 549 | free(smcap);
|
---|
[9c2c7d2] | 550 | free(label);
|
---|
[8227d63] | 551 | if (tinput != NULL)
|
---|
| 552 | tinput_destroy(tinput);
|
---|
| 553 | return rc;
|
---|
| 554 | }
|
---|
| 555 |
|
---|
[b7fd2a0] | 556 | static errno_t fdsk_delete_part(fdisk_dev_t *dev)
|
---|
[8227d63] | 557 | {
|
---|
| 558 | nchoice_t *choice = NULL;
|
---|
| 559 | fdisk_part_t *part;
|
---|
| 560 | fdisk_part_info_t pinfo;
|
---|
| 561 | char *scap = NULL;
|
---|
[21f1543] | 562 | char *spkind = NULL;
|
---|
[8227d63] | 563 | char *sfstype = NULL;
|
---|
| 564 | char *sdesc = NULL;
|
---|
[9c2c7d2] | 565 | const char *label;
|
---|
[ef9dac04] | 566 | bool confirm;
|
---|
[8227d63] | 567 | void *sel;
|
---|
[b7fd2a0] | 568 | errno_t rc;
|
---|
[8227d63] | 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 |
|
---|
[6c4eedf] | 592 | cap_simplify(&pinfo.capacity);
|
---|
[9854a8f] | 593 |
|
---|
[6c4eedf] | 594 | rc = cap_format(&pinfo.capacity, &scap);
|
---|
[8227d63] | 595 | if (rc != EOK) {
|
---|
| 596 | printf("Out of memory.\n");
|
---|
| 597 | goto error;
|
---|
| 598 | }
|
---|
| 599 |
|
---|
[21f1543] | 600 | rc = fdisk_pkind_format(pinfo.pkind, &spkind);
|
---|
[8227d63] | 601 | if (rc != EOK) {
|
---|
[21f1543] | 602 | printf("\nOut of memory.\n");
|
---|
[8227d63] | 603 | goto error;
|
---|
| 604 | }
|
---|
| 605 |
|
---|
[21f1543] | 606 | if (pinfo.pkind != lpk_extended) {
|
---|
[2dab624] | 607 | rc = fdsk_pcnt_fs_format(pinfo.pcnt, pinfo.fstype, &sfstype);
|
---|
[21f1543] | 608 | if (rc != EOK) {
|
---|
| 609 | printf("Out of memory.\n");
|
---|
| 610 | goto error;
|
---|
| 611 | }
|
---|
| 612 |
|
---|
[9c2c7d2] | 613 | if (str_size(pinfo.label) > 0)
|
---|
| 614 | label = pinfo.label;
|
---|
| 615 | else
|
---|
| 616 | label = "(No name)";
|
---|
| 617 |
|
---|
[d5c1051] | 618 | int ret = asprintf(&sdesc, "%s %s, %s, %s", label,
|
---|
[9c2c7d2] | 619 | scap, spkind, sfstype);
|
---|
[d5c1051] | 620 | if (ret < 0) {
|
---|
[21f1543] | 621 | rc = ENOMEM;
|
---|
| 622 | goto error;
|
---|
| 623 | }
|
---|
| 624 |
|
---|
| 625 | } else {
|
---|
[d5c1051] | 626 | int ret = asprintf(&sdesc, "%s, %s", scap, spkind);
|
---|
| 627 | if (ret < 0) {
|
---|
[21f1543] | 628 | rc = ENOMEM;
|
---|
| 629 | goto error;
|
---|
| 630 | }
|
---|
[8227d63] | 631 | }
|
---|
| 632 |
|
---|
[68b5dd11] | 633 | rc = nchoice_add(choice, sdesc, (void *)part, 0);
|
---|
[8227d63] | 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;
|
---|
[21f1543] | 642 | free(spkind);
|
---|
| 643 | spkind = NULL;
|
---|
[8227d63] | 644 | free(sfstype);
|
---|
| 645 | sfstype = NULL;
|
---|
| 646 | free(sdesc);
|
---|
| 647 | sdesc = NULL;
|
---|
| 648 |
|
---|
| 649 | part = fdisk_part_next(part);
|
---|
| 650 | }
|
---|
| 651 |
|
---|
[21f1543] | 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 |
|
---|
[8227d63] | 659 | rc = nchoice_get(choice, &sel);
|
---|
[ef9dac04] | 660 | if (rc == ENOENT)
|
---|
| 661 | return EOK;
|
---|
[8227d63] | 662 | if (rc != EOK) {
|
---|
| 663 | printf("Error getting user selection.\n");
|
---|
| 664 | goto error;
|
---|
| 665 | }
|
---|
| 666 |
|
---|
| 667 |
|
---|
[e96047c] | 668 | nchoice_destroy(choice);
|
---|
[ef9dac04] | 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 |
|
---|
[e96047c] | 690 | return EOK;
|
---|
| 691 | error:
|
---|
[8227d63] | 692 | free(scap);
|
---|
[21f1543] | 693 | free(spkind);
|
---|
[8227d63] | 694 | free(sfstype);
|
---|
| 695 | free(sdesc);
|
---|
| 696 |
|
---|
[e96047c] | 697 | if (choice != NULL)
|
---|
| 698 | nchoice_destroy(choice);
|
---|
| 699 | return rc;
|
---|
| 700 | }
|
---|
| 701 |
|
---|
| 702 | /** Device menu */
|
---|
[b7fd2a0] | 703 | static errno_t fdsk_dev_menu(fdisk_dev_t *dev)
|
---|
[e96047c] | 704 | {
|
---|
| 705 | nchoice_t *choice = NULL;
|
---|
| 706 | fdisk_label_info_t linfo;
|
---|
[8227d63] | 707 | fdisk_part_t *part;
|
---|
| 708 | fdisk_part_info_t pinfo;
|
---|
[6c4eedf] | 709 | cap_spec_t cap;
|
---|
| 710 | cap_spec_t mcap;
|
---|
[ea0ff6b] | 711 | fdisk_dev_flags_t dflags;
|
---|
[e96047c] | 712 | char *sltype = NULL;
|
---|
[8227d63] | 713 | char *sdcap = NULL;
|
---|
| 714 | char *scap = NULL;
|
---|
[852664b9] | 715 | char *smcap = NULL;
|
---|
[8227d63] | 716 | char *sfstype = NULL;
|
---|
| 717 | char *svcname = NULL;
|
---|
[b7a4d06] | 718 | char *spkind;
|
---|
[d858a660] | 719 | const char *label;
|
---|
[b7fd2a0] | 720 | errno_t rc;
|
---|
[8227d63] | 721 | int npart;
|
---|
[e96047c] | 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 |
|
---|
[8227d63] | 738 | rc = fdisk_dev_capacity(dev, &cap);
|
---|
| 739 | if (rc != EOK) {
|
---|
| 740 | printf("Error getting device capacity.\n");
|
---|
| 741 | goto error;
|
---|
| 742 | }
|
---|
| 743 |
|
---|
[6c4eedf] | 744 | cap_simplify(&cap);
|
---|
[9854a8f] | 745 |
|
---|
[6c4eedf] | 746 | rc = cap_format(&cap, &sdcap);
|
---|
[8227d63] | 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 |
|
---|
[ea0ff6b] | 758 | fdisk_dev_get_flags(dev, &dflags);
|
---|
| 759 |
|
---|
[d858a660] | 760 | printf("Device: %s (%s)\n", svcname, sdcap);
|
---|
[22fb7ab] | 761 | free(sdcap);
|
---|
| 762 | sdcap = NULL;
|
---|
| 763 |
|
---|
[e96047c] | 764 | rc = fdisk_label_get_info(dev, &linfo);
|
---|
| 765 | if (rc != EOK) {
|
---|
| 766 | printf("Error getting label information.\n");
|
---|
| 767 | goto error;
|
---|
| 768 | }
|
---|
| 769 |
|
---|
[0ecfc62] | 770 | switch (linfo.ltype) {
|
---|
| 771 | case lt_none:
|
---|
| 772 | printf("Disk contains no label.\n");
|
---|
[22fb7ab] | 773 | break;
|
---|
[0ecfc62] | 774 | default:
|
---|
[22fb7ab] | 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 | }
|
---|
[e96047c] | 781 |
|
---|
[22fb7ab] | 782 | printf("Label type: %s\n", sltype);
|
---|
| 783 | free(sltype);
|
---|
| 784 | sltype = NULL;
|
---|
| 785 | break;
|
---|
| 786 | }
|
---|
[8227d63] | 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 |
|
---|
[6c4eedf] | 798 | cap_simplify(&pinfo.capacity);
|
---|
[9854a8f] | 799 |
|
---|
[6c4eedf] | 800 | rc = cap_format(&pinfo.capacity, &scap);
|
---|
[8227d63] | 801 | if (rc != EOK) {
|
---|
| 802 | printf("Out of memory.\n");
|
---|
| 803 | goto error;
|
---|
| 804 | }
|
---|
| 805 |
|
---|
[2dab624] | 806 | rc = fdsk_pcnt_fs_format(pinfo.pcnt, pinfo.fstype, &sfstype);
|
---|
[8227d63] | 807 | if (rc != EOK) {
|
---|
| 808 | printf("Out of memory.\n");
|
---|
| 809 | goto error;
|
---|
| 810 | }
|
---|
| 811 |
|
---|
[d858a660] | 812 | if (str_size(pinfo.label) > 0)
|
---|
| 813 | label = pinfo.label;
|
---|
| 814 | else
|
---|
[9c2c7d2] | 815 | label = "(No name)";
|
---|
[d858a660] | 816 |
|
---|
[edebb4a1] | 817 | if (linfo.ltype == lt_none)
|
---|
[d858a660] | 818 | printf("Entire disk: %s %s", label, scap);
|
---|
[edebb4a1] | 819 | else
|
---|
[d858a660] | 820 | printf("Partition %d: %s %s", npart, label, scap);
|
---|
[edebb4a1] | 821 |
|
---|
[b7a4d06] | 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 |
|
---|
[4b6635a7] | 833 | if (pinfo.pkind != lpk_extended) {
|
---|
[2dab624] | 834 | printf(", %s", sfstype);
|
---|
[4b6635a7] | 835 | }
|
---|
| 836 |
|
---|
[b7a4d06] | 837 | printf("\n");
|
---|
| 838 |
|
---|
[8227d63] | 839 | free(scap);
|
---|
| 840 | scap = NULL;
|
---|
| 841 | free(sfstype);
|
---|
| 842 | sfstype = NULL;
|
---|
| 843 |
|
---|
| 844 | part = fdisk_part_next(part);
|
---|
| 845 | }
|
---|
[e96047c] | 846 |
|
---|
[852664b9] | 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 |
|
---|
[6c4eedf] | 855 | cap_simplify(&mcap);
|
---|
[9854a8f] | 856 |
|
---|
[6c4eedf] | 857 | rc = cap_format(&mcap, &smcap);
|
---|
[852664b9] | 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 |
|
---|
[6c4eedf] | 877 | cap_simplify(&mcap);
|
---|
[9854a8f] | 878 |
|
---|
[6c4eedf] | 879 | rc = cap_format(&mcap, &smcap);
|
---|
[852664b9] | 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 |
|
---|
[6c4eedf] | 902 | cap_simplify(&mcap);
|
---|
[9854a8f] | 903 |
|
---|
[6c4eedf] | 904 | rc = cap_format(&mcap, &smcap);
|
---|
[852664b9] | 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 |
|
---|
[6c4eedf] | 920 | cap_simplify(&mcap);
|
---|
[9854a8f] | 921 |
|
---|
[6c4eedf] | 922 | rc = cap_format(&mcap, &smcap);
|
---|
[852664b9] | 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 |
|
---|
[e96047c] | 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 |
|
---|
[0ecfc62] | 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",
|
---|
[68b5dd11] | 944 | (void *)devac_create_pri_part, 0);
|
---|
[0ecfc62] | 945 | if (rc != EOK) {
|
---|
| 946 | assert(rc == ENOMEM);
|
---|
| 947 | printf("Out of memory.\n");
|
---|
| 948 | goto error;
|
---|
[b7a4d06] | 949 | }
|
---|
[0ecfc62] | 950 | }
|
---|
[b7a4d06] | 951 |
|
---|
[0ecfc62] | 952 | if ((linfo.flags & lf_can_create_ext) != 0) {
|
---|
| 953 | rc = nchoice_add(choice, "Create extended "
|
---|
| 954 | "partition",
|
---|
[68b5dd11] | 955 | (void *)devac_create_ext_part, 0);
|
---|
[0ecfc62] | 956 | if (rc != EOK) {
|
---|
| 957 | assert(rc == ENOMEM);
|
---|
| 958 | printf("Out of memory.\n");
|
---|
| 959 | goto error;
|
---|
[b7a4d06] | 960 | }
|
---|
[0ecfc62] | 961 | }
|
---|
[b7a4d06] | 962 |
|
---|
[0ecfc62] | 963 | if ((linfo.flags & lf_can_create_log) != 0) {
|
---|
| 964 | rc = nchoice_add(choice, "Create logical "
|
---|
| 965 | "partition",
|
---|
[68b5dd11] | 966 | (void *)devac_create_log_part, 0);
|
---|
[0ecfc62] | 967 | if (rc != EOK) {
|
---|
| 968 | assert(rc == ENOMEM);
|
---|
| 969 | printf("Out of memory.\n");
|
---|
| 970 | goto error;
|
---|
[b7a4d06] | 971 | }
|
---|
[0ecfc62] | 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",
|
---|
[68b5dd11] | 976 | (void *)devac_create_pri_part, 0);
|
---|
[0ecfc62] | 977 | if (rc != EOK) {
|
---|
| 978 | assert(rc == ENOMEM);
|
---|
[b7a4d06] | 979 | printf("Out of memory.\n");
|
---|
| 980 | goto error;
|
---|
| 981 | }
|
---|
[8227d63] | 982 | }
|
---|
| 983 | }
|
---|
| 984 |
|
---|
[edebb4a1] | 985 | if ((linfo.flags & lf_can_delete_part) != 0) {
|
---|
[8227d63] | 986 | rc = nchoice_add(choice, "Delete partition",
|
---|
[68b5dd11] | 987 | (void *)devac_delete_part, 0);
|
---|
[8227d63] | 988 | if (rc != EOK) {
|
---|
| 989 | assert(rc == ENOMEM);
|
---|
| 990 | printf("Out of memory.\n");
|
---|
| 991 | goto error;
|
---|
| 992 | }
|
---|
| 993 | }
|
---|
| 994 |
|
---|
[ea0ff6b] | 995 | if ((dflags & fdf_can_create_label) != 0) {
|
---|
[e96047c] | 996 | rc = nchoice_add(choice, "Create label",
|
---|
[68b5dd11] | 997 | (void *)devac_create_label, 0);
|
---|
[e96047c] | 998 | if (rc != EOK) {
|
---|
| 999 | assert(rc == ENOMEM);
|
---|
| 1000 | printf("Out of memory.\n");
|
---|
| 1001 | goto error;
|
---|
| 1002 | }
|
---|
[ea0ff6b] | 1003 | }
|
---|
| 1004 |
|
---|
| 1005 | if ((dflags & fdf_can_delete_label) != 0) {
|
---|
[e96047c] | 1006 | rc = nchoice_add(choice, "Delete label",
|
---|
[68b5dd11] | 1007 | (void *)devac_delete_label, 0);
|
---|
[e96047c] | 1008 | if (rc != EOK) {
|
---|
| 1009 | assert(rc == ENOMEM);
|
---|
| 1010 | printf("Out of memory.\n");
|
---|
| 1011 | goto error;
|
---|
| 1012 | }
|
---|
| 1013 | }
|
---|
| 1014 |
|
---|
[ea0ff6b] | 1015 | if ((dflags & fdf_can_erase_dev) != 0) {
|
---|
| 1016 | rc = nchoice_add(choice, "Erase disk",
|
---|
[68b5dd11] | 1017 | (void *)devac_erase_disk, 0);
|
---|
[ea0ff6b] | 1018 | if (rc != EOK) {
|
---|
| 1019 | assert(rc == ENOMEM);
|
---|
| 1020 | printf("Out of memory.\n");
|
---|
| 1021 | goto error;
|
---|
| 1022 | }
|
---|
| 1023 | }
|
---|
| 1024 |
|
---|
[68b5dd11] | 1025 | rc = nchoice_add(choice, "Exit", (void *)devac_exit, 0);
|
---|
[e96047c] | 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:
|
---|
[22fb7ab] | 1040 | (void) fdsk_create_label(dev);
|
---|
[e96047c] | 1041 | break;
|
---|
| 1042 | case devac_delete_label:
|
---|
[22fb7ab] | 1043 | (void) fdsk_delete_label(dev);
|
---|
[e96047c] | 1044 | break;
|
---|
[ea0ff6b] | 1045 | case devac_erase_disk:
|
---|
| 1046 | (void) fdsk_erase_disk(dev);
|
---|
| 1047 | break;
|
---|
[b7a4d06] | 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);
|
---|
[e96047c] | 1056 | break;
|
---|
| 1057 | case devac_delete_part:
|
---|
[22fb7ab] | 1058 | (void) fdsk_delete_part(dev);
|
---|
[e96047c] | 1059 | break;
|
---|
| 1060 | case devac_exit:
|
---|
| 1061 | quit = true;
|
---|
| 1062 | break;
|
---|
| 1063 | }
|
---|
| 1064 |
|
---|
| 1065 | nchoice_destroy(choice);
|
---|
| 1066 | return EOK;
|
---|
| 1067 | error:
|
---|
[8227d63] | 1068 | free(sdcap);
|
---|
| 1069 | free(scap);
|
---|
[852664b9] | 1070 | free(smcap);
|
---|
[8227d63] | 1071 | free(sfstype);
|
---|
| 1072 | free(svcname);
|
---|
[e96047c] | 1073 | if (choice != NULL)
|
---|
| 1074 | nchoice_destroy(choice);
|
---|
| 1075 | return rc;
|
---|
| 1076 | }
|
---|
| 1077 |
|
---|
| 1078 | int main(int argc, char *argv[])
|
---|
| 1079 | {
|
---|
| 1080 | service_id_t svcid;
|
---|
| 1081 | fdisk_dev_t *dev;
|
---|
[b7fd2a0] | 1082 | errno_t rc;
|
---|
[e96047c] | 1083 |
|
---|
[22fb7ab] | 1084 | rc = fdisk_create(&fdisk);
|
---|
| 1085 | if (rc != EOK) {
|
---|
| 1086 | printf("Error initializing Fdisk.\n");
|
---|
| 1087 | return 1;
|
---|
| 1088 | }
|
---|
| 1089 |
|
---|
[e96047c] | 1090 | rc = fdsk_dev_sel_choice(&svcid);
|
---|
| 1091 | if (rc != EOK)
|
---|
| 1092 | return 1;
|
---|
| 1093 |
|
---|
[8227d63] | 1094 | if (svcid == 0)
|
---|
| 1095 | return 0;
|
---|
| 1096 |
|
---|
[22fb7ab] | 1097 | rc = fdisk_dev_open(fdisk, svcid, &dev);
|
---|
[e96047c] | 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);
|
---|
[22fb7ab] | 1112 | fdisk_destroy(fdisk);
|
---|
[e96047c] | 1113 |
|
---|
| 1114 | return 0;
|
---|
| 1115 | }
|
---|
| 1116 |
|
---|
| 1117 |
|
---|
| 1118 | /** @}
|
---|
| 1119 | */
|
---|