Click here to Skip to main content
13,002,628 members (60,919 online)
Click here to Skip to main content
Add your own
alternative version

Stats

109.6K views
1.5K downloads
99 bookmarked
Posted 15 Nov 2007

QVFS - A FAT32 Based Virtual File System Powered by VB.NET 2005

, 8 Jun 2009
Rate this:
Please Sign up or sign in to vote.
A virtual file system created using VB.NET, an easy way to store multiple folders and files in two real physical files.

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 files to store everything 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 split by different dates, thus one year it will have at least 365 files. Why not use 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 that this is a virtual compact FAT32 file system. Some concepts will be described below and knowing something about FAT32 will make it easy for you 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 an information file of which the extension is ".qfi" and the other is a data file of which the 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

NameDescription
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 words, which clusters are used to store a file.
Here a sample for the FAT is shown. One cell indicates one FAT entry and FAT entries with the same color indicate 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 the next FAT entry (for FAT reader) and cluster (for file reader) of the file and the 'FFFFFFFF' indicates the file has ended in that cluster, the '0' indicates that entry has not been used yet, it is empty.

FATSample.jpg - Click to enlarge

  • Each FAT entry is 4 bytes(32 bit integer)
  • Each cluster is 4KB
  • In principle, the total amount of the bytes that can be stored in QVFS is 232*4K=17TB
Folder (Directory)A folder can contain many files and sub folders. In FAT32, the folder is just a special file, the only the difference is that the 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 are 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 follow 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 is indicated by two files(.qfi & .qfd) which are 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 thoroughly, 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

Points of Interest

There are many file systems till now. Here I have just shown a virtual file system based on FAT32. FAT32 is a more "simple" file system compared to NTFS and Linux ext., and it is good enough for the VFS only for storing files except for security requirement, compress requirement and so on. I would like to discuss the latest VFS technology with you. Please give your ideas and suggestions.

History

  • 2004.08.26, Version 0.9.0, VFileSystem version 1
  • 2004.09.24, Version 0.9.5, VFileSystem version 2
  • 2007.10.10, Version 1.0.0, bug fixes & converted into .NET Framework 2.0 library, formal release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

TonyTonyQ
Architect
China China
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionCan we access the file path? Pin
Harshw27-Nov-13 3:42
memberHarshw27-Nov-13 3:42 
AnswerRe: Can we access the file path? Pin
TonyTonyQ22-Apr-14 22:57
memberTonyTonyQ22-Apr-14 22:57 
QuestionThanks for this wonderful article. Implemented this QVFS with some major enhancements Pin
Manoj Natarajan28-Oct-13 0:37
professionalManoj Natarajan28-Oct-13 0:37 
PraiseRe: Thanks for this wonderful article. Implemented this QVFS with some major enhancements Pin
Binief Ta9-Nov-16 19:42
memberBinief Ta9-Nov-16 19:42 
QuestionWhat are the limitations? Pin
Member 956176620-Feb-13 13:37
memberMember 956176620-Feb-13 13:37 
QuestionMessage Removed Pin
nhchmg27-Jun-11 20:28
membernhchmg27-Jun-11 20:28 
GeneralMy vote of 5 Pin
Tushar Arora13-May-11 23:29
memberTushar Arora13-May-11 23:29 
GeneralMy vote of 3 Pin
Dave Kreskowiak21-Jan-11 9:21
mvpDave Kreskowiak21-Jan-11 9:21 
QuestionMessage Removed Pin
nhchmg15-Dec-10 22:41
membernhchmg15-Dec-10 22:41 
JokeMy vote of 5 (contains joke) Pin
MaximilianReisch13-Sep-10 5:00
memberMaximilianReisch13-Sep-10 5:00 
GeneralMy vote of 5 Pin
Kraeven6-Jul-10 0:33
memberKraeven6-Jul-10 0:33 
QuestionExpose Files To Another Proccess Pin
omarooo28-Aug-09 12:16
memberomarooo28-Aug-09 12:16 
QuestionAccess QVFS file from multiple computers Pin
Kraeven15-Jul-09 10:16
memberKraeven15-Jul-09 10:16 
AnswerRe: Access QVFS file from multiple computers Pin
TonyTonyQ15-Jul-09 15:16
memberTonyTonyQ15-Jul-09 15:16 
GeneralRe: Access QVFS file from multiple computers Pin
Kraeven15-Jul-09 21:06
memberKraeven15-Jul-09 21:06 
Generalbug fix... Pin
silverdox13-Jun-09 6:34
membersilverdox13-Jun-09 6:34 
QuestionAre two files needed? Pin
supercat99-Jun-09 6:32
membersupercat99-Jun-09 6:32 
AnswerRe: Are two files needed? Pin
TonyTonyQ9-Jun-09 16:25
memberTonyTonyQ9-Jun-09 16:25 
QuestionISO? Pin
Unruled Boy8-Jun-09 23:21
memberUnruled Boy8-Jun-09 23:21 
AnswerRe: ISO? Pin
supercat99-Jun-09 6:21
membersupercat99-Jun-09 6:21 
QuestionCopyright infringement? Pin
zlezj8-Jun-09 21:17
memberzlezj8-Jun-09 21:17 
AnswerRe: Copyright infringement? Pin
Dewey8-Jun-09 21:31
memberDewey8-Jun-09 21:31 
GeneralDeleting Files and Folders from QVFS Pin
Kraeven8-Jun-09 5:21
memberKraeven8-Jun-09 5:21 
Hi everybody,


I'm having problems with deleting folders from the Virtual File System (QVFS)
Below you'll see some example code (simplified)

I think I just don't understand what's actually happening, when I do a deletion from the virtual folder. The deletion seems to succeed, but when I would re-create the folder, I get the error below :

Error occured while trying to delete file from the Virtual Storage

No file data detected, the file may be physically deleted

Filename : C:\Documents and Settings\acketk\Desktop\VB - Stuff\VirtualSpace\qvfsKE\QVirtualFileSystemManager.vb
Method : System.IO.MemoryStream GetVFileData(Int32, Int32)
LineN° : 946

Filename : C:\Documents and Settings\acketk\Desktop\VB - Stuff\VirtualSpace\qvfsKE\QVirtualFileSystemManager.vb
Method : System.IO.MemoryStream GetVFileData(QVFS.FileEntry)
LineN° : 903

Filename : C:\Documents and Settings\acketk\Desktop\VB - Stuff\VirtualSpace\qvfsKE\QVirtualFileSystemManager.vb
Method : System.IO.MemoryStream GetVDirEntry(System.String, System.IO.MemoryStream, Int32 ByRef)
LineN° : 1244

Filename : C:\Documents and Settings\acketk\Desktop\VB - Stuff\VirtualSpace\qvfsKE\QVirtualFileSystemManager.vb
Method : System.IO.MemoryStream GetVDirEntry(System.String, Int32 ByRef)
LineN° : 1203

Filename : C:\Documents and Settings\acketk\Desktop\VB - Stuff\VirtualSpace\qvfsKE\QVirtualFileSystemManager.vb
Method : Boolean IsExisted(System.String, Boolean)
LineN° : 356

Filename : C:\Documents and Settings\acketk\Desktop\VB - Stuff\VirtualSpace\qvfsKE\QVirtualFileSystemManager.vb
Method : Void CreateVDirectory(System.String)
LineN° : 295

Filename : C:\Documents and Settings\acketk\Desktop\VB - Stuff\VirtualSpace\VirtualSpace\VirtualStorage.vb
Method : Void btnAddFolder_Click(System.Object, System.EventArgs)
LineN° : 364


No file data detected, the file may be physically deleted ==> I know that's what I wanted

*************************************************************
Source Code :
*************************************************************

Sub DeleteVFile(byval VPath as string)

Dim vfm As QVFS.QVirtualFileSystemManager = Nothing
Try

vfm = New QVirtualFileSystemManager()
vfm.OpenQVFSFile(SettingsFile)
If vfm.IsExisted(VPath) then
vfm.DeleteVFile(VPath, True)
End If
vfm.CloseQVFSFile()
vfm = Nothing

Catch ex As Exception
MsgBox("Error occured while trying to delete file from the Virtual Storage" + vbCrLf + ex.Message + vbCrLf + GetErrorInfo(ex))
Finally
If Not vfm Is Nothing Then
vfm.CloseQVFSFile()
vfm = Nothing
End If
End Try
End Sub


Sub DeleteVFolder(byval VPath as string)

Dim vfm As QVFS.QVirtualFileSystemManager = Nothing
Try

vfm = New QVirtualFileSystemManager()
vfm.OpenQVFSFile(SettingsFile)
vfm.DeleteAllVFiles(VPath)
vfm.DeleteVFile(VPath, True)
vfm.CloseQVFSFile()
vfm = Nothing

Catch ex As Exception
MsgBox("Error occured while trying to delete file from the Virtual Storage" + vbCrLf + ex.Message + vbCrLf + GetErrorInfo(ex))
Finally
If Not vfm Is Nothing Then
vfm.CloseQVFSFile()
vfm = Nothing
End If
End Try
End Sub

Sub AddVFile(byval VPath as string, byval FPath as string)
Dim vfm As QVFS.QVirtualFileSystemManager = Nothing

Try

vfm = New QVirtualFileSystemManager()
vfm.OpenQVFSFile(SettingsFile)
vfm.AddNewVFile(FPath, VPath, True)
vfm.CloseQVFSFile()
vfm = Nothing

Catch ex As Exception
MsgBox("Error occured while trying to delete file from the Virtual Storage" + vbCrLf + ex.Message + vbCrLf + GetErrorInfo(ex))
Finally
If Not vfm Is Nothing Then
vfm.CloseQVFSFile()
vfm = Nothing
End If
End Try

End Sub


Sub AddVFolder(byval VPath as string)
Dim vfm As QVFS.QVirtualFileSystemManager = Nothing

Try
vfm = New QVirtualFileSystemManager()
vfm.OpenQVFSFile(SettingsFile)
vfm.CreateVDirectory(VPath)
vfm.CloseQVFSFile()
vfm = Nothing

Catch ex As Exception
MsgBox("Error occured while trying to delete file from the Virtual Storage" + vbCrLf + ex.Message + vbCrLf + GetErrorInfo(ex))
Finally
If Not vfm Is Nothing Then
vfm.CloseQVFSFile()
vfm = Nothing
End If

End Try
End Sub


******************************************************

I know it has something to do with the vfm.DeleteVFile function
(Can this be used on folders too?)With files this works ok!

If I would say physical delete = False
The folder gets removed from the treeview/listview but it's still in the data file,
I don't want that, it needs to be permanent deleted.
Also if not permanent deleted and I would re-create the folder, it isn't shown in the treeview/listview (probably because the status of the folder is still deleted), but it can be accessed by code.

If I would say physical delete = True, the file/folder is deleted ok, the listview/treeview is updated ok. Al seems fine, like it supposed to look.
But if I would re-create the folder (same name and virtual path) I get an error upon creating the folder. (Error above)

My guess is that there's a check in the code (isExisted) that's not made compatible for detecting the folders. Only files and this results in the error above.

Can someone help me to remove/create folders?
What am I doing wrong? Is there a special function I'm not aware of, that actually handles the deletion of folders?

Many thanks out front,

Kraeven
GeneralRe: Deleting Files and Folders from QVFS Pin
TonyTonyQ8-Jun-09 18:33
memberTonyTonyQ8-Jun-09 18:33 
GeneralRe: Deleting Files and Folders from QVFS Pin
Kraeven9-Jun-09 22:27
memberKraeven9-Jun-09 22:27 
QuestionGreat Job! Pin
Kraeven5-Jun-09 4:15
memberKraeven5-Jun-09 4:15 
Generalhm... Pin
Member 316538423-Feb-09 7:04
memberMember 316538423-Feb-09 7:04 
GeneralRe: hm... Pin
TonyTonyQ23-Feb-09 15:15
memberTonyTonyQ23-Feb-09 15:15 
GeneralA quick question Pin
Member 368472319-Aug-08 13:18
memberMember 368472319-Aug-08 13:18 
GeneralRe: A quick question Pin
TonyTonyQ19-Aug-08 16:02
memberTonyTonyQ19-Aug-08 16:02 
GeneralRe: A quick question Pin
ddili19-Aug-08 16:35
memberddili19-Aug-08 16:35 
GeneralGreat Article, I think adding a virtual driver to explorer will benefit anyone who uses this project Pin
xanth1-Jan-08 2:04
memberxanth1-Jan-08 2:04 
GeneralIt´s perfect Pin
TTP (NewBee)26-Dec-07 10:43
memberTTP (NewBee)26-Dec-07 10:43 
GeneralGood but... Pin
arepetti7722-Nov-07 3:12
memberarepetti7722-Nov-07 3:12 
GeneralRe: Good but... Pin
TonyTonyQ23-Nov-07 19:42
memberTonyTonyQ23-Nov-07 19:42 
GeneralGood articles and good idea Pin
Irwan Hassan21-Nov-07 1:42
memberIrwan Hassan21-Nov-07 1:42 
GeneralI Like It Pin
robvon19-Nov-07 23:20
memberrobvon19-Nov-07 23:20 
GeneralExcellent Idea! Pin
dawmail33317-Nov-07 21:24
memberdawmail33317-Nov-07 21:24 
Questionwhat i've been looking for Pin
Amrykid17-Nov-07 17:49
memberAmrykid17-Nov-07 17:49 
QuestionUsing from within Windows Explorer? Pin
M.K.A. Monster17-Nov-07 3:41
memberM.K.A. Monster17-Nov-07 3:41 
AnswerRe: Using from within Windows Explorer? Pin
TonyTonyQ17-Nov-07 20:41
memberTonyTonyQ17-Nov-07 20:41 
QuestionUnpacking all files from virtual disk? Pin
Megalan16-Nov-07 23:55
memberMegalan16-Nov-07 23:55 
AnswerRe: Unpacking all files from virtual disk? Pin
TonyTonyQ17-Nov-07 0:09
memberTonyTonyQ17-Nov-07 0:09 
GeneralRe: Unpacking all files from virtual disk? Pin
Megalan17-Nov-07 2:50
memberMegalan17-Nov-07 2:50 
GeneralInteresting Pin
Pawel Gielmuda16-Nov-07 1:11
memberPawel Gielmuda16-Nov-07 1:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170626.1 | Last Updated 9 Jun 2009
Article Copyright 2007 by TonyTonyQ
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid