Changeset 8227d63 in mainline for uspace/app/fdisk/fdisk.c
- Timestamp:
- 2015-06-22T21:20:23Z (9 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 1356f85a
- Parents:
- e96047c
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/fdisk/fdisk.c
re96047c r8227d63 66 66 char *svcname = NULL; 67 67 fdisk_cap_t cap; 68 fdisk_dev_info_t *sdev; 68 69 char *scap = NULL; 69 70 char *dtext = NULL; 70 71 service_id_t svcid; 71 72 void *sel; 73 int ndevs; 72 74 int rc; 73 75 … … 93 95 94 96 info = fdisk_dev_first(devlist); 97 ndevs = 0; 95 98 while (info != NULL) { 96 rc = fdisk_dev_get_svcname(info, &svcname); 99 ++ndevs; 100 101 rc = fdisk_dev_info_get_svcname(info, &svcname); 97 102 if (rc != EOK) { 98 103 printf("Error getting device service name.\n"); … … 100 105 } 101 106 102 rc = fdisk_dev_ capacity(info, &cap);107 rc = fdisk_dev_info_capacity(info, &cap); 103 108 if (rc != EOK) { 104 109 printf("Error getting device capacity.\n"); … … 138 143 } 139 144 145 if (ndevs == 0) { 146 printf("No disk devices found.\n"); 147 rc = ENOENT; 148 goto error; 149 } 150 151 rc = nchoice_add(choice, "Exit", NULL); 152 if (rc != EOK) { 153 assert(rc == ENOMEM); 154 printf("Out of memory.\n"); 155 goto error; 156 } 157 140 158 rc = nchoice_get(choice, &sel); 141 159 if (rc != EOK) { … … 144 162 } 145 163 146 fdisk_dev_get_svcid((fdisk_dev_info_t *)sel, &svcid); 164 sdev = (fdisk_dev_info_t *)sel; 165 if (sdev != NULL) { 166 fdisk_dev_info_get_svcid(sdev, &svcid); 167 } else { 168 svcid = 0; 169 } 170 147 171 fdisk_dev_list_free(devlist); 148 149 printf("Selected dev with sid=%zu\n", svcid);150 172 151 173 nchoice_destroy(choice); … … 167 189 nchoice_t *choice = NULL; 168 190 void *sel; 191 char *sltype = NULL; 192 int i; 169 193 int rc; 170 194 … … 183 207 } 184 208 185 rc = nchoice_add(choice, "GPT", (void *)fdl_gpt); 186 if (rc != EOK) { 187 assert(rc == ENOMEM); 188 printf("Out of memory.\n"); 189 goto error; 190 } 191 192 rc = nchoice_add(choice, "MBR", (void *)fdl_mbr); 193 if (rc != EOK) { 194 assert(rc == ENOMEM); 195 printf("Out of memory.\n"); 196 goto error; 209 for (i = FDL_CREATE_LO; i < FDL_CREATE_HI; i++) { 210 rc = fdisk_ltype_format(i, &sltype); 211 if (rc != EOK) 212 goto error; 213 214 rc = nchoice_add(choice, sltype, (void *)(uintptr_t)i); 215 if (rc != EOK) { 216 assert(rc == ENOMEM); 217 printf("Out of memory.\n"); 218 goto error; 219 } 220 221 free(sltype); 222 sltype = NULL; 197 223 } 198 224 … … 212 238 return EOK; 213 239 error: 240 free(sltype); 214 241 if (choice != NULL) 215 242 nchoice_destroy(choice); … … 217 244 } 218 245 246 static int fdsk_delete_label(fdisk_dev_t *dev) 247 { 248 int rc; 249 250 rc = fdisk_label_destroy(dev); 251 if (rc != EOK) { 252 printf("Error deleting label.\n"); 253 return rc; 254 } 255 256 return EOK; 257 } 258 259 static int fdsk_select_fstype(fdisk_fstype_t *fstype) 260 { 261 nchoice_t *choice = NULL; 262 void *sel; 263 char *sfstype; 264 int i; 265 int rc; 266 267 rc = nchoice_create(&choice); 268 if (rc != EOK) { 269 assert(rc == ENOMEM); 270 printf("Out of memory.\n"); 271 goto error; 272 } 273 274 rc = nchoice_set_prompt(choice, "Select file system type"); 275 if (rc != EOK) { 276 assert(rc == ENOMEM); 277 printf("Out of memory.\n"); 278 goto error; 279 } 280 281 for (i = FDFS_CREATE_LO; i < FDFS_CREATE_HI; i++) { 282 rc = fdisk_fstype_format(i, &sfstype); 283 if (rc != EOK) 284 goto error; 285 286 rc = nchoice_add(choice, sfstype, (void *)(uintptr_t)i); 287 if (rc != EOK) { 288 assert(rc == ENOMEM); 289 printf("Out of memory.\n"); 290 goto error; 291 } 292 293 free(sfstype); 294 sfstype = 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 nchoice_destroy(choice); 304 *fstype = (fdisk_fstype_t)sel; 305 return EOK; 306 error: 307 free(sfstype); 308 if (choice != NULL) 309 nchoice_destroy(choice); 310 return rc; 311 } 312 313 static int fdsk_create_part(fdisk_dev_t *dev) 314 { 315 int rc; 316 fdisk_part_spec_t pspec; 317 fdisk_cap_t cap; 318 fdisk_fstype_t fstype; 319 tinput_t *tinput = NULL; 320 char *scap; 321 322 tinput = tinput_new(); 323 if (tinput == NULL) { 324 rc = ENOMEM; 325 goto error; 326 } 327 328 rc = tinput_set_prompt(tinput, "?> "); 329 if (rc != EOK) 330 goto error; 331 332 while (true) { 333 printf("Enter capacity of new partition.\n"); 334 rc = tinput_read(tinput, &scap); 335 if (rc != EOK) 336 goto error; 337 338 rc = fdisk_cap_parse(scap, &cap); 339 if (rc == EOK) 340 break; 341 } 342 343 tinput_destroy(tinput); 344 345 rc = fdsk_select_fstype(&fstype); 346 if (rc != EOK) 347 goto error; 348 349 fdisk_pspec_init(&pspec); 350 pspec.capacity = cap; 351 pspec.fstype = fstype; 352 353 rc = fdisk_part_create(dev, &pspec, NULL); 354 if (rc != EOK) { 355 printf("Error creating partition.\n"); 356 goto error; 357 } 358 359 return EOK; 360 error: 361 if (tinput != NULL) 362 tinput_destroy(tinput); 363 return rc; 364 } 365 366 static int fdsk_delete_part(fdisk_dev_t *dev) 367 { 368 nchoice_t *choice = NULL; 369 fdisk_part_t *part; 370 fdisk_part_info_t pinfo; 371 char *scap = NULL; 372 char *sfstype = NULL; 373 char *sdesc = NULL; 374 void *sel; 375 int rc; 376 377 rc = nchoice_create(&choice); 378 if (rc != EOK) { 379 assert(rc == ENOMEM); 380 printf("Out of memory.\n"); 381 goto error; 382 } 383 384 rc = nchoice_set_prompt(choice, "Select partition to delete"); 385 if (rc != EOK) { 386 assert(rc == ENOMEM); 387 printf("Out of memory.\n"); 388 goto error; 389 } 390 391 part = fdisk_part_first(dev); 392 while (part != NULL) { 393 rc = fdisk_part_get_info(part, &pinfo); 394 if (rc != EOK) { 395 printf("Error getting partition information.\n"); 396 goto error; 397 } 398 399 rc = fdisk_cap_format(&pinfo.capacity, &scap); 400 if (rc != EOK) { 401 printf("Out of memory.\n"); 402 goto error; 403 } 404 405 rc = fdisk_fstype_format(pinfo.fstype, &sfstype); 406 if (rc != EOK) { 407 printf("Out of memory.\n"); 408 goto error; 409 } 410 411 rc = asprintf(&sdesc, "%s, %s", scap, sfstype); 412 if (rc < 0) { 413 rc = ENOMEM; 414 goto error; 415 } 416 417 rc = nchoice_add(choice, sdesc, (void *)part); 418 if (rc != EOK) { 419 assert(rc == ENOMEM); 420 printf("Out of memory.\n"); 421 goto error; 422 } 423 424 free(scap); 425 scap = NULL; 426 free(sfstype); 427 sfstype = NULL; 428 free(sdesc); 429 sdesc = NULL; 430 431 part = fdisk_part_next(part); 432 } 433 434 rc = nchoice_get(choice, &sel); 435 if (rc != EOK) { 436 printf("Error getting user selection.\n"); 437 goto error; 438 } 439 440 rc = fdisk_part_destroy((fdisk_part_t *)sel); 441 if (rc != EOK) { 442 printf("Error deleting partition.\n"); 443 return rc; 444 } 445 446 nchoice_destroy(choice); 447 return EOK; 448 error: 449 free(scap); 450 free(sfstype); 451 free(sdesc); 452 453 if (choice != NULL) 454 nchoice_destroy(choice); 455 return rc; 456 } 457 219 458 /** Device menu */ 220 459 static int fdsk_dev_menu(fdisk_dev_t *dev) … … 222 461 nchoice_t *choice = NULL; 223 462 fdisk_label_info_t linfo; 463 fdisk_part_t *part; 464 fdisk_part_info_t pinfo; 465 fdisk_cap_t cap; 224 466 char *sltype = NULL; 225 int rc; 467 char *sdcap = NULL; 468 char *scap = NULL; 469 char *sfstype = NULL; 470 char *svcname = NULL; 471 int rc; 472 int npart; 226 473 void *sel; 227 474 … … 240 487 } 241 488 489 rc = fdisk_dev_capacity(dev, &cap); 490 if (rc != EOK) { 491 printf("Error getting device capacity.\n"); 492 goto error; 493 } 494 495 rc = fdisk_cap_format(&cap, &sdcap); 496 if (rc != EOK) { 497 printf("Out of memory.\n"); 498 goto error; 499 } 500 501 rc = fdisk_dev_get_svcname(dev, &svcname); 502 if (rc != EOK) { 503 printf("Error getting device service name.\n"); 504 goto error; 505 } 506 242 507 rc = fdisk_label_get_info(dev, &linfo); 243 508 if (rc != EOK) { … … 253 518 } 254 519 520 printf("Device: %s, %s\n", sdcap, svcname); 255 521 printf("Label type: %s\n", sltype); 256 522 free(sltype); 257 523 sltype = NULL; 524 free(sdcap); 525 sdcap = NULL; 526 527 part = fdisk_part_first(dev); 528 npart = 0; 529 while (part != NULL) { 530 ++npart; 531 rc = fdisk_part_get_info(part, &pinfo); 532 if (rc != EOK) { 533 printf("Error getting partition information.\n"); 534 goto error; 535 } 536 537 rc = fdisk_cap_format(&pinfo.capacity, &scap); 538 if (rc != EOK) { 539 printf("Out of memory.\n"); 540 goto error; 541 } 542 543 rc = fdisk_fstype_format(pinfo.fstype, &sfstype); 544 if (rc != EOK) { 545 printf("Out of memory.\n"); 546 goto error; 547 } 548 549 printf("Partition %d: %s, %s\n", npart, scap, sfstype); 550 free(scap); 551 scap = NULL; 552 free(sfstype); 553 sfstype = NULL; 554 555 part = fdisk_part_next(part); 556 } 258 557 259 558 rc = nchoice_set_prompt(choice, "Select action"); … … 262 561 printf("Out of memory.\n"); 263 562 goto error; 563 } 564 565 if (linfo.ltype != fdl_none) { 566 rc = nchoice_add(choice, "Create partition", 567 (void *)devac_create_part); 568 if (rc != EOK) { 569 assert(rc == ENOMEM); 570 printf("Out of memory.\n"); 571 goto error; 572 } 573 } 574 575 if (npart > 0) { 576 rc = nchoice_add(choice, "Delete partition", 577 (void *)devac_delete_part); 578 if (rc != EOK) { 579 assert(rc == ENOMEM); 580 printf("Out of memory.\n"); 581 goto error; 582 } 264 583 } 265 584 … … 280 599 goto error; 281 600 } 282 }283 284 rc = nchoice_add(choice, "Create partition",285 (void *)devac_create_part);286 if (rc != EOK) {287 assert(rc == ENOMEM);288 printf("Out of memory.\n");289 goto error;290 }291 292 rc = nchoice_add(choice, "Delete partition",293 (void *)devac_delete_part);294 if (rc != EOK) {295 assert(rc == ENOMEM);296 printf("Out of memory.\n");297 goto error;298 601 } 299 602 … … 318 621 break; 319 622 case devac_delete_label: 320 rc = fd isk_label_destroy(dev);623 rc = fdsk_delete_label(dev); 321 624 if (rc != EOK) 322 625 goto error; 323 626 break; 324 627 case devac_create_part: 628 rc = fdsk_create_part(dev); 629 if (rc != EOK) 630 goto error; 325 631 break; 326 632 case devac_delete_part: 633 rc = fdsk_delete_part(dev); 634 if (rc != EOK) 635 goto error; 327 636 break; 328 637 case devac_exit: … … 334 643 return EOK; 335 644 error: 645 free(sdcap); 646 free(scap); 647 free(sfstype); 648 free(svcname); 336 649 if (choice != NULL) 337 650 nchoice_destroy(choice); … … 349 662 return 1; 350 663 664 if (svcid == 0) 665 return 0; 666 351 667 rc = fdisk_dev_open(svcid, &dev); 352 668 if (rc != EOK) {
Note:
See TracChangeset
for help on using the changeset viewer.