home *** CD-ROM | disk | FTP | other *** search
/ James Briskly's Game Magazine 2 / JBGM002S.ZIP / SOURCE / FAT.CPP < prev    next >
C/C++ Source or Header  |  1995-08-22  |  4KB  |  162 lines

  1. /*
  2.     FIPS - the First nondestructive Interactive Partition Splitting program
  3.  
  4.     Module fat.cpp
  5.  
  6.     RCS - Header:
  7.     $Header: c:/daten/fips/source/main/RCS/fat.cpp 1.4 1995/01/19 00:00:51 schaefer Exp schaefer $
  8.  
  9.     Copyright (C) 1993 Arno Schaefer
  10.  
  11.     This program is free software; you can redistribute it and/or modify
  12.     it under the terms of the GNU General Public License as published by
  13.     the Free Software Foundation; either version 2 of the License, or
  14.     (at your option) any later version.
  15.  
  16.     This program is distributed in the hope that it will be useful,
  17.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.     GNU General Public License for more details.
  20.  
  21.     You should have received a copy of the GNU General Public License
  22.     along with this program; if not, write to the Free Software
  23.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  
  25.  
  26.     Report problems and direct all questions to:
  27.  
  28.     schaefer@rbg.informatik.th-darmstadt.de
  29. */
  30.  
  31. #include <stdlib.h>
  32.  
  33. #include "logdr_st.h"
  34. #include "global.h"
  35. #include "fat.h"
  36. #include "input.h"
  37.  
  38.  
  39. fat::fat (class logical_drive *logical_drive,int number)
  40. {
  41.     fat::logical_drive = logical_drive;
  42.     fat::number = number;
  43.     buffer = new sector;
  44.     start_sector = (number == 1) ? logical_drive->info().start_fat1 : logical_drive->info().start_fat2;
  45.     sector_in_buffer = -1;
  46. }
  47.  
  48.  
  49. dword fat16::next_cluster (dword cluster_number)
  50. {
  51.     dword sector = cluster_number / 256;
  52.     int offset = (cluster_number % 256) * 2;
  53.  
  54.     if (sector != sector_in_buffer)
  55.     {
  56.         read_sector (sector);
  57.     }
  58.  
  59.     return ((dword) buffer->data[offset] | ((dword) buffer->data[offset + 1] << 8));
  60. }
  61.  
  62.  
  63. void fat::read_sector (dword sector)
  64. {
  65.     if (logical_drive->read_sector (sector + start_sector,buffer))
  66.         if (number == 1)
  67.             error ("Error reading FAT 1");
  68.         else
  69.             error ("Error reading FAT 2");
  70.  
  71.     sector_in_buffer = sector;
  72. }
  73.  
  74.  
  75. void fat16::check_against (class fat16 *fat2)
  76. {
  77.     printx ("Checking FAT ... ");
  78.  
  79.     for (int i=0;i<logical_drive->bpb().sectors_per_fat;i++)
  80.     {
  81.         read_sector (i);
  82.         fat2->read_sector (i);
  83.  
  84.         for (int j=0;j<512;j++) if (buffer->data[j] != fat2->buffer->data[j])
  85.             error ("FAT copies differ: FAT 1 -> %02Xh, FAT 2 -> %02Xh in sector %u, byte %u",buffer->data[j],fat2->buffer->data[j],i,j);
  86.  
  87.         if (i == 0)
  88.         {
  89.             if (buffer->data[0] != 0xf8)
  90.             {
  91.                 warning (false, "Wrong media descriptor byte in FAT: %02Xh",buffer->data[0]);
  92.  
  93.                 printx ("Continue (y/n)? ");
  94.                 if (ask_yes_no () == 'n') exit (-1);
  95.             }
  96.  
  97.             if ((buffer->data[1] != 0xff) || (buffer->data[2] != 0xff) || (buffer->data[3] != 0xff))
  98.                 warning (true, "Wrong FAT entries 1 & 2: %02X %02X %02X %02X",buffer->data[0],buffer->data[1],buffer->data[2],buffer->data[3]);
  99.         }
  100.     }
  101.     printx ("OK\n");
  102. }
  103.  
  104.  
  105. void fat16::check_empty (dword new_start_sector)
  106. {
  107.     dword first_cluster = (new_start_sector - logical_drive->info().start_data) / logical_drive->bpb().sectors_per_cluster + 2;
  108.  
  109.     dword last_cluster = logical_drive->info().no_of_clusters + 1;
  110.  
  111.     if (last_cluster > ((dword) 256 * logical_drive->bpb().sectors_per_fat - 1)) last_cluster = (dword) 256 * logical_drive->bpb().sectors_per_fat - 1;
  112.  
  113.     printx ("First Cluster: %lu\nLast Cluster: %lu\n\n",first_cluster,last_cluster);
  114.     printx ("Testing if empty ... ");
  115.  
  116.     for (dword i=first_cluster;i <= last_cluster;i++)
  117.     {
  118.         dword fat_entry = next_cluster (i);
  119.  
  120.         if (fat_entry != 0) if (fat_entry != 0xfff7)
  121.         {
  122.             if (fat_entry == 0xffff)
  123.             {
  124.                 error ("New partition not empty: cluster %lu ( FAT entry: <EOF> )",i);
  125.             }
  126.             else
  127.             {
  128.                 error ("New partition not empty: cluster %lu ( FAT entry: %lu )",i,fat_entry);
  129.             }
  130.         }
  131.     }
  132.     
  133.     printx ("OK\n");
  134. }
  135.  
  136.  
  137. dword fat16::min_free_cluster (void)
  138. {
  139.     dword first_cluster = 2;
  140.  
  141.     dword last_cluster = logical_drive->info().no_of_clusters + 1;
  142.  
  143.     if (last_cluster > ((dword) 256 * logical_drive->bpb().sectors_per_fat - 1)) last_cluster = (dword) 256 * logical_drive->bpb().sectors_per_fat - 1;
  144.  
  145.     printx ("Searching for free space ... ");
  146.  
  147.     dword i;
  148.  
  149.     for (i=last_cluster;i >= first_cluster;i--)
  150.     {
  151.         dword fat_entry = next_cluster (i);
  152.  
  153.         if (fat_entry != 0) if (fat_entry != 0xfff7)
  154.         {
  155.             i++;
  156.             break;
  157.         }
  158.     }
  159.     printx ("OK\n\n");
  160.     return (i);
  161. }
  162.