Thursday, January 10, 2008

QVFS - A FAT32 based virtual file system powered by VB.NET 2005

QVFS - A FAT32 based virtual file system powered by VB.NET 2005 (CodeProject)
A virtual file system created using VB.NET, a easy way to store multiple folders and files in two real physical files.
Download source - 45 KB


Introduction

This is a .NET library implementing a virtual file system(VFS) named QVFS which is based on FAT32 concepts. It uses two files(one information file and one data file) to store multiple folders and files. This is just a virtual file system for storing files, and no other function, such as compress function, is included.

The source codes can be compiled under both .NET Framework 1.1 and .NET Framework 2.0, however, it is not tested under .NET Framework 3.0.

The goal of this library is to use two file to store every thing for a program usage. For example, you may write your personal mail management system to store all the mails in VFS instead of creating one file for each mail and creating many many folders for categorizing mails. Your program may have many log files and it is splitted by different date, thus one year it will have at lease 365 files. Why not using a VFS, only two files, easy to backup, restore and manage.

Background

This virtual file system uses most concepts of FAT32 file system. You can think this is a virtual compact FAT32 file system. Some concepts will be descript below and know something about FAT32 will make you easy to go through this document.

Concepts & Technique Details About QVFS

File structure of QVFS

QVFS uses two files to create the "virtual disk" for storing files.

One is information file of which extension is ".qfi" and the other is data file of which extension is ".qfd" and the file names for these two files will be the same by default.

The information file stores the FAT only and the data file stores the files' data.

The following figure shows the file structure of the information file and the data file.

Screenshot - FileStru.jpg

Main concepts

Name Description
Cluster The basic unit to store data, in this VFS it is 4KB for each cluster.
FAT File allocation table, which tells how the file is stored. In another word, which clusters are used to store a file.
Here shows a sample for the FAT. One cell indicates one FAT entry and FAT entries with the same color indicates one file storage information. The FAT is using a chain table data structure and one FAT entry is mapping to one cluster. The number in the FAT entry tells where to find next FAT entry(for FAT reader) and cluster(for file reader) of the file and the 'FFFFFFFF' indicates the file is ended in that cluster, the '0' indicates that entry has not been used yet, it is empty.
Screenshot - FATSample.jpg
  • Each FAT entry is 4 bytes(32 bit integer)
  • Each cluster is 4KB
  • In principle the total amount of the bytes can be stored in QVFS is 232*4K=17TB
Folder (Directory) A folder can contains many file and sub folders. In FAT32, folder is just a special file, only the difference is that file contains the information data about the files and sub folders under itself and a file contains its own data.

How does the program work

When adding a file in QVFS, it will first find the empty FAT entries which is mark as '00000000' and return a list of empty clusters which can be used and then read the source file block by block (the block size is equal to cluster size) from the very beginning and put each block into each cluster and write the cluster index in the FAT till the file's end.

When reading a file in QVFS, it will first read the first cluster index number of the root folder, cluster 0 in the above figure. Then find out the root folder data 'file' and search the root folder for the file you want. If the file is in one sub folder it will search the sub folders one by one from the root, finally get the file's start cluster index and following to FAT to read all the data of that file.

Using the codes

To use the QVFS, you need to create the virtual disk first. The virtual disk indicated by two files(.qfi & .qfd) which is mentioned above.

'Create QVFS disk file

QVFS.QVirtualFileSystemManager.CreateQVFSFile("C:\\test.qfi", true);


Add disk files into QVFS

'Add the file d:\abc.txt to the root folder of QVFS

QVFS.QVirtualFileSystemManager vfm = new QVirtualFileSystemManager();
vfm.OpenQVFSFile("C:\\test.qfi");
vfm.AddNewVFile("D:\\abc.txt","
\\" true);
vfm.CloseQVFSFile();


Delete a file from QVFS

'Delete the file '\abc.txt' from QVFS

QVFS.QVirtualFileSystemManager vfm=new QVirtualFileSystemManager();
vfm.OpenQVFSFile("C:\\test.qfi");
vfm.DeleteVFile("\\abc.txt", true);
vfm.CloseQVFSFile();

Save file in QVFS to disk

'Save the file "\abc.txt" to "C:\"

QVFS.QVirtualFileSystemManager vfm=new QVirtualFileSystemManager();
vfm.OpenQVFSFile("C:\\test.qfi");
vfm.SaveVFileToDisk("\\abc.txt", "C:\\abc.txt");
vfm.CloseQVFSFile();


About the DEMO program


The DEMO program shows the above functions using real codes. It may have some bugs and it is NOT tested carefully. It is only for DEMO. In the other hand, the QVFS library is tested throughly, no big function bug is found in the current version.


It shows the folder structure on the left section and shows the detail file's information in QVFS.


Screenshot - DemoPro.jpg