1N/A/*
1N/A libparted
1N/A Copyright (C) 1998-2000, 2007-2010 Free Software Foundation, Inc.
1N/A
1N/A This program is free software; you can redistribute it and/or modify
1N/A it under the terms of the GNU General Public License as published by
1N/A the Free Software Foundation; either version 3 of the License, or
1N/A (at your option) any later version.
1N/A
1N/A This program is distributed in the hope that it will be useful,
1N/A but WITHOUT ANY WARRANTY; without even the implied warranty of
1N/A MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1N/A GNU General Public License for more details.
1N/A
1N/A You should have received a copy of the GNU General Public License
1N/A along with this program. If not, see <http://www.gnu.org/licenses/>.
1N/A*/
1N/A
1N/A#include <config.h>
1N/A#include "fat.h"
1N/A#include "traverse.h"
1N/A#include "count.h"
1N/A#include "fatio.h"
1N/A#include "calc.h"
1N/A
1N/A#include <stdio.h>
1N/A#include <stdlib.h>
1N/A#include <sys/types.h>
1N/A#include <sys/stat.h>
1N/A#include <fcntl.h>
1N/A#include <errno.h>
1N/A#include <ctype.h>
1N/A#include <stdarg.h>
1N/A#include <string.h>
1N/A
1N/A#ifndef DISCOVER_ONLY
1N/A
1N/A/* Recursively builds (i.e. makes consistent) the duplicated directory tree
1N/A * (leaving the original directory tree in tact)
1N/A */
1N/Astatic int
1N/Afat_construct_directory (FatOpContext* ctx, FatTraverseInfo* trav_info)
1N/A{
1N/A FatTraverseInfo* sub_dir_info;
1N/A FatDirEntry* dir_entry;
1N/A FatCluster old_first_cluster;
1N/A
1N/A while ( (dir_entry = fat_traverse_next_dir_entry (trav_info)) ) {
1N/A if (fat_dir_entry_is_null_term (dir_entry))
1N/A break;
1N/A if (!fat_dir_entry_has_first_cluster (dir_entry, ctx->old_fs))
1N/A continue;
1N/A
1N/A fat_traverse_mark_dirty (trav_info);
1N/A
1N/A old_first_cluster = fat_dir_entry_get_first_cluster (dir_entry,
1N/A ctx->old_fs);
1N/A fat_dir_entry_set_first_cluster (dir_entry, ctx->new_fs,
1N/A fat_op_context_map_cluster (ctx, old_first_cluster));
1N/A
1N/A if (fat_dir_entry_is_directory (dir_entry)
1N/A && dir_entry->name [0] != '.') {
1N/A sub_dir_info
1N/A = fat_traverse_directory (trav_info, dir_entry);
1N/A if (!sub_dir_info)
1N/A return 0;
1N/A if (!fat_construct_directory (ctx, sub_dir_info))
1N/A return 0;
1N/A }
1N/A }
1N/A /* remove "stale" entries at the end */
1N/A while ((dir_entry = fat_traverse_next_dir_entry (trav_info))) {
1N/A memset (dir_entry, 0, sizeof (FatDirEntry));
1N/A fat_traverse_mark_dirty (trav_info);
1N/A }
1N/A fat_traverse_complete (trav_info);
1N/A return 1;
1N/A}
1N/A
1N/Astatic int
1N/Aduplicate_legacy_root_dir (FatOpContext* ctx)
1N/A{
1N/A FatSpecific* old_fs_info = FAT_SPECIFIC (ctx->old_fs);
1N/A FatSpecific* new_fs_info = FAT_SPECIFIC (ctx->new_fs);
1N/A
1N/A PED_ASSERT (old_fs_info->root_dir_sector_count
1N/A == new_fs_info->root_dir_sector_count, return 0);
1N/A
1N/A if (!ped_geometry_read (ctx->old_fs->geom, old_fs_info->buffer,
1N/A old_fs_info->root_dir_offset,
1N/A old_fs_info->root_dir_sector_count))
1N/A return 0;
1N/A
1N/A if (!ped_geometry_write (ctx->new_fs->geom, old_fs_info->buffer,
1N/A new_fs_info->root_dir_offset,
1N/A new_fs_info->root_dir_sector_count))
1N/A return 0;
1N/A
1N/A return 1;
1N/A}
1N/A
1N/A/*
1N/A Constructs the new directory tree for legacy (FAT16) file systems.
1N/A*/
1N/Astatic int
1N/Afat_construct_legacy_root (FatOpContext* ctx)
1N/A{
1N/A FatTraverseInfo* trav_info;
1N/A
1N/A if (!duplicate_legacy_root_dir (ctx))
1N/A return 0;
1N/A trav_info = fat_traverse_begin (ctx->new_fs, FAT_ROOT, "\\");
1N/A return fat_construct_directory (ctx, trav_info);
1N/A}
1N/A
1N/A/*
1N/A Constructs the new directory tree for new (FAT32) file systems.
1N/A*/
1N/Astatic int
1N/Afat_construct_root (FatOpContext* ctx)
1N/A{
1N/A FatSpecific* new_fs_info = FAT_SPECIFIC (ctx->new_fs);
1N/A FatTraverseInfo* trav_info;
1N/A
1N/A trav_info = fat_traverse_begin (ctx->new_fs, new_fs_info->root_cluster,
1N/A "\\");
1N/A fat_construct_directory (ctx, trav_info);
1N/A return 1;
1N/A}
1N/A
1N/A/* Converts the root directory between FAT16 and FAT32. NOTE: this code
1N/A * can also do no conversion. I'm leaving fat_construct_directory(), because
1N/A * it's really pretty :-) It also leaves a higher chance of deleted file
1N/A * recovery, because it doesn't remove redundant entries. (We do this here,
1N/A * because brain-damaged FAT16 has an arbitary limit on root directory entries,
1N/A * so we save room)
1N/A */
1N/Astatic int
1N/Afat_convert_directory (FatOpContext* ctx, FatTraverseInfo* old_trav,
1N/A FatTraverseInfo* new_trav)
1N/A{
1N/A FatTraverseInfo* sub_old_dir_trav;
1N/A FatTraverseInfo* sub_new_dir_trav;
1N/A FatDirEntry* new_dir_entry;
1N/A FatDirEntry* old_dir_entry;
1N/A FatCluster old_first_cluster;
1N/A
1N/A while ( (old_dir_entry = fat_traverse_next_dir_entry (old_trav)) ) {
1N/A if (fat_dir_entry_is_null_term (old_dir_entry))
1N/A break;
1N/A if (!fat_dir_entry_is_active (old_dir_entry))
1N/A continue;
1N/A
1N/A new_dir_entry = fat_traverse_next_dir_entry (new_trav);
1N/A if (!new_dir_entry) {
1N/A return ped_exception_throw (PED_EXCEPTION_ERROR,
1N/A PED_EXCEPTION_IGNORE_CANCEL,
1N/A _("There's not enough room in the root "
1N/A "directory for all of the files. Either "
1N/A "cancel, or ignore to lose the files."))
1N/A == PED_EXCEPTION_IGNORE;
1N/A }
1N/A
1N/A *new_dir_entry = *old_dir_entry;
1N/A fat_traverse_mark_dirty (new_trav);
1N/A
1N/A if (!fat_dir_entry_has_first_cluster (old_dir_entry,
1N/A ctx->old_fs))
1N/A continue;
1N/A
1N/A old_first_cluster = fat_dir_entry_get_first_cluster (
1N/A old_dir_entry, ctx->old_fs);
1N/A fat_dir_entry_set_first_cluster (new_dir_entry, ctx->new_fs,
1N/A fat_op_context_map_cluster (ctx, old_first_cluster));
1N/A
1N/A if (fat_dir_entry_is_directory (old_dir_entry)
1N/A && old_dir_entry->name [0] != '.') {
1N/A sub_old_dir_trav
1N/A = fat_traverse_directory (old_trav, old_dir_entry);
1N/A sub_new_dir_trav
1N/A = fat_traverse_directory (new_trav, new_dir_entry);
1N/A if (!sub_old_dir_trav || !sub_new_dir_trav)
1N/A return 0;
1N/A
1N/A if (!fat_convert_directory (ctx, sub_old_dir_trav,
1N/A sub_new_dir_trav))
1N/A return 0;
1N/A }
1N/A }
1N/A
1N/A /* remove "stale" entries at the end, just in case there is some
1N/A * overlap
1N/A */
1N/A while ((new_dir_entry = fat_traverse_next_dir_entry (new_trav))) {
1N/A memset (new_dir_entry, 0, sizeof (FatDirEntry));
1N/A fat_traverse_mark_dirty (new_trav);
1N/A }
1N/A
1N/A fat_traverse_complete (old_trav);
1N/A fat_traverse_complete (new_trav);
1N/A return 1;
1N/A}
1N/A
1N/Astatic void
1N/Aclear_cluster (PedFileSystem* fs, FatCluster cluster)
1N/A{
1N/A FatSpecific* fs_info = FAT_SPECIFIC (fs);
1N/A
1N/A memset (fs_info->buffer, 0, fs_info->cluster_size);
1N/A fat_write_cluster (fs, fs_info->buffer, cluster);
1N/A}
1N/A
1N/A/* This MUST be called BEFORE the fat_construct_new_fat(), because cluster
1N/A * allocation depend on the old FAT. The reason is, old clusters may
1N/A * still be needed during the resize, (particularly clusters in the directory
1N/A * tree) even if they will be discarded later.
1N/A */
1N/Astatic int
1N/Aalloc_root_dir (FatOpContext* ctx)
1N/A{
1N/A FatSpecific* old_fs_info = FAT_SPECIFIC (ctx->old_fs);
1N/A FatSpecific* new_fs_info = FAT_SPECIFIC (ctx->new_fs);
1N/A FatCluster i;
1N/A FatCluster cluster;
1N/A FatCluster cluster_count;
1N/A
1N/A PED_ASSERT (new_fs_info->fat_type == FAT_TYPE_FAT32, return 0);
1N/A
1N/A cluster_count = ped_div_round_up (
1N/A PED_MAX (16, old_fs_info->root_dir_sector_count),
1N/A new_fs_info->cluster_sectors);
1N/A
1N/A for (i = 0; i < cluster_count; i++) {
1N/A cluster = fat_table_alloc_check_cluster (new_fs_info->fat,
1N/A ctx->new_fs);
1N/A if (!cluster)
1N/A return 0;
1N/A ctx->new_root_dir [i] = cluster;
1N/A clear_cluster (ctx->new_fs, cluster);
1N/A }
1N/A ctx->new_root_dir [i] = 0;
1N/A new_fs_info->root_cluster = ctx->new_root_dir [0];
1N/A return 1;
1N/A}
1N/A
1N/A/* when converting FAT32 -> FAT16
1N/A * fat_duplicate clusters() duplicated the root directory unnecessarily.
1N/A * Let's free it.
1N/A *
1N/A * This must be called AFTER fat_construct_new_fat(). (otherwise, our
1N/A * changes just get overwritten)
1N/A */
1N/Astatic int
1N/Afree_root_dir (FatOpContext* ctx)
1N/A{
1N/A FatSpecific* old_fs_info = FAT_SPECIFIC (ctx->old_fs);
1N/A FatSpecific* new_fs_info = FAT_SPECIFIC (ctx->new_fs);
1N/A FatCluster old_cluster;
1N/A FatFragment i;
1N/A
1N/A PED_ASSERT (old_fs_info->fat_type == FAT_TYPE_FAT32, return 0);
1N/A PED_ASSERT (new_fs_info->fat_type == FAT_TYPE_FAT16, return 0);
1N/A
1N/A for (old_cluster = old_fs_info->root_cluster;
1N/A !fat_table_is_eof (old_fs_info->fat, old_cluster);
1N/A old_cluster = fat_table_get (old_fs_info->fat, old_cluster)) {
1N/A FatFragment old_frag;
1N/A old_frag = fat_cluster_to_frag (ctx->old_fs, old_cluster);
1N/A for (i = 0; i < new_fs_info->cluster_frags; i++) {
1N/A FatFragment new_frag;
1N/A FatCluster new_clst;
1N/A new_frag = fat_op_context_map_fragment (ctx,
1N/A old_frag + i);
1N/A new_clst = fat_frag_to_cluster (ctx->old_fs, new_frag);
1N/A if (!fat_table_set_avail (new_fs_info->fat, new_clst))
1N/A return 0;
1N/A }
1N/A }
1N/A
1N/A return 1;
1N/A}
1N/A
1N/Astatic int
1N/Afat_clear_root_dir (PedFileSystem* fs)
1N/A{
1N/A FatSpecific* fs_info = FAT_SPECIFIC (fs);
1N/A int i;
1N/A
1N/A PED_ASSERT (fs_info->fat_type == FAT_TYPE_FAT16, return 0);
1N/A PED_ASSERT (fs_info->root_dir_sector_count, return 0);
1N/A
1N/A memset (fs_info->buffer, 0, 512);
1N/A
1N/A for (i = 0; i < fs_info->root_dir_sector_count; i++) {
1N/A if (!ped_geometry_write (fs->geom, fs_info->buffer,
1N/A fs_info->root_dir_offset + i, 1)) {
1N/A if (ped_exception_throw (PED_EXCEPTION_ERROR,
1N/A PED_EXCEPTION_IGNORE_CANCEL,
1N/A _("Error writing to the root directory."))
1N/A == PED_EXCEPTION_CANCEL)
1N/A return 0;
1N/A }
1N/A }
1N/A return 1;
1N/A}
1N/A
1N/Astatic int
1N/Afat_construct_converted_tree (FatOpContext* ctx)
1N/A{
1N/A FatSpecific* old_fs_info = FAT_SPECIFIC (ctx->old_fs);
1N/A FatSpecific* new_fs_info = FAT_SPECIFIC (ctx->new_fs);
1N/A FatTraverseInfo* old_trav_info;
1N/A FatTraverseInfo* new_trav_info;
1N/A
1N/A if (new_fs_info->fat_type == FAT_TYPE_FAT32) {
1N/A new_trav_info = fat_traverse_begin (ctx->new_fs,
1N/A new_fs_info->root_cluster, "\\");
1N/A old_trav_info = fat_traverse_begin (ctx->old_fs, FAT_ROOT,
1N/A "\\");
1N/A } else {
1N/A fat_clear_root_dir (ctx->new_fs);
1N/A new_trav_info = fat_traverse_begin (ctx->new_fs, FAT_ROOT,
1N/A "\\");
1N/A old_trav_info = fat_traverse_begin (ctx->old_fs,
1N/A old_fs_info->root_cluster, "\\");
1N/A }
1N/A if (!new_trav_info || !old_trav_info)
1N/A return 0;
1N/A if (!fat_convert_directory (ctx, old_trav_info, new_trav_info))
1N/A return 0;
1N/A return 1;
1N/A}
1N/A
1N/A/*
1N/A Constructs the new directory tree to match the new file locations.
1N/A*/
1N/Astatic int
1N/Afat_construct_dir_tree (FatOpContext* ctx)
1N/A{
1N/A FatSpecific* new_fs_info = FAT_SPECIFIC (ctx->new_fs);
1N/A FatSpecific* old_fs_info = FAT_SPECIFIC (ctx->old_fs);
1N/A
1N/A if (new_fs_info->fat_type == old_fs_info->fat_type) {
1N/A switch (old_fs_info->fat_type) {
1N/A case FAT_TYPE_FAT12:
1N/A PED_ASSERT (0, (void) 0);
1N/A break;
1N/A
1N/A case FAT_TYPE_FAT16:
1N/A return fat_construct_legacy_root (ctx);
1N/A
1N/A case FAT_TYPE_FAT32:
1N/A return fat_construct_root (ctx);
1N/A }
1N/A } else {
1N/A return fat_construct_converted_tree (ctx);
1N/A }
1N/A
1N/A return 0;
1N/A}
1N/A
1N/Astatic FatFragment
1N/A_get_next_old_frag (FatOpContext* ctx, FatFragment frag)
1N/A{
1N/A FatSpecific* old_fs_info = FAT_SPECIFIC (ctx->old_fs);
1N/A FatCluster cluster;
1N/A FatCluster next_cluster;
1N/A
1N/A if ((frag + 1) % old_fs_info->cluster_frags != 0) {
1N/A if (fat_is_fragment_active (ctx->old_fs, frag + 1))
1N/A return frag + 1;
1N/A else
1N/A return -1;
1N/A } else {
1N/A cluster = fat_frag_to_cluster (ctx->old_fs, frag);
1N/A next_cluster = fat_table_get (old_fs_info->fat, cluster);
1N/A
1N/A if (fat_table_is_eof (old_fs_info->fat, next_cluster))
1N/A return -1;
1N/A else
1N/A return fat_cluster_to_frag (ctx->old_fs, next_cluster);
1N/A }
1N/A}
1N/A
1N/A/*
1N/A Constructs the new fat for the resized file system.
1N/A*/
1N/Astatic int
1N/Afat_construct_new_fat (FatOpContext* ctx)
1N/A{
1N/A FatSpecific* old_fs_info = FAT_SPECIFIC (ctx->old_fs);
1N/A FatSpecific* new_fs_info = FAT_SPECIFIC (ctx->new_fs);
1N/A FatFragment old_frag;
1N/A FatCluster new_cluster;
1N/A FatFragment new_frag;
1N/A FatFragment old_next_frag;
1N/A FatFragment new_next_frag;
1N/A FatCluster new_next_cluster;
1N/A FatClusterFlag flag;
1N/A int i;
1N/A
1N/A fat_table_clear (new_fs_info->fat);
1N/A if (!fat_table_set_cluster_count (new_fs_info->fat,
1N/A new_fs_info->cluster_count))
1N/A return 0;
1N/A
1N/A for (old_frag = 0; old_frag < old_fs_info->frag_count; old_frag++) {
1N/A flag = fat_get_fragment_flag (ctx->old_fs, old_frag);
1N/A if (flag == FAT_FLAG_FREE)
1N/A continue;
1N/A if (flag == FAT_FLAG_BAD) {
1N/A new_frag = fat_op_context_map_static_fragment (
1N/A ctx, old_frag);
1N/A if (new_frag == -1)
1N/A continue;
1N/A new_cluster = fat_frag_to_cluster (ctx->new_fs,
1N/A new_frag);
1N/A fat_table_set_bad (new_fs_info->fat, new_cluster);
1N/A continue;
1N/A }
1N/A
1N/A new_frag = fat_op_context_map_fragment (ctx, old_frag);
1N/A new_cluster = fat_frag_to_cluster (ctx->new_fs, new_frag);
1N/A
1N/A old_next_frag = _get_next_old_frag (ctx, old_frag);
1N/A if (old_next_frag == -1) {
1N/A fat_table_set_eof (new_fs_info->fat, new_cluster);
1N/A continue;
1N/A }
1N/A
1N/A new_next_frag = fat_op_context_map_fragment (ctx,
1N/A old_next_frag);
1N/A PED_ASSERT (new_next_frag != -1, return 0);
1N/A
1N/A new_next_cluster = fat_frag_to_cluster (ctx->new_fs,
1N/A new_next_frag);
1N/A PED_ASSERT (new_next_cluster != new_cluster, return 0);
1N/A
1N/A fat_table_set (new_fs_info->fat, new_cluster, new_next_cluster);
1N/A }
1N/A
1N/A#if 0
1N/A#ifdef PED_VERBOSE
1N/A for (old_cluster=2; old_cluster < old_fs_info->cluster_count+2;
1N/A old_cluster++) {
1N/A if (fat_table_is_available (old_fs_info->fat, old_cluster))
1N/A continue;
1N/A
1N/A printf ("%d->%d\t(next: %d->%d)\n",
1N/A old_cluster,
1N/A ctx->remap [old_cluster],
1N/A fat_table_get (old_fs_info->fat, old_cluster),
1N/A fat_table_get (new_fs_info->fat,
1N/A ctx->remap [old_cluster]));
1N/A }
1N/A#endif /* PED_VERBOSE */
1N/A#endif
1N/A
1N/A if (old_fs_info->fat_type == FAT_TYPE_FAT32
1N/A && new_fs_info->fat_type == FAT_TYPE_FAT32) {
1N/A new_fs_info->root_cluster
1N/A = fat_op_context_map_cluster (ctx,
1N/A old_fs_info->root_cluster);
1N/A }
1N/A
1N/A if (old_fs_info->fat_type == FAT_TYPE_FAT16
1N/A && new_fs_info->fat_type == FAT_TYPE_FAT32) {
1N/A for (i=0; ctx->new_root_dir[i+1]; i++) {
1N/A fat_table_set (new_fs_info->fat,
1N/A ctx->new_root_dir[i],
1N/A ctx->new_root_dir[i+1]);
1N/A }
1N/A fat_table_set_eof (new_fs_info->fat, ctx->new_root_dir[i]);
1N/A }
1N/A
1N/A return 1;
1N/A}
1N/A
1N/Astatic int
1N/Aask_type (PedFileSystem* fs, int fat16_ok, int fat32_ok, FatType* out_fat_type)
1N/A{
1N/A FatSpecific* fs_info = FAT_SPECIFIC (fs);
1N/A PedExceptionOption status;
1N/A const char* fat16_msg;
1N/A const char* fat32_msg;
1N/A
1N/A if (fs_info->fat_type == FAT_TYPE_FAT16)
1N/A fat16_msg = _("If you leave your file system as FAT16, "
1N/A "then you will have no problems.");
1N/A else
1N/A fat16_msg = _("If you convert to FAT16, and MS Windows "
1N/A "is installed on this partition, then "
1N/A "you must re-install the MS Windows boot "
1N/A "loader. If you want to do this, you "
1N/A "should consult the Parted manual (or "
1N/A "your distribution's manual).");
1N/A
1N/A if (fs_info->fat_type == FAT_TYPE_FAT32)
1N/A fat32_msg = _("If you leave your file system as FAT32, "
1N/A "then you will not introduce any new "
1N/A "problems.");
1N/A else
1N/A fat32_msg = _("If you convert to FAT32, and MS Windows "
1N/A "is installed on this partition, then "
1N/A "you must re-install the MS Windows boot "
1N/A "loader. If you want to do this, you "
1N/A "should consult the Parted manual (or "
1N/A "your distribution's manual). Also, "
1N/A "converting to FAT32 will make the file "
1N/A "system unreadable by MS DOS, MS Windows "
1N/A "95a, and MS Windows NT.");
1N/A
1N/A if (fat16_ok && fat32_ok) {
1N/A status = ped_exception_throw (
1N/A PED_EXCEPTION_INFORMATION,
1N/A PED_EXCEPTION_YES_NO_CANCEL,
1N/A _("%s %s %s"),
1N/A _("Would you like to use FAT32?"),
1N/A fat16_msg,
1N/A fat32_msg);
1N/A
1N/A switch (status) {
1N/A case PED_EXCEPTION_YES:
1N/A *out_fat_type = FAT_TYPE_FAT32;
1N/A return 1;
1N/A
1N/A case PED_EXCEPTION_NO:
1N/A *out_fat_type = FAT_TYPE_FAT16;
1N/A return 1;
1N/A
1N/A case PED_EXCEPTION_UNHANDLED:
1N/A *out_fat_type = fs_info->fat_type;
1N/A return 1;
1N/A
1N/A case PED_EXCEPTION_CANCEL:
1N/A return 0;
1N/A
1N/A default:
1N/A PED_ASSERT (0, (void) 0);
1N/A break;
1N/A }
1N/A }
1N/A
1N/A if (fat16_ok) {
1N/A if (fs_info->fat_type != FAT_TYPE_FAT16) {
1N/A status = ped_exception_throw (
1N/A PED_EXCEPTION_WARNING,
1N/A PED_EXCEPTION_OK_CANCEL,
1N/A _("%s %s"),
1N/A _("The file system can only be resized to this "
1N/A "size by converting to FAT16."),
1N/A fat16_msg);
1N/A if (status == PED_EXCEPTION_CANCEL)
1N/A return 0;
1N/A }
1N/A *out_fat_type = FAT_TYPE_FAT16;
1N/A return 1;
1N/A }
1N/A
1N/A if (fat32_ok) {
1N/A if (fs_info->fat_type != FAT_TYPE_FAT32) {
1N/A status = ped_exception_throw (
1N/A PED_EXCEPTION_WARNING,
1N/A PED_EXCEPTION_OK_CANCEL,
1N/A _("%s %s"),
1N/A _("The file system can only be resized to this "
1N/A "size by converting to FAT32."),
1N/A fat32_msg);
1N/A if (status == PED_EXCEPTION_CANCEL)
1N/A return 0;
1N/A }
1N/A *out_fat_type = FAT_TYPE_FAT32;
1N/A return 1;
1N/A }
1N/A
1N/A ped_exception_throw (
1N/A PED_EXCEPTION_NO_FEATURE,
1N/A PED_EXCEPTION_CANCEL,
1N/A _("GNU Parted cannot resize this partition to this size. "
1N/A "We're working on it!"));
1N/A
1N/A return 0;
1N/A}
1N/A
1N/A/* For resize operations: determine if the file system must be FAT16 or FAT32,
1N/A * or either. If the new file system must be FAT32, then query for
1N/A * confirmation. If either file system can be used, query for which one.
1N/A */
1N/Astatic int
1N/Aget_fat_type (PedFileSystem* fs, const PedGeometry* new_geom,
1N/A FatType* out_fat_type)
1N/A{
1N/A FatSpecific* fs_info = FAT_SPECIFIC (fs);
1N/A PedSector fat16_cluster_sectors;
1N/A PedSector fat32_cluster_sectors;
1N/A FatCluster dummy_cluster_count;
1N/A PedSector dummy_fat_sectors;
1N/A int fat16_ok;
1N/A int fat32_ok;
1N/A
1N/A fat16_ok = fat_calc_resize_sizes (
1N/A new_geom,
1N/A fs_info->cluster_sectors,
1N/A FAT_TYPE_FAT16,
1N/A fs_info->root_dir_sector_count,
1N/A fs_info->cluster_sectors,
1N/A &fat16_cluster_sectors,
1N/A &dummy_cluster_count,
1N/A &dummy_fat_sectors);
1N/A
1N/A fat32_ok = fat_calc_resize_sizes (
1N/A new_geom,
1N/A fs_info->cluster_sectors,
1N/A FAT_TYPE_FAT32,
1N/A fs_info->root_dir_sector_count,
1N/A fs_info->cluster_sectors,
1N/A &fat32_cluster_sectors,
1N/A &dummy_cluster_count,
1N/A &dummy_fat_sectors);
1N/A
1N/A return ask_type (fs, fat16_ok, fat32_ok, out_fat_type);
1N/A}
1N/A
1N/A/* Creates the PedFileSystem struct for the new resized file system, and
1N/A sticks it in a FatOpContext. At the end of the process, the original
1N/A (ctx->old_fs) is destroyed, and replaced with the new one (ctx->new_fs).
1N/A */
1N/Astatic FatOpContext*
1N/Acreate_resize_context (PedFileSystem* fs, const PedGeometry* new_geom)
1N/A{
1N/A FatSpecific* fs_info = FAT_SPECIFIC (fs);
1N/A FatSpecific* new_fs_info;
1N/A PedFileSystem* new_fs;
1N/A PedSector new_cluster_sectors;
1N/A FatCluster new_cluster_count;
1N/A PedSector new_fat_sectors;
1N/A FatType new_fat_type;
1N/A PedSector root_dir_sector_count;
1N/A FatOpContext* context;
1N/A
1N/A /* hypothetical number of root dir sectors, if we end up using
1N/A * FAT16
1N/A */
1N/A if (fs_info->root_dir_sector_count)
1N/A root_dir_sector_count = fs_info->root_dir_sector_count;
1N/A else
1N/A root_dir_sector_count = FAT_ROOT_DIR_ENTRY_COUNT
1N/A * sizeof (FatDirEntry) / 512;
1N/A
1N/A if (!get_fat_type (fs, new_geom, &new_fat_type))
1N/A return 0;
1N/A
1N/A fat_calc_resize_sizes (new_geom, fs_info->cluster_sectors, new_fat_type,
1N/A root_dir_sector_count, fs_info->cluster_sectors,
1N/A &new_cluster_sectors, &new_cluster_count, &new_fat_sectors);
1N/A
1N/A if (!fat_check_resize_geometry (fs, new_geom, new_cluster_sectors,
1N/A new_cluster_count))
1N/A goto error;
1N/A
1N/A new_fs = fat_alloc (new_geom);
1N/A if (!new_fs)
1N/A goto error;
1N/A
1N/A new_fs_info = FAT_SPECIFIC (new_fs);
1N/A if (!new_fs_info)
1N/A goto error_free_new_fs;
1N/A
1N/A/* preserve boot code, etc. */
1N/A memcpy (&new_fs_info->boot_sector, &fs_info->boot_sector,
1N/A sizeof (FatBootSector));
1N/A memcpy (&new_fs_info->info_sector, &fs_info->info_sector,
1N/A sizeof (FatInfoSector));
1N/A
1N/A new_fs_info->logical_sector_size = fs_info->logical_sector_size;
1N/A new_fs_info->sector_count = new_geom->length;
1N/A
1N/A new_fs_info->sectors_per_track = fs_info->sectors_per_track;
1N/A new_fs_info->heads = fs_info->heads;
1N/A
1N/A new_fs_info->cluster_size = new_cluster_sectors * 512;
1N/A new_fs_info->cluster_sectors = new_cluster_sectors;
1N/A new_fs_info->cluster_count = new_cluster_count;
1N/A new_fs_info->dir_entries_per_cluster = fs_info->dir_entries_per_cluster;
1N/A
1N/A new_fs_info->fat_type = new_fat_type;
1N/A new_fs_info->fat_table_count = 2;
1N/A new_fs_info->fat_sectors = new_fat_sectors;
1N/A
1N/A /* what about copying? */
1N/A new_fs_info->serial_number = fs_info->serial_number;
1N/A
1N/A if (new_fs_info->fat_type == FAT_TYPE_FAT32) {
1N/A new_fs_info->info_sector_offset = 1;
1N/A new_fs_info->boot_sector_backup_offset = 6;
1N/A
1N/A new_fs_info->root_dir_offset = 0;
1N/A new_fs_info->root_dir_entry_count = 0;
1N/A new_fs_info->root_dir_sector_count = 0;
1N/A
1N/A /* we add calc_align_sectors to push the cluster_offset
1N/A forward, to keep the clusters aligned between the new
1N/A and old file systems
1N/A */
1N/A new_fs_info->fat_offset
1N/A = fat_min_reserved_sector_count (FAT_TYPE_FAT32)
1N/A + fat_calc_align_sectors (new_fs, fs);
1N/A
1N/A new_fs_info->cluster_offset
1N/A = new_fs_info->fat_offset
1N/A + 2 * new_fs_info->fat_sectors;
1N/A } else {
1N/A new_fs_info->root_dir_sector_count = root_dir_sector_count;
1N/A new_fs_info->root_dir_entry_count
1N/A = root_dir_sector_count * 512 / sizeof (FatDirEntry);
1N/A
1N/A new_fs_info->fat_offset
1N/A = fat_min_reserved_sector_count (FAT_TYPE_FAT16)
1N/A + fat_calc_align_sectors (new_fs, fs);
1N/A
1N/A new_fs_info->root_dir_offset = new_fs_info->fat_offset
1N/A + 2 * new_fs_info->fat_sectors;
1N/A
1N/A new_fs_info->cluster_offset = new_fs_info->root_dir_offset
1N/A + new_fs_info->root_dir_sector_count;
1N/A }
1N/A
1N/A new_fs_info->total_dir_clusters = fs_info->total_dir_clusters;
1N/A
1N/A context = fat_op_context_new (new_fs, fs);
1N/A if (!context)
1N/A goto error_free_new_fs_info;
1N/A
1N/A if (!fat_op_context_create_initial_fat (context))
1N/A goto error_free_context;
1N/A
1N/A if (!fat_alloc_buffers (new_fs))
1N/A goto error_free_fat;
1N/A
1N/A return context;
1N/A
1N/Aerror_free_fat:
1N/A fat_table_destroy (new_fs_info->fat);
1N/Aerror_free_context:
1N/A free (context);
1N/Aerror_free_new_fs_info:
1N/A free (new_fs_info);
1N/Aerror_free_new_fs:
1N/A free (new_fs);
1N/Aerror:
1N/A return NULL;
1N/A}
1N/A
1N/Astatic int
1N/Aresize_context_assimilate (FatOpContext* ctx)
1N/A{
1N/A FatSpecific* old_fs_info = FAT_SPECIFIC (ctx->old_fs);
1N/A FatSpecific* new_fs_info = FAT_SPECIFIC (ctx->new_fs);
1N/A
1N/A fat_free_buffers (ctx->old_fs);
1N/A fat_table_destroy (old_fs_info->fat);
1N/A free (old_fs_info);
1N/A ped_geometry_destroy (ctx->old_fs->geom);
1N/A
1N/A ctx->old_fs->type_specific = ctx->new_fs->type_specific;
1N/A ctx->old_fs->geom = ctx->new_fs->geom;
1N/A ctx->old_fs->type = (new_fs_info->fat_type == FAT_TYPE_FAT16)
1N/A ? &fat16_type
1N/A : &fat32_type;
1N/A
1N/A free (ctx->new_fs);
1N/A
1N/A fat_op_context_destroy (ctx);
1N/A
1N/A return 1;
1N/A}
1N/A
1N/Astatic int
1N/Aresize_context_abort (FatOpContext* ctx)
1N/A{
1N/A FatSpecific* new_fs_info = FAT_SPECIFIC (ctx->new_fs);
1N/A
1N/A fat_free_buffers (ctx->new_fs);
1N/A fat_table_destroy (new_fs_info->fat);
1N/A free (new_fs_info);
1N/A ped_geometry_destroy (ctx->new_fs->geom);
1N/A free (ctx->new_fs);
1N/A
1N/A fat_op_context_destroy (ctx);
1N/A
1N/A return 1;
1N/A}
1N/A
1N/A/* copies the "hidden" sectors, between the boot sector and the FAT. Required,
1N/A * for the Windows 98 FAT32 boot loader
1N/A */
1N/Aint
1N/A_copy_hidden_sectors (FatOpContext* ctx)
1N/A{
1N/A FatSpecific* old_fs_info = FAT_SPECIFIC (ctx->old_fs);
1N/A FatSpecific* new_fs_info = FAT_SPECIFIC (ctx->new_fs);
1N/A PedSector first = 1;
1N/A PedSector last;
1N/A PedSector count;
1N/A
1N/A /* nothing to copy for FAT16 */
1N/A if (old_fs_info->fat_type == FAT_TYPE_FAT16
1N/A || new_fs_info->fat_type == FAT_TYPE_FAT16)
1N/A return 1;
1N/A
1N/A last = PED_MIN (old_fs_info->fat_offset, new_fs_info->fat_offset) - 1;
1N/A count = last - first + 1;
1N/A
1N/A PED_ASSERT (count < BUFFER_SIZE, return 0);
1N/A
1N/A if (!ped_geometry_read (ctx->old_fs->geom, old_fs_info->buffer,
1N/A first, count))
1N/A return 0;
1N/A if (!ped_geometry_write (ctx->new_fs->geom, old_fs_info->buffer,
1N/A first, count))
1N/A return 0;
1N/A return 1;
1N/A}
1N/A
1N/Aint
1N/Afat_resize (PedFileSystem* fs, PedGeometry* geom, PedTimer* timer)
1N/A{
1N/A FatSpecific* fs_info = FAT_SPECIFIC (fs);
1N/A FatSpecific* new_fs_info;
1N/A FatOpContext* ctx;
1N/A PedFileSystem* new_fs;
1N/A
1N/A ctx = create_resize_context (fs, geom);
1N/A if (!ctx)
1N/A goto error;
1N/A new_fs = ctx->new_fs;
1N/A new_fs_info = FAT_SPECIFIC (new_fs);
1N/A
1N/A if (!fat_duplicate_clusters (ctx, timer))
1N/A goto error_abort_ctx;
1N/A if (fs_info->fat_type == FAT_TYPE_FAT16
1N/A && new_fs_info->fat_type == FAT_TYPE_FAT32) {
1N/A if (!alloc_root_dir (ctx))
1N/A goto error_abort_ctx;
1N/A }
1N/A if (!fat_construct_new_fat (ctx))
1N/A goto error_abort_ctx;
1N/A if (fs_info->fat_type == FAT_TYPE_FAT32
1N/A && new_fs_info->fat_type == FAT_TYPE_FAT16) {
1N/A if (!free_root_dir (ctx))
1N/A goto error_abort_ctx;
1N/A }
1N/A if (!fat_construct_dir_tree (ctx))
1N/A goto error_abort_ctx;
1N/A if (!fat_table_write_all (new_fs_info->fat, new_fs))
1N/A goto error_abort_ctx;
1N/A
1N/A _copy_hidden_sectors (ctx);
1N/A fat_boot_sector_generate (&new_fs_info->boot_sector, new_fs);
1N/A fat_boot_sector_write (&new_fs_info->boot_sector, new_fs);
1N/A if (new_fs_info->fat_type == FAT_TYPE_FAT32) {
1N/A fat_info_sector_generate (&new_fs_info->info_sector, new_fs);
1N/A fat_info_sector_write (&new_fs_info->info_sector, new_fs);
1N/A }
1N/A
1N/A if (!resize_context_assimilate (ctx))
1N/A goto error;
1N/A
1N/A return 1;
1N/A
1N/Aerror_abort_ctx:
1N/A resize_context_abort (ctx);
1N/Aerror:
1N/A return 0;
1N/A}
1N/A
1N/A#endif /* !DISCOVER_ONLY */