1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
|
/*
libparted
Copyright (C) 1998-2000, 2007-2014 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include "fat.h"
#include "traverse.h"
#include "count.h"
#include "fatio.h"
#include "calc.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
#include <stdarg.h>
#include <string.h>
#ifndef DISCOVER_ONLY
/* Recursively builds (i.e. makes consistent) the duplicated directory tree
* (leaving the original directory tree in tact)
*/
static int
fat_construct_directory (FatOpContext* ctx, FatTraverseInfo* trav_info)
{
FatTraverseInfo* sub_dir_info;
FatDirEntry* dir_entry;
FatCluster old_first_cluster;
while ( (dir_entry = fat_traverse_next_dir_entry (trav_info)) ) {
if (fat_dir_entry_is_null_term (dir_entry))
break;
if (!fat_dir_entry_has_first_cluster (dir_entry, ctx->old_fs))
continue;
fat_traverse_mark_dirty (trav_info);
old_first_cluster = fat_dir_entry_get_first_cluster (dir_entry,
ctx->old_fs);
fat_dir_entry_set_first_cluster (dir_entry, ctx->new_fs,
fat_op_context_map_cluster (ctx, old_first_cluster));
if (fat_dir_entry_is_directory (dir_entry)
&& dir_entry->name [0] != '.') {
sub_dir_info
= fat_traverse_directory (trav_info, dir_entry);
if (!sub_dir_info)
return 0;
if (!fat_construct_directory (ctx, sub_dir_info))
return 0;
}
}
/* remove "stale" entries at the end */
while ((dir_entry = fat_traverse_next_dir_entry (trav_info))) {
memset (dir_entry, 0, sizeof (FatDirEntry));
fat_traverse_mark_dirty (trav_info);
}
fat_traverse_complete (trav_info);
return 1;
}
static int
duplicate_legacy_root_dir (FatOpContext* ctx)
{
FatSpecific* old_fs_info = FAT_SPECIFIC (ctx->old_fs);
FatSpecific* new_fs_info = FAT_SPECIFIC (ctx->new_fs);
PED_ASSERT (old_fs_info->root_dir_sector_count
== new_fs_info->root_dir_sector_count);
if (!ped_geometry_read (ctx->old_fs->geom, old_fs_info->buffer,
old_fs_info->root_dir_offset,
old_fs_info->root_dir_sector_count))
return 0;
if (!ped_geometry_write (ctx->new_fs->geom, old_fs_info->buffer,
new_fs_info->root_dir_offset,
new_fs_info->root_dir_sector_count))
return 0;
return 1;
}
/*
Constructs the new directory tree for legacy (FAT16) file systems.
*/
static int
fat_construct_legacy_root (FatOpContext* ctx)
{
FatTraverseInfo* trav_info;
if (!duplicate_legacy_root_dir (ctx))
return 0;
trav_info = fat_traverse_begin (ctx->new_fs, FAT_ROOT, "\\");
return fat_construct_directory (ctx, trav_info);
}
/*
Constructs the new directory tree for new (FAT32) file systems.
*/
static int
fat_construct_root (FatOpContext* ctx)
{
FatSpecific* new_fs_info = FAT_SPECIFIC (ctx->new_fs);
FatTraverseInfo* trav_info;
trav_info = fat_traverse_begin (ctx->new_fs, new_fs_info->root_cluster,
"\\");
fat_construct_directory (ctx, trav_info);
return 1;
}
/* Converts the root directory between FAT16 and FAT32. NOTE: this code
* can also do no conversion. I'm leaving fat_construct_directory(), because
* it's really pretty :-) It also leaves a higher chance of deleted file
* recovery, because it doesn't remove redundant entries. (We do this here,
* because brain-damaged FAT16 has an arbitary limit on root directory entries,
* so we save room)
*/
static int
fat_convert_directory (FatOpContext* ctx, FatTraverseInfo* old_trav,
FatTraverseInfo* new_trav)
{
FatTraverseInfo* sub_old_dir_trav;
FatTraverseInfo* sub_new_dir_trav;
FatDirEntry* new_dir_entry;
FatDirEntry* old_dir_entry;
FatCluster old_first_cluster;
while ( (old_dir_entry = fat_traverse_next_dir_entry (old_trav)) ) {
if (fat_dir_entry_is_null_term (old_dir_entry))
break;
if (!fat_dir_entry_is_active (old_dir_entry))
continue;
new_dir_entry = fat_traverse_next_dir_entry (new_trav);
if (!new_dir_entry) {
return ped_exception_throw (PED_EXCEPTION_ERROR,
PED_EXCEPTION_IGNORE_CANCEL,
_("There's not enough room in the root "
"directory for all of the files. Either "
"cancel, or ignore to lose the files."))
== PED_EXCEPTION_IGNORE;
}
*new_dir_entry = *old_dir_entry;
fat_traverse_mark_dirty (new_trav);
if (!fat_dir_entry_has_first_cluster (old_dir_entry,
ctx->old_fs))
continue;
old_first_cluster = fat_dir_entry_get_first_cluster (
old_dir_entry, ctx->old_fs);
fat_dir_entry_set_first_cluster (new_dir_entry, ctx->new_fs,
fat_op_context_map_cluster (ctx, old_first_cluster));
if (fat_dir_entry_is_directory (old_dir_entry)
&& old_dir_entry->name [0] != '.') {
sub_old_dir_trav
= fat_traverse_directory (old_trav, old_dir_entry);
sub_new_dir_trav
= fat_traverse_directory (new_trav, new_dir_entry);
if (!sub_old_dir_trav || !sub_new_dir_trav)
return 0;
if (!fat_convert_directory (ctx, sub_old_dir_trav,
sub_new_dir_trav))
return 0;
}
}
/* remove "stale" entries at the end, just in case there is some
* overlap
*/
while ((new_dir_entry = fat_traverse_next_dir_entry (new_trav))) {
memset (new_dir_entry, 0, sizeof (FatDirEntry));
fat_traverse_mark_dirty (new_trav);
}
fat_traverse_complete (old_trav);
fat_traverse_complete (new_trav);
return 1;
}
static void
clear_cluster (PedFileSystem* fs, FatCluster cluster)
{
FatSpecific* fs_info = FAT_SPECIFIC (fs);
memset (fs_info->buffer, 0, fs_info->cluster_size);
fat_write_cluster (fs, fs_info->buffer, cluster);
}
/* This MUST be called BEFORE the fat_construct_new_fat(), because cluster
* allocation depend on the old FAT. The reason is, old clusters may
* still be needed during the resize, (particularly clusters in the directory
* tree) even if they will be discarded later.
*/
static int
alloc_root_dir (FatOpContext* ctx)
{
FatSpecific* old_fs_info = FAT_SPECIFIC (ctx->old_fs);
FatSpecific* new_fs_info = FAT_SPECIFIC (ctx->new_fs);
FatCluster i;
FatCluster cluster;
FatCluster cluster_count;
PED_ASSERT (new_fs_info->fat_type == FAT_TYPE_FAT32);
cluster_count = ped_div_round_up (
PED_MAX (16, old_fs_info->root_dir_sector_count),
new_fs_info->cluster_sectors);
for (i = 0; i < cluster_count; i++) {
cluster = fat_table_alloc_check_cluster (new_fs_info->fat,
ctx->new_fs);
if (!cluster)
return 0;
ctx->new_root_dir [i] = cluster;
clear_cluster (ctx->new_fs, cluster);
}
ctx->new_root_dir [i] = 0;
new_fs_info->root_cluster = ctx->new_root_dir [0];
return 1;
}
/* when converting FAT32 -> FAT16
* fat_duplicate clusters() duplicated the root directory unnecessarily.
* Let's free it.
*
* This must be called AFTER fat_construct_new_fat(). (otherwise, our
* changes just get overwritten)
*/
static int
free_root_dir (FatOpContext* ctx)
{
FatSpecific* old_fs_info = FAT_SPECIFIC (ctx->old_fs);
FatSpecific* new_fs_info = FAT_SPECIFIC (ctx->new_fs);
FatCluster old_cluster;
FatFragment i;
PED_ASSERT (old_fs_info->fat_type == FAT_TYPE_FAT32);
PED_ASSERT (new_fs_info->fat_type == FAT_TYPE_FAT16);
for (old_cluster = old_fs_info->root_cluster;
!fat_table_is_eof (old_fs_info->fat, old_cluster);
old_cluster = fat_table_get (old_fs_info->fat, old_cluster)) {
FatFragment old_frag;
old_frag = fat_cluster_to_frag (ctx->old_fs, old_cluster);
for (i = 0; i < new_fs_info->cluster_frags; i++) {
FatFragment new_frag;
FatCluster new_clst;
new_frag = fat_op_context_map_fragment (ctx,
old_frag + i);
new_clst = fat_frag_to_cluster (ctx->old_fs, new_frag);
if (!fat_table_set_avail (new_fs_info->fat, new_clst))
return 0;
}
}
return 1;
}
static int
fat_clear_root_dir (PedFileSystem* fs)
{
FatSpecific* fs_info = FAT_SPECIFIC (fs);
int i;
PED_ASSERT (fs_info->fat_type == FAT_TYPE_FAT16);
PED_ASSERT (fs_info->root_dir_sector_count);
memset (fs_info->buffer, 0, 512);
for (i = 0; i < fs_info->root_dir_sector_count; i++) {
if (!ped_geometry_write (fs->geom, fs_info->buffer,
fs_info->root_dir_offset + i, 1)) {
if (ped_exception_throw (PED_EXCEPTION_ERROR,
PED_EXCEPTION_IGNORE_CANCEL,
_("Error writing to the root directory."))
== PED_EXCEPTION_CANCEL)
return 0;
}
}
return 1;
}
static int
fat_construct_converted_tree (FatOpContext* ctx)
{
FatSpecific* old_fs_info = FAT_SPECIFIC (ctx->old_fs);
FatSpecific* new_fs_info = FAT_SPECIFIC (ctx->new_fs);
FatTraverseInfo* old_trav_info;
FatTraverseInfo* new_trav_info;
if (new_fs_info->fat_type == FAT_TYPE_FAT32) {
new_trav_info = fat_traverse_begin (ctx->new_fs,
new_fs_info->root_cluster, "\\");
old_trav_info = fat_traverse_begin (ctx->old_fs, FAT_ROOT,
"\\");
} else {
fat_clear_root_dir (ctx->new_fs);
new_trav_info = fat_traverse_begin (ctx->new_fs, FAT_ROOT,
"\\");
old_trav_info = fat_traverse_begin (ctx->old_fs,
old_fs_info->root_cluster, "\\");
}
if (!new_trav_info || !old_trav_info)
return 0;
if (!fat_convert_directory (ctx, old_trav_info, new_trav_info))
return 0;
return 1;
}
/*
Constructs the new directory tree to match the new file locations.
*/
static int
fat_construct_dir_tree (FatOpContext* ctx)
{
FatSpecific* new_fs_info = FAT_SPECIFIC (ctx->new_fs);
FatSpecific* old_fs_info = FAT_SPECIFIC (ctx->old_fs);
if (new_fs_info->fat_type == old_fs_info->fat_type) {
switch (old_fs_info->fat_type) {
case FAT_TYPE_FAT12:
PED_ASSERT (0);
break;
case FAT_TYPE_FAT16:
return fat_construct_legacy_root (ctx);
case FAT_TYPE_FAT32:
return fat_construct_root (ctx);
}
} else {
return fat_construct_converted_tree (ctx);
}
return 0;
}
static FatFragment
_get_next_old_frag (FatOpContext* ctx, FatFragment frag)
{
FatSpecific* old_fs_info = FAT_SPECIFIC (ctx->old_fs);
FatCluster cluster;
FatCluster next_cluster;
if ((frag + 1) % old_fs_info->cluster_frags != 0) {
if (fat_is_fragment_active (ctx->old_fs, frag + 1))
return frag + 1;
else
return -1;
} else {
cluster = fat_frag_to_cluster (ctx->old_fs, frag);
next_cluster = fat_table_get (old_fs_info->fat, cluster);
if (fat_table_is_eof (old_fs_info->fat, next_cluster))
return -1;
else
return fat_cluster_to_frag (ctx->old_fs, next_cluster);
}
}
/*
Constructs the new fat for the resized file system.
*/
static int
fat_construct_new_fat (FatOpContext* ctx)
{
FatSpecific* old_fs_info = FAT_SPECIFIC (ctx->old_fs);
FatSpecific* new_fs_info = FAT_SPECIFIC (ctx->new_fs);
FatFragment old_frag;
FatCluster new_cluster;
FatFragment new_frag;
FatFragment old_next_frag;
FatFragment new_next_frag;
FatCluster new_next_cluster;
FatClusterFlag flag;
int i;
fat_table_clear (new_fs_info->fat);
if (!fat_table_set_cluster_count (new_fs_info->fat,
new_fs_info->cluster_count))
return 0;
for (old_frag = 0; old_frag < old_fs_info->frag_count; old_frag++) {
flag = fat_get_fragment_flag (ctx->old_fs, old_frag);
if (flag == FAT_FLAG_FREE)
continue;
if (flag == FAT_FLAG_BAD) {
new_frag = fat_op_context_map_static_fragment (
ctx, old_frag);
if (new_frag == -1)
continue;
new_cluster = fat_frag_to_cluster (ctx->new_fs,
new_frag);
fat_table_set_bad (new_fs_info->fat, new_cluster);
continue;
}
new_frag = fat_op_context_map_fragment (ctx, old_frag);
new_cluster = fat_frag_to_cluster (ctx->new_fs, new_frag);
old_next_frag = _get_next_old_frag (ctx, old_frag);
if (old_next_frag == -1) {
fat_table_set_eof (new_fs_info->fat, new_cluster);
continue;
}
new_next_frag = fat_op_context_map_fragment (ctx,
old_next_frag);
PED_ASSERT (new_next_frag != -1);
new_next_cluster = fat_frag_to_cluster (ctx->new_fs,
new_next_frag);
PED_ASSERT (new_next_cluster != new_cluster);
fat_table_set (new_fs_info->fat, new_cluster, new_next_cluster);
}
#if 0
#ifdef PED_VERBOSE
for (old_cluster=2; old_cluster < old_fs_info->cluster_count+2;
old_cluster++) {
if (fat_table_is_available (old_fs_info->fat, old_cluster))
continue;
printf ("%d->%d\t(next: %d->%d)\n",
old_cluster,
ctx->remap [old_cluster],
fat_table_get (old_fs_info->fat, old_cluster),
fat_table_get (new_fs_info->fat,
ctx->remap [old_cluster]));
}
#endif /* PED_VERBOSE */
#endif
if (old_fs_info->fat_type == FAT_TYPE_FAT32
&& new_fs_info->fat_type == FAT_TYPE_FAT32) {
new_fs_info->root_cluster
= fat_op_context_map_cluster (ctx,
old_fs_info->root_cluster);
}
if (old_fs_info->fat_type == FAT_TYPE_FAT16
&& new_fs_info->fat_type == FAT_TYPE_FAT32) {
for (i=0; ctx->new_root_dir[i+1]; i++) {
fat_table_set (new_fs_info->fat,
ctx->new_root_dir[i],
ctx->new_root_dir[i+1]);
}
fat_table_set_eof (new_fs_info->fat, ctx->new_root_dir[i]);
}
return 1;
}
static int
ask_type (PedFileSystem* fs, int fat16_ok, int fat32_ok, FatType* out_fat_type)
{
FatSpecific* fs_info = FAT_SPECIFIC (fs);
PedExceptionOption status;
const char* fat16_msg;
const char* fat32_msg;
if (fs_info->fat_type == FAT_TYPE_FAT16)
fat16_msg = _("If you leave your file system as FAT16, "
"then you will have no problems.");
else
fat16_msg = _("If you convert to FAT16, and MS Windows "
"is installed on this partition, then "
"you must re-install the MS Windows boot "
"loader. If you want to do this, you "
"should consult the Parted manual (or "
"your distribution's manual).");
if (fs_info->fat_type == FAT_TYPE_FAT32)
fat32_msg = _("If you leave your file system as FAT32, "
"then you will not introduce any new "
"problems.");
else
fat32_msg = _("If you convert to FAT32, and MS Windows "
"is installed on this partition, then "
"you must re-install the MS Windows boot "
"loader. If you want to do this, you "
"should consult the Parted manual (or "
"your distribution's manual). Also, "
"converting to FAT32 will make the file "
"system unreadable by MS DOS, MS Windows "
"95a, and MS Windows NT.");
if (fat16_ok && fat32_ok) {
status = ped_exception_throw (
PED_EXCEPTION_INFORMATION,
PED_EXCEPTION_YES_NO_CANCEL,
_("%s %s %s"),
_("Would you like to use FAT32?"),
fat16_msg,
fat32_msg);
switch (status) {
case PED_EXCEPTION_YES:
*out_fat_type = FAT_TYPE_FAT32;
return 1;
case PED_EXCEPTION_NO:
*out_fat_type = FAT_TYPE_FAT16;
return 1;
case PED_EXCEPTION_UNHANDLED:
*out_fat_type = fs_info->fat_type;
return 1;
case PED_EXCEPTION_CANCEL:
return 0;
default:
PED_ASSERT (0);
break;
}
}
if (fat16_ok) {
if (fs_info->fat_type != FAT_TYPE_FAT16) {
status = ped_exception_throw (
PED_EXCEPTION_WARNING,
PED_EXCEPTION_OK_CANCEL,
_("%s %s"),
_("The file system can only be resized to this "
"size by converting to FAT16."),
fat16_msg);
if (status == PED_EXCEPTION_CANCEL)
return 0;
}
*out_fat_type = FAT_TYPE_FAT16;
return 1;
}
if (fat32_ok) {
if (fs_info->fat_type != FAT_TYPE_FAT32) {
status = ped_exception_throw (
PED_EXCEPTION_WARNING,
PED_EXCEPTION_OK_CANCEL,
_("%s %s"),
_("The file system can only be resized to this "
"size by converting to FAT32."),
fat32_msg);
if (status == PED_EXCEPTION_CANCEL)
return 0;
}
*out_fat_type = FAT_TYPE_FAT32;
return 1;
}
ped_exception_throw (
PED_EXCEPTION_NO_FEATURE,
PED_EXCEPTION_CANCEL,
_("GNU Parted cannot resize this partition to this size. "
"We're working on it!"));
return 0;
}
/* For resize operations: determine if the file system must be FAT16 or FAT32,
* or either. If the new file system must be FAT32, then query for
* confirmation. If either file system can be used, query for which one.
*/
static int
get_fat_type (PedFileSystem* fs, const PedGeometry* new_geom,
FatType* out_fat_type)
{
FatSpecific* fs_info = FAT_SPECIFIC (fs);
PedSector fat16_cluster_sectors;
PedSector fat32_cluster_sectors;
FatCluster dummy_cluster_count;
PedSector dummy_fat_sectors;
int fat16_ok;
int fat32_ok;
fat16_ok = fat_calc_resize_sizes (
new_geom,
fs_info->cluster_sectors,
FAT_TYPE_FAT16,
fs_info->root_dir_sector_count,
fs_info->cluster_sectors,
&fat16_cluster_sectors,
&dummy_cluster_count,
&dummy_fat_sectors);
fat32_ok = fat_calc_resize_sizes (
new_geom,
fs_info->cluster_sectors,
FAT_TYPE_FAT32,
fs_info->root_dir_sector_count,
fs_info->cluster_sectors,
&fat32_cluster_sectors,
&dummy_cluster_count,
&dummy_fat_sectors);
return ask_type (fs, fat16_ok, fat32_ok, out_fat_type);
}
/* Creates the PedFileSystem struct for the new resized file system, and
sticks it in a FatOpContext. At the end of the process, the original
(ctx->old_fs) is destroyed, and replaced with the new one (ctx->new_fs).
*/
static FatOpContext*
create_resize_context (PedFileSystem* fs, const PedGeometry* new_geom)
{
FatSpecific* fs_info = FAT_SPECIFIC (fs);
FatSpecific* new_fs_info;
PedFileSystem* new_fs;
PedSector new_cluster_sectors;
FatCluster new_cluster_count;
PedSector new_fat_sectors;
FatType new_fat_type;
PedSector root_dir_sector_count;
FatOpContext* context;
/* hypothetical number of root dir sectors, if we end up using
* FAT16
*/
if (fs_info->root_dir_sector_count)
root_dir_sector_count = fs_info->root_dir_sector_count;
else
root_dir_sector_count = FAT_ROOT_DIR_ENTRY_COUNT
* sizeof (FatDirEntry) / 512;
if (!get_fat_type (fs, new_geom, &new_fat_type))
return 0;
fat_calc_resize_sizes (new_geom, fs_info->cluster_sectors, new_fat_type,
root_dir_sector_count, fs_info->cluster_sectors,
&new_cluster_sectors, &new_cluster_count, &new_fat_sectors);
if (!fat_check_resize_geometry (fs, new_geom, new_cluster_sectors,
new_cluster_count))
goto error;
new_fs = fat_alloc (new_geom);
if (!new_fs)
goto error;
new_fs_info = FAT_SPECIFIC (new_fs);
if (!new_fs_info)
goto error_free_new_fs;
/* preserve boot code, etc. */
new_fs_info->boot_sector = ped_malloc (new_geom->dev->sector_size);
memcpy (new_fs_info->boot_sector, fs_info->boot_sector,
new_geom->dev->sector_size);
new_fs_info->info_sector = NULL;
if (fs_info->fat_type == FAT_TYPE_FAT32)
{
PED_ASSERT (fs_info->info_sector != NULL);
new_fs_info->info_sector =
ped_malloc (new_geom->dev->sector_size);
memcpy (new_fs_info->info_sector, fs_info->info_sector,
new_geom->dev->sector_size);
}
new_fs_info->logical_sector_size = fs_info->logical_sector_size;
new_fs_info->sector_count = new_geom->length;
new_fs_info->sectors_per_track = fs_info->sectors_per_track;
new_fs_info->heads = fs_info->heads;
new_fs_info->cluster_size = new_cluster_sectors * 512;
new_fs_info->cluster_sectors = new_cluster_sectors;
new_fs_info->cluster_count = new_cluster_count;
new_fs_info->dir_entries_per_cluster = fs_info->dir_entries_per_cluster;
new_fs_info->fat_type = new_fat_type;
new_fs_info->fat_table_count = 2;
new_fs_info->fat_sectors = new_fat_sectors;
/* what about copying? */
new_fs_info->serial_number = fs_info->serial_number;
if (new_fs_info->fat_type == FAT_TYPE_FAT32) {
new_fs_info->info_sector_offset = 1;
new_fs_info->boot_sector_backup_offset = 6;
new_fs_info->root_dir_offset = 0;
new_fs_info->root_dir_entry_count = 0;
new_fs_info->root_dir_sector_count = 0;
/* we add calc_align_sectors to push the cluster_offset
forward, to keep the clusters aligned between the new
and old file systems
*/
new_fs_info->fat_offset
= fat_min_reserved_sector_count (FAT_TYPE_FAT32)
+ fat_calc_align_sectors (new_fs, fs);
new_fs_info->cluster_offset
= new_fs_info->fat_offset
+ 2 * new_fs_info->fat_sectors;
} else {
new_fs_info->root_dir_sector_count = root_dir_sector_count;
new_fs_info->root_dir_entry_count
= root_dir_sector_count * 512 / sizeof (FatDirEntry);
new_fs_info->fat_offset
= fat_min_reserved_sector_count (FAT_TYPE_FAT16)
+ fat_calc_align_sectors (new_fs, fs);
new_fs_info->root_dir_offset = new_fs_info->fat_offset
+ 2 * new_fs_info->fat_sectors;
new_fs_info->cluster_offset = new_fs_info->root_dir_offset
+ new_fs_info->root_dir_sector_count;
}
new_fs_info->total_dir_clusters = fs_info->total_dir_clusters;
context = fat_op_context_new (new_fs, fs);
if (!context)
goto error_free_new_fs_info;
if (!fat_op_context_create_initial_fat (context))
goto error_free_context;
if (!fat_alloc_buffers (new_fs))
goto error_free_fat;
return context;
error_free_fat:
fat_table_destroy (new_fs_info->fat);
error_free_context:
free (context);
error_free_new_fs_info:
free (new_fs_info);
error_free_new_fs:
free (new_fs);
error:
return NULL;
}
static int
resize_context_assimilate (FatOpContext* ctx)
{
FatSpecific* old_fs_info = FAT_SPECIFIC (ctx->old_fs);
FatSpecific* new_fs_info = FAT_SPECIFIC (ctx->new_fs);
fat_free_buffers (ctx->old_fs);
fat_table_destroy (old_fs_info->fat);
free (old_fs_info);
ped_geometry_destroy (ctx->old_fs->geom);
ctx->old_fs->type_specific = ctx->new_fs->type_specific;
ctx->old_fs->geom = ctx->new_fs->geom;
ctx->old_fs->type = (new_fs_info->fat_type == FAT_TYPE_FAT16)
? &fat16_type
: &fat32_type;
free (ctx->new_fs);
fat_op_context_destroy (ctx);
return 1;
}
static int
resize_context_abort (FatOpContext* ctx)
{
FatSpecific* new_fs_info = FAT_SPECIFIC (ctx->new_fs);
fat_free_buffers (ctx->new_fs);
fat_table_destroy (new_fs_info->fat);
free (new_fs_info);
ped_geometry_destroy (ctx->new_fs->geom);
free (ctx->new_fs);
fat_op_context_destroy (ctx);
return 1;
}
/* copies the "hidden" sectors, between the boot sector and the FAT. Required,
* for the Windows 98 FAT32 boot loader
*/
int
_copy_hidden_sectors (FatOpContext* ctx)
{
FatSpecific* old_fs_info = FAT_SPECIFIC (ctx->old_fs);
FatSpecific* new_fs_info = FAT_SPECIFIC (ctx->new_fs);
PedSector first = 1;
PedSector last;
PedSector count;
/* nothing to copy for FAT16 */
if (old_fs_info->fat_type == FAT_TYPE_FAT16
|| new_fs_info->fat_type == FAT_TYPE_FAT16)
return 1;
last = PED_MIN (old_fs_info->fat_offset, new_fs_info->fat_offset) - 1;
count = last - first + 1;
PED_ASSERT (count < BUFFER_SIZE);
if (!ped_geometry_read (ctx->old_fs->geom, old_fs_info->buffer,
first, count))
return 0;
if (!ped_geometry_write (ctx->new_fs->geom, old_fs_info->buffer,
first, count))
return 0;
return 1;
}
int
fat_resize (PedFileSystem* fs, PedGeometry* geom, PedTimer* timer)
{
FatSpecific* fs_info = FAT_SPECIFIC (fs);
FatSpecific* new_fs_info;
FatOpContext* ctx;
PedFileSystem* new_fs;
ctx = create_resize_context (fs, geom);
if (!ctx)
goto error;
new_fs = ctx->new_fs;
new_fs_info = FAT_SPECIFIC (new_fs);
if (!fat_duplicate_clusters (ctx, timer))
goto error_abort_ctx;
if (fs_info->fat_type == FAT_TYPE_FAT16
&& new_fs_info->fat_type == FAT_TYPE_FAT32) {
if (!alloc_root_dir (ctx))
goto error_abort_ctx;
}
if (!fat_construct_new_fat (ctx))
goto error_abort_ctx;
if (fs_info->fat_type == FAT_TYPE_FAT32
&& new_fs_info->fat_type == FAT_TYPE_FAT16) {
if (!free_root_dir (ctx))
goto error_abort_ctx;
}
if (!fat_construct_dir_tree (ctx))
goto error_abort_ctx;
if (!fat_table_write_all (new_fs_info->fat, new_fs))
goto error_abort_ctx;
_copy_hidden_sectors (ctx);
fat_boot_sector_generate (&new_fs_info->boot_sector, new_fs);
fat_boot_sector_write (new_fs_info->boot_sector, new_fs);
if (new_fs_info->fat_type == FAT_TYPE_FAT32) {
fat_info_sector_generate (&new_fs_info->info_sector, new_fs);
fat_info_sector_write (new_fs_info->info_sector, new_fs);
}
if (!resize_context_assimilate (ctx))
goto error;
return 1;
error_abort_ctx:
resize_context_abort (ctx);
error:
return 0;
}
#endif /* !DISCOVER_ONLY */
|