2

I’m trying to understand how FAT file systems work from a higher (more logical) level. A FAT file system has a file allocation table, with 1 entry for each available allocation unit (cluster) in the disk. This data structure is used to map a file with the address of the first cluster in which the file appears on the disk (a file almost certainly occupies more than 1 cluster, and these clusters are connected together in a linked-list style). So far so good. But what is the key in the file allocation table? Is the file name with full path?

For instance, let’s say that I need to access file C:\folder1\myFile.txt, the I/O manager searches the file name (including the path) in the file allocation table until it finds an entry, and if so, it returns the address of the first cluster. Is that how it works? I read a lot of documentation online about this topic, but somehow the access to the file allocation table is still fuzzy for me. Thank you in advance for your help.

[EDIT]

The more I read on line, the more confused I am. I'm going to try with this simple example, hopefully it will clarify my question. Let's say I have only 2 files in my C drive:

C:\myFile.txt

C:\folder1\myFile.txt

The file allocation table (in a very abstract way) would have 3 entries:

  | Filename | Extension | First Cluster |
  |----------|-----------|---------------|
1 | MYFILE   | TXT       | 150           |
2 | FOLDER1  |           | 300           |
3 | MYFILE   | TXT       | 900           |

Assuming I'm correct so far, let's say that I want to access myFile.txt in C:\folder1: I cannot use the file name (MYFILE.TXT) itself as my key, because I have 2 entries with the same name (I wouldn't know which entry to pick).

At this point I guess I have to start from the folder, so I scan the FAT to find FOLDER1: I get entry #2 (cluster 300). What's next? How I would I keep scanning the table to find the directory entry that I need for "MYFILE.TXT", so that I can access the hard drive at specified cluster?

Maybe I'm looking at this from the wrong perspective, I don't know. Thanks everyone for your help.

Frederic
  • 83
  • 1
  • 10

1 Answers1

0

Take a look at this code, It is a somehow dir command which print all the names of directories and file in a root directory from disk D:\

#include<dos.h>
#include<stdio.h>
#include<iostream.h>

struct Boot {
     char ignore1[11];
     int Byte_Per_Sector;
     char Serctors_Per_Cluster;
     int Number_Of_Reserved_sectors;
     char Number_of_FATS;
     int Maximum_Number_of_root_dir;
     int Total_Sector_count;
     char Ignore2;
     int Sectors_per_fat;
     int Sectors_per_trach;
     int Number_of_heads;
     char ignore3[4];
     char Total_sector_count_for_fat[4];
     int ignore4;
     char boot_signature;
     char Volume_id[4];
     char Volume_lable[11];
     char File_system_type[8];
     char Rest_of_boot_sector[450];
};
struct FCB {
      char file_name[8];
      char extension[3];
      char attribute;
      int reserved;
      int creation_time;
      int creation_date;
      int last_access_date;
      int ignore_in_fat;
      int last_write_time;
      int last_write_date;
      int first_logic_cluster;
      char file_size[4];
   };

  void main(){
       FCB root[16]; //to read all the Root directory 
       if(absread(3,1,217,&root) ==0) { // start read from disk number 3 , 1 sector and the first sector number is 217 (from the hard disk)
       cout<<"Read of Root Directory started:"<<endl;

       for(int j=1;j<15;j++){
        if(root[j].file_name[0]==0x00){//0x00 represents Unused file
                break;
        }
        if(root[j].attribute!=0x0F) //0x0f mean this directory entry is part of a long file name
        if(root[j].file_name[0]!='?')
        for(int i=0;i<7;i++){
            printf("%c",root[j].file_name[i]); // print the name of all files and directories

        }
        cout<<endl<<root[j].first_logic_cluster; to locate where is the first cluster contains the data on HD
        cout<<endl;
    } 
    cout<<"end of dirs in this cluster"<<endl;
    }
  else {
cout<<"error"<<endl;
}
 cout<<"===================="<<endl;
 char buffer[512];
 //here to read what a file contains after calculating the physical address of that file 
 if(absread(3,1,283,&buffer) ==0) {
    for(int i=0;i<512;i++ ){
        cout<<buffer[i];
    }
 }
}

Things to notice

1-The first struct is the most important thing in FAT because allinformation is stored here

2- FCB contains information about files and directories in root directory

3- This code can be run on Windows 3.11 and (Turbo c++) is the program to code and compile

4- the code represent FAT12 and Integer is 2 bytes

If you have any questions i hope i can help you

Allloush
  • 1,140
  • 8
  • 27
  • 1
    Thank you, Allloush. This helps me a lot. Looking at the FCB, the file system solely uses the file name (with the extension) as the key to retrieve the directory entry. In a scenario like this: C:\myFile.txt C:\folder1\myFile.txt the allocation table (in a very abstract way) would have 3 entries, with 2 with same file name. If I want to access myFile.txt in C:\folder1, DOS would first scan to retrieve the entry for "folder1", then for "myFile.txt". So I'm guessing the First Cluster for file FOLDER1 would be the cluster of the FAT directory entry for MYFILE. Am I getting this right? Thanks. – Frederic Mar 22 '15 at 17:24