Changeset 8227d63 in mainline
- Timestamp:
- 2015-06-22T21:20:23Z (10 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 1356f85a
- Parents:
- e96047c
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
.bzrignore
re96047c r8227d63 34 34 uspace/app/dnsres/dnsres 35 35 uspace/app/edit/edit 36 uspace/app/fdisk/fdisk 36 37 uspace/app/fontviewer/fontviewer 37 38 uspace/app/getterm/getterm … … 91 92 uspace/dist/app/dnsres 92 93 uspace/dist/app/edit 94 uspace/dist/app/fdisk 93 95 uspace/dist/app/fontviewer 94 96 uspace/dist/app/getterm -
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) { -
uspace/lib/c/generic/str.c
re96047c r8227d63 519 519 wchar_t c1 = 0; 520 520 wchar_t c2 = 0; 521 521 522 522 size_t off1 = 0; 523 523 size_t off2 = 0; … … 529 529 if (c1 < c2) 530 530 return -1; 531 531 532 532 if (c1 > c2) 533 533 return 1; 534 534 535 535 if (c1 == 0 || c2 == 0) 536 break; 536 break; 537 537 } 538 538 … … 566 566 wchar_t c1 = 0; 567 567 wchar_t c2 = 0; 568 568 569 569 size_t off1 = 0; 570 570 size_t off2 = 0; 571 571 572 572 size_t len = 0; 573 573 … … 578 578 c1 = str_decode(s1, &off1, STR_NO_LIMIT); 579 579 c2 = str_decode(s2, &off2, STR_NO_LIMIT); 580 581 if (c1 < c2) 582 return -1; 583 584 if (c1 > c2) 585 return 1; 586 587 if (c1 == 0 || c2 == 0) 588 break; 589 590 ++len; 591 } 592 593 return 0; 594 595 } 596 597 /** Compare two NULL terminated strings in case-insensitive manner. 598 * 599 * Do a char-by-char comparison of two NULL-terminated strings. 600 * The strings are considered equal iff their length is equal 601 * and both strings consist of the same sequence of characters 602 * when converted to lower case. 603 * 604 * A string S1 is less than another string S2 if it has a character with 605 * lower value at the first character position where the strings differ. 606 * If the strings differ in length, the shorter one is treated as if 607 * padded by characters with a value of zero. 608 * 609 * @param s1 First string to compare. 610 * @param s2 Second string to compare. 611 * 612 * @return 0 if the strings are equal, -1 if the first is less than the second, 613 * 1 if the second is less than the first. 614 * 615 */ 616 int str_casecmp(const char *s1, const char *s2) 617 { 618 wchar_t c1 = 0; 619 wchar_t c2 = 0; 620 621 size_t off1 = 0; 622 size_t off2 = 0; 623 624 while (true) { 625 c1 = tolower(str_decode(s1, &off1, STR_NO_LIMIT)); 626 c2 = tolower(str_decode(s2, &off2, STR_NO_LIMIT)); 627 628 if (c1 < c2) 629 return -1; 630 631 if (c1 > c2) 632 return 1; 633 634 if (c1 == 0 || c2 == 0) 635 break; 636 } 637 638 return 0; 639 } 640 641 /** Compare two NULL terminated strings with length limit in case-insensitive 642 * manner. 643 * 644 * Do a char-by-char comparison of two NULL-terminated strings. 645 * The strings are considered equal iff 646 * min(str_length(s1), max_len) == min(str_length(s2), max_len) 647 * and both strings consist of the same sequence of characters, 648 * up to max_len characters. 649 * 650 * A string S1 is less than another string S2 if it has a character with 651 * lower value at the first character position where the strings differ. 652 * If the strings differ in length, the shorter one is treated as if 653 * padded by characters with a value of zero. Only the first max_len 654 * characters are considered. 655 * 656 * @param s1 First string to compare. 657 * @param s2 Second string to compare. 658 * @param max_len Maximum number of characters to consider. 659 * 660 * @return 0 if the strings are equal, -1 if the first is less than the second, 661 * 1 if the second is less than the first. 662 * 663 */ 664 int str_lcasecmp(const char *s1, const char *s2, size_t max_len) 665 { 666 wchar_t c1 = 0; 667 wchar_t c2 = 0; 668 669 size_t off1 = 0; 670 size_t off2 = 0; 671 672 size_t len = 0; 673 674 while (true) { 675 if (len >= max_len) 676 break; 677 678 c1 = tolower(str_decode(s1, &off1, STR_NO_LIMIT)); 679 c2 = tolower(str_decode(s2, &off2, STR_NO_LIMIT)); 580 680 581 681 if (c1 < c2) -
uspace/lib/c/include/str.h
re96047c r8227d63 82 82 extern int str_cmp(const char *s1, const char *s2); 83 83 extern int str_lcmp(const char *s1, const char *s2, size_t max_len); 84 extern int str_casecmp(const char *s1, const char *s2); 85 extern int str_lcasecmp(const char *s1, const char *s2, size_t max_len); 84 86 85 87 extern bool str_test_prefix(const char *s, const char *p); -
uspace/lib/fdisk/include/fdisk.h
re96047c r8227d63 44 44 extern fdisk_dev_info_t *fdisk_dev_first(fdisk_dev_list_t *); 45 45 extern fdisk_dev_info_t *fdisk_dev_next(fdisk_dev_info_t *); 46 extern int fdisk_dev_ get_svcname(fdisk_dev_info_t *, char **);47 extern void fdisk_dev_ get_svcid(fdisk_dev_info_t *, service_id_t *);48 extern int fdisk_dev_ capacity(fdisk_dev_info_t *, fdisk_cap_t *);46 extern int fdisk_dev_info_get_svcname(fdisk_dev_info_t *, char **); 47 extern void fdisk_dev_info_get_svcid(fdisk_dev_info_t *, service_id_t *); 48 extern int fdisk_dev_info_capacity(fdisk_dev_info_t *, fdisk_cap_t *); 49 49 50 50 extern int fdisk_dev_open(service_id_t, fdisk_dev_t **); 51 51 extern void fdisk_dev_close(fdisk_dev_t *); 52 extern int fdisk_dev_get_svcname(fdisk_dev_t *, char **); 53 extern int fdisk_dev_capacity(fdisk_dev_t *, fdisk_cap_t *); 52 54 53 55 extern int fdisk_label_get_info(fdisk_dev_t *, fdisk_label_info_t *); … … 57 59 extern fdisk_part_t *fdisk_part_first(fdisk_dev_t *); 58 60 extern fdisk_part_t *fdisk_part_next(fdisk_part_t *); 61 extern int fdisk_part_get_info(fdisk_part_t *, fdisk_part_info_t *); 59 62 extern int fdisk_part_get_max_avail(fdisk_dev_t *, fdisk_cap_t *); 60 extern int fdisk_part_create(fdisk_dev_t *, fdisk_part spec_t *,63 extern int fdisk_part_create(fdisk_dev_t *, fdisk_part_spec_t *, 61 64 fdisk_part_t **); 62 65 extern int fdisk_part_destroy(fdisk_part_t *); 66 extern void fdisk_pspec_init(fdisk_part_spec_t *); 63 67 64 68 extern int fdisk_cap_format(fdisk_cap_t *, char **); 69 extern int fdisk_cap_parse(const char *, fdisk_cap_t *); 65 70 extern int fdisk_ltype_format(fdisk_label_type_t, char **); 71 extern int fdisk_fstype_format(fdisk_fstype_t, char **); 66 72 67 73 #endif -
uspace/lib/fdisk/include/types/fdisk.h
re96047c r8227d63 41 41 #include <stdint.h> 42 42 43 typedef enum { 44 cu_byte = 0, 45 cu_kbyte, 46 cu_mbyte, 47 cu_gbyte, 48 cu_tbyte, 49 cu_pbyte, 50 cu_ebyte, 51 cu_zbyte, 52 cu_ybyte 53 } fdisk_cunit_t; 54 55 #define CU_LIMIT (cu_ybyte + 1) 56 57 /** File system type */ 58 typedef enum { 59 fdfs_none = 0, 60 fdfs_unknown, 61 fdfs_exfat, 62 fdfs_fat, 63 fdfs_minix, 64 fdfs_ext4 65 } fdisk_fstype_t; 66 67 /** Highest fstype value + 1 */ 68 #define FDFS_LIMIT (fdfs_ext4 + 1) 69 /** Lowest fstype allowed for creation */ 70 #define FDFS_CREATE_LO fdfs_exfat 71 /** Highest fstype allowed for creation + 1 */ 72 #define FDFS_CREATE_HI (fdfs_ext4 + 1) 73 74 /** Partition capacity */ 75 typedef struct { 76 uint64_t value; 77 fdisk_cunit_t cunit; 78 } fdisk_cap_t; 79 43 80 /** List of devices available for managing by fdisk */ 44 81 typedef struct { … … 70 107 } fdisk_label_type_t; 71 108 109 /** Highest label type + 1 */ 110 #define FDL_LIMIT (fdl_gpt + 1) 111 /** Lowest label type allowed for creation */ 112 #define FDL_CREATE_LO fdl_mbr 113 /** Highest label type allowed for creation + 1 */ 114 #define FDL_CREATE_HI (fdl_gpt + 1) 115 72 116 /** Open fdisk device */ 73 117 typedef struct { 118 /** Label type */ 74 119 fdisk_label_type_t ltype; 120 /** Partitions */ 121 list_t parts; /* of fdisk_part_t */ 122 /** Service ID */ 123 service_id_t sid; 75 124 } fdisk_dev_t; 76 125 … … 82 131 /** Partition */ 83 132 typedef struct { 133 /** Containing device */ 134 fdisk_dev_t *dev; 135 /** Link to fdisk_dev_t.parts */ 136 link_t ldev; 137 /** Capacity */ 138 fdisk_cap_t capacity; 139 /** File system type */ 140 fdisk_fstype_t fstype; 84 141 } fdisk_part_t; 85 86 typedef enum {87 cu_byte = 0,88 cu_kbyte,89 cu_mbyte,90 cu_gbyte,91 cu_tbyte,92 cu_pbyte,93 cu_ebyte,94 cu_zbyte,95 cu_ybyte96 } fdisk_cunit_t;97 98 /** Partition capacity */99 typedef struct {100 uint64_t value;101 fdisk_cunit_t cunit;102 } fdisk_cap_t;103 142 104 143 /** Specification of new partition */ 105 144 typedef struct { 106 } fdisk_partspec_t; 145 /** Desired capacity */ 146 fdisk_cap_t capacity; 147 /** File system type */ 148 fdisk_fstype_t fstype; 149 } fdisk_part_spec_t; 150 151 /** Partition info */ 152 typedef struct { 153 fdisk_cap_t capacity; 154 /** File system type */ 155 fdisk_fstype_t fstype; 156 } fdisk_part_info_t; 107 157 108 158 #endif -
uspace/lib/fdisk/src/fdisk.c
re96047c r8227d63 39 39 #include <fdisk.h> 40 40 #include <loc.h> 41 #include <mem.h> 41 42 #include <stdio.h> 42 43 #include <stdlib.h> 43 44 #include <str.h> 45 46 static const char *cu_str[] = { 47 [cu_byte] = "B", 48 [cu_kbyte] = "kB", 49 [cu_mbyte] = "MB", 50 [cu_gbyte] = "GB", 51 [cu_tbyte] = "TB", 52 [cu_pbyte] = "PB", 53 [cu_ebyte] = "EB", 54 [cu_zbyte] = "ZB", 55 [cu_ybyte] = "YB" 56 }; 44 57 45 58 static void fdisk_dev_info_delete(fdisk_dev_info_t *info) … … 144 157 } 145 158 146 void fdisk_dev_ get_svcid(fdisk_dev_info_t *info, service_id_t *rsid)159 void fdisk_dev_info_get_svcid(fdisk_dev_info_t *info, service_id_t *rsid) 147 160 { 148 161 *rsid = info->svcid; 149 162 } 150 163 151 int fdisk_dev_ get_svcname(fdisk_dev_info_t *info, char **rname)164 int fdisk_dev_info_get_svcname(fdisk_dev_info_t *info, char **rname) 152 165 { 153 166 char *name; … … 169 182 } 170 183 171 int fdisk_dev_ capacity(fdisk_dev_info_t *info, fdisk_cap_t *cap)184 int fdisk_dev_info_capacity(fdisk_dev_info_t *info, fdisk_cap_t *cap) 172 185 { 173 186 size_t bsize; … … 179 192 if (rc != EOK) 180 193 return rc; 194 195 info->blk_inited = true; 181 196 } 182 197 … … 204 219 205 220 dev->ltype = fdl_none; 221 dev->sid = sid; 222 list_initialize(&dev->parts); 206 223 *rdev = dev; 207 224 return EOK; … … 211 228 { 212 229 free(dev); 230 } 231 232 int fdisk_dev_get_svcname(fdisk_dev_t *dev, char **rname) 233 { 234 char *name; 235 int rc; 236 237 rc = loc_service_get_name(dev->sid, &name); 238 if (rc != EOK) 239 return rc; 240 241 *rname = name; 242 return EOK; 243 } 244 245 int fdisk_dev_capacity(fdisk_dev_t *dev, fdisk_cap_t *cap) 246 { 247 size_t bsize; 248 aoff64_t nblocks; 249 int rc; 250 251 rc = block_init(EXCHANGE_SERIALIZE, dev->sid, 2048); 252 if (rc != EOK) 253 return rc; 254 255 rc = block_get_bsize(dev->sid, &bsize); 256 if (rc != EOK) 257 return EIO; 258 259 rc = block_get_nblocks(dev->sid, &nblocks); 260 if (rc != EOK) 261 return EIO; 262 263 block_fini(dev->sid); 264 265 cap->value = bsize * nblocks; 266 cap->cunit = cu_byte; 267 268 return EOK; 213 269 } 214 270 … … 230 286 int fdisk_label_destroy(fdisk_dev_t *dev) 231 287 { 288 fdisk_part_t *part; 289 232 290 if (dev->ltype == fdl_none) 233 291 return ENOENT; 234 292 293 part = fdisk_part_first(dev); 294 while (part != NULL) { 295 (void) fdisk_part_destroy(part); /* XXX */ 296 part = fdisk_part_first(dev); 297 } 298 235 299 dev->ltype = fdl_none; 236 300 return EOK; … … 239 303 fdisk_part_t *fdisk_part_first(fdisk_dev_t *dev) 240 304 { 241 return NULL; 305 link_t *link; 306 307 link = list_first(&dev->parts); 308 if (link == NULL) 309 return NULL; 310 311 return list_get_instance(link, fdisk_part_t, ldev); 242 312 } 243 313 244 314 fdisk_part_t *fdisk_part_next(fdisk_part_t *part) 245 315 { 246 return NULL; 316 link_t *link; 317 318 link = list_next(&part->ldev, &part->dev->parts); 319 if (link == NULL) 320 return NULL; 321 322 return list_get_instance(link, fdisk_part_t, ldev); 323 } 324 325 int fdisk_part_get_info(fdisk_part_t *part, fdisk_part_info_t *info) 326 { 327 info->capacity = part->capacity; 328 info->fstype = part->fstype; 329 return EOK; 247 330 } 248 331 … … 252 335 } 253 336 254 int fdisk_part_create(fdisk_dev_t *dev, fdisk_part spec_t *pspec,337 int fdisk_part_create(fdisk_dev_t *dev, fdisk_part_spec_t *pspec, 255 338 fdisk_part_t **rpart) 256 339 { 340 fdisk_part_t *part; 341 342 part = calloc(1, sizeof(fdisk_part_t)); 343 if (part == NULL) 344 return ENOMEM; 345 346 part->dev = dev; 347 list_append(&part->ldev, &dev->parts); 348 part->capacity = pspec->capacity; 349 part->fstype = pspec->fstype; 350 351 if (rpart != NULL) 352 *rpart = part; 257 353 return EOK; 258 354 } … … 260 356 int fdisk_part_destroy(fdisk_part_t *part) 261 357 { 262 return EOK; 358 list_remove(&part->ldev); 359 free(part); 360 return EOK; 361 } 362 363 void fdisk_pspec_init(fdisk_part_spec_t *pspec) 364 { 365 memset(pspec, 0, sizeof(fdisk_part_spec_t)); 263 366 } 264 367 … … 266 369 { 267 370 int rc; 268 269 rc = asprintf(rstr, "%" PRIu64 " B", cap->value); 371 const char *sunit; 372 373 sunit = NULL; 374 375 if (cap->cunit < 0 || cap->cunit >= CU_LIMIT) 376 assert(false); 377 378 sunit = cu_str[cap->cunit]; 379 rc = asprintf(rstr, "%" PRIu64 " %s", cap->value, sunit); 270 380 if (rc < 0) 271 381 return ENOMEM; 272 382 383 return EOK; 384 } 385 386 int fdisk_cap_parse(const char *str, fdisk_cap_t *cap) 387 { 388 char *eptr; 389 char *p; 390 unsigned long val; 391 int i; 392 393 val = strtoul(str, &eptr, 10); 394 395 while (*eptr == ' ') 396 ++eptr; 397 398 if (*eptr == '\0') { 399 cap->cunit = cu_byte; 400 } else { 401 for (i = 0; i < CU_LIMIT; i++) { 402 if (str_lcasecmp(eptr, cu_str[i], 403 str_length(cu_str[i])) == 0) { 404 p = eptr + str_size(cu_str[i]); 405 while (*p == ' ') 406 ++p; 407 if (*p == '\0') 408 goto found; 409 } 410 } 411 412 return EINVAL; 413 found: 414 cap->cunit = i; 415 } 416 417 cap->value = val; 273 418 return EOK; 274 419 } … … 303 448 } 304 449 450 int fdisk_fstype_format(fdisk_fstype_t fstype, char **rstr) 451 { 452 const char *sfstype; 453 char *s; 454 455 sfstype = NULL; 456 switch (fstype) { 457 case fdfs_none: 458 sfstype = "None"; 459 break; 460 case fdfs_unknown: 461 sfstype = "Unknown"; 462 break; 463 case fdfs_exfat: 464 sfstype = "ExFAT"; 465 break; 466 case fdfs_fat: 467 sfstype = "FAT"; 468 break; 469 case fdfs_minix: 470 sfstype = "MINIX"; 471 break; 472 case fdfs_ext4: 473 sfstype = "Ext4"; 474 break; 475 } 476 477 s = str_dup(sfstype); 478 if (s == NULL) 479 return ENOMEM; 480 481 *rstr = s; 482 return EOK; 483 } 484 305 485 /** @} 306 486 */
Note:
See TracChangeset
for help on using the changeset viewer.