home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 8 / IOPROG_8.ISO / install / fips / source / fat.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-25  |  4.4 KB  |  162 lines

  1. /*
  2.  
  3.     FIPS - the First nondestructive Interactive Partition Splitting program
  4.  
  5.  
  6.  
  7.     Module fat.cpp
  8.  
  9.  
  10.  
  11.     RCS - Header:
  12.  
  13.     $Header: c:/daten/fips/source/main/RCS/fat.cpp 1.4 1995/01/19 00:00:51 schaefer Exp schaefer $
  14.  
  15.  
  16.  
  17.     Copyright (C) 1993 Arno Schaefer
  18.  
  19.  
  20.  
  21.     This program is free software; you can redistribute it and/or modify
  22.  
  23.     it under the terms of the GNU General Public License as published by
  24.  
  25.     the Free Software Foundation; either version 2 of the License, or
  26.  
  27.     (at your option) any later version.
  28.  
  29.  
  30.  
  31.     This program is distributed in the hope that it will be useful,
  32.  
  33.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  34.  
  35.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  36.  
  37.     GNU General Public License for more details.
  38.  
  39.  
  40.  
  41.     You should have received a copy of the GNU General Public License
  42.  
  43.     along with this program; if not, write to the Free Software
  44.  
  45.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  46.  
  47.  
  48.  
  49.  
  50.  
  51.     Report problems and direct all questions to:
  52.  
  53.  
  54.  
  55.     schaefer@rbg.informatik.th-darmstadt.de
  56.  
  57. */
  58.  
  59.  
  60.  
  61. #include <stdlib.h>
  62.  
  63.  
  64.  
  65. #include "logdr_st.h"
  66.  
  67. #include "global.h"
  68.  
  69. #include "fat.h"
  70.  
  71. #include "input.h"
  72.  
  73.  
  74.  
  75.  
  76.  
  77. fat::fat (class logical_drive *logical_drive,int number)
  78.  
  79. {
  80.  
  81.     fat::logical_drive = logical_drive;
  82.  
  83.     fat::number = number;
  84.  
  85.     buffer = new sector;
  86.  
  87.     start_sector = (number == 1) ? logical_drive->info().start_fat1 : logical_drive->info().start_fat2;
  88.  
  89.     sector_in_buffer = -1;
  90.  
  91. }
  92.  
  93.  
  94.  
  95.  
  96.  
  97. dword fat16::next_cluster (dword cluster_number)
  98.  
  99. {
  100.  
  101.     dword sector = cluster_number / 256;
  102.  
  103.     int offset = (cluster_number % 256) * 2;
  104.  
  105.  
  106.  
  107.     if (sector != sector_in_buffer)
  108.  
  109.     {
  110.  
  111.         read_sector (sector);
  112.  
  113.     }
  114.  
  115.  
  116.  
  117.     return ((dword) buffer->data[offset] | ((dword) buffer->data[offset + 1] << 8));
  118.  
  119. }
  120.  
  121.  
  122.  
  123.  
  124.  
  125. void fat::read_sector (dword sector)
  126.  
  127. {
  128.  
  129.     if (logical_drive->read_sector (sector + start_sector,buffer))
  130.  
  131.         if (number == 1)
  132.  
  133.             error ("Error reading FAT 1");
  134.  
  135.         else
  136.  
  137.             error ("Error reading FAT 2");
  138.  
  139.  
  140.  
  141.     sector_in_buffer = sector;
  142.  
  143. }
  144.  
  145.  
  146.  
  147.  
  148.  
  149. void fat16::check_against (class fat16 *fat2)
  150.  
  151. {
  152.  
  153.     printx ("Checking FAT ... ");
  154.  
  155.  
  156.  
  157.     for (int i=0;i<logical_drive->bpb().sectors_per_fat;i++)
  158.  
  159.     {
  160.  
  161.         read_sector (i);
  162.  
  163.         fat2->read_sector (i);
  164.  
  165.  
  166.  
  167.         for (int j=0;j<512;j++) if (buffer->data[j] != fat2->buffer->data[j])
  168.  
  169.             error ("FAT copies differ: FAT 1 -> %02Xh, FAT 2 -> %02Xh in sector %u, byte %u",buffer->data[j],fat2->buffer->data[j],i,j);
  170.  
  171.  
  172.  
  173.         if (i == 0)
  174.  
  175.         {
  176.  
  177.             if (buffer->data[0] != 0xf8)
  178.  
  179.             {
  180.  
  181.                 warning (false, "Wrong media descriptor byte in FAT: %02Xh",buffer->data[0]);
  182.  
  183.  
  184.  
  185.                 printx ("Continue (y/n)? ");
  186.  
  187.                 if (ask_yes_no () == 'n') exit (-1);
  188.  
  189.             }
  190.  
  191.  
  192.  
  193.             if ((buffer->data[1] != 0xff) || (buffer->data[2] != 0xff) || (buffer->data[3] != 0xff))
  194.  
  195.                 warning (true, "Wrong FAT entries 1 & 2: %02X %02X %02X %02X",buffer->data[0],buffer->data[1],buffer->data[2],buffer->data[3]);
  196.  
  197.         }
  198.  
  199.     }
  200.  
  201.     printx ("OK\n");
  202.  
  203. }
  204.  
  205.  
  206.  
  207.  
  208.  
  209. void fat16::check_empty (dword new_start_sector)
  210.  
  211. {
  212.  
  213.     dword first_cluster = (new_start_sector - logical_drive->info().start_data) / logical_drive->bpb().sectors_per_cluster + 2;
  214.  
  215.  
  216.  
  217.     dword last_cluster = logical_drive->info().no_of_clusters + 1;
  218.  
  219.  
  220.  
  221.     if (last_cluster > ((dword) 256 * logical_drive->bpb().sectors_per_fat - 1)) last_cluster = (dword) 256 * logical_drive->bpb().sectors_per_fat - 1;
  222.  
  223.  
  224.  
  225.     printx ("First Cluster: %lu\nLast Cluster: %lu\n\n",first_cluster,last_cluster);
  226.  
  227.     printx ("Testing if empty ... ");
  228.  
  229.  
  230.  
  231.     for (dword i=first_cluster;i <= last_cluster;i++)
  232.  
  233.     {
  234.  
  235.         dword fat_entry = next_cluster (i);
  236.  
  237.  
  238.  
  239.         if (fat_entry != 0) if (fat_entry != 0xfff7)
  240.  
  241.         {
  242.  
  243.             if (fat_entry == 0xffff)
  244.  
  245.             {
  246.  
  247.                 error ("New partition not empty: cluster %lu ( FAT entry: <EOF> )",i);
  248.  
  249.             }
  250.  
  251.             else
  252.  
  253.             {
  254.  
  255.                 error ("New partition not empty: cluster %lu ( FAT entry: %lu )",i,fat_entry);
  256.  
  257.             }
  258.  
  259.         }
  260.  
  261.     }
  262.  
  263.     
  264.  
  265.     printx ("OK\n");
  266.  
  267. }
  268.  
  269.  
  270.  
  271.  
  272.  
  273. dword fat16::min_free_cluster (void)
  274.  
  275. {
  276.  
  277.     dword first_cluster = 2;
  278.  
  279.  
  280.  
  281.     dword last_cluster = logical_drive->info().no_of_clusters + 1;
  282.  
  283.  
  284.  
  285.     if (last_cluster > ((dword) 256 * logical_drive->bpb().sectors_per_fat - 1)) last_cluster = (dword) 256 * logical_drive->bpb().sectors_per_fat - 1;
  286.  
  287.  
  288.  
  289.     printx ("Searching for free space ... ");
  290.  
  291.  
  292.  
  293.     dword i;
  294.  
  295.  
  296.  
  297.     for (i=last_cluster;i >= first_cluster;i--)
  298.  
  299.     {
  300.  
  301.         dword fat_entry = next_cluster (i);
  302.  
  303.  
  304.  
  305.         if (fat_entry != 0) if (fat_entry != 0xfff7)
  306.  
  307.         {
  308.  
  309.             i++;
  310.  
  311.             break;
  312.  
  313.         }
  314.  
  315.     }
  316.  
  317.     printx ("OK\n\n");
  318.  
  319.     return (i);
  320.  
  321. }
  322.  
  323.