home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 8 / CDACTUAL8.iso / install / fips / source / fat.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-11  |  4.1 KB  |  143 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.1 1994/05/25 22:19:46 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 "logdr_st.h" 
  32. #include "global.h" 
  33. #include "fat.h" 
  34.  
  35. fat::fat (class logical_drive *logical_drive,int number) 
  36.     fat::logical_drive = logical_drive; 
  37.     fat::number = number; 
  38.     buffer = new sector; 
  39.     start_sector = (number == 1) ? logical_drive->info().start_fat1 : logical_drive->info().start_fat2; 
  40.     sector_in_buffer = -1; 
  41.  
  42. dword fat16::next_cluster (dword cluster_number) 
  43.     dword sector = cluster_number / 256; 
  44.     int offset = (cluster_number % 256) * 2; 
  45.  
  46.     if (sector != sector_in_buffer) read_sector (sector); 
  47.  
  48.     return ((dword) buffer->data[offset] | ((dword) buffer->data[offset + 1] << 8)); 
  49.  
  50. void fat::read_sector (dword sector) 
  51.     if (logical_drive->read_sector (sector + start_sector,buffer)) 
  52.         if (number == 1) 
  53.             error ("Error reading FAT 1"); 
  54.         else 
  55.             error ("Error reading FAT 2"); 
  56.     sector_in_buffer = sector; 
  57.  
  58. void fat16::check_against (class fat16 *fat2) 
  59.     printx ("Checking FAT ... "); 
  60.  
  61.     for (int i=0;i<logical_drive->bpb().sectors_per_fat;i++) 
  62.     { 
  63.         read_sector (i); 
  64.         fat2->read_sector (i); 
  65.  
  66.         for (int j=0;j<512;j++) if (buffer->data[j] != fat2->buffer->data[j]) 
  67.             error ("FAT copies differ: FAT 1 -> %02Xh, FAT 2 -> %02Xh in sector %u, byte %u",buffer->data[j],fat2->buffer->data[j],i,j); 
  68.  
  69.         if (i == 0) 
  70.         { 
  71.             if (buffer->data[0] != 0xf8) 
  72.                 if (!global.override_media_descriptor) 
  73.                     error ("Wrong Media Descriptor Byte in FAT: %02Xh",buffer->data[0]); 
  74.             if ((buffer->data[1] != 0xff) || (buffer->data[2] != 0xff) || (buffer->data[3] != 0xff)) 
  75.                 warning ("Wrong FAT entries 1 & 2: %02X %02X %02X %02X",buffer->data[0],buffer->data[1],buffer->data[2],buffer->data[3]); 
  76.         } 
  77.     } 
  78.     printx ("OK\n"); 
  79.  
  80. void fat16::check_empty (dword new_start_sector) 
  81.     dword first_cluster = (new_start_sector - logical_drive->info().start_data) / logical_drive->bpb().sectors_per_cluster + 2; 
  82.  
  83.     dword last_cluster = logical_drive->info().no_of_clusters + 1; 
  84.  
  85.     if (last_cluster > ((dword) 256 * logical_drive->bpb().sectors_per_fat - 1)) last_cluster = (dword) 256 * logical_drive->bpb().sectors_per_fat - 1; 
  86.  
  87.     printx ("First Cluster: %lu\nLast Cluster: %lu\n\n",first_cluster,last_cluster); 
  88.     printx ("Testing if empty ... "); 
  89.  
  90.     for (dword i=first_cluster;i <= last_cluster;i++) 
  91.     { 
  92.         dword fat_entry = next_cluster (i); 
  93.  
  94.         if (fat_entry != 0) if (fat_entry != 0xfff7) 
  95.         { 
  96.             if (fat_entry == 0xffff) 
  97.             { 
  98.                 error ("New Partition not empty: cluster %lu ( FAT entry: <EOF> )",i); 
  99.             } 
  100.             else 
  101.             { 
  102.                 error ("New Partition not empty: cluster %lu ( FAT entry: %lu )",i,fat_entry); 
  103.             } 
  104.         } 
  105.     } 
  106.     printx ("OK\n"); 
  107.  
  108. dword fat16::min_cluster (void) 
  109.     dword first_cluster = 2; 
  110.  
  111.     dword last_cluster = logical_drive->info().no_of_clusters + 1; 
  112.  
  113.     if (last_cluster > ((dword) 256 * logical_drive->bpb().sectors_per_fat - 1)) last_cluster = (dword) 256 * logical_drive->bpb().sectors_per_fat - 1; 
  114.  
  115.     printx ("Searching for free space ... "); 
  116.  
  117.     dword i; 
  118.  
  119.     for (i=last_cluster;i >= first_cluster;i--) 
  120.     { 
  121.         dword fat_entry = next_cluster (i); 
  122.  
  123.         if (fat_entry != 0) if (fat_entry != 0xfff7) 
  124.         { 
  125.             i++; 
  126.             break; 
  127.         } 
  128.     } 
  129.     printx ("OK\n\n"); 
  130.     return (i); 
  131.