1

我试图从更高(更合乎逻辑)的级别了解 FAT 文件系统是如何工作的。FAT 文件系统有一个文件分配表,磁盘中每个可用的分配单元(集群)都有一个条目。该数据结构用于将文件与该文件出现在磁盘上的第一个簇的地址进行映射(一个文件几乎肯定会占用多个簇,并且这些簇以链表样式连接在一起)。到目前为止,一切都很好。但是文件分配表中的key是什么?文件名是否带有完整路径?

例如,假设我需要访问文件 C:\folder1\myFile.txt,I/O 管理器在文件分配表中搜索文件名(包括路径),直到找到一个条目,如果是,它返回第一个集群的地址。它是这样工作的吗?我在网上阅读了很多关于这个主题的文档,但不知何故,对文件分配表的访问对我来说仍然是模糊的。预先感谢您的帮助。

[编辑]

我在网上阅读的越多,我就越困惑。我将尝试这个简单的例子,希望它能澄清我的问题。假设我的 C 盘中只有 2 个文件:

C:\myFile.txt

C:\folder1\myFile.txt

文件分配表(以非常抽象的方式)将有 3 个条目:

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

假设到目前为止我是正确的,假设我想访问 C:\folder1 中的 myFile.txt:我不能使用文件名 (MYFILE.TXT) 本身作为我的密钥,因为我有 2 个同名条目(我不知道该选择哪个条目)。

此时我想我必须从文件夹开始,所以我扫描 FAT 以找到 FOLDER1:我得到条目 #2(集群 300)。下一步是什么?我将如何继续扫描表以查找“MYFILE.TXT”所需的目录条目,以便可以访问指定集群的硬盘驱动器?

也许我从错误的角度来看这个,我不知道。感谢大家的帮助。

4

1 回答 1

0

看看这段代码,它是一个不知何故的 dir 命令,它打印磁盘 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];
    }
 }
}

注意事项

1-第一个结构是FAT中最重要的,因为所有信息都存储在这里

2- FCB 包含有关根目录中文件和目录的信息

3-此代码可以在 Windows 3.11 上运行,并且 (Turbo c++) 是用于编码和编译的程序

4-代码代表FAT12,整数为2字节

如果您有任何问题,我希望我能帮助您

于 2015-03-22T16:24:38.607 回答